trennfix/sw: Makefile: Move objects etc into build folder
[eisenbahn.git] / trennfix / sw / Makefile
CommitLineData
54de37bf 1
f12652df 2#######################################
54de37bf 3
f12652df 4HW?=trennfix_0.4
8ac2b48a 5PROG?=trennfix
54de37bf 6
8ac2b48a 7BUILD_DIR=build
54de37bf 8
f12652df
PH
9#######################################
10# VERBOSITY CONTROL
11ifeq ($(V),)
12Q=@
13else
14Q=
15endif
54de37bf 16
f12652df
PH
17include mk/hw/${HW}.mk
18include mk/prog/*.mk
54de37bf 19
f12652df
PH
20INCLUDES+=-I mm/include -I include -I.
21CFLAGS+=-D__HW_CONF_HEADER__="<config/${CONFIG}.h>"
54de37bf 22
f12652df
PH
23CFLAGS += -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL
24CFLAGS += -std=gnu99
54de37bf
PH
25CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
26CFLAGS += -Wall -Wstrict-prototypes
f12652df 27CFLAGS += -Os -gdwarf-2
54de37bf 28CFLAGS += -Wa,-adhlns=$(<:.c=.lst)
f12652df 29CFLAGS += -MD $(@:.c:.d)
93cb14d4 30CFLAGS += $(INCLUDES)
54de37bf 31
f12652df
PH
32ASFLAGS += -Wa,-adhlns=$(<:.S=.lst),-gstabs -mmcu=$(MCU)
33ASFLAGS += -x assembler-with-cpp
54de37bf
PH
34
35#---------------- Library Options ----------------
36# Minimalistic printf version
37PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
38
39# Floating point printf version (requires MATH_LIB = -lm below)
40PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
41
42# If this is left blank, then it will use the Standard printf version.
43PRINTF_LIB =
44#PRINTF_LIB = $(PRINTF_LIB_MIN)
45#PRINTF_LIB = $(PRINTF_LIB_FLOAT)
46
54de37bf
PH
47# Minimalistic scanf version
48SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
49
50# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
51SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
52
53# If this is left blank, then it will use the Standard scanf version.
54SCANF_LIB =
55#SCANF_LIB = $(SCANF_LIB_MIN)
56#SCANF_LIB = $(SCANF_LIB_FLOAT)
57
f12652df
PH
58LDFLAGS = -Wl,-Map=$(@:.elf=.map),--cref
59LIBS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
54de37bf
PH
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#
54de37bf
PH
69AVRDUDE_PROGRAMMER ?= avrisp2
70
54de37bf
PH
71# com1 = serial port. Use lpt1 to connect to parallel port.
72AVRDUDE_PORT ?= usb
73
54de37bf
PH
74AVRDUDE_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
91AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
92AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
93AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
94AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
f12652df 95AVRDUDE_FLAGS += -B 5
54de37bf
PH
96
97# Define programs and commands.
98SHELL = sh
99CC = avr-gcc
100OBJCOPY = avr-objcopy
101OBJDUMP = avr-objdump
102SIZE = avr-size
103NM = avr-nm
104AVRDUDE = avrdude
f12652df 105RM = rm -rf
54de37bf 106
f12652df
PH
107define SELECT_DEFAULT_template =
108elf: $(1).elf
109hex: $(1).hex
110eep: $(1).eep
111lss: $(1).lss
112sym: $(1).sym
113clean: $(1)_clean
114build: $(1)_build
115program: $(1)_program
116fuses: $(1)_fuses
117endef
54de37bf 118
f12652df 119all: build
54de37bf 120
f12652df
PH
121cleanall: $(foreach prog, $(PROGRAMS), $(prog)_clean)
122buildall: $(foreach prog, $(PROGRAMS), $(prog)_build)
54de37bf 123
f12652df 124.PHONY: clean build program cleanall buildall all
54de37bf 125
f12652df 126$(eval $(call SELECT_DEFAULT_template, $(PROG)))
54de37bf
PH
127
128# Create final output files (.hex, .eep) from ELF output file.
129%.hex: %.elf
f12652df
PH
130 @echo HEX $@
131 $(Q)$(OBJCOPY) -O ihex -R .fuse -R .eeprom $< $@
54de37bf
PH
132
133%.eep: %.elf
f12652df
PH
134 @echo EEP $@
135 $(Q)$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
136 --change-section-lma .eeprom=0 -O ihex $< $@
54de37bf
PH
137
138# Create extended listing file from ELF output file.
139%.lss: %.elf
f12652df
PH
140 @echo LSS $@
141 $(Q)$(OBJDUMP) -h -S $< > $@
54de37bf
PH
142
143# Create a symbol table from ELF output file.
144%.sym: %.elf
f12652df
PH
145 @echo SYM $@
146 $(Q)$(NM) -n $< > $@
54de37bf
PH
147
148# Link: create ELF output file from object files.
54de37bf 149%.elf: $(OBJ)
f12652df
PH
150 @echo LD $@
151 $(Q)$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)
54de37bf
PH
152
153# Compile: create object files from C source files.
8ac2b48a 154$(BUILD_DIR)/%.o : %.c Makefile $(wildcard mk/hw/*)
f12652df 155 @echo CC $@
8ac2b48a 156 @mkdir -p $(@D)
f12652df 157 $(Q)$(CC) -c $(CFLAGS) $< -o $@
54de37bf
PH
158
159# Compile: create assembler files from C source files.
160%.s : %.c Makefile
f12652df
PH
161 @echo CC-ASM $<
162 $(Q)$(CC) -S $(CFLAGS) $< -o $@
54de37bf
PH
163
164# Assemble: create object files from assembler source files.
165%.o : %.S
f12652df 166 @echo ASM $@
8ac2b48a 167 @mkdir -p $(@D)
f12652df 168 $(Q)$(CC) -c $(ASFLAGS) $< -o $@
54de37bf 169
f12652df
PH
170########################### The big template for each program ##################
171define PROGRAM_template=
54de37bf 172
8ac2b48a
PH
173$(1)_mupf:
174 @echo "$(1) Objects: " $$($(1)_OBJS)
175
f12652df 176# Determine program's object files
8ac2b48a 177$(1)_OBJS=$$(foreach obj, $$($(1)_SRC:.c=.o), $(BUILD_DIR)/$$(obj))
f12652df 178$(1)_OBJS+=$$($(1)_ASRC:.S=.o)
54de37bf 179
f12652df
PH
180# Collect all listing files - for cleanup
181$(1)_LST += $$($(1)_SRC:.c=.lst) $$($(1)_ASRC:.S=.lst)
54de37bf 182
f12652df
PH
183$(1): $(1).elf
184$(1).elf: $$($(1)_OBJS)
54de37bf 185
f12652df
PH
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
54de37bf 189
f12652df
PH
190$(1)_fuses.bin: $(1).elf
191 @echo OBJCOPY $$@
192 $(Q)$(OBJCOPY) -j .fuse $(1).elf -O binary $$@
193 $(Q)chmod -x $$@
54de37bf 194
f12652df
PH
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
54de37bf 201
f12652df
PH
202$(1)_size: $(1).elf
203 @echo; echo
204 $(Q) $(SIZE) -A $(1).elf
54de37bf 205
f12652df
PH
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)
8ac2b48a
PH
220 $(Q)$(RM) $(1)_fuses.bin
221 $(Q) rm -rf $(BUILD_DIR)/
f12652df
PH
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#############
231endef
232################################################################################
233
234$(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))