b81f6a563c698f08475bc786f36fb068e23ea407
[pub/USBasp.git] / LUFA / Build / lufa.build.in
1 #
2 # LUFA Library
3 # Copyright (C) Dean Camera, 2012.
4 #
5 # dean [at] fourwalledcubicle [dot] com
6 # www.lufa-lib.org
7 #
8
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
13
14 # -----------------------------------------------------------------------------
15 # LUFA GCC Compiler Buildsystem Makefile Module.
16 # -----------------------------------------------------------------------------
17 # DESCRIPTION:
18 # Provides a set of targets to build a C, C++ and/or Assembly application
19 # via the AVR-GCC compiler.
20 # -----------------------------------------------------------------------------
21 # TARGETS:
22 #
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
29 #
30 # MANDATORY PARAMETERS:
31 #
32 # TARGET - Application name
33 # ARCH - Device architecture name
34 # MCU - Microcontroller device model name
35 # SRC - List of input source files (*.c, *.cpp, *.S)
36 # F_USB - Speed of the input clock of the USB controller
37 # in Hz
38 # LUFA_PATH - Path to the LUFA library core
39 #
40 # OPTIONAL PARAMETERS:
41 #
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
51 # assembler
52 # LD_FLAGS - Flags to pass to the linker
53 #
54 # -----------------------------------------------------------------------------
55
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)
63
64 ifeq ($(MCU),)
65 $(error Makefile MCU option cannot be blank)
66 endif
67 ifeq ($(TARGET),)
68 $(error Makefile TARGET option cannot be blank)
69 endif
70 ifeq ($(ARCH),)
71 $(error Makefile ARCH option cannot be blank)
72 endif
73 ifeq ($(F_USB),)
74 $(error Makefile F_USB option cannot be blank)
75 endif
76
77 # Default values of optionally user-supplied variables
78 BOARD ?= NONE
79 OPTIMIZATION ?= s
80 F_CPU ?=
81 C_STANDARD ?= gnu99
82 CPP_STANDARD ?= gnu++98
83 C_FLAGS ?=
84 CPP_FLAGS ?=
85 ASM_FLAGS ?=
86 CC_FLAGS ?=
87
88 # Determine the utility prefix to use for the selected architecture
89 ifeq ($(ARCH), AVR8)
90 CROSS := avr-
91 else ifeq ($(ARCH), XMEGA)
92 CROSS := avr-
93 else ifeq ($(ARCH), UC3)
94 CROSS := avr32-
95 else
96 $(error Unsupported architecture "$(ARCH)".)
97 endif
98
99 # Output Messages
100 MSG_BUILD_BEGIN := Begin compilation of project \"$(TARGET)\"...
101 MSG_BUILD_END := Finished building project \"$(TARGET)\".
102 MSG_COMPILE_CMD := ' [CC] :'
103 MSG_ASSEMBLE_CMD := ' [AS] :'
104 MSG_REMOVE_CMD := ' [RM] :'
105 MSG_LINKER_CMD := ' [LNK] :'
106 MSG_SIZE_CMD := ' [SIZE] :'
107 MSG_OBJCPY_CMD := ' [OBJCPY] :'
108 MSG_OBJDMP_CMD := ' [OBJDMP] :'
109
110 # Convert input source file list to differentiate them by type
111 C_SOURCE = $(filter %.c, $(SRC))
112 CPP_SOURCE = $(filter %.cpp, $(SRC))
113 ASM_SOURCE = $(filter %.S, $(SRC))
114
115 # Convert input source filenames into a list of required output object files
116 OBJECT_FILES = $(filter %.o, $(C_SOURCE:%.c=%.o) $(CPP_SOURCE:%.cpp=%.o) $(ASM_SOURCE:%.S=%.o))
117 DEPENDENCY_FILES = $(OBJECT_FILES:%=%.d)
118
119 # Create a list of flags to pass to the compiler
120 ifeq ($(ARCH), AVR8)
121 CC_FLAGS += -mmcu=$(MCU) -gdwarf-2 -fshort-enums -fno-inline-small-functions -fpack-struct
122 else ifeq ($(ARCH), XMEGA)
123 CC_FLAGS += -mmcu=$(MCU) -gdwarf-2 -fshort-enums -fno-inline-small-functions -fpack-struct
124 else ifeq ($(ARCH), UC3)
125 CC_FLAGS += -mpart=$(MCU) -g3 -masm-addr-pseudos
126 endif
127 CC_FLAGS += -Wall -fno-strict-aliasing -funsigned-char -funsigned-bitfields -ffunction-sections
128 CC_FLAGS += -I. -I$(patsubst %/,%,$(LUFA_PATH))/..
129 CC_FLAGS += -DARCH=ARCH_$(ARCH) -DBOARD=BOARD_$(BOARD) -DF_USB=$(F_USB)UL
130 ifneq ($(F_CPU),)
131 CC_FLAGS += -DF_CPU=$(F_CPU)UL
132 endif
133
134 # Additional language specific compiler flags
135 C_FLAGS += -O$(OPTIMIZATION) -std=$(C_STANDARD) -MMD -MP -MF $@.d -Wstrict-prototypes
136 CPP_FLAGS += -O$(OPTIMIZATION) -std=$(CPP_STANDARD) -MMD -MP -MF $@.d
137
138 # Create a list of flags to pass to the linker
139 LD_FLAGS += -lm -Wl,-Map=$(TARGET).map,--cref -Wl,--gc-sections
140 ifeq ($(ARCH), UC3)
141 LD_FLAGS += --rodata-writable --direct-data
142 else
143 LD_FLAGS += -Wl,--relax
144 endif
145
146 # Create a list of unknown source file types, if any are found throw an error
147 UNKNOWN_SOURCE = $(filter-out $(C_SOURCE) $(CPP_SOURCE) $(ASM_SOURCE), $(SRC))
148 ifneq ($(UNKNOWN_SOURCE),)
149 $(error Unknown input source formats: $(UNKNOWN_SOURCE))
150 endif
151
152 # Determine flags to pass to the size utility based on its reported features
153 SIZE_MCU_FLAG := $(shell $(CROSS)size --help | grep -- --mcu > /dev/null && echo --mcu=$(MCU) )
154 SIZE_FORMAT_FLAG := $(shell $(CROSS)size --help | grep -- --format=.*avr > /dev/null && echo --format=avr )
155
156
157 begin:
158 @echo ""
159 @echo $(MSG_BUILD_BEGIN)
160 @echo ""
161
162 end:
163 @echo ""
164 @echo $(MSG_BUILD_END)
165 @echo ""
166
167 gcc_version:
168 @$(CROSS)gcc --version
169
170 check_source:
171 @for f in $(SRC); do \
172 if [ ! -f $$f ]; then \
173 echo "Error: Source file not found: $$f"; \
174 exit 1; \
175 fi; \
176 done
177
178 size:
179 @echo $(MSG_SIZE_CMD) Determining size of \"$(TARGET).elf\"
180 @if test -f $(TARGET).elf; then \
181 $(CROSS)size $(SIZE_MCU_FLAG) $(SIZE_FORMAT_FLAG) $(TARGET).elf ; 2>/dev/null; \
182 fi
183
184 clean:
185 @echo $(MSG_REMOVE_CMD) Removing object files of \"$(TARGET)\"
186 rm -f $(OBJECT_FILES)
187 @echo $(MSG_REMOVE_CMD) Removing dependency files of \"$(TARGET)\"
188 rm -f $(DEPENDENCY_FILES)
189 @echo $(MSG_REMOVE_CMD) Removing output files of \"$(TARGET)\"
190 rm -f $(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).map $(TARGET).lss
191
192 all: begin check_source gcc_version elf hex lss size end
193
194 elf: $(TARGET).elf
195 hex: $(TARGET).hex $(TARGET).eep
196 lss: $(TARGET).lss
197
198 %.o: %.c $(MAKEFILE_LIST)
199 @echo $(MSG_COMPILE_CMD) Compiling C file \"$<\"
200 $(CROSS)gcc -c $(CC_FLAGS) $(C_FLAGS) $< -o $@
201
202 %.o: %.cpp $(MAKEFILE_LIST)
203 @echo $(MSG_COMPILE_CMD) Compiling C++ file \"$<\"
204 $(CROSS)gcc -c $(CC_FLAGS) $(CPP_FLAGS) -x c++ $< -o $@
205
206 %.o: %.S $(MAKEFILE_LIST)
207 @echo $(MSG_ASSEMBLE_CMD) Assembling \"$<\"
208 $(CROSS)gcc -c $(CC_FLAGS) $(ASM_FLAGS) -x assembler-with-cpp $< -o $@
209
210 .PRECIOUS : $(OBJECT_FILES)
211 %.elf: $(OBJECT_FILES)
212 @echo $(MSG_LINKER_CMD) Linking object files into \"$@\"
213 $(CROSS)gcc $(CC_FLAGS) $(LD_FLAGS) $^ -o $@
214
215 %.hex: %.elf
216 @echo $(MSG_OBJCPY_CMD) Extracting HEX file data from \"$<\"
217 $(CROSS)objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@
218
219 %.eep: %.elf
220 @echo $(MSG_OBJCPY_CMD) Extracting EEP file data from \"$<\"
221 $(CROSS)objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings -O ihex $< $@ || exit 0
222
223 %.lss: %.elf
224 @echo $(MSG_OBJDMP_CMD) Extracting LSS file data from \"$<\"
225 $(CROSS)objdump -h -S -z $< > $@
226
227 # Include build dependency files
228 -include $(DEPENDENCY_FILES)
229
230 # Phony build targets for this module
231 .PHONY: begin end gcc_version check_source size elf hex lss clean