include $(CONNTRACK_APPS_DIR)/common.mk

APPL_SUB_BLDDIR := $(CONNTRACK_APPS_BLDDIR)/extensions

LSRCS := libct_proto_tcp.c libct_proto_udp.c libct_proto_icmp.c \
		libct_proto_icmpv6.c libct_proto_unknown.c libct_proto_udplite.c \
		libct_proto_sctp.c libct_proto_dccp.c libct_proto_gre.c
		
LOBJS := $(addsuffix .o, $(basename ${LSRCS}))
BOBJS := $(addprefix ${APPL_SUB_BLDDIR}/,${LOBJS})

SO_LIB := $(APPL_SUB_BLDDIR)/libconntrack.so
STATIC_LIB := $(foreach T,$(SO_LIB),$(basename $(T))_pic.a)

all: $(SO_LIB) $(STATIC_LIB)

$(STATIC_LIB): $(BOBJS)
	@$(AR) rcs $@ $^
	@$(RANLIB) $@
	
$(SO_LIB): $(BOBJS)
	@$(CC) -shared -Wl,-soname,$(notdir $@) $(CFLAGS) -o $@ $^

$(APPL_SUB_BLDDIR)/%.o: %.c
	@echo "Compiling $<"
	@$(CC) $(CFLAGS) -c $< -o $@
	
clean:
	@rm -f $(BOBJS:.o=.d)
	@rm -f $(BOBJS)
	@rm -f $(APPL_SUB_BLDDIR)/libconntrack_pic.a
	@rm -f $(APPL_SUB_BLDDIR)/libconntrack.so
	
ifeq (,$(findstring clean,$(MAKECMDGOALS)))
-include $(addprefix ${APPL_SUB_BLDDIR}/,$(addsuffix .d,$(basename $(LSRCS))))
$(APPL_SUB_BLDDIR)/%.d: %.c
	-@if [ ! -d $(APPL_SUB_BLDDIR)  ]; then mkdir -p $(APPL_SUB_BLDDIR); fi
	@$(CC) -M $(CFLAGS) $< >$@.tmp; \
	echo "$@ \\" > $@; \
	sed 's,\($*\)\.o[ :]*,$$(APPL_SUB_BLDDIR)\/\1.o: ,g' < $@.tmp >> $@; \
	rm -f $@.tmp
endif

.PHONY: all clean