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