3 # Copyright (C) Dean Camera, 2012.
5 # dean [at] fourwalledcubicle [dot] com
9 LUFA_BUILD_MODULES += BUILD
10 LUFA_BUILD_TARGETS += size checksource all elf hex 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 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 application size
24 # checksource - Check existance of listed input source files
25 # all - Build application and list size
26 # elf - Build application ELF debug object file
27 # hex - Build application HEX object files
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 # -----------------------------------------------------------------------------
57 MSG_BUILD_BEGIN = Begin compilation of project \"$(TARGET)\"...
58 MSG_BUILD_END = Finished building project \"$(TARGET)\"...
59 MSG_COMPILE_CMD = ' [CC] :'
60 MSG_REMOVE_CMD = ' [RM] :'
61 MSG_LINKER_CMD = ' [LNK] :'
62 MSG_SIZE_CMD = ' [SIZE] :'
63 MSG_OBJCPY_CMD = ' [OBJCPY] :'
64 MSG_OBJDMP_CMD = ' [OBJDMP] :'
66 # Sanity check the user MCU, TARGET, ARCH, SRC, F_USB and LUFA_PATH makefile options
68 $(error Makefile TARGET value not set.)
71 $(error Makefile ARCH value not set.)
74 $(error Makefile MCU value not set.)
77 $(error Makefile SRC value not set.)
80 $(error Makefile F_USB value not set.)
83 $(error Makefile LUFA_PATH value not set.)
86 # Default values of user-supplied variables
91 CPP_STANDARD ?= gnu++98
97 # Convert input source file list to differentiate them by type
98 C_SOURCE = $(filter %.c, $(SRC))
99 CPP_SOURCE = $(filter %.cpp, $(SRC))
100 ASM_SOURCE = $(filter %.S, $(SRC))
102 # Convert input source filenames into a list of required output object files
103 OBJECT_FILES = $(filter %.o, $(C_SOURCE:%.c=%.o) $(CPP_SOURCE:%.cpp=%.o) $(ASM_SOURCE:%.S=%.o))
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
109 else ifeq ($(ARCH), XMEGA)
110 CC_FLAGS += -mmcu=$(MCU) -gdwarf-2 -fshort-enums -fno-inline-small-functions -fpack-struct
112 else ifeq ($(ARCH), UC3)
113 CC_FLAGS += -mpart=$(MCU) -g3 -masm-addr-pseudos
116 $(error Unsupported architecture.)
118 CC_FLAGS += -Wall -fno-strict-aliasing -funsigned-char -funsigned-bitfields -ffunction-sections
119 CC_FLAGS += -I. -I$(LUFA_PATH)/..
120 CC_FLAGS += -DARCH=ARCH_$(ARCH) -DBOARD=BOARD_$(BOARD) -DF_USB=$(F_USB)UL
122 CC_FLAGS += -DF_CPU=$(F_CPU)UL
125 # Additional language specific compiler flags
126 C_FLAGS += -Wstrict-prototypes
128 # Create a list of flags to pass to the linker
129 LD_FLAGS += -Wl,-Map=$(TARGET).map,--cref -Wl,--gc-sections -lm
130 ifneq ($(F_CPU), UC3)
131 LD_FLAGS += -Wl,--relax
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 formats: $(UNKNOWN_SOURCE))
140 # Determine flags to pass to the size utility based on its reported features
141 SIZE_MCU_FLAG = $(shell avr-size --help | grep -- --mcu > /dev/null && echo --mcu=$(MCU) )
142 SIZE_FORMAT_FLAG = $(shell avr-size --help | grep -- --format=.*avr > /dev/null && echo --format=avr )
147 @echo $(MSG_BUILD_BEGIN)
150 @echo $(MSG_BUILD_END)
154 @for f in $(SRC) $(CPPSRC) $(ASRC); do \
155 if [ -f $$f ]; then \
156 echo "Found Source File: $$f" ; \
158 echo "Source File Not Found: $$f" ; \
163 @echo $(MSG_SIZE_CMD) Determining size of \"$(TARGET).elf\"
164 @if test -f $(TARGET).elf; then \
165 avr-size $(SIZE_MCU_FLAG) $(SIZE_FORMAT_FLAG) $(TARGET).elf ; 2>/dev/null; \
168 .PHONY: begin hex lss end size
169 all: begin hex end size
172 hex: $(TARGET).hex $(TARGET).eep
176 @echo $(MSG_COMPILE_CMD) Compiling C file \"$^\"
177 $(CROSS)gcc -c $(CC_FLAGS) $(C_FLAGS) -O$(OPTIMIZATION) -std=$(C_STANDARD) $< -o $@
180 @echo $(MSG_COMPILE_CMD) Compiling C++ file \"$^\"
181 $(CROSS)gcc -c $(CC_FLAGS) $(CPP_FLAGS) -O$(OPTIMIZATION) -std=$(CPP_STANDARD) -x c++ $< -o $@
184 @echo $(MSG_COMPILE_CMD) Assembling \"$^\"
185 $(CROSS)gcc -c $(CC_FLAGS) $(ASM_FLAGS) -x assembler-with-cpp $< -o $@
187 .PRECIOUS : $(OBJECT_FILES)
188 %.elf: $(OBJECT_FILES)
189 @echo $(MSG_LINKER_CMD) Linking object files into \"$@\"
190 $(CROSS)gcc $^ $(CC_FLAGS) $(LD_FLAGS) -o $@
193 @echo $(MSG_OBJCPY_CMD) Extracting HEX file data from \"$@\"
194 $(CROSS)objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@
197 @echo $(MSG_OBJCPY_CMD) Extracting EEP file data from \"$@\"
198 $(CROSS)objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings -O ihex $< $@ || exit 0
201 @echo $(MSG_OBJDMP_CMD) Extracting LSS file data from \"$@\"
202 $(CROSS)objdump -h -S -z $< > $@
205 @echo $(MSG_REMOVE_CMD) Removing object files \"$(notdir $(OBJECT_FILES))\"
206 rm -f $(OBJECT_FILES)
207 @echo $(MSG_REMOVE_CMD) Removing output files \"$(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).map $(TARGET).lss\"
208 rm -f $(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).map $(TARGET).lss