diff --git a/Makefile b/Makefile index ae05b29..4e9a03d 100644 --- a/Makefile +++ b/Makefile @@ -37,13 +37,14 @@ OPENCM3_LIB = $(OPENCM3_DIR)/lib # library for the STM32F1 (provided by opencm3) STM32F1_LIB = opencm3_stm32f1 - +# libraries with source code used (also to be compiled) +SRCLIBS = . lib # source files (this will be populated using includes based DEPENDENCIES) -CSRC = global.c -# headers corresponding to source files -CHDR = $(sort $(patsubst %.c,%.h,$(CSRC))) -# objects compiled from source files -OBJ = $(sort $(patsubst %.c,%.o,$(CSRC))) +CSRC = +# headers (unique, and if existing) corresponding to source files +CHDR = $(sort $(patsubst %.c,%.h,$(wildcard $(CSRC)))) +# objects (unique, and if existing) compiled from source files +OBJ = $(sort $(patsubst %.c,%.o,$(wildcard $(CSRC)))) # figure out based on the main sources files which library files are used DEPENDENCIES = $(patsubst %,%.inc,$(FIRMWARE)) # populates CSRC based on the library files used @@ -89,7 +90,7 @@ CFLAGS += -nostdlib -nostdinc # include ELLCC libraries CFLAGS += -I $(ELLCC)libecc/include/ -I $(ELLCC)libecc/include/arm/ # include own libraries -CFLAGS += -I . -I lib +CFLAGS += $(foreach lib,$(SRCLIBS),-I $(lib)) # include opencm3 libraries CFLAGS += -I $(OPENCM3_INC) # add defines for micro-controller and board @@ -152,14 +153,20 @@ hex: $(patsubst %,%.hex,$(FIRMWARE)) $(info compiling $(@)) $(Q)$(CC) $(CFLAGS) $(ARCH_FLAGS) -o $(@) -c $(<) -# generate dependencies (requires libopencm3 to be built to generate all libraries) -%.d: %.c $(OPENCM3_LIB)/lib$(STM32F1_LIB).a - @# check which libraries are used - $(Q)$(CC) $(CFLAGS) $(ARCH_FLAGS) -MM -MF $(@) -o /dev/null -c $(<) - -# figure out which library source files are used for later inclusion -%.inc: %.d - $(Q)grep -o -e " lib\/[^ ]*\.h" $(<) | sed -e 's|\(.*\)\.h$$|CSRC +=\1.c\n-include\1.inc|g' -e 's|.*${*}.*||g' > $(@) +# find out which library source files also need to be compiled and linked, based on its dependencies +%.inc: %.c $(OPENCM3_LIB)/lib$(STM32F1_LIB).a + $(Q)echo "" > $(@) + $(Q)for dependency in $(shell $(CC) $(CFLAGS) $(ARCH_FLAGS) -MM -c $(<)); do \ + if [ -f "$${dependency}" ]; then \ + for lib in $(SRCLIBS); do \ + if [ `dirname "$${dependency}"` = "$${lib}" ]; then \ + if [ -f `echo "$${dependency}" | sed -e 's|\(.*\)\.h$$|\1.c|'` ]; then \ + echo "$${dependency}" | sed -e 's|\(.*\)\.h$$|CSRC += \1.c\n-include \1.inc|g' -e 's|.*${*}.*||g' >> $(@); \ + fi; \ + fi; \ + done; \ + fi; \ + done # get libopencm3 $(OPENCM3_DIR)/Makefile: