Add svn:eol-style property on all source files where it was missing. Fix line endings...
[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 symbol-sizes 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 OBJDIR
13 LUFA_BUILD_PROVIDED_VARS +=
14 LUFA_BUILD_PROVIDED_MACROS +=
15
16 # -----------------------------------------------------------------------------
17 # LUFA GCC Compiler Buildsystem Makefile Module.
18 # -----------------------------------------------------------------------------
19 # DESCRIPTION:
20 # Provides a set of targets to build a C, C++ and/or Assembly application
21 # via the AVR-GCC compiler.
22 # -----------------------------------------------------------------------------
23 # TARGETS:
24 #
25 # size - List built application size
26 # symbol-sizes - Print application symbols from the binary ELF
27 # file as a list sorted by size in bytes
28 # all - Build application and list size
29 # elf - Build application ELF debug object file
30 # hex - Build application HEX object files
31 # lss - Build application LSS assembly listing file
32 # clean - Remove output files
33 #
34 # MANDATORY PARAMETERS:
35 #
36 # TARGET - Application name
37 # ARCH - Device architecture name
38 # MCU - Microcontroller device model name
39 # SRC - List of input source files (*.c, *.cpp, *.S)
40 # F_USB - Speed of the input clock of the USB controller
41 # in Hz
42 # LUFA_PATH - Path to the LUFA library core
43 #
44 # OPTIONAL PARAMETERS:
45 #
46 # BOARD - LUFA board hardware
47 # OPTIMIZATION - Optimization level
48 # C_STANDARD - C Language Standard to use
49 # CPP_STANDARD - C++ Language Standard to use
50 # F_CPU - Speed of the CPU, in Hz
51 # C_FLAGS - Flags to pass to the C compiler only
52 # CPP_FLAGS - Flags to pass to the C++ compiler only
53 # ASM_FLAGS - Flags to pass to the assembler only
54 # CC_FLAGS - Common flags to pass to the C/C++ compiler and
55 # assembler
56 # LD_FLAGS - Flags to pass to the linker
57 # OBJDIR - Directory for the output object and dependency
58 # files; if equal to ".", the output files will
59 # be generated in the same folder as the sources
60 #
61 # PROVIDED VARIABLES:
62 #
63 # (None)
64 #
65 # PROVIDED MACROS:
66 #
67 # (None)
68 #
69 # -----------------------------------------------------------------------------
70
71 SHELL = /bin/sh
72
73 ERROR_IF_UNSET ?= $(if $(filter undefined, $(origin $(strip $(1)))), $(error Makefile $(strip $(1)) value not set))
74 ERROR_IF_EMPTY ?= $(if $(strip $($(strip $(1)))), , $(error Makefile $(strip $(1)) option cannot be blank))
75 ERROR_IF_NONBOOL ?= $(if $(filter Y N, $($(strip $(1)))), , $(error Makefile $(strip $(1)) option must be Y or N))
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 OBJDIR ?= .
88
89 # Sanity check user supplied values
90 $(foreach MANDATORY_VAR, $(LUFA_BUILD_MANDATORY_VARS), $(call ERROR_IF_UNSET, $(MANDATORY_VAR)))
91 $(call ERROR_IF_EMPTY, MCU)
92 $(call ERROR_IF_EMPTY, TARGET)
93 $(call ERROR_IF_EMPTY, ARCH)
94 $(call ERROR_IF_EMPTY, F_USB)
95 $(call ERROR_IF_EMPTY, LUFA_PATH)
96 $(call ERROR_IF_EMPTY, BOARD)
97 $(call ERROR_IF_EMPTY, OPTIMIZATION)
98 $(call ERROR_IF_EMPTY, C_STANDARD)
99 $(call ERROR_IF_EMPTY, CPP_STANDARD)
100 $(call ERROR_IF_EMPTY, OBJDIR)
101
102 # Determine the utility prefix to use for the selected architecture
103 ifeq ($(ARCH), AVR8)
104 CROSS := avr
105 else ifeq ($(ARCH), XMEGA)
106 CROSS := avr
107 $(warning The XMEGA device support is currently EXPERIMENTAL (incomplete and/or non-functional), and is included for preview purposes only.)
108 else ifeq ($(ARCH), UC3)
109 CROSS := avr32
110 $(warning The UC3 device support is currently EXPERIMENTAL (incomplete and/or non-functional), and is included for preview purposes only.)
111 else
112 $(error Unsupported architecture "$(ARCH)")
113 endif
114
115 # Output Messages
116 MSG_BUILD_BEGIN := Begin compilation of project \"$(TARGET)\"...
117 MSG_BUILD_END := Finished building project \"$(TARGET)\".
118 MSG_COMPILE_CMD := ' [GCC] :'
119 MSG_ASSEMBLE_CMD := ' [GAS] :'
120 MSG_NM_CMD := ' [NM] :'
121 MSG_REMOVE_CMD := ' [RM] :'
122 MSG_LINKER_CMD := ' [LNK] :'
123 MSG_SIZE_CMD := ' [SIZE] :'
124 MSG_OBJCPY_CMD := ' [OBJCPY] :'
125 MSG_OBJDMP_CMD := ' [OBJDMP] :'
126
127 # Convert input source file list to differentiate them by type
128 C_SOURCE = $(filter %.c, $(SRC))
129 CPP_SOURCE = $(filter %.cpp, $(SRC))
130 ASM_SOURCE = $(filter %.S, $(SRC))
131
132 # Create a list of unknown source file types, if any are found throw an error
133 UNKNOWN_SOURCE = $(filter-out $(C_SOURCE) $(CPP_SOURCE) $(ASM_SOURCE), $(SRC))
134 ifneq ($(UNKNOWN_SOURCE),)
135 $(error Unknown input source formats: $(UNKNOWN_SOURCE))
136 endif
137
138 # Convert input source filenames into a list of required output object files
139 OBJECT_FILES = $(filter %.o, $(C_SOURCE:%.c=%.o) $(CPP_SOURCE:%.cpp=%.o) $(ASM_SOURCE:%.S=%.o))
140 ifneq ($(OBJDIR),.)
141 $(shell mkdir $(OBJDIR) 2>&1 | /dev/null)
142 VPATH += $(dir $(SRC))
143
144 OBJECT_FILES := $(addprefix $(patsubst %/,%,$(OBJDIR))/, $(notdir $(OBJECT_FILES)))
145 endif
146
147 DEPENDENCY_FILES = $(OBJECT_FILES:%.o=%.d)
148
149 # Create a list of common flags to pass to the compiler/linker/assembler
150 BASE_CC_FLAGS := -pipe
151 ifeq ($(ARCH), AVR8)
152 BASE_CC_FLAGS += -mmcu=$(MCU) -gdwarf-2 -fshort-enums -fno-inline-small-functions -fpack-struct
153 else ifeq ($(ARCH), XMEGA)
154 BASE_CC_FLAGS += -mmcu=$(MCU) -gdwarf-2 -fshort-enums -fno-inline-small-functions -fpack-struct
155 else ifeq ($(ARCH), UC3)
156 BASE_CC_FLAGS += -mpart=$(MCU:at32%=%) -g3 -masm-addr-pseudos
157 endif
158 BASE_CC_FLAGS += -Wall -fno-strict-aliasing -funsigned-char -funsigned-bitfields -ffunction-sections
159 BASE_CC_FLAGS += -I. -I$(patsubst %/,%,$(LUFA_PATH))/..
160 BASE_CC_FLAGS += -DARCH=ARCH_$(ARCH) -DBOARD=BOARD_$(BOARD) -DF_USB=$(F_USB)UL
161 ifneq ($(F_CPU),)
162 BASE_CC_FLAGS += -DF_CPU=$(F_CPU)UL
163 endif
164
165 # Additional language specific compiler flags
166 BASE_C_FLAGS := -x c -O$(OPTIMIZATION) -std=$(C_STANDARD) -Wstrict-prototypes
167 BASE_CPP_FLAGS := -x c++ -O$(OPTIMIZATION) -std=$(CPP_STANDARD)
168 BASE_ASM_FLAGS := -x assembler-with-cpp
169
170 # Create a list of flags to pass to the linker
171 BASE_LD_FLAGS := -lm -Wl,-Map=$(TARGET).map,--cref -Wl,--gc-sections
172 ifeq ($(ARCH), UC3)
173 BASE_LD_FLAGS += --rodata-writable --direct-data
174 else
175 BASE_LD_FLAGS += -Wl,--relax
176 endif
177
178 # Determine flags to pass to the size utility based on its reported features
179 SIZE_MCU_FLAG := $(shell $(CROSS)-size --help | grep -- --mcu > /dev/null && echo --mcu=$(MCU) )
180 SIZE_FORMAT_FLAG := $(shell $(CROSS)-size --help | grep -- --format=.*avr > /dev/null && echo --format=avr )
181
182
183 build_begin:
184 @echo ""
185 @echo $(MSG_BUILD_BEGIN)
186 @echo ""
187
188 build_end:
189 @echo $(MSG_BUILD_END)
190 @echo ""
191
192 gcc_version:
193 @$(CROSS)-gcc --version
194
195 check_source:
196 @for f in $(SRC); do \
197 if [ ! -f $$f ]; then \
198 echo "Error: Source file not found: $$f"; \
199 exit 1; \
200 fi; \
201 done
202
203 size: $(TARGET).elf
204 @echo $(MSG_SIZE_CMD) Determining size of \"$<\"
205 @echo ""
206 $(CROSS)-size $(SIZE_MCU_FLAG) $(SIZE_FORMAT_FLAG) $< ; 2>/dev/null;
207
208 symbol-sizes: $(TARGET).elf
209 @echo $(MSG_NM_CMD) Extracting \"$<\" symbols with decimal byte sizes
210 avr-nm --size-sort --demangle --radix=d $<
211
212 clean:
213 @echo $(MSG_REMOVE_CMD) Removing object files of \"$(TARGET)\"
214 rm -f $(OBJECT_FILES)
215 @echo $(MSG_REMOVE_CMD) Removing dependency files of \"$(TARGET)\"
216 rm -f $(DEPENDENCY_FILES)
217 @echo $(MSG_REMOVE_CMD) Removing output files of \"$(TARGET)\"
218 rm -f $(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).map $(TARGET).lss $(TARGET).sym
219
220 all: build_begin check_source gcc_version elf hex lss sym size build_end
221
222 elf: $(TARGET).elf
223 hex: $(TARGET).hex $(TARGET).eep
224 lss: $(TARGET).lss
225 sym: $(TARGET).sym
226
227 $(OBJDIR)/%.o: %.c $(MAKEFILE_LIST)
228 @echo $(MSG_COMPILE_CMD) Compiling C file \"$(notdir $<)\"
229 $(CROSS)-gcc -c $(BASE_CC_FLAGS) $(BASE_C_FLAGS) $(CC_FLAGS) $(C_FLAGS) -MMD -MP -MF $(@:%.o=%.d) $< -o $@
230
231 $(OBJDIR)/%.o: %.cpp $(MAKEFILE_LIST)
232 @echo $(MSG_COMPILE_CMD) Compiling C++ file \"$(notdir $<)\"
233 $(CROSS)-gcc -c $(BASE_CC_FLAGS) $(BASE_CPP_FLAGS) $(CC_FLAGS) $(CPP_FLAGS) -MMD -MP -MF $(@:%.o=%.d) $< -o $@
234
235 $(OBJDIR)/%.o: %.S $(MAKEFILE_LIST)
236 @echo $(MSG_ASSEMBLE_CMD) Assembling \"$(notdir $<)\"
237 $(CROSS)-gcc -c $(BASE_CC_FLAGS) $(BASE_ASM_FLAGS) $(CC_FLAGS) $(ASM_FLAGS) $< -o $@
238
239 .PRECIOUS : $(OBJECT_FILES)
240 .SECONDARY : %.elf
241 %.elf: $(OBJECT_FILES)
242 @echo $(MSG_LINKER_CMD) Linking object files into \"$@\"
243 $(CROSS)-gcc $(BASE_CC_FLAGS) $(BASE_LD_FLAGS) $(CC_FLAGS) $(LD_FLAGS) $^ -o $@
244
245 %.hex: %.elf
246 @echo $(MSG_OBJCPY_CMD) Extracting HEX file data from \"$<\"
247 $(CROSS)-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@
248
249 %.eep: %.elf
250 @echo $(MSG_OBJCPY_CMD) Extracting EEP file data from \"$<\"
251 $(CROSS)-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings -O ihex $< $@ || exit 0
252
253 %.lss: %.elf
254 @echo $(MSG_OBJDMP_CMD) Extracting LSS file data from \"$<\"
255 $(CROSS)-objdump -h -S -z $< > $@
256
257 %.sym: %.elf
258 @echo $(MSG_NM_CMD) Extracting SYM file data from \"$<\"
259 $(CROSS)-nm -n $< > $@
260
261 # Include build dependency files
262 -include $(DEPENDENCY_FILES)
263
264 # Phony build targets for this module
265 .PHONY: build_begin build_end gcc_version check_source size symbol-sizes elf hex lss clean