Browse Source

Makefile: Add automatic dependency generation

Aaron Lindsay 7 years ago
parent
commit
c84fd58ce4
2 changed files with 18 additions and 8 deletions
  1. 1 0
      .gitignore
  2. 17 8
      Makefile

+ 1 - 0
.gitignore

@@ -2,3 +2,4 @@ aedrix-kernel.elf
2 2
 aedrix-kernel.img
3 3
 *.o
4 4
 *.swp
5
+*.d

+ 17 - 8
Makefile

@@ -50,21 +50,27 @@ aedrix-kernel.img: aedrix-kernel.elf
50 50
 
51 51
 %.o: %.c
52 52
 	@echo '     CC   $@'
53
-	$(V)$(CC) $(KCFLAGS) -c -o $@ $<
53
+	$(V)$(CC) $(KCFLAGS) -MD -c -o $@ $<
54
+	@# Automatic dependency generation fixups (http://mad-scientist.net/make/autodep.html)
55
+	@cp $*.d $(*D)/.$(*F).d; \
56
+		sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
57
+		-e '/^$$/ d' -e 's/$$/ :/' < $*.d >> $(*D)/.$(*F).d; \
58
+		rm -f $*.d
54 59
 
55
-# Assembly files without preprocessor directives
56
-%.o: %.s
57
-	@echo '     AS   $@'
58
-	$(V)$(AS) -o $@ $<
59
-
60
-# Assembly files with preprocessor directives
61 60
 %.o: %.S
62 61
 	@echo '     AS   $@'
63
-	$(V)$(AS) -o $@ $<
62
+	$(V)$(CC) $(KCFLAGS) -MD -c -o $@ $<
63
+	@# Automatic dependency generation fixups (http://mad-scientist.net/make/autodep.html)
64
+	@cp $*.d $(*D)/.$(*F).d; \
65
+		sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
66
+		-e '/^$$/ d' -e 's/$$/ :/' < $*.d >> $(*D)/.$(*F).d; \
67
+		rm -f $*.d
64 68
 
65 69
 clean:
66 70
 	@echo '  CLEAN   *.o'
67 71
 	$(V)rm -f $(KOBJS)
72
+	@echo '  CLEAN   .*.d'
73
+	$(V)rm -f $(DEPENDENCY_FILES)
68 74
 	@echo '  CLEAN   aedrix-kernel.elf'
69 75
 	$(V)rm -f aedrix-kernel.elf
70 76
 	@echo '  CLEAN   aedrix-kernel.objdump'
@@ -76,3 +82,6 @@ boot: aedrix-kernel.img
76 82
 	$(V)qemu-system-arm -m 1024 -M vexpress-a9 -kernel aedrix-kernel.img -serial stdio
77 83
 boot-gdb: aedrix-kernel.img
78 84
 	$(V)qemu-system-arm -m 1024 -M vexpress-a9 -kernel aedrix-kernel.img -serial stdio -S -s
85
+
86
+DEPENDENCY_FILES = $(foreach file,$(KOBJS), $(dir $(file)).$(notdir $(basename $(file))).d)
87
+-include $(DEPENDENCY_FILES)