# DIRS
BUILD = ./build
SRC = .
INC = .
override CFLAGS += -fPIC
LIB_NAME=libasct_helper
STATIC_LIB = $(LIB_NAME).a
DYNAMIC_LIB = $(LIB_NAME).so
SONAME = $(DYNAMIC_LIB).1
SO_FULLNAME = $(SONAME).0

SRC_FILES = $(wildcard $(SRC)/*.c)
HEADERS = $(wildcard $(INC)/*.h)
EXEC_FILES = $(EXEC:%=$(BIN)/%)
OBJ_FILES = $(patsubst $(SRC)/%.c, $(BUILD)/%.o, $(SRC_FILES))

default: $(SO_FULLNAME) $(STATIC_LIB) symlinks
all: default

clean:
	-rm -r -f $(BUILD) $(SO_FULLNAME) $(SONAME) $(SO_SYMLINK) $(STATIC_LIB)

# Build the static library (without -fPIC)
$(STATIC_LIB): $(OBJ_FILES)
	@echo "Building static library: $@"
	$(AR) rcs $@ $^

# Create the asct_helper shared library
$(SO_FULLNAME): $(OBJ_FILES)
	@echo "Building shared library: $@"
	ld -shared -soname $(SONAME) -o $@ $^
	ldconfig -n ./ 

$(OBJ_FILES): $(BUILD)/%.o: %.c
	echo compiling $<
	mkdir -p $(@D)
	$(CC) $(CFLAGS) -c $< -o $@

# Create symbolic links for versioned library
symlinks: $(SO_FULLNAME)
	@echo "Creating symbolic links..."
	ln -sf $(SO_FULLNAME) $(SONAME)
	ln -sf $(SONAME) $(DYNAMIC_LIB)

.PHONY: all clean symlinks