Browse Source

Added automatic dependency generation to makefile

Thomas Buck 8 years ago
parent
commit
cc6468b6f8
1 changed files with 38 additions and 25 deletions
  1. 38
    25
      makefile

+ 38
- 25
makefile View File

@@ -1,9 +1,8 @@
1
+TARGET = rx
1 2
 MCU = attiny85
2 3
 F_CPU = 16000000
3
-RM = rm -rf
4
-MKDIR = mkdir -p
5 4
 EXTRAINCDIR = include
6
-CSTANDARD = gnu99
5
+CSTANDARD = c99
7 6
 BUILDDIR = build
8 7
 
9 8
 GCC = avr-gcc
@@ -11,6 +10,8 @@ SIZE = avr-size
11 10
 OBJCOPY = avr-objcopy
12 11
 OBJDUMP = avr-objdump
13 12
 AVRDUDE = avrdude
13
+RM = rm -rf
14
+MKDIR = mkdir -p
14 15
 
15 16
 SRC = src/main.c
16 17
 SRC += src/timer.c
@@ -23,27 +24,24 @@ ifeq ($(DEBUG),1)
23 24
 	SRC += src/serial.c
24 25
 endif
25 26
 
26
-OBJ = $(addprefix $(BUILDDIR)/, $(SRC:.c=.o))
27
-
28 27
 CARGS = -mmcu=$(MCU)
29 28
 CARGS += -I$(EXTRAINCDIR)
30 29
 CARGS += -Os
31
-#CARGS += -funsigned-char
32
-#CARGS += -funsigned-bitfields
33
-#CARGS += -fpack-struct
34
-#CARGS += -fshort-enums
35
-CARGS += -Wall -pedantic -Wstrict-prototypes -Wshadow
36
-CARGS += -Wpointer-arith -Wcast-qual -Wextra
30
+CARGS += -funsigned-bitfields
31
+CARGS += -fpack-struct
32
+CARGS += -fshort-enums
33
+CARGS += -ffunction-sections
34
+CARGS += -Wall -Wextra -pedantic -Werror -Wshadow
35
+CARGS += -Wpointer-arith -Wcast-qual -Wstrict-prototypes
37 36
 CARGS += -Wno-main -Wno-write-strings -Wno-unused-parameter
38 37
 CARGS += -std=$(CSTANDARD)
39 38
 CARGS += -DF_CPU=$(F_CPU)
40
-#CARGS += -ffunction-sections
41 39
 
42 40
 ifeq ($(DEBUG),1)
43 41
 	CARGS += -DDEBUG
44 42
 endif
45 43
 
46
-#LINKER = -Wl,--relax
44
+LINKER = -Wl,--relax
47 45
 
48 46
 #PROGRAMMER = avrisp2
49 47
 #ISPPORT = usb
@@ -51,31 +49,46 @@ PROGRAMMER = arduino
51 49
 ISPPORT = /dev/tty.usbserial-A100OZQ1
52 50
 ISPBAUD = 57600
53 51
 
54
-TARGET = rx
52
+# -----------------------------------------------------------------------------
55 53
 
54
+OBJ = $(addprefix $(BUILDDIR)/, $(SRC:.c=.o))
55
+
56
+.PHONY: all
56 57
 all: $(BUILDDIR)/$(TARGET).hex
57 58
 
59
+.PHONY: debug
58 60
 debug:
59 61
 	$(MAKE) BUILDDIR=build/debug TARGET=debug MCU=atmega328 DEBUG=1 all
60 62
 
63
+.PHONY: debugProgram
64
+debugProgram:
65
+	$(MAKE) BUILDDIR=build/debug TARGET=debug MCU=atmega328p DEBUG=1 program
66
+
67
+.PHONY: program
68
+program: $(BUILDDIR)/$(TARGET).hex
69
+	$(AVRDUDE) -p $(MCU) -c $(PROGRAMMER) -b $(ISPBAUD) -P $(ISPPORT) -e -D -U $(BUILDDIR)/$(TARGET).hex
70
+
71
+.PHONY: clean
72
+clean:
73
+	$(RM) $(BUILDDIR)
74
+
75
+-include $(OBJ:.o=.d)
76
+
61 77
 $(BUILDDIR)/%.o: %.c
62 78
 	@$(MKDIR) $(BUILDDIR)/$(dir $<)
63
-	$(GCC) -c $< -o $@ $(CARGS)
79
+	$(GCC) $(CARGS) -c $< -o $@
80
+	@$(GCC) $(CARGS) -MM $*.c > $(BUILDDIR)/$*.d
81
+	@mv -f $(BUILDDIR)/$*.d $(BUILDDIR)/$*.d.tmp
82
+	@sed -e 's|.*:|$(BUILDDIR)/$*.o:|' < $(BUILDDIR)/$*.d.tmp > $(BUILDDIR)/$*.d
83
+	@sed -e 's/.*://' < $(BUILDDIR)/$*.d.tmp | sed -e 's/\\$$//' | \
84
+		fmt -1 | sed -e 's/^ *//' | sed -e 's/$$/:/' >> $(BUILDDIR)/$*.d
85
+	@rm -f $(BUILDDIR)/$*.d.tmp
64 86
 
65 87
 $(BUILDDIR)/$(TARGET).elf: $(OBJ)
66
-	$(GCC) $(CARGS) $(OBJ) --output $@ $(LINKER) -Wl,-Map -Wl,$(@:.elf=.map)
88
+	$(GCC) $(CARGS) $(OBJ) $(LINKER) -Wl,-Map -Wl,$(@:.elf=.map) --output $@
67 89
 	$(SIZE) $@
68 90
 
69 91
 $(BUILDDIR)/$(TARGET).hex: $(BUILDDIR)/$(TARGET).elf
70 92
 	$(OBJCOPY) -O ihex $< $@
71 93
 	$(OBJDUMP) -h -S $< > $(@:.hex=.lss)
72 94
 
73
-program: $(BUILDDIR)/$(TARGET).hex
74
-	$(AVRDUDE) -p $(MCU) -c $(PROGRAMMER) -b $(ISPBAUD) -P $(ISPPORT) -e -D -U $(BUILDDIR)/$(TARGET).hex
75
-
76
-debugProgram:
77
-	$(MAKE) BUILDDIR=build/debug TARGET=debug MCU=atmega328p DEBUG=1 program
78
-
79
-clean:
80
-	$(RM) $(BUILDDIR)
81
-

Loading…
Cancel
Save