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
14 # -----------------------------------------------------------------------------
15 # LUFA GCC Compiler Buildsystem Makefile Module.
16 # -----------------------------------------------------------------------------
18 # Provides a set of targets to build a C, C++ and/or Assembly application
19 # via the AVR-GCC compiler.
20 # -----------------------------------------------------------------------------
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
32 # MANDATORY PARAMETERS:
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
40 # LUFA_PATH - Path to the LUFA library core
42 # OPTIONAL PARAMETERS:
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
54 # LD_FLAGS - Flags to pass to the linker
56 # -----------------------------------------------------------------------------
58 ERROR_IF_UNSET = $(if $(filter undefined, $(origin $(strip $(1)))), $(error Makefile $(strip $(1)) value not set))
59 ERROR_IF_EMPTY = $(if $(strip $($(strip $(1)))), , $(error Makefile $(strip $(1)) option cannot be blank))
60 ERROR_IF_NONBOOL = $(if $(filter Y N, $($(strip $(1)))), , $(error Makefile $(strip $(1)) option must be Y or N))
62 # Default values of optionally user-supplied variables
67 CPP_STANDARD ?= gnu++98
73 # Sanity check user supplied values
74 $(foreach MANDATORY_VAR, $(LUFA_BUILD_MANDATORY_VARS), $(call ERROR_IF_UNSET, $(MANDATORY_VAR)))
75 $(call ERROR_IF_EMPTY, MCU)
76 $(call ERROR_IF_EMPTY, TARGET)
77 $(call ERROR_IF_EMPTY, ARCH)
78 $(call ERROR_IF_EMPTY, F_USB)
79 $(call ERROR_IF_EMPTY, LUFA_PATH)
80 $(call ERROR_IF_EMPTY, BOARD)
81 $(call ERROR_IF_EMPTY, OPTIMIZATION)
82 $(call ERROR_IF_EMPTY, C_STANDARD)
83 $(call ERROR_IF_EMPTY, CPP_STANDARD)
85 # Determine the utility prefix to use for the selected architecture
88 else ifeq ($(ARCH), XMEGA)
90 $(warning The XMEGA device support is currently EXPERIMENTAL (incomplete and/or non-functional), and is included for preview purposes only.)
91 else ifeq ($(ARCH), UC3)
93 $(warning The UC3 device support is currently EXPERIMENTAL (incomplete and/or non-functional), and is included for preview purposes only.)
95 $(error Unsupported architecture "$(ARCH)")
99 MSG_BUILD_BEGIN := Begin compilation of project \"$(TARGET)\"...
100 MSG_BUILD_END := Finished building project \"$(TARGET)\".
101 MSG_COMPILE_CMD := ' [CC] :'
102 MSG_ASSEMBLE_CMD := ' [AS] :'
103 MSG_NM_CMD := ' [NM] :'
104 MSG_REMOVE_CMD := ' [RM] :'
105 MSG_LINKER_CMD := ' [LNK] :'
106 MSG_SIZE_CMD := ' [SIZE] :'
107 MSG_OBJCPY_CMD := ' [OBJCPY] :'
108 MSG_OBJDMP_CMD := ' [OBJDMP] :'
110 # Convert input source file list to differentiate them by type
111 C_SOURCE = $(filter %.c, $(SRC))
112 CPP_SOURCE = $(filter %.cpp, $(SRC))
113 ASM_SOURCE = $(filter %.S, $(SRC))
115 # Create a list of unknown source file types, if any are found throw an error
116 UNKNOWN_SOURCE = $(filter-out $(C_SOURCE) $(CPP_SOURCE) $(ASM_SOURCE), $(SRC))
117 ifneq ($(UNKNOWN_SOURCE),)
118 $(error Unknown input source formats: $(UNKNOWN_SOURCE))
121 # Convert input source filenames into a list of required output object files
122 OBJECT_FILES = $(filter %.o, $(C_SOURCE:%.c=%.o) $(CPP_SOURCE:%.cpp=%.o) $(ASM_SOURCE:%.S=%.o))
123 DEPENDENCY_FILES = $(OBJECT_FILES:%.o=%.d)
125 # Create a list of common flags to pass to the compiler/linker/assembler
128 BASE_CC_FLAGS += -mmcu=$(MCU) -gdwarf-2 -fshort-enums -fno-inline-small-functions -fpack-struct
129 else ifeq ($(ARCH), XMEGA)
130 BASE_CC_FLAGS += -mmcu=$(MCU) -gdwarf-2 -fshort-enums -fno-inline-small-functions -fpack-struct
131 else ifeq ($(ARCH), UC3)
132 BASE_CC_FLAGS += -mpart=$(MCU:at32%=%) -g3 -masm-addr-pseudos
134 BASE_CC_FLAGS += -Wall -fno-strict-aliasing -funsigned-char -funsigned-bitfields -ffunction-sections
135 BASE_CC_FLAGS += -I. -I$(patsubst %/,%,$(LUFA_PATH))/..
136 BASE_CC_FLAGS += -DARCH=ARCH_$(ARCH) -DBOARD=BOARD_$(BOARD) -DF_USB=$(F_USB)UL
138 BASE_CC_FLAGS += -DF_CPU=$(F_CPU)UL
141 # Additional language specific compiler flags
142 BASE_C_FLAGS := -x c -O$(OPTIMIZATION) -std=$(C_STANDARD) -Wstrict-prototypes
143 BASE_CPP_FLAGS := -x c++ -O$(OPTIMIZATION) -std=$(CPP_STANDARD)
144 BASE_ASM_FLAGS := -x assembler-with-cpp
146 # Create a list of flags to pass to the linker
147 BASE_LD_FLAGS := -lm -Wl,-Map=$(TARGET).map,--cref -Wl,--gc-sections
149 BASE_LD_FLAGS += --rodata-writable --direct-data
151 BASE_LD_FLAGS += -Wl,--relax
154 # Determine flags to pass to the size utility based on its reported features
155 SIZE_MCU_FLAG := $(shell $(CROSS)size --help | grep -- --mcu > /dev/null && echo --mcu=$(MCU) )
156 SIZE_FORMAT_FLAG := $(shell $(CROSS)size --help | grep -- --format=.*avr > /dev/null && echo --format=avr )
161 @echo $(MSG_BUILD_BEGIN)
166 @echo $(MSG_BUILD_END)
170 @$(CROSS)gcc --version
173 @for f in $(SRC); do \
174 if [ ! -f $$f ]; then \
175 echo "Error: Source file not found: $$f"; \
181 @echo $(MSG_SIZE_CMD) Determining size of \"$<\"
182 @if test -f $(TARGET).elf; then \
183 $(CROSS)size $(SIZE_MCU_FLAG) $(SIZE_FORMAT_FLAG) $< ; 2>/dev/null; \
186 symbol-sizes: $(TARGET).elf
187 @echo $(MSG_NM_CMD) Extracting \"$<\" symbols with decimal byte sizes
188 avr-nm --size-sort --demangle --radix=d $<
191 @echo $(MSG_REMOVE_CMD) Removing object files of \"$(TARGET)\"
192 rm -f $(OBJECT_FILES)
193 @echo $(MSG_REMOVE_CMD) Removing dependency files of \"$(TARGET)\"
194 rm -f $(DEPENDENCY_FILES)
195 @echo $(MSG_REMOVE_CMD) Removing output files of \"$(TARGET)\"
196 rm -f $(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).map $(TARGET).lss
198 all: begin check_source gcc_version elf hex lss size end
201 hex: $(TARGET).hex $(TARGET).eep
204 %.o: %.c $(MAKEFILE_LIST)
205 @echo $(MSG_COMPILE_CMD) Compiling C file \"$<\"
206 $(CROSS)gcc -c $(BASE_CC_FLAGS) $(BASE_C_FLAGS) $(CC_FLAGS) $(C_FLAGS) -MMD -MP -MF $(@:%.o=%.d) $< -o $@
208 %.o: %.cpp $(MAKEFILE_LIST)
209 @echo $(MSG_COMPILE_CMD) Compiling C++ file \"$<\"
210 $(CROSS)gcc -c $(BASE_CC_FLAGS) $(BASE_CPP_FLAGS) $(CC_FLAGS) $(CPP_FLAGS) -MMD -MP -MF $(@:%.o=%.d) $< -o $@
212 %.o: %.S $(MAKEFILE_LIST)
213 @echo $(MSG_ASSEMBLE_CMD) Assembling \"$<\"
214 $(CROSS)gcc -c $(BASE_CC_FLAGS) $(BASE_ASM_FLAGS) $(CC_FLAGS) $(ASM_FLAGS) $< -o $@
216 .PRECIOUS : $(OBJECT_FILES)
217 %.elf: $(OBJECT_FILES)
218 @echo $(MSG_LINKER_CMD) Linking object files into \"$@\"
219 $(CROSS)gcc $(BASE_CC_FLAGS) $(BASE_LD_FLAGS) $(CC_FLAGS) $(LD_FLAGS) $^ -o $@
222 @echo $(MSG_OBJCPY_CMD) Extracting HEX file data from \"$<\"
223 $(CROSS)objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@
226 @echo $(MSG_OBJCPY_CMD) Extracting EEP file data from \"$<\"
227 $(CROSS)objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings -O ihex $< $@ || exit 0
230 @echo $(MSG_OBJDMP_CMD) Extracting LSS file data from \"$<\"
231 $(CROSS)objdump -h -S -z $< > $@
233 # Include build dependency files
234 -include $(DEPENDENCY_FILES)
236 # Phony build targets for this module
237 .PHONY: begin end gcc_version check_source size symbol-sizes elf hex lss clean