# List of all source files
MEMBERS = fastresample 

CFLAGS += -pipe -g -D_GNU_SOURCE -DRDL                                 \
         -Wall -Wshadow -Wbad-function-cast                                \
         -Wchar-subscripts -Winline -Wmissing-prototypes                   \
         -Wstrict-prototypes -Wmissing-declarations -Wnested-externs       \
         -Wpointer-arith -Wredundant-decls -Wunused -Werror -Wformat       \
         -Wno-missing-format-attribute -Wwrite-strings -fPIC

OBJS       = $(patsubst %,build/%.o,$(MEMBERS)) 
DEPS       = $(patsubst %,build/%.d,$(MEMBERS)) 

SONAME = libfastresample.so.0
SYMLINK = libfastresample.so

$(SYMLINK): $(SONAME)
	ln -f -s $< $@

$(SONAME): $(OBJS)
	$(CC) -shared -o $@ $^ $(LDFLAGS) -Wl,-Bsymbolic -Wl,-soname,$(SONAME)
	cp $@ $@.debug
	$(STRIP) $@
 
# C
build/%.o build/%-unit.o: %.c 
	mkdir -p $(dir $@)
	$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
	$(CC) -MM $(CFLAGS) $(CPPFLAGS) $< > build/$*.d
	@mv -f build/$*.d build/$*.d.tmp
	@sed -e 's|.*:|$*.o $@ $*.i: Makefile |' < build/$*.d.tmp > build/$*.d
	@sed -e 's/.*://' -e 's/\\$$//' < build/$*.d.tmp | fmt -1 | \
	  sed -e 's/^ *//' -e 's/$$/:/' >> build/$*.d
	@rm -f build/$*.d.tmp

clean: 
	rm -rf build
	find . -name \*.o | xargs rm -f 
	rm -f *.so*
	echo "OBJS:"
	echo $(OBJS)

# dependencies
-include $(DEPS)

# Headercheck
%.h.c: %.h
	if [ -f $@.template ] ; then cp $@.template $@ ; fi
	echo "#include \"$^\"" >> $@
	echo "#include \"../empty_main.c\"" >> $@

hdrcheck: $(patsubst %, build/%.o, $(shell find . -name \*.h -type f))
