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
85 CPP_STANDARD ?= gnu++98
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
106 CC_FLAGS += -fno-strict-aliasing -funsigned-char -funsigned-bitfields -ffunction-sections
107 CC_FLAGS += -Wall -Wstrict-prototypes
108 CC_FLAGS += -I. -I$(LUFA_PATH)/..
109 CC_FLAGS += -DARCH=ARCH_$(ARCH) -DBOARD=BOARD_$(BOARD) -DF_USB=$(F_USB)UL
111 CC_FLAGS += -DF_CPU=$(F_CPU)UL
114 # Create a list of flags to pass to the linker
115 LD_FLAGS += -Wl,-Map=$(TARGET).map,--cref -Wl,--relax -Wl,--gc-sections -lm
117 # Create a list of unknown source file types, if any are found throw an error
118 UNKNOWN_SOURCE = $(filter-out $(C_SOURCE) $(CPP_SOURCE) $(ASM_SOURCE), $(SRC))
119 ifneq ($(UNKNOWN_SOURCE),)
120 $(error Unknown input source formats: $(UNKNOWN_SOURCE))
123 # Determine flags to pass to the size utility based on its reported features
124 SIZE_MCU_FLAG = $(shell avr-size --help | grep -- --mcu > /dev/null && echo --mcu=$(MCU) )
125 SIZE_FORMAT_FLAG = $(shell avr-size --help | grep -- --format=.*avr > /dev/null && echo --format=avr )
130 @echo $(MSG_BUILD_BEGIN)
133 @echo $(MSG_BUILD_END)
137 @for f in $(SRC) $(CPPSRC) $(ASRC); do \
138 if [ -f $$f ]; then \
139 echo "Found Source File: $$f" ; \
141 echo "Source File Not Found: $$f" ; \
146 @echo $(MSG_SIZE_CMD) Determining size of \"$(TARGET).elf\"
147 @if test -f $(TARGET).elf; then \
148 avr-size $(SIZE_MCU_FLAG) $(SIZE_FORMAT_FLAG) $(TARGET).elf ; 2>/dev/null; \
151 .PHONY: begin hex lss end size
152 all: begin hex end size
155 hex: $(TARGET).hex $(TARGET).eep
159 @echo $(MSG_COMPILE_CMD) Compiling C file \"$^\"
160 $(CROSS)gcc -c $(CC_FLAGS) -O$(OPTIMIZATION) -std=$(C_STANDARD) $< -o $@
163 @echo $(MSG_COMPILE_CMD) Compiling C++ file \"$^\"
164 $(CROSS)gcc -c $(CC_FLAGS) -O$(OPTIMIZATION) -std=$(CPP_STANDARD) -x c++ $< -o $@
167 @echo $(MSG_COMPILE_CMD) Assembling \"$^\"
168 $(CROSS)gcc -c $(CC_FLAGS) -x assembler-with-cpp $< -o $@
170 .PRECIOUS : $(OBJECT_FILES)
171 %.elf: $(OBJECT_FILES)
172 @echo $(MSG_LINKER_CMD) Linking object files into \"$@\"
173 $(CROSS)gcc $^ $(CC_FLAGS) $(LD_FLAGS) -o $@
176 @echo $(MSG_OBJCPY_CMD) Extracting HEX file data from \"$@\"
177 $(CROSS)objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@
180 @echo $(MSG_OBJCPY_CMD) Extracting EEP file data from \"$@\"
181 $(CROSS)objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings -O ihex $< $@ || exit 0
184 @echo $(MSG_OBJDMP_CMD) Extracting LSS file data from \"$@\"
185 $(CROSS)objdump -h -S -z $< > $@
188 @echo $(MSG_REMOVE_CMD) Removing object files \"$(OBJECT_FILES)\"
189 rm -f $(OBJECT_FILES)
190 @echo $(MSG_REMOVE_CMD) Removing output files \"$(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).map $(TARGET).lss\"
191 rm -f $(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).map $(TARGET).lss