d399d82d9e60c6b51cd9291ca7e067eda5bd5861
[pub/USBasp.git] / LUFA / Build / lufa.build.in
1 #
2 # LUFA Library
3 # Copyright (C) Dean Camera, 2012.
4 #
5 # dean [at] fourwalledcubicle [dot] com
6 # www.lufa-lib.org
7 #
8
9 LUFA_BUILD_MODULES += BUILD
10 LUFA_BUILD_TARGETS += size symbol-sizes all elf hex lss clean
11 LUFA_BUILD_MANDATORY_VARS += TARGET ARCH MCU SRC F_USB LUFA_PATH
12 LUFA_BUILD_OPTIONAL_VARS += BOARD OPTIMIZATION C_STANDARD CPP_STANDARD F_CPU C_FLAGS CPP_FLAGS ASM_FLAGS CC_FLAGS LD_FLAGS
13
14 # -----------------------------------------------------------------------------
15 # LUFA GCC Compiler Buildsystem Makefile Module.
16 # -----------------------------------------------------------------------------
17 # DESCRIPTION:
18 # Provides a set of targets to build a C, C++ and/or Assembly application
19 # via the AVR-GCC compiler.
20 # -----------------------------------------------------------------------------
21 # TARGETS:
22 #
23 # size - List built application size
24 # symbol-sizes - Print application symbols from the binary ELF
25 # file as a list sorted by size in bytes
26 # all - Build application and list size
27 # elf - Build application ELF debug object file
28 # hex - Build application HEX object files
29 # lss - Build application LSS assembly listing file
30 # clean - Remove output files
31 #
32 # MANDATORY PARAMETERS:
33 #
34 # TARGET - Application name
35 # ARCH - Device architecture name
36 # MCU - Microcontroller device model name
37 # SRC - List of input source files (*.c, *.cpp, *.S)
38 # F_USB - Speed of the input clock of the USB controller
39 # in Hz
40 # LUFA_PATH - Path to the LUFA library core
41 #
42 # OPTIONAL PARAMETERS:
43 #
44 # BOARD - LUFA board hardware
45 # OPTIMIZATION - Optimization level
46 # C_STANDARD - C Language Standard to use
47 # CPP_STANDARD - C++ Language Standard to use
48 # F_CPU - Speed of the CPU, in Hz
49 # C_FLAGS - Flags to pass to the C compiler only
50 # CPP_FLAGS - Flags to pass to the C++ compiler only
51 # ASM_FLAGS - Flags to pass to the assembler only
52 # CC_FLAGS - Common flags to pass to the C/C++ compiler and
53 # assembler
54 # LD_FLAGS - Flags to pass to the linker
55 #
56 # -----------------------------------------------------------------------------
57
58 # Sanity-check values of mandatory user-supplied variables
59 MCU ?= $(error Makefile MCU value not set)
60 TARGET ?= $(error Makefile TARGET value not set)
61 ARCH ?= $(error Makefile ARCH value not set)
62 SRC ?= $(error Makefile SRC value not set)
63 F_USB ?= $(error Makefile F_USB value not set)
64 LUFA_PATH ?= $(error Makefile LUFA_PATH value not set)
65
66 ifeq ($(MCU),)
67 $(error Makefile MCU option cannot be blank)
68 endif
69 ifeq ($(TARGET),)
70 $(error Makefile TARGET option cannot be blank)
71 endif
72 ifeq ($(ARCH),)
73 $(error Makefile ARCH option cannot be blank)
74 endif
75 ifeq ($(F_USB),)
76 $(error Makefile F_USB option cannot be blank)
77 endif
78
79 # Default values of optionally user-supplied variables
80 BOARD ?= NONE
81 OPTIMIZATION ?= s
82 F_CPU ?=
83 C_STANDARD ?= gnu99
84 CPP_STANDARD ?= gnu++98
85 C_FLAGS ?=
86 CPP_FLAGS ?=
87 ASM_FLAGS ?=
88 CC_FLAGS ?=
89
90 # Determine the utility prefix to use for the selected architecture
91 ifeq ($(ARCH), AVR8)
92 CROSS := avr-
93 else ifeq ($(ARCH), XMEGA)
94 CROSS := avr-
95 else ifeq ($(ARCH), UC3)
96 CROSS := avr32-
97 else
98 $(error Unsupported architecture "$(ARCH)".)
99 endif
100
101 # Output Messages
102 MSG_BUILD_BEGIN := Begin compilation of project \"$(TARGET)\"...
103 MSG_BUILD_END := Finished building project \"$(TARGET)\".
104 MSG_COMPILE_CMD := ' [CC] :'
105 MSG_ASSEMBLE_CMD := ' [AS] :'
106 MSG_NM_CMD := ' [NM] :'
107 MSG_REMOVE_CMD := ' [RM] :'
108 MSG_LINKER_CMD := ' [LNK] :'
109 MSG_SIZE_CMD := ' [SIZE] :'
110 MSG_OBJCPY_CMD := ' [OBJCPY] :'
111 MSG_OBJDMP_CMD := ' [OBJDMP] :'
112
113 # Convert input source file list to differentiate them by type
114 C_SOURCE = $(filter %.c, $(SRC))
115 CPP_SOURCE = $(filter %.cpp, $(SRC))
116 ASM_SOURCE = $(filter %.S, $(SRC))
117
118 # Convert input source filenames into a list of required output object files
119 OBJECT_FILES = $(filter %.o, $(C_SOURCE:%.c=%.o) $(CPP_SOURCE:%.cpp=%.o) $(ASM_SOURCE:%.S=%.o))
120 DEPENDENCY_FILES = $(OBJECT_FILES:%=%.d)
121
122 # Create a list of flags to pass to the compiler
123 ifeq ($(ARCH), AVR8)
124 CC_FLAGS += -mmcu=$(MCU) -gdwarf-2 -fshort-enums -fno-inline-small-functions -fpack-struct
125 else ifeq ($(ARCH), XMEGA)
126 CC_FLAGS += -mmcu=$(MCU) -gdwarf-2 -fshort-enums -fno-inline-small-functions -fpack-struct
127 else ifeq ($(ARCH), UC3)
128 CC_FLAGS += -mpart=$(MCU) -g3 -masm-addr-pseudos
129 endif
130 CC_FLAGS += -Wall -fno-strict-aliasing -funsigned-char -funsigned-bitfields -ffunction-sections
131 CC_FLAGS += -I. -I$(patsubst %/,%,$(LUFA_PATH))/..
132 CC_FLAGS += -DARCH=ARCH_$(ARCH) -DBOARD=BOARD_$(BOARD) -DF_USB=$(F_USB)UL
133 ifneq ($(F_CPU),)
134 CC_FLAGS += -DF_CPU=$(F_CPU)UL
135 endif
136
137 # Additional language specific compiler flags
138 C_FLAGS += -O$(OPTIMIZATION) -std=$(C_STANDARD) -MMD -MP -MF $@.d -Wstrict-prototypes
139 CPP_FLAGS += -O$(OPTIMIZATION) -std=$(CPP_STANDARD) -MMD -MP -MF $@.d
140
141 # Create a list of flags to pass to the linker
142 LD_FLAGS += -lm -Wl,-Map=$(TARGET).map,--cref -Wl,--gc-sections
143 ifeq ($(ARCH), UC3)
144 LD_FLAGS += --rodata-writable --direct-data
145 else
146 LD_FLAGS += -Wl,--relax
147 endif
148
149 # Create a list of unknown source file types, if any are found throw an error
150 UNKNOWN_SOURCE = $(filter-out $(C_SOURCE) $(CPP_SOURCE) $(ASM_SOURCE), $(SRC))
151 ifneq ($(UNKNOWN_SOURCE),)
152 $(error Unknown input source formats: $(UNKNOWN_SOURCE))
153 endif
154
155 # Determine flags to pass to the size utility based on its reported features
156 SIZE_MCU_FLAG := $(shell $(CROSS)size --help | grep -- --mcu > /dev/null && echo --mcu=$(MCU) )
157 SIZE_FORMAT_FLAG := $(shell $(CROSS)size --help | grep -- --format=.*avr > /dev/null && echo --format=avr )
158
159
160 begin:
161 @echo ""
162 @echo $(MSG_BUILD_BEGIN)
163 @echo ""
164
165 end:
166 @echo ""
167 @echo $(MSG_BUILD_END)
168 @echo ""
169
170 gcc_version:
171 @$(CROSS)gcc --version
172
173 check_source:
174 @for f in $(SRC); do \
175 if [ ! -f $$f ]; then \
176 echo "Error: Source file not found: $$f"; \
177 exit 1; \
178 fi; \
179 done
180
181 size: $(TARGET).elf
182 @echo $(MSG_SIZE_CMD) Determining size of \"$(TARGET).elf\"
183 @if test -f $(TARGET).elf; then \
184 $(CROSS)size $(SIZE_MCU_FLAG) $(SIZE_FORMAT_FLAG) $(TARGET).elf ; 2>/dev/null; \
185 fi
186
187 symbol-sizes: $(TARGET).elf
188 @echo $(MSG_NM_CMD) Extracting \"$(TARGET).elf\" symbols with decimal byte sizes
189 avr-nm --size-sort --demangle --radix=d $(TARGET).elf
190
191 clean:
192 @echo $(MSG_REMOVE_CMD) Removing object files of \"$(TARGET)\"
193 rm -f $(OBJECT_FILES)
194 @echo $(MSG_REMOVE_CMD) Removing dependency files of \"$(TARGET)\"
195 rm -f $(DEPENDENCY_FILES)
196 @echo $(MSG_REMOVE_CMD) Removing output files of \"$(TARGET)\"
197 rm -f $(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).map $(TARGET).lss
198
199 all: begin check_source gcc_version elf hex lss size end
200
201 elf: $(TARGET).elf
202 hex: $(TARGET).hex $(TARGET).eep
203 lss: $(TARGET).lss
204
205 %.o: %.c $(MAKEFILE_LIST)
206 @echo $(MSG_COMPILE_CMD) Compiling C file \"$<\"
207 $(CROSS)gcc -c $(CC_FLAGS) $(C_FLAGS) $< -o $@
208
209 %.o: %.cpp $(MAKEFILE_LIST)
210 @echo $(MSG_COMPILE_CMD) Compiling C++ file \"$<\"
211 $(CROSS)gcc -c $(CC_FLAGS) $(CPP_FLAGS) -x c++ $< -o $@
212
213 %.o: %.S $(MAKEFILE_LIST)
214 @echo $(MSG_ASSEMBLE_CMD) Assembling \"$<\"
215 $(CROSS)gcc -c $(CC_FLAGS) $(ASM_FLAGS) -x assembler-with-cpp $< -o $@
216
217 .PRECIOUS : $(OBJECT_FILES)
218 %.elf: $(OBJECT_FILES)
219 @echo $(MSG_LINKER_CMD) Linking object files into \"$@\"
220 $(CROSS)gcc $(CC_FLAGS) $(LD_FLAGS) $^ -o $@
221
222 %.hex: %.elf
223 @echo $(MSG_OBJCPY_CMD) Extracting HEX file data from \"$<\"
224 $(CROSS)objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@
225
226 %.eep: %.elf
227 @echo $(MSG_OBJCPY_CMD) Extracting EEP file data from \"$<\"
228 $(CROSS)objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings -O ihex $< $@ || exit 0
229
230 %.lss: %.elf
231 @echo $(MSG_OBJDMP_CMD) Extracting LSS file data from \"$<\"
232 $(CROSS)objdump -h -S -z $< > $@
233
234 # Include build dependency files
235 -include $(DEPENDENCY_FILES)
236
237 # Phony build targets for this module
238 .PHONY: begin end gcc_version check_source size symbol-sizes elf hex lss clean