3 # Copyright (C) Dean Camera, 2012.
5 # dean [at] fourwalledcubicle [dot] com
9 LUFA_BUILD_MODULES += BUILD
10 LUFA_BUILD_TARGETS += size 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 # all - Build application and list size
25 # elf - Build application ELF debug object file
26 # hex - Build application HEX object files
27 # lss - Build application LSS assembly listing file
28 # clean - Remove output files
30 # MANDATORY PARAMETERS:
32 # TARGET - Application name
33 # ARCH - Device architecture name
34 # MCU - Microcontroller device model name
35 # SRC - List of input source files (.c, .cpp/.c++, .S)
36 # F_USB - Speed of the input clock of the USB controller
38 # LUFA_PATH - Path to the LUFA library core
40 # OPTIONAL PARAMETERS:
42 # BOARD - LUFA board hardware
43 # OPTIMIZATION - Optimization level
44 # C_STANDARD - C Language Standard to use
45 # CPP_STANDARD - C++ Language Standard to use
46 # F_CPU - Speed of the CPU, in Hz
47 # C_FLAGS - Flags to pass to the C compiler only
48 # CPP_FLAGS - Flags to pass to the C++ compiler only
49 # ASM_FLAGS - Flags to pass to the assembler only
50 # CC_FLAGS - Common flags to pass to the C/C++ compiler and
52 # LD_FLAGS - Flags to pass to the linker
54 # -----------------------------------------------------------------------------
56 # Sanity-check values of mandatory user-supplied variables
57 MCU ?= $(error Makefile MCU value not set.)
58 TARGET ?= $(error Makefile TARGET value not set.)
59 ARCH ?= $(error Makefile ARCH value not set.)
60 SRC ?= $(error Makefile SRC value not set.)
61 F_USB ?= $(error Makefile F_USB value not set.)
62 LUFA_PATH ?= $(error Makefile LUFA_PATH value not set.)
64 # Default values of optionally user-supplied variables
69 CPP_STANDARD ?= gnu++98
75 # Determine the utility prefix to use for the selected architecture
78 else ifeq ($(ARCH), XMEGA)
80 else ifeq ($(ARCH), UC3)
83 $(error Unsupported architecture.)
87 MSG_BUILD_BEGIN := Begin compilation of project \"$(TARGET)\"...
88 MSG_BUILD_END := Finished building project \"$(TARGET)\".
89 MSG_COMPILE_CMD := ' [CC] :'
90 MSG_REMOVE_CMD := ' [RM] :'
91 MSG_LINKER_CMD := ' [LNK] :'
92 MSG_SIZE_CMD := ' [SIZE] :'
93 MSG_OBJCPY_CMD := ' [OBJCPY] :'
94 MSG_OBJDMP_CMD := ' [OBJDMP] :'
96 # Convert input source file list to differentiate them by type
97 C_SOURCE = $(filter %.c, $(SRC))
98 CPP_SOURCE = $(filter %.cpp, $(SRC))
99 ASM_SOURCE = $(filter %.S, $(SRC))
101 # Convert input source filenames into a list of required output object files
102 OBJECT_FILES = $(filter %.o, $(C_SOURCE:%.c=%.o) $(CPP_SOURCE:%.cpp=%.o) $(ASM_SOURCE:%.S=%.o))
103 DEPENDENCY_FILES = $(OBJECT_FILES:%=%.d)
105 # Create a list of flags to pass to the compiler
107 CC_FLAGS += -mmcu=$(MCU) -gdwarf-2 -fshort-enums -fno-inline-small-functions -fpack-struct
108 else ifeq ($(ARCH), XMEGA)
109 CC_FLAGS += -mmcu=$(MCU) -gdwarf-2 -fshort-enums -fno-inline-small-functions -fpack-struct
110 else ifeq ($(ARCH), UC3)
111 CC_FLAGS += -mpart=$(MCU) -g3 -masm-addr-pseudos
113 CC_FLAGS += -Wall -fno-strict-aliasing -funsigned-char -funsigned-bitfields -ffunction-sections
114 CC_FLAGS += -I. -I$(patsubst %/,%,$(LUFA_PATH))/..
115 CC_FLAGS += -DARCH=ARCH_$(ARCH) -DBOARD=BOARD_$(BOARD) -DF_USB=$(F_USB)UL
117 CC_FLAGS += -DF_CPU=$(F_CPU)UL
120 # Additional language specific compiler flags
121 C_FLAGS += -O$(OPTIMIZATION) -std=$(C_STANDARD) -MMD -MP -MF $@.d -Wstrict-prototypes
122 CPP_FLAGS += -O$(OPTIMIZATION) -std=$(CPP_STANDARD) -MMD -MP -MF $@.d
124 # Create a list of flags to pass to the linker
125 LD_FLAGS += -lm -Wl,-Map=$(TARGET).map,--cref -Wl,--gc-sections
127 LD_FLAGS += --rodata-writable --direct-data
129 LD_FLAGS += -Wl,--relax
132 # Create a list of unknown source file types, if any are found throw an error
133 UNKNOWN_SOURCE = $(filter-out $(C_SOURCE) $(CPP_SOURCE) $(ASM_SOURCE), $(SRC))
134 ifneq ($(UNKNOWN_SOURCE),)
135 $(error Unknown input source formats: $(UNKNOWN_SOURCE))
138 # Determine flags to pass to the size utility based on its reported features
139 SIZE_MCU_FLAG := $(shell $(CROSS)size --help | grep -- --mcu > /dev/null && echo --mcu=$(MCU) )
140 SIZE_FORMAT_FLAG := $(shell $(CROSS)size --help | grep -- --format=.*avr > /dev/null && echo --format=avr )
145 @echo $(MSG_BUILD_BEGIN)
150 @echo $(MSG_BUILD_END)
154 @$(CROSS)gcc --version
157 @for f in $(SRC); do \
158 if [ ! -f $$f ]; then \
159 echo "Error: Source file not found: $$f"; \
165 @echo $(MSG_SIZE_CMD) Determining size of \"$(TARGET).elf\"
166 @if test -f $(TARGET).elf; then \
167 $(CROSS)size $(SIZE_MCU_FLAG) $(SIZE_FORMAT_FLAG) $(TARGET).elf ; 2>/dev/null; \
171 @echo $(MSG_REMOVE_CMD) Removing object files \"$(strip $(notdir $(OBJECT_FILES)))\"
172 rm -f $(OBJECT_FILES)
173 @echo $(MSG_REMOVE_CMD) Removing dependency files \"$(strip $(notdir $(DEPENDENCY_FILES)))\"
174 rm -f $(DEPENDENCY_FILES)
175 @echo $(MSG_REMOVE_CMD) Removing output files \"$(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).map $(TARGET).lss\"
176 rm -f $(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).map $(TARGET).lss
178 all: begin check_source gcc_version elf hex lss size end
181 hex: $(TARGET).hex $(TARGET).eep
184 %.o: %.c $(MAKEFILE_LIST)
185 @echo $(MSG_COMPILE_CMD) Compiling C file \"$<\"
186 $(CROSS)gcc -c $(CC_FLAGS) $(C_FLAGS) $< -o $@
188 %.o: %.cpp $(MAKEFILE_LIST)
189 @echo $(MSG_COMPILE_CMD) Compiling C++ file \"$<\"
190 $(CROSS)gcc -c $(CC_FLAGS) $(CPP_FLAGS) -x c++ $< -o $@
192 %.o: %.S $(MAKEFILE_LIST)
193 @echo $(MSG_COMPILE_CMD) Assembling \"$<\"
194 $(CROSS)gcc -c $(CC_FLAGS) $(ASM_FLAGS) -x assembler-with-cpp $< -o $@
196 .PRECIOUS : $(OBJECT_FILES)
197 %.elf: $(OBJECT_FILES)
198 @echo $(MSG_LINKER_CMD) Linking object files into \"$@\"
199 $(CROSS)gcc $(CC_FLAGS) $(LD_FLAGS) $^ -o $@
202 @echo $(MSG_OBJCPY_CMD) Extracting HEX file data from \"$<\"
203 $(CROSS)objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@
206 @echo $(MSG_OBJCPY_CMD) Extracting EEP file data from \"$<\"
207 $(CROSS)objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings -O ihex $< $@ || exit 0
210 @echo $(MSG_OBJDMP_CMD) Extracting LSS file data from \"$<\"
211 $(CROSS)objdump -h -S -z $< > $@
213 # Include build dependency files
214 -include $(DEPENDENCY_FILES)
216 # Phony build targets for this module
217 .PHONY: begin end gcc_version check_source size elf hex lss clean