3 # Copyright (C) Dean Camera, 2012.
5 # dean [at] fourwalledcubicle [dot] com
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 OBJDIR
13 LUFA_BUILD_PROVIDED_VARS +=
14 LUFA_BUILD_PROVIDED_MACROS +=
16 # -----------------------------------------------------------------------------
17 # LUFA GCC Compiler Buildsystem Makefile Module.
18 # -----------------------------------------------------------------------------
20 # Provides a set of targets to build a C, C++ and/or Assembly application
21 # via the AVR-GCC compiler.
22 # -----------------------------------------------------------------------------
25 # size - List built application size
26 # symbol-sizes - Print application symbols from the binary ELF
27 # file as a list sorted by size in bytes
28 # all - Build application and list size
29 # elf - Build application ELF debug object file
30 # hex - Build application HEX object files
31 # lss - Build application LSS assembly listing file
32 # clean - Remove output files
34 # MANDATORY PARAMETERS:
36 # TARGET - Application name
37 # ARCH - Device architecture name
38 # MCU - Microcontroller device model name
39 # SRC - List of input source files (*.c, *.cpp, *.S)
40 # F_USB - Speed of the input clock of the USB controller
42 # LUFA_PATH - Path to the LUFA library core
44 # OPTIONAL PARAMETERS:
46 # BOARD - LUFA board hardware
47 # OPTIMIZATION - Optimization level
48 # C_STANDARD - C Language Standard to use
49 # CPP_STANDARD - C++ Language Standard to use
50 # F_CPU - Speed of the CPU, in Hz
51 # C_FLAGS - Flags to pass to the C compiler only
52 # CPP_FLAGS - Flags to pass to the C++ compiler only
53 # ASM_FLAGS - Flags to pass to the assembler only
54 # CC_FLAGS - Common flags to pass to the C/C++ compiler and
56 # LD_FLAGS - Flags to pass to the linker
57 # OBJDIR - Directory for the output object and dependency
58 # files; if equal to ".", the output files will
59 # be generated in the same folder as the sources
69 # -----------------------------------------------------------------------------
71 ERROR_IF_UNSET = $(if $(filter undefined, $(origin $(strip $(1)))), $(error Makefile $(strip $(1)) value not set))
72 ERROR_IF_EMPTY = $(if $(strip $($(strip $(1)))), , $(error Makefile $(strip $(1)) option cannot be blank))
73 ERROR_IF_NONBOOL = $(if $(filter Y N, $($(strip $(1)))), , $(error Makefile $(strip $(1)) option must be Y or N))
75 # Default values of optionally user-supplied variables
80 CPP_STANDARD ?= gnu++98
87 # Sanity check user supplied values
88 $(foreach MANDATORY_VAR, $(LUFA_BUILD_MANDATORY_VARS), $(call ERROR_IF_UNSET, $(MANDATORY_VAR)))
89 $(call ERROR_IF_EMPTY, MCU)
90 $(call ERROR_IF_EMPTY, TARGET)
91 $(call ERROR_IF_EMPTY, ARCH)
92 $(call ERROR_IF_EMPTY, F_USB)
93 $(call ERROR_IF_EMPTY, LUFA_PATH)
94 $(call ERROR_IF_EMPTY, BOARD)
95 $(call ERROR_IF_EMPTY, OPTIMIZATION)
96 $(call ERROR_IF_EMPTY, C_STANDARD)
97 $(call ERROR_IF_EMPTY, CPP_STANDARD)
98 $(call ERROR_IF_EMPTY, OBJDIR)
100 # Determine the utility prefix to use for the selected architecture
103 else ifeq ($(ARCH), XMEGA)
105 $(warning The XMEGA device support is currently EXPERIMENTAL (incomplete and/or non-functional), and is included for preview purposes only.)
106 else ifeq ($(ARCH), UC3)
108 $(warning The UC3 device support is currently EXPERIMENTAL (incomplete and/or non-functional), and is included for preview purposes only.)
110 $(error Unsupported architecture "$(ARCH)")
114 MSG_BUILD_BEGIN := Begin compilation of project \"$(TARGET)\"...
115 MSG_BUILD_END := Finished building project \"$(TARGET)\".
116 MSG_COMPILE_CMD := ' [CC] :'
117 MSG_ASSEMBLE_CMD := ' [AS] :'
118 MSG_NM_CMD := ' [NM] :'
119 MSG_REMOVE_CMD := ' [RM] :'
120 MSG_LINKER_CMD := ' [LNK] :'
121 MSG_SIZE_CMD := ' [SIZE] :'
122 MSG_OBJCPY_CMD := ' [OBJCPY] :'
123 MSG_OBJDMP_CMD := ' [OBJDMP] :'
125 # Convert input source file list to differentiate them by type
126 C_SOURCE = $(filter %.c, $(SRC))
127 CPP_SOURCE = $(filter %.cpp, $(SRC))
128 ASM_SOURCE = $(filter %.S, $(SRC))
130 # Create a list of unknown source file types, if any are found throw an error
131 UNKNOWN_SOURCE = $(filter-out $(C_SOURCE) $(CPP_SOURCE) $(ASM_SOURCE), $(SRC))
132 ifneq ($(UNKNOWN_SOURCE),)
133 $(error Unknown input source formats: $(UNKNOWN_SOURCE))
136 # Convert input source filenames into a list of required output object files
137 OBJECT_FILES = $(filter %.o, $(C_SOURCE:%.c=%.o) $(CPP_SOURCE:%.cpp=%.o) $(ASM_SOURCE:%.S=%.o))
139 $(shell mkdir $(OBJDIR) 2>&1 | /dev/null)
140 VPATH += $(dir $(SRC))
142 OBJECT_FILES := $(addprefix $(patsubst %/,%,$(OBJDIR))/, $(notdir $(OBJECT_FILES)))
145 DEPENDENCY_FILES = $(OBJECT_FILES:%.o=%.d)
147 # Create a list of common flags to pass to the compiler/linker/assembler
150 BASE_CC_FLAGS += -mmcu=$(MCU) -gdwarf-2 -fshort-enums -fno-inline-small-functions -fpack-struct
151 else ifeq ($(ARCH), XMEGA)
152 BASE_CC_FLAGS += -mmcu=$(MCU) -gdwarf-2 -fshort-enums -fno-inline-small-functions -fpack-struct
153 else ifeq ($(ARCH), UC3)
154 BASE_CC_FLAGS += -mpart=$(MCU:at32%=%) -g3 -masm-addr-pseudos
156 BASE_CC_FLAGS += -Wall -fno-strict-aliasing -funsigned-char -funsigned-bitfields -ffunction-sections
157 BASE_CC_FLAGS += -I. -I$(patsubst %/,%,$(LUFA_PATH))/..
158 BASE_CC_FLAGS += -DARCH=ARCH_$(ARCH) -DBOARD=BOARD_$(BOARD) -DF_USB=$(F_USB)UL
160 BASE_CC_FLAGS += -DF_CPU=$(F_CPU)UL
163 # Additional language specific compiler flags
164 BASE_C_FLAGS := -x c -O$(OPTIMIZATION) -std=$(C_STANDARD) -Wstrict-prototypes
165 BASE_CPP_FLAGS := -x c++ -O$(OPTIMIZATION) -std=$(CPP_STANDARD)
166 BASE_ASM_FLAGS := -x assembler-with-cpp
168 # Create a list of flags to pass to the linker
169 BASE_LD_FLAGS := -lm -Wl,-Map=$(TARGET).map,--cref -Wl,--gc-sections
171 BASE_LD_FLAGS += --rodata-writable --direct-data
173 BASE_LD_FLAGS += -Wl,--relax
176 # Determine flags to pass to the size utility based on its reported features
177 SIZE_MCU_FLAG := $(shell $(CROSS)size --help | grep -- --mcu > /dev/null && echo --mcu=$(MCU) )
178 SIZE_FORMAT_FLAG := $(shell $(CROSS)size --help | grep -- --format=.*avr > /dev/null && echo --format=avr )
183 @echo $(MSG_BUILD_BEGIN)
187 @echo $(MSG_BUILD_END)
191 @$(CROSS)gcc --version
194 @for f in $(SRC); do \
195 if [ ! -f $$f ]; then \
196 echo "Error: Source file not found: $$f"; \
202 @echo $(MSG_SIZE_CMD) Determining size of \"$<\"
204 $(CROSS)size $(SIZE_MCU_FLAG) $(SIZE_FORMAT_FLAG) $< ; 2>/dev/null;
206 symbol-sizes: $(TARGET).elf
207 @echo $(MSG_NM_CMD) Extracting \"$<\" symbols with decimal byte sizes
208 avr-nm --size-sort --demangle --radix=d $<
211 @echo $(MSG_REMOVE_CMD) Removing object files of \"$(TARGET)\"
212 rm -f $(OBJECT_FILES)
213 @echo $(MSG_REMOVE_CMD) Removing dependency files of \"$(TARGET)\"
214 rm -f $(DEPENDENCY_FILES)
215 @echo $(MSG_REMOVE_CMD) Removing output files of \"$(TARGET)\"
216 rm -f $(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).map $(TARGET).lss $(TARGET).sym
218 all: build_begin check_source gcc_version elf hex lss sym size build_end
221 hex: $(TARGET).hex $(TARGET).eep
225 $(OBJDIR)/%.o: %.c $(MAKEFILE_LIST)
226 @echo $(MSG_COMPILE_CMD) Compiling C file \"$(notdir $<)\"
227 $(CROSS)gcc -c $(BASE_CC_FLAGS) $(BASE_C_FLAGS) $(CC_FLAGS) $(C_FLAGS) -MMD -MP -MF $(@:%.o=%.d) $< -o $@
229 $(OBJDIR)/%.o: %.cpp $(MAKEFILE_LIST)
230 @echo $(MSG_COMPILE_CMD) Compiling C++ file \"$(notdir $<)\"
231 $(CROSS)gcc -c $(BASE_CC_FLAGS) $(BASE_CPP_FLAGS) $(CC_FLAGS) $(CPP_FLAGS) -MMD -MP -MF $(@:%.o=%.d) $< -o $@
233 $(OBJDIR)/%.o: %.S $(MAKEFILE_LIST)
234 @echo $(MSG_ASSEMBLE_CMD) Assembling \"$(notdir $<)\"
235 $(CROSS)gcc -c $(BASE_CC_FLAGS) $(BASE_ASM_FLAGS) $(CC_FLAGS) $(ASM_FLAGS) $< -o $@
237 .PRECIOUS : $(OBJECT_FILES)
239 %.elf: $(OBJECT_FILES)
240 @echo $(MSG_LINKER_CMD) Linking object files into \"$@\"
241 $(CROSS)gcc $(BASE_CC_FLAGS) $(BASE_LD_FLAGS) $(CC_FLAGS) $(LD_FLAGS) $^ -o $@
244 @echo $(MSG_OBJCPY_CMD) Extracting HEX file data from \"$<\"
245 $(CROSS)objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@
248 @echo $(MSG_OBJCPY_CMD) Extracting EEP file data from \"$<\"
249 $(CROSS)objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings -O ihex $< $@ || exit 0
252 @echo $(MSG_OBJDMP_CMD) Extracting LSS file data from \"$<\"
253 $(CROSS)objdump -h -S -z $< > $@
256 @echo $(MSG_NM_CMD) Extracting SYM file data from \"$<\"
257 $(CROSS)nm -n $< > $@
259 # Include build dependency files
260 -include $(DEPENDENCY_FILES)
262 # Phony build targets for this module
263 .PHONY: build_begin build_end gcc_version check_source size symbol-sizes elf hex lss clean