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
12 # -----------------------------------------------------------------------------
13 # LUFA Compiler Buildsystem Makefile Module.
14 # -----------------------------------------------------------------------------
16 # Provides a set of targets to build a C, C++ and/or Assembly application
17 # via the AVR-GCC compiler.
18 # -----------------------------------------------------------------------------
21 # size - List application size
22 # checksource - Check existance of listed input source files
23 # all - Build application and list size
24 # elf - Build application ELF debug object file
25 # hex - Build application HEX object files
26 # clean - Remove output files
28 # MANDATORY PARAMETERS:
30 # TARGET - Application name
31 # ARCH - Device architecture name
32 # MCU - Microcontroller device model name
33 # SRC - List of input source files (.c, .cpp/.c++, .S)
34 # F_USB - Speed of the input clock of the USB controller
36 # LUFA_PATH - Path to the LUFA library core
38 # OPTIONAL PARAMETERS:
40 # BOARD - LUFA board hardware
41 # OPTIMIZATION - Optimization level
42 # C_STANDARD - C Language Standard to use
43 # CPP_STANDARD - C++ Language Standard to use
44 # F_CPU - Speed of the CPU, in Hz
45 # CC_FLAGS - Flags to pass to the compiler
46 # LD_FLAGS - Flags to pass to the linker
48 # -----------------------------------------------------------------------------
51 MSG_BUILD_BEGIN = Begin compilation of project \"$(TARGET)\"...
52 MSG_BUILD_END = Finished building project \"$(TARGET)\"...
53 MSG_COMPILE_CMD = ' [CC] :'
54 MSG_REMOVE_CMD = ' [RM] :'
55 MSG_LINKER_CMD = ' [LNK] :'
56 MSG_SIZE_CMD = ' [SIZE] :'
57 MSG_OBJCPY_CMD = ' [OBJCPY] :'
58 MSG_OBJDMP_CMD = ' [OBJDMP] :'
60 # Sanity check the user MCU, TARGET, ARCH, SRC, F_USB and LUFA_PATH makefile options
62 $(error Makefile TARGET value not set.)
65 $(error Makefile ARCH value not set.)
68 $(error Makefile MCU value not set.)
71 $(error Makefile SRC value not set.)
74 $(error Makefile F_USB value not set.)
77 $(error Makefile LUFA_PATH value not set.)
80 # Default values of user-supplied variables
87 # Convert input source file list to differentiate them by type
88 C_SOURCE = $(filter %.c, $(SRC))
89 CPP_SOURCE = $(filter %.cpp, $(SRC))
90 ASM_SOURCE = $(filter %.S, $(SRC))
92 # Convert input source filenames into a list of required output object files
93 OBJECT_FILES = $(filter %.o, $(C_SOURCE:%.c=%.o) $(CPP_SOURCE:%.cpp=%.o) $(ASM_SOURCE:%.S=%.o))
95 # Create a list of flags to pass to the compiler
97 CC_FLAGS += -mmcu=$(MCU) -gdwarf-2 -fshort-enums -fno-inline-small-functions -fpack-struct
99 else ifeq ($(ARCH),XMEGA)
100 CC_FLAGS += -mmcu=$(MCU) -gdwarf-2 -fshort-enums -fno-inline-small-functions -fpack-struct
102 else ifeq ($(ARCH),UC3)
103 CC_FLAGS += -mpart=$(MCU) -g3 -masm-addr-pseudos
107 CC_FLAGS += -fno-strict-aliasing -funsigned-char -funsigned-bitfields -ffunction-sections
108 CC_FLAGS += -Wall -Wstrict-prototypes
109 CC_FLAGS += -I. -I$(LUFA_PATH)/.. -pipe
110 CC_FLAGS += -DARCH=ARCH_$(ARCH) -DBOARD=BOARD_$(BOARD) -DF_USB=$(F_USB)UL
112 CC_FLAGS += -DF_CPU=$(F_CPU)UL
115 # Create a list of flags to pass to the linker
116 LD_FLAGS += -Wl,-Map=$(TARGET).map,--cref -Wl,--relax -Wl,--gc-sections -lm
118 # Create a list of unknown source file types, if any are found throw an error
119 UNKNOWN_SOURCE = $(filter-out $(C_SOURCE) $(CPP_SOURCE) $(ASM_SOURCE), $(SRC))
120 ifneq ($(UNKNOWN_SOURCE),)
121 $(error Unknown input source formats: $(UNKNOWN_SOURCE))
124 # Determine flags to pass to the size utility based on its reported features
125 SIZE_MCU_FLAG = $(shell avr-size --help | grep -- --mcu > /dev/null && echo --mcu=$(MCU) )
126 SIZE_FORMAT_FLAG = $(shell avr-size --help | grep -- --format=.*avr > /dev/null && echo --format=avr )
131 @echo $(MSG_BUILD_BEGIN)
134 @echo $(MSG_BUILD_END)
138 @for f in $(SRC) $(CPPSRC) $(ASRC); do \
139 if [ -f $$f ]; then \
140 echo "Found Source File: $$f" ; \
142 echo "Source File Not Found: $$f" ; \
147 @echo $(MSG_SIZE_CMD) Determining size of \"$(TARGET).elf\"
148 @if test -f $(TARGET).elf; then \
149 avr-size $(SIZE_MCU_FLAG) $(SIZE_FORMAT_FLAG) $(TARGET).elf ; 2>/dev/null; \
152 .PHONY: begin hex lss end size
153 all: begin hex end size
156 hex: $(TARGET).hex $(TARGET).eep
160 @echo $(MSG_COMPILE_CMD) Compiling C file \"$^\"
161 $(CROSS)gcc -c $(CC_FLAGS) -O$(OPTIMIZATION) --std=$(C_STANDARD) $< -o $@
164 @echo $(MSG_COMPILE_CMD) Compiling C++ file \"$^\"
165 $(CROSS)gcc -c $(CC_FLAGS) -O$(OPTIMIZATION) --std=$(CPP_STANDARD) -x c++ $< -o $@
168 @echo $(MSG_COMPILE_CMD) Assembling \"$^\"
169 $(CROSS)gcc -c $(CC_FLAGS) -x assembler-with-cpp $< -o $@
171 .PRECIOUS : $(OBJECT_FILES)
172 %.elf: $(OBJECT_FILES)
173 @echo $(MSG_LINKER_CMD) Linking object files into \"$@\"
174 $(CROSS)gcc $^ $(CC_FLAGS) $(LD_FLAGS) -o $@
177 @echo $(MSG_OBJCPY_CMD) Extracting HEX file data from \"$@\"
178 $(CROSS)objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@
181 @echo $(MSG_OBJCPY_CMD) Extracting EEP file data from \"$@\"
182 $(CROSS)objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings -O ihex $< $@ || exit 0
185 @echo $(MSG_OBJDMP_CMD) Extracting LSS file data from \"$@\"
186 $(CROSS)objdump -h -S -z $< > $@
189 @echo $(MSG_REMOVE_CMD) Removing object files \"$(OBJECT_FILES)\"
190 rm -f $(OBJECT_FILES)
191 @echo $(MSG_REMOVE_CMD) Removing output files \"$(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).map $(TARGET).lss\"
192 rm -f $(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).map $(TARGET).lss