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