Automatically calculate make dependencies
[rapper.git] / src / Makefile
index bf7dcc7..fbc9ad9 100644 (file)
@@ -11,6 +11,9 @@ USB_OBJS=usb/serial.o usb/core.o usb/desc.o usb/hw.o usb/user.o usb/vcom.o usb/c
 BOARD_OBJS=$(BPATH)/startup.o $(BPATH)/system.o boards/cortex_m3/core_cm3.o
 OBJECTS=main.o $(BOARD_OBJS) $(USB_OBJS)
 
+depend=$(shell sh -c 'b=$$(echo $1|sed -e"s/\\.o$$//g");([ -f $$b.d ] \
+       && sed -e":a;N;$$!ba;s/\\\\\\n//g" $$b.d|grep "^$$(basename $1):"\
+       | cut -d: -f2)||echo $$b.c')
 
 all: crc
 
@@ -20,7 +23,8 @@ $(PROJECT).elf: $(OBJECTS)
 %.bin: %.elf
        $(PLATFORM)-objcopy -O binary $< $*.bin
 
-%.o: %.c
+.SECONDEXPANSION:
+%.o: $$(call depend,$$@)
        $(PLATFORM)-gcc -MM $< -MF $(patsubst %.o,%.d,$@) -MP
        $(PLATFORM)-gcc $(CFLAGS) -c $< -o $@