trennfix/sw: Makefile improvements
[eisenbahn.git] / trennfix / sw / Makefile
1
2 #######################################
3
4 HW?=trennfix_0.4
5
6 # Default program to build
7 PROG?=trennfix
8
9 BUILD_DIR=build
10
11 #######################################
12 # VERBOSITY CONTROL
13 ifeq ($(V),)
14 Q=@
15 else
16 Q=
17 endif
18
19 include mk/hw/${HW}.mk
20 include mk/prog/*.mk
21
22 INCLUDES+=-I mm/include -I include -I.
23 CFLAGS+=-D__HW_CONF_HEADER__="<config/${CONFIG}.h>"
24
25 CFLAGS += -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL
26 CFLAGS += -std=gnu99
27 CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
28 CFLAGS += -Wall -Wstrict-prototypes
29 CFLAGS += -Os -gdwarf-2
30 CFLAGS += -Wa,-adhlns=$(<:.c=.lst)
31 CFLAGS += -MD $(@:.c:.d)
32 CFLAGS += $(INCLUDES)
33
34 ASFLAGS += -Wa,-adhlns=$(<:.S=.lst),-gstabs -mmcu=$(MCU)
35 ASFLAGS += -x assembler-with-cpp
36
37 #---------------- Library Options ----------------
38 # Minimalistic printf version
39 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
40
41 # Floating point printf version (requires MATH_LIB = -lm below)
42 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
43
44 # If this is left blank, then it will use the Standard printf version.
45 PRINTF_LIB =
46 #PRINTF_LIB = $(PRINTF_LIB_MIN)
47 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
48
49 # Minimalistic scanf version
50 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
51
52 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
53 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
54
55 # If this is left blank, then it will use the Standard scanf version.
56 SCANF_LIB =
57 #SCANF_LIB = $(SCANF_LIB_MIN)
58 #SCANF_LIB = $(SCANF_LIB_FLOAT)
59
60 LDFLAGS = -Wl,-Map=$(@:.elf=.map),--cref
61 LIBS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
62
63 #---------------- Programming Options (avrdude) ----------------
64
65 # Programming hardware: alf avr910 avrisp bascom bsd
66 # dt006 pavr picoweb pony-stk200 sp12 stk200 stk500
67 #
68 # Type: avrdude -c ?
69 # to get a full listing.
70 #
71 AVRDUDE_PROGRAMMER ?= avrisp2
72
73 # com1 = serial port. Use lpt1 to connect to parallel port.
74 AVRDUDE_PORT ?= usb
75
76 AVRDUDE_WRITE_FUSES = -U lfuse:w:${LFUSE}:m -U hfuse:w:${HFUSE}:m \
77 -U efuse:w:${EFUSE}:m
78
79 # Uncomment the following if you want avrdude's erase cycle counter.
80 # Note that this counter needs to be initialized first using -Yn,
81 # see avrdude manual.
82 #AVRDUDE_ERASE_COUNTER = -y
83
84 # Uncomment the following if you do /not/ wish a verification to be
85 # performed after programming the device.
86 # AVRDUDE_NO_VERIFY = -V
87
88 # Increase verbosity level. Please use this when submitting bug
89 # reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
90 # to submit bug reports.
91 #AVRDUDE_VERBOSE = -v -v
92
93 AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
94 AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
95 AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
96 AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
97 AVRDUDE_FLAGS += -B 5
98
99 # Define programs and commands.
100 SHELL = sh
101 CC = avr-gcc
102 OBJCOPY = avr-objcopy
103 OBJDUMP = avr-objdump
104 SIZE = avr-size
105 NM = avr-nm
106 AVRDUDE = avrdude
107 RM = rm -rf
108
109 define SELECT_DEFAULT_template =
110 elf: $(1).elf
111 hex: $(1).hex
112 eep: $(1).eep
113 lss: $(1).lss
114 sym: $(1).sym
115 clean: $(1)_clean
116 build: $(1)_build
117 program: $(1)_program
118 fuses: $(1)_fuses
119 endef
120
121 default:build
122
123 all: buildall
124
125 cleanall: $(foreach prog, $(PROGRAMS), $(prog)_clean)
126 buildall: $(foreach prog, $(PROGRAMS), $(prog)_build)
127
128 .PHONY: clean build program cleanall buildall all
129
130 $(eval $(call SELECT_DEFAULT_template, $(PROG)))
131
132 # Create final output files (.hex, .eep) from ELF output file.
133 %.hex: %.elf
134 @echo HEX $@
135 $(Q)$(OBJCOPY) -O ihex -R .fuse -R .eeprom $< $@
136
137 %.eep: %.elf
138 @echo EEP $@
139 $(Q)$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
140 --change-section-lma .eeprom=0 -O ihex $< $@
141
142 # Create extended listing file from ELF output file.
143 %.lss: %.elf
144 @echo LSS $@
145 $(Q)$(OBJDUMP) -h -S $< > $@
146
147 # Create a symbol table from ELF output file.
148 %.sym: %.elf
149 @echo SYM $@
150 $(Q)$(NM) -n $< > $@
151
152 # Link: create ELF output file from object files.
153 %.elf: $(OBJ)
154 @echo LD $@
155 $(Q)$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)
156
157 # Compile: create object files from C source files.
158 $(BUILD_DIR)/%.o : %.c Makefile $(wildcard mk/hw/*)
159 @echo CC $@
160 @mkdir -p $(@D)
161 $(Q)$(CC) -c $(CFLAGS) $< -o $@
162
163 # Compile: create assembler files from C source files.
164 %.s : %.c Makefile
165 @echo CC-ASM $<
166 $(Q)$(CC) -S $(CFLAGS) $< -o $@
167
168 # Assemble: create object files from assembler source files.
169 %.o : %.S
170 @echo ASM $@
171 @mkdir -p $(@D)
172 $(Q)$(CC) -c $(ASFLAGS) $< -o $@
173
174 ########################### The big template for each program ##################
175 define PROGRAM_template=
176
177 $(1)_mupf:
178 @echo "$(1) Objects: " $$($(1)_OBJS)
179
180 # Determine program's object files
181 $(1)_OBJS=$$(foreach obj, $$($(1)_SRC:.c=.o), $(BUILD_DIR)/$$(obj))
182 $(1)_OBJS+=$$($(1)_ASRC:.S=.o)
183
184 # Collect all listing files - for cleanup
185 $(1)_LST += $$($(1)_SRC:.c=.lst) $$($(1)_ASRC:.S=.lst)
186
187 $(1): $(1).elf
188 $(1).elf: $$($(1)_OBJS)
189
190 $(1)_program : $(1).hex $(1).eep
191 @echo PROG $(1)
192 $(Q)$(AVRDUDE) $(AVRDUDE_FLAGS) -U flash:w:$(1).hex -U eeprom:w:$(1).eep
193
194 $(1)_fuses.bin: $(1).elf
195 @echo OBJCOPY $$@
196 $(Q)$(OBJCOPY) -j .fuse $(1).elf -O binary $$@
197 $(Q)chmod -x $$@
198
199 $(1)_fuses: $(1)_fuses.bin
200 @echo FUSES $$@
201 $(Q)$(AVRDUDE) $(AVRDUDE_FLAGS)\
202 -U lfuse:w:0x$$$$(hd $(1)_fuses.bin| awk '{print $$$$2}'):m \
203 -U hfuse:w:0x$$$$(hd $(1)_fuses.bin| awk '{print $$$$3}'):m \
204 -U efuse:w:0x$$$$(hd $(1)_fuses.bin| head -n1 |awk '{print $$$$4}'):m
205
206 $(1)_size: $(1).elf
207 @echo; echo
208 $(Q) $(SIZE) -A $(1).elf
209
210 # Target: clean project.
211 $(1)_clean:
212 @echo CLEAN $(1)
213 $(Q)$(RM) $(1).hex
214 $(Q)$(RM) $(1).eep
215 $(Q)$(RM) $(1).cof
216 $(Q)$(RM) $(1).elf
217 $(Q)$(RM) $(1).map
218 $(Q)$(RM) $(1).sym
219 $(Q)$(RM) $(1).lss
220 $(Q)$(RM) $$($(1)_OBJS)
221 $(Q)$(RM) $$($(1)_LST)
222 $(Q)$(RM) $$($(1)_SRC:.c=.s)
223 $(Q)$(RM) $$($(1)_SRC:.c=.d)
224 $(Q)$(RM) $(1)_fuses.bin
225 $(Q) rm -rf $(BUILD_DIR)/
226
227 $(1)_build: $(1).hex $(1)_size
228
229 -include $$($(1)_OBJS:.o=.d)
230
231 .PRECIOUS : $$($(1)_OBJS)
232 .PHONY: $(1) $(1)_clean $(1)_size $(1)_build
233
234 #############
235 endef
236 ################################################################################
237
238 $(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))