f3c695502fa2e942292bb10feb3a5683051b5170
[pub/USBasp.git] / LUFA / Build / DMBS / DMBS / gcc.mk
1 #
2 # DMBS Build System
3 # Released into the public domain.
4 #
5 # dean [at] fourwalledcubicle [dot] com
6 # www.fourwalledcubicle.com
7 #
8
9 DMBS_BUILD_MODULES += GCC
10 DMBS_BUILD_TARGETS += size symbol-sizes all lib elf bin hex lss clean mostlyclean
11 DMBS_BUILD_MANDATORY_VARS += TARGET ARCH MCU SRC
12 DMBS_BUILD_OPTIONAL_VARS += BOARD OPTIMIZATION C_STANDARD CPP_STANDARD F_CPU C_FLAGS CPP_FLAGS ASM_FLAGS CC_FLAGS LD_FLAGS OBJDIR OBJECT_FILES DEBUG_TYPE DEBUG_LEVEL LINKER_RELAXATIONS COMPILER_PATH
13 DMBS_BUILD_PROVIDED_VARS +=
14 DMBS_BUILD_PROVIDED_MACROS +=
15
16 # -----------------------------------------------------------------------------
17 # DMBS 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 # lib - Build and archive source files into a library
30 # elf - Build application ELF debug object file
31 # bin - Build application BIN binary object file
32 # hex - Build application HEX object file
33 # lss - Build application LSS assembly listing file
34 # clean - Remove all project intermediary and binary
35 # output files
36 # mostlyclean - Remove intermediary output files, but
37 # preserve binaries
38 # <filename>.s - Compile C/C++ source file into an assembly file
39 # for manual code inspection
40 #
41 # MANDATORY PARAMETERS:
42 #
43 # TARGET - Application name
44 # ARCH - Device architecture name
45 # MCU - Microcontroller device model name
46 # SRC - List of input source files (*.c, *.cpp, *.S)
47 #
48 # OPTIONAL PARAMETERS:
49 #
50 # OPTIMIZATION - Optimization level
51 # C_STANDARD - C Language Standard to use
52 # CPP_STANDARD - C++ Language Standard to use
53 # F_CPU - Speed of the CPU, in Hz
54 # C_FLAGS - Flags to pass to the C compiler only
55 # CPP_FLAGS - Flags to pass to the C++ compiler only
56 # ASM_FLAGS - Flags to pass to the assembler only
57 # CC_FLAGS - Common flags to pass to the C/C++ compiler and
58 # assembler
59 # LD_FLAGS - Flags to pass to the linker
60 # LINKER_RELAXATIONS - Enable or disable linker relaxations to
61 # decrease binary size (note: can cause link
62 # failures on systems with an unpatched binutils)
63 # OBJDIR - Directory for the output object and dependency
64 # files; if equal to ".", the output files will
65 # be generated in the same folder as the sources
66 # OBJECT_FILES - Extra object files to link in to the binaries
67 # DEBUG_FORMAT - Format of the debugging information to
68 # generate in the compiled object files
69 # DEBUG_LEVEL - Level the debugging information to generate in
70 # the compiled object files
71 # COMPILER_PATH - Location of the GCC toolchain to use
72 #
73 # PROVIDED VARIABLES:
74 #
75 # (None)
76 #
77 # PROVIDED MACROS:
78 #
79 # (None)
80 #
81 # -----------------------------------------------------------------------------
82
83 SHELL = /bin/sh
84
85 ERROR_IF_UNSET ?= $(if $(filter undefined, $(origin $(strip $(1)))), $(error Makefile $(strip $(1)) value not set))
86 ERROR_IF_EMPTY ?= $(if $(strip $($(strip $(1)))), , $(error Makefile $(strip $(1)) option cannot be blank))
87 ERROR_IF_NONBOOL ?= $(if $(filter Y N, $($(strip $(1)))), , $(error Makefile $(strip $(1)) option must be Y or N))
88
89 # Default values of optionally user-supplied variables
90 COMPILER_PATH ?=
91 OPTIMIZATION ?= s
92 F_CPU ?=
93 C_STANDARD ?= gnu99
94 CPP_STANDARD ?= gnu++98
95 C_FLAGS ?=
96 CPP_FLAGS ?=
97 ASM_FLAGS ?=
98 CC_FLAGS ?=
99 OBJDIR ?= obj
100 OBJECT_FILES ?=
101 DEBUG_FORMAT ?= dwarf-2
102 DEBUG_LEVEL ?= 2
103 LINKER_RELAXATIONS ?= Y
104
105 # Sanity check user supplied values
106 $(foreach MANDATORY_VAR, $(DMBS_BUILD_MANDATORY_VARS), $(call ERROR_IF_UNSET, $(MANDATORY_VAR)))
107 $(call ERROR_IF_EMPTY, MCU)
108 $(call ERROR_IF_EMPTY, TARGET)
109 $(call ERROR_IF_EMPTY, ARCH)
110 $(call ERROR_IF_EMPTY, OPTIMIZATION)
111 $(call ERROR_IF_EMPTY, C_STANDARD)
112 $(call ERROR_IF_EMPTY, CPP_STANDARD)
113 $(call ERROR_IF_EMPTY, OBJDIR)
114 $(call ERROR_IF_EMPTY, DEBUG_FORMAT)
115 $(call ERROR_IF_EMPTY, DEBUG_LEVEL)
116 $(call ERROR_IF_NONBOOL, LINKER_RELAXATIONS)
117
118 # Determine the utility prefix to use for the selected architecture
119 ifeq ($(ARCH), AVR8)
120 CROSS := $(COMPILER_PATH)avr
121 else ifeq ($(ARCH), XMEGA)
122 CROSS := $(COMPILER_PATH)avr
123 else ifeq ($(ARCH), UC3)
124 CROSS := $(COMPILER_PATH)avr32
125 else
126 $(error Unsupported architecture "$(ARCH)")
127 endif
128
129 # Output Messages
130 MSG_INFO_MESSAGE := ' [INFO] :'
131 MSG_COMPILE_CMD := ' [GCC] :'
132 MSG_ASSEMBLE_CMD := ' [GAS] :'
133 MSG_NM_CMD := ' [NM] :'
134 MSG_REMOVE_CMD := ' [RM] :'
135 MSG_LINK_CMD := ' [LNK] :'
136 MSG_ARCHIVE_CMD := ' [AR] :'
137 MSG_SIZE_CMD := ' [SIZE] :'
138 MSG_OBJCPY_CMD := ' [OBJCPY] :'
139 MSG_OBJDMP_CMD := ' [OBJDMP] :'
140
141 # Convert input source file list to differentiate them by type
142 C_SOURCE := $(filter %.c, $(SRC))
143 CPP_SOURCE := $(filter %.cpp, $(SRC))
144 ASM_SOURCE := $(filter %.S, $(SRC))
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 file formats: $(UNKNOWN_SOURCE))
150 endif
151
152 # Convert input source filenames into a list of required output object files
153 OBJECT_FILES += $(addsuffix .o, $(basename $(SRC)))
154
155 # Check if an output object file directory was specified instead of the input file location
156 ifneq ($(OBJDIR),.)
157 # Prefix all the object filenames with the output object file directory path
158 OBJECT_FILES := $(addprefix $(patsubst %/,%,$(OBJDIR))/, $(notdir $(OBJECT_FILES)))
159
160 # Check if any object file (without path) appears more than once in the object file list
161 ifneq ($(words $(sort $(OBJECT_FILES))), $(words $(OBJECT_FILES)))
162 $(error Cannot build with OBJDIR parameter set - one or more object file name is not unique)
163 endif
164
165 # Create the output object file directory if it does not exist and add it to the virtual path list
166 $(shell mkdir $(OBJDIR) 2> /dev/null)
167 VPATH += $(dir $(SRC))
168 endif
169
170 # Create a list of dependency files from the list of object files
171 DEPENDENCY_FILES := $(OBJECT_FILES:%.o=%.d)
172
173 # Create a list of common flags to pass to the compiler/linker/assembler
174 BASE_CC_FLAGS := -pipe -g$(DEBUG_FORMAT) -g$(DEBUG_LEVEL)
175 ifneq ($(findstring $(ARCH), AVR8 XMEGA),)
176 BASE_CC_FLAGS += -mmcu=$(MCU) -fshort-enums -fno-inline-small-functions -fpack-struct
177 else ifneq ($(findstring $(ARCH), UC3),)
178 BASE_CC_FLAGS += -mpart=$(MCU:at32%=%) -masm-addr-pseudos
179 endif
180 BASE_CC_FLAGS += -Wall -fno-strict-aliasing -funsigned-char -funsigned-bitfields -ffunction-sections
181 BASE_CC_FLAGS += -I.
182 BASE_CC_FLAGS += -DARCH=ARCH_$(ARCH)
183 ifneq ($(F_CPU),)
184 BASE_CC_FLAGS += -DF_CPU=$(F_CPU)UL
185 endif
186 ifeq ($(LINKER_RELAXATIONS), Y)
187 BASE_CC_FLAGS += -mrelax
188 endif
189
190 # Additional language specific compiler flags
191 BASE_C_FLAGS := -x c -O$(OPTIMIZATION) -std=$(C_STANDARD) -Wstrict-prototypes
192 BASE_CPP_FLAGS := -x c++ -O$(OPTIMIZATION) -std=$(CPP_STANDARD)
193 BASE_ASM_FLAGS := -x assembler-with-cpp
194
195 # This flag is required for bootloaders as GCC will emit invalid jump table
196 # assembly code for devices with large amounts of flash; the jump table target
197 # is extracted from FLASH without using the correct ELPM instruction, resulting
198 # in a pseudo-random jump target.
199 BASE_CC_FLAGS += -fno-jump-tables
200
201 # Create a list of flags to pass to the linker
202 BASE_LD_FLAGS := -lm -Wl,-Map=$(TARGET).map,--cref -Wl,--gc-sections
203 ifeq ($(LINKER_RELAXATIONS), Y)
204 BASE_LD_FLAGS += -Wl,--relax
205 endif
206 ifneq ($(findstring $(ARCH), AVR8 XMEGA),)
207 BASE_LD_FLAGS += -mmcu=$(MCU)
208 else ifneq ($(findstring $(ARCH), UC3),)
209 BASE_LD_FLAGS += -mpart=$(MCU:at32%=%) --rodata-writable --direct-data
210 endif
211
212 # Determine flags to pass to the size utility based on its reported features (only invoke if size target required)
213 # and on an architecture where this non-standard patch is available
214 ifneq ($(ARCH), UC3)
215 size: SIZE_MCU_FLAG := $(shell $(CROSS)-size --help | grep -- --mcu > /dev/null && echo --mcu=$(MCU) )
216 size: SIZE_FORMAT_FLAG := $(shell $(CROSS)-size --help | grep -- --format=.*avr > /dev/null && echo --format=avr )
217 endif
218
219 # Pre-build informational target, to give compiler and project name information when building
220 build_begin:
221 @echo $(MSG_INFO_MESSAGE) Begin compilation of project \"$(TARGET)\"...
222 @echo ""
223 @$(CROSS)-gcc --version
224
225 # Post-build informational target, to project name information when building has completed
226 build_end:
227 @echo $(MSG_INFO_MESSAGE) Finished building project \"$(TARGET)\".
228
229 # Prints size information of a compiled application (FLASH, RAM and EEPROM usages)
230 size: $(TARGET).elf
231 @echo $(MSG_SIZE_CMD) Determining size of \"$<\"
232 @echo ""
233 $(CROSS)-size $(SIZE_MCU_FLAG) $(SIZE_FORMAT_FLAG) $<
234
235 # Prints size information on the symbols within a compiled application in decimal bytes
236 symbol-sizes: $(TARGET).elf
237 @echo $(MSG_NM_CMD) Extracting \"$<\" symbols with decimal byte sizes
238 $(CROSS)-nm --size-sort --demangle --radix=d $<
239
240 # Cleans intermediary build files, leaving only the compiled application files
241 mostlyclean:
242 @echo $(MSG_REMOVE_CMD) Removing object files of \"$(TARGET)\"
243 rm -f $(OBJECT_FILES)
244 @echo $(MSG_REMOVE_CMD) Removing dependency files of \"$(TARGET)\"
245 rm -f $(DEPENDENCY_FILES)
246
247 # Cleans all build files, leaving only the original source code
248 clean: mostlyclean
249 @echo $(MSG_REMOVE_CMD) Removing output files of \"$(TARGET)\"
250 rm -f $(TARGET).elf $(TARGET).hex $(TARGET).bin $(TARGET).eep $(TARGET).map $(TARGET).lss $(TARGET).sym lib$(TARGET).a
251
252 # Performs a complete build of the user application and prints size information afterwards
253 all: build_begin elf hex bin lss sym size build_end
254
255 # Helper targets, to build a specific type of output file without having to know the project target name
256 lib: lib$(TARGET).a
257 elf: $(TARGET).elf
258 hex: $(TARGET).hex $(TARGET).eep
259 bin: $(TARGET).bin
260 lss: $(TARGET).lss
261 sym: $(TARGET).sym
262
263 # Default target to *create* the user application's specified source files; if this rule is executed by
264 # make, the input source file doesn't exist and an error needs to be presented to the user
265 $(SRC):
266 $(error Source file does not exist: $@)
267
268 # Compiles an input C source file and generates an assembly listing for it
269 %.s: %.c $(MAKEFILE_LIST)
270 @echo $(MSG_COMPILE_CMD) Generating assembly from C file \"$(notdir $<)\"
271 $(CROSS)-gcc -S $(BASE_CC_FLAGS) $(BASE_C_FLAGS) $(CC_FLAGS) $(C_FLAGS) $< -o $@
272
273 # Compiles an input C++ source file and generates an assembly listing for it
274 %.s: %.cpp $(MAKEFILE_LIST)
275 @echo $(MSG_COMPILE_CMD) Generating assembly from C++ file \"$(notdir $<)\"
276 $(CROSS)-gcc -S $(BASE_CC_FLAGS) $(BASE_CPP_FLAGS) $(CC_FLAGS) $(CPP_FLAGS) $< -o $@
277
278 # Compiles an input C source file and generates a linkable object file for it
279 $(OBJDIR)/%.o: %.c $(MAKEFILE_LIST)
280 @echo $(MSG_COMPILE_CMD) Compiling C file \"$(notdir $<)\"
281 $(CROSS)-gcc -c $(BASE_CC_FLAGS) $(BASE_C_FLAGS) $(CC_FLAGS) $(C_FLAGS) -MMD -MP -MF $(@:%.o=%.d) $< -o $@
282
283 # Compiles an input C++ source file and generates a linkable object file for it
284 $(OBJDIR)/%.o: %.cpp $(MAKEFILE_LIST)
285 @echo $(MSG_COMPILE_CMD) Compiling C++ file \"$(notdir $<)\"
286 $(CROSS)-gcc -c $(BASE_CC_FLAGS) $(BASE_CPP_FLAGS) $(CC_FLAGS) $(CPP_FLAGS) -MMD -MP -MF $(@:%.o=%.d) $< -o $@
287
288 # Assembles an input ASM source file and generates a linkable object file for it
289 $(OBJDIR)/%.o: %.S $(MAKEFILE_LIST)
290 @echo $(MSG_ASSEMBLE_CMD) Assembling \"$(notdir $<)\"
291 $(CROSS)-gcc -c $(BASE_CC_FLAGS) $(BASE_ASM_FLAGS) $(CC_FLAGS) $(ASM_FLAGS) -MMD -MP -MF $(@:%.o=%.d) $< -o $@
292
293 # Generates a library archive file from the user application, which can be linked into other applications
294 .PRECIOUS : $(OBJECT_FILES)
295 .SECONDARY : %.a
296 %.a: $(OBJECT_FILES)
297 @echo $(MSG_ARCHIVE_CMD) Archiving object files into \"$@\"
298 $(CROSS)-ar rcs $@ $(OBJECT_FILES)
299
300 # Generates an ELF debug file from the user application, which can be further processed for FLASH and EEPROM data
301 # files, or used for programming and debugging directly
302 .PRECIOUS : $(OBJECT_FILES)
303 .SECONDARY : %.elf
304 %.elf: $(OBJECT_FILES)
305 @echo $(MSG_LINK_CMD) Linking object files into \"$@\"
306 $(CROSS)-gcc $^ -o $@ $(BASE_LD_FLAGS) $(LD_FLAGS)
307
308 # Extracts out the loadable FLASH memory data from the project ELF file, and creates an Intel HEX format file of it
309 %.hex: %.elf
310 @echo $(MSG_OBJCPY_CMD) Extracting HEX file data from \"$<\"
311 $(CROSS)-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@
312
313 # Extracts out the loadable FLASH memory data from the project ELF file, and creates an Binary format file of it
314 %.bin: %.elf
315 @echo $(MSG_OBJCPY_CMD) Extracting BIN file data from \"$<\"
316 $(CROSS)-objcopy -O binary -R .eeprom -R .fuse -R .lock -R .signature $< $@
317
318 # Extracts out the loadable EEPROM memory data from the project ELF file, and creates an Intel HEX format file of it
319 %.eep: %.elf
320 @echo $(MSG_OBJCPY_CMD) Extracting EEP file data from \"$<\"
321 $(CROSS)-objcopy -O ihex -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings $< $@ || exit 0
322
323 # Creates an assembly listing file from an input project ELF file, containing interleaved assembly and source data
324 %.lss: %.elf
325 @echo $(MSG_OBJDMP_CMD) Extracting LSS file data from \"$<\"
326 $(CROSS)-objdump -h -d -S -z $< > $@
327
328 # Creates a symbol file listing the loadable and discarded symbols from an input project ELF file
329 %.sym: %.elf
330 @echo $(MSG_NM_CMD) Extracting SYM file data from \"$<\"
331 $(CROSS)-nm -n $< > $@
332
333 # Include build dependency files
334 -include $(DEPENDENCY_FILES)
335
336 # Phony build targets for this module
337 .PHONY: build_begin build_end $(DMBS_BUILD_TARGETS)