3 # Copyright (C) Dean Camera, 2012.
5 # dean [at] fourwalledcubicle [dot] com
9 LUFA_BUILD_MODULES += BUILD
10 LUFA_BUILD_TARGETS += size check-source symbol-sizes all lib 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 # check-source - Print a list of SRC source files that cannot
30 # all - Build application and list size
31 # lib - Build and archive source files into a library
32 # elf - Build application ELF debug object file
33 # hex - Build application HEX object files
34 # lss - Build application LSS assembly listing file
35 # clean - Remove output files
37 # MANDATORY PARAMETERS:
39 # TARGET - Application name
40 # ARCH - Device architecture name
41 # MCU - Microcontroller device model name
42 # SRC - List of input source files (*.c, *.cpp, *.S)
43 # F_USB - Speed of the input clock of the USB controller
45 # LUFA_PATH - Path to the LUFA library core
47 # OPTIONAL PARAMETERS:
49 # BOARD - LUFA board hardware
50 # OPTIMIZATION - Optimization level
51 # C_STANDARD - C Language Standard to use
52 # CPP_STANDARD - C++ Language Standard to use
53 # F_CPU - Speed of the CPU, in Hz
54 # C_FLAGS - Flags to pass to the C compiler only
55 # CPP_FLAGS - Flags to pass to the C++ compiler only
56 # ASM_FLAGS - Flags to pass to the assembler only
57 # CC_FLAGS - Common flags to pass to the C/C++ compiler and
59 # LD_FLAGS - Flags to pass to the linker
60 # OBJDIR - Directory for the output object and dependency
61 # files; if equal to ".", the output files will
62 # be generated in the same folder as the sources
72 # -----------------------------------------------------------------------------
76 ERROR_IF_UNSET ?= $(if $(filter undefined, $(origin $(strip $(1)))), $(error Makefile $(strip $(1)) value not set))
77 ERROR_IF_EMPTY ?= $(if $(strip $($(strip $(1)))), , $(error Makefile $(strip $(1)) option cannot be blank))
78 ERROR_IF_NONBOOL ?= $(if $(filter Y N, $($(strip $(1)))), , $(error Makefile $(strip $(1)) option must be Y or N))
80 # Default values of optionally user-supplied variables
85 CPP_STANDARD ?= gnu++98
92 # Sanity check user supplied values
93 $(foreach MANDATORY_VAR, $(LUFA_BUILD_MANDATORY_VARS), $(call ERROR_IF_UNSET, $(MANDATORY_VAR)))
94 $(call ERROR_IF_EMPTY, MCU)
95 $(call ERROR_IF_EMPTY, TARGET)
96 $(call ERROR_IF_EMPTY, ARCH)
97 $(call ERROR_IF_EMPTY, F_USB)
98 $(call ERROR_IF_EMPTY, LUFA_PATH)
99 $(call ERROR_IF_EMPTY, BOARD)
100 $(call ERROR_IF_EMPTY, OPTIMIZATION)
101 $(call ERROR_IF_EMPTY, C_STANDARD)
102 $(call ERROR_IF_EMPTY, CPP_STANDARD)
103 $(call ERROR_IF_EMPTY, OBJDIR)
105 # Determine the utility prefix to use for the selected architecture
108 else ifeq ($(ARCH), XMEGA)
110 $(warning The XMEGA device support is currently EXPERIMENTAL (incomplete and/or non-functional), and is included for preview purposes only.)
111 else ifeq ($(ARCH), UC3)
113 $(warning The UC3 device support is currently EXPERIMENTAL (incomplete and/or non-functional), and is included for preview purposes only.)
115 $(error Unsupported architecture "$(ARCH)")
119 MSG_COMPILE_CMD := ' [GCC] :'
120 MSG_ASSEMBLE_CMD := ' [GAS] :'
121 MSG_NM_CMD := ' [NM] :'
122 MSG_REMOVE_CMD := ' [RM] :'
123 MSG_LINK_CMD := ' [LNK] :'
124 MSG_ARCHIVE_CMD := ' [AR] :'
125 MSG_SIZE_CMD := ' [SIZE] :'
126 MSG_OBJCPY_CMD := ' [OBJCPY] :'
127 MSG_OBJDMP_CMD := ' [OBJDMP] :'
129 # Convert input source file list to differentiate them by type
130 C_SOURCE := $(filter %.c, $(SRC))
131 CPP_SOURCE := $(filter %.cpp, $(SRC))
132 ASM_SOURCE := $(filter %.S, $(SRC))
134 # Create a list of unknown source file types, if any are found throw an error
135 UNKNOWN_SOURCE := $(filter-out $(C_SOURCE) $(CPP_SOURCE) $(ASM_SOURCE), $(SRC))
136 ifneq ($(UNKNOWN_SOURCE),)
137 $(error Unknown input source file formats: $(UNKNOWN_SOURCE))
140 # Convert input source filenames into a list of required output object files
141 OBJECT_FILES := $(addsuffix .o, $(basename $(SRC)))
143 $(shell mkdir $(OBJDIR) 2>&1 | /dev/null)
144 VPATH += $(dir $(SRC))
146 OBJECT_FILES := $(addprefix $(patsubst %/,%,$(OBJDIR))/, $(notdir $(OBJECT_FILES)))
149 DEPENDENCY_FILES := $(OBJECT_FILES:%.o=%.d)
151 # Create a list of common flags to pass to the compiler/linker/assembler
152 BASE_CC_FLAGS := -pipe
154 BASE_CC_FLAGS += -mmcu=$(MCU) -gdwarf-2 -fshort-enums -fno-inline-small-functions -fpack-struct
155 else ifeq ($(ARCH), XMEGA)
156 BASE_CC_FLAGS += -mmcu=$(MCU) -gdwarf-2 -fshort-enums -fno-inline-small-functions -fpack-struct
157 else ifeq ($(ARCH), UC3)
158 BASE_CC_FLAGS += -mpart=$(MCU:at32%=%) -g3 -masm-addr-pseudos
160 BASE_CC_FLAGS += -Wall -fno-strict-aliasing -funsigned-char -funsigned-bitfields -ffunction-sections
161 BASE_CC_FLAGS += -I. -I$(patsubst %/,%,$(LUFA_PATH))/..
162 BASE_CC_FLAGS += -DARCH=ARCH_$(ARCH) -DBOARD=BOARD_$(BOARD) -DF_USB=$(F_USB)UL
164 BASE_CC_FLAGS += -DF_CPU=$(F_CPU)UL
167 # Additional language specific compiler flags
168 BASE_C_FLAGS := -x c -O$(OPTIMIZATION) -std=$(C_STANDARD) -Wstrict-prototypes
169 BASE_CPP_FLAGS := -x c++ -O$(OPTIMIZATION) -std=$(CPP_STANDARD)
170 BASE_ASM_FLAGS := -x assembler-with-cpp
172 # Create a list of flags to pass to the linker
173 BASE_LD_FLAGS := -lm -Wl,-Map=$(TARGET).map,--cref -Wl,--gc-sections
175 BASE_LD_FLAGS += --rodata-writable --direct-data
177 BASE_LD_FLAGS += -Wl,--relax
180 # Determine flags to pass to the size utility based on its reported features
181 SIZE_MCU_FLAG := $(shell $(CROSS)-size --help | grep -- --mcu > /dev/null && echo --mcu=$(MCU) )
182 SIZE_FORMAT_FLAG := $(shell $(CROSS)-size --help | grep -- --format=.*avr > /dev/null && echo --format=avr )
187 @echo Begin compilation of project \"$(TARGET)\"...
191 @echo Finished building project \"$(TARGET)\".
195 @$(CROSS)-gcc --version
198 @for f in $(SRC); do \
199 if [ ! -f $$f ]; then \
200 echo "Error: Source file not found: $$f"; \
206 @echo $(MSG_SIZE_CMD) Determining size of \"$<\"
208 $(CROSS)-size $(SIZE_MCU_FLAG) $(SIZE_FORMAT_FLAG) $< ; 2>/dev/null;
210 symbol-sizes: $(TARGET).elf
211 @echo $(MSG_NM_CMD) Extracting \"$<\" symbols with decimal byte sizes
212 avr-nm --size-sort --demangle --radix=d $<
215 @echo $(MSG_REMOVE_CMD) Removing object files of \"$(TARGET)\"
216 rm -f $(OBJECT_FILES)
217 @echo $(MSG_REMOVE_CMD) Removing dependency files of \"$(TARGET)\"
218 rm -f $(DEPENDENCY_FILES)
219 @echo $(MSG_REMOVE_CMD) Removing output files of \"$(TARGET)\"
220 rm -f $(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).map $(TARGET).lss $(TARGET).sym $(TARGET).a
222 all: build_begin check-source gcc-version elf hex lss sym size build_end
226 hex: $(TARGET).hex $(TARGET).eep
230 $(OBJDIR)/%.o: %.c $(MAKEFILE_LIST)
231 @echo $(MSG_COMPILE_CMD) Compiling C file \"$(notdir $<)\"
232 $(CROSS)-gcc -c $(BASE_CC_FLAGS) $(BASE_C_FLAGS) $(CC_FLAGS) $(C_FLAGS) -MMD -MP -MF $(@:%.o=%.d) $< -o $@
234 $(OBJDIR)/%.o: %.cpp $(MAKEFILE_LIST)
235 @echo $(MSG_COMPILE_CMD) Compiling C++ file \"$(notdir $<)\"
236 $(CROSS)-gcc -c $(BASE_CC_FLAGS) $(BASE_CPP_FLAGS) $(CC_FLAGS) $(CPP_FLAGS) -MMD -MP -MF $(@:%.o=%.d) $< -o $@
238 $(OBJDIR)/%.o: %.S $(MAKEFILE_LIST)
239 @echo $(MSG_ASSEMBLE_CMD) Assembling \"$(notdir $<)\"
240 $(CROSS)-gcc -c $(BASE_CC_FLAGS) $(BASE_ASM_FLAGS) $(CC_FLAGS) $(ASM_FLAGS) $< -o $@
242 .PRECIOUS : $(OBJECT_FILES)
245 @echo $(MSG_ARCHIVE_CMD) Archiving object files into \"$@\"
246 $(CROSS)-ar rcs $@ $(OBJECT_FILES)
248 .PRECIOUS : $(OBJECT_FILES)
250 %.elf: $(OBJECT_FILES)
251 @echo $(MSG_LINK_CMD) Linking object files into \"$@\"
252 $(CROSS)-gcc $(BASE_CC_FLAGS) $(BASE_LD_FLAGS) $(CC_FLAGS) $(LD_FLAGS) $^ -o $@
255 @echo $(MSG_OBJCPY_CMD) Extracting HEX file data from \"$<\"
256 $(CROSS)-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@
259 @echo $(MSG_OBJCPY_CMD) Extracting EEP file data from \"$<\"
260 $(CROSS)-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings -O ihex $< $@ || exit 0
263 @echo $(MSG_OBJDMP_CMD) Extracting LSS file data from \"$<\"
264 $(CROSS)-objdump -h -S -z $< > $@
267 @echo $(MSG_NM_CMD) Extracting SYM file data from \"$<\"
268 $(CROSS)-nm -n $< > $@
270 # Include build dependency files
271 -include $(DEPENDENCY_FILES)
273 # Phony build targets for this module
274 .PHONY: build_begin build_end gcc-version check-source size symbol-sizes lib elf hex lss clean