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)) $(filter %.c++, $(SRC))
90 ASM_SOURCE = $(filter %.S, $(SRC))
92 # Convert input source filenames into a list of required output object files
93 OBJECT_FILES = $(C_SOURCE:%.c=%.o) $(CPP_SOURCE:%.c++=%.o) $(CPP_SOURCE:%.c++=%.o) $(ASM_SOURCE:%.S=%.o)
95 # Create a list of flags to pass to the compiler
96 CC_FLAGS += -mmcu=$(MCU) -I. -I$(LUFA_PATH)/.. -gdwarf-2 -pipe
97 CC_FLAGS += -Wall -Wstrict-prototypes
98 CC_FLAGS += -fno-strict-aliasing -funsigned-char -funsigned-bitfields -ffunction-sections -fshort-enums -fno-inline-small-functions -fpack-struct -fshort-enums
99 CC_FLAGS += -DARCH=ARCH_$(ARCH) -DBOARD=BOARD_$(BOARD) -DF_USB=$(F_USB)UL
101 CC_FLAGS += -DF_CPU=$(F_CPU)UL
104 # Create a list of flags to pass to the linker
105 LD_FLAGS += -Wl,-Map=$(TARGET).map,--cref -Wl,--relax -Wl,--gc-sections -lm
107 # Create a list of unknown source file types, if any are found throw an error
108 UNKNOWN_SOURCE = $(filter-out $(C_SOURCE) $(CPP_SOURCE) $(ASM_SOURCE), $(SRC))
109 ifneq ($(UNKNOWN_SOURCE),)
110 $(error Unknown input source formats: $(UNKNOWN_SOURCE))
113 # Determine flags to pass to the size utility based on its reported features
114 SIZE_MCU_FLAG = $(shell avr-size --help | grep -- --mcu > /dev/null && echo --mcu=$(MCU) )
115 SIZE_FORMAT_FLAG = $(shell avr-size --help | grep -- --format=.*avr > /dev/null && echo --format=avr )
120 @echo $(MSG_BUILD_BEGIN)
123 @echo $(MSG_BUILD_END)
127 @for f in $(SRC) $(CPPSRC) $(ASRC); do \
128 if [ -f $$f ]; then \
129 echo "Found Source File: $$f" ; \
131 echo "Source File Not Found: $$f" ; \
136 @echo $(MSG_SIZE_CMD) Determining size of \"$(TARGET).elf\"
137 @if test -f $(TARGET).elf; then \
138 avr-size $(SIZE_MCU_FLAG) $(SIZE_FORMAT_FLAG) $(TARGET).elf ; 2>/dev/null; \
141 .PHONY: begin hex lss end size
142 all: begin hex end size
145 hex: $(TARGET).hex $(TARGET).eep
149 @echo $(MSG_COMPILE_CMD) Compiling C file \"$^\"
150 avr-gcc -c $(CC_FLAGS) -O$(OPTIMIZATION) --std=$(C_STANDARD) $< -o $@
154 @echo $(MSG_COMPILE_CMD) Compiling C++ file \"$^\"
155 avr-gcc -c $(CC_FLAGS) -O$(OPTIMIZATION) --std=$(CPP_STANDARD) -x c++ $< -o $@
158 @echo $(MSG_COMPILE_CMD) Assembling \"$^\"
159 avr-gcc -c $(CC_FLAGS) -x assembler-with-cpp $< -o $@
161 .PRECIOUS : $(OBJECT_FILES)
162 %.elf: $(OBJECT_FILES)
163 @echo $(MSG_LINKER_CMD) Linking object files into \"$@\"
164 avr-gcc $^ $(CC_FLAGS) $(LD_FLAGS) -o $@
167 @echo $(MSG_OBJCPY_CMD) Extracting HEX file data from \"$@\"
168 avr-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@
171 @echo $(MSG_OBJCPY_CMD) Extracting EEP file data from \"$@\"
172 avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings -O ihex $< $@ || exit 0
175 @echo $(MSG_OBJDMP_CMD) Extracting LSS file data from \"$@\"
176 avr-objdump -h -S -z $< > $@
179 @echo $(MSG_REMOVE_CMD) Removing object files \"$(OBJECT_FILES)\"
180 rm -f $(OBJECT_FILES)
181 @echo $(MSG_REMOVE_CMD) Removing output files \"$(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).map $(TARGET).lss\"
182 rm -f $(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).map $(TARGET).lss