Remove unused/obsolete maintenance scripts.
[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 += COMPILER_PATH OPTIMIZATION C_STANDARD CPP_STANDARD F_CPU C_FLAGS
13 DMBS_BUILD_OPTIONAL_VARS += CPP_FLAGS ASM_FLAGS CC_FLAGS LD_FLAGS OBJDIR OBJECT_FILES DEBUG_TYPE
14 DMBS_BUILD_OPTIONAL_VARS += DEBUG_LEVEL LINKER_RELAXATIONS JUMP_TABLES LTO
15 DMBS_BUILD_PROVIDED_VARS +=
16 DMBS_BUILD_PROVIDED_MACROS +=
17
18 # Import the CORE module of DMBS
19 DMBS_MODULE_PATH := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
20 include $(DMBS_MODULE_PATH)/core.mk
21
22 # Default values of optionally user-supplied variables
23 COMPILER_PATH ?=
24 OPTIMIZATION ?= s
25 F_CPU ?=
26 C_STANDARD ?= gnu99
27 CPP_STANDARD ?= gnu++98
28 C_FLAGS ?=
29 CPP_FLAGS ?=
30 ASM_FLAGS ?=
31 CC_FLAGS ?=
32 OBJDIR ?= obj
33 OBJECT_FILES ?=
34 DEBUG_FORMAT ?= dwarf-2
35 DEBUG_LEVEL ?= 2
36 LINKER_RELAXATIONS ?= Y
37 JUMP_TABLES ?= N
38 LTO ?= N
39
40 # Sanity check user supplied values
41 $(foreach MANDATORY_VAR, $(DMBS_BUILD_MANDATORY_VARS), $(call ERROR_IF_UNSET, $(MANDATORY_VAR)))
42 $(call ERROR_IF_EMPTY, MCU)
43 $(call ERROR_IF_EMPTY, TARGET)
44 $(call ERROR_IF_EMPTY, ARCH)
45 $(call ERROR_IF_EMPTY, OPTIMIZATION)
46 $(call ERROR_IF_EMPTY, C_STANDARD)
47 $(call ERROR_IF_EMPTY, CPP_STANDARD)
48 $(call ERROR_IF_EMPTY, OBJDIR)
49 $(call ERROR_IF_EMPTY, DEBUG_FORMAT)
50 $(call ERROR_IF_EMPTY, DEBUG_LEVEL)
51 $(call ERROR_IF_NONBOOL, LINKER_RELAXATIONS)
52 $(call ERROR_IF_NONBOOL, JUMP_TABLES)
53 $(call ERROR_IF_NONBOOL, LTO)
54
55 # Determine the utility prefix to use for the selected architecture
56 ifeq ($(ARCH), AVR8)
57 CROSS := $(COMPILER_PATH)avr
58 else ifeq ($(ARCH), XMEGA)
59 CROSS := $(COMPILER_PATH)avr
60 else ifeq ($(ARCH), UC3)
61 CROSS := $(COMPILER_PATH)avr32
62 else
63 $(error Unsupported architecture "$(ARCH)")
64 endif
65
66 # Output Messages
67 MSG_INFO_MESSAGE := ' [INFO] :'
68 MSG_COMPILE_CMD := ' [GCC] :'
69 MSG_ASSEMBLE_CMD := ' [GAS] :'
70 MSG_NM_CMD := ' [NM] :'
71 MSG_REMOVE_CMD := ' [RM] :'
72 MSG_LINK_CMD := ' [LNK] :'
73 MSG_ARCHIVE_CMD := ' [AR] :'
74 MSG_SIZE_CMD := ' [SIZE] :'
75 MSG_OBJCPY_CMD := ' [OBJCPY] :'
76 MSG_OBJDMP_CMD := ' [OBJDMP] :'
77
78 # Convert input source file list to differentiate them by type
79 C_SOURCE := $(filter %.c, $(SRC))
80 CPP_SOURCE := $(filter %.cpp, $(SRC))
81 ASM_SOURCE := $(filter %.S, $(SRC))
82
83 # Create a list of unknown source file types, if any are found throw an error
84 UNKNOWN_SOURCE := $(filter-out $(C_SOURCE) $(CPP_SOURCE) $(ASM_SOURCE), $(SRC))
85 ifneq ($(UNKNOWN_SOURCE),)
86 $(error Unknown input source file formats: $(UNKNOWN_SOURCE))
87 endif
88
89 # Convert input source filenames into a list of required output object files
90 OBJECT_FILES += $(addsuffix .o, $(basename $(SRC)))
91
92 # Check if an output object file directory was specified instead of the input file location
93 ifneq ($(OBJDIR),.)
94 # Prefix all the object filenames with the output object file directory path
95 OBJECT_FILES := $(addprefix $(patsubst %/,%,$(OBJDIR))/, $(notdir $(OBJECT_FILES)))
96
97 # Check if any object file (without path) appears more than once in the object file list
98 ifneq ($(words $(sort $(OBJECT_FILES))), $(words $(OBJECT_FILES)))
99 $(error Cannot build with OBJDIR parameter set - one or more object file name is not unique)
100 endif
101
102 # Create the output object file directory if it does not exist and add it to the virtual path list
103 $(shell mkdir -p $(OBJDIR) 2> /dev/null)
104 VPATH += $(dir $(SRC))
105 endif
106
107 # Create a list of dependency files from the list of object files
108 DEPENDENCY_FILES := $(OBJECT_FILES:%.o=%.d)
109
110 # Create a list of common flags to pass to the compiler/linker/assembler
111 BASE_CC_FLAGS := -pipe -g$(DEBUG_FORMAT) -g$(DEBUG_LEVEL)
112 ifneq ($(findstring $(ARCH), AVR8 XMEGA),)
113 BASE_C_FLAGS += -fpack-struct
114 BASE_CC_FLAGS += -mmcu=$(MCU) -fshort-enums -fno-inline-small-functions
115 else ifneq ($(findstring $(ARCH), UC3),)
116 BASE_CC_FLAGS += -mpart=$(MCU:at32%=%) -masm-addr-pseudos
117 endif
118 BASE_CC_FLAGS += -Wall -fno-strict-aliasing -funsigned-char -funsigned-bitfields -ffunction-sections -fdiagnostics-color
119 BASE_CC_FLAGS += -I.
120 BASE_CC_FLAGS += -DARCH=ARCH_$(ARCH)
121 ifneq ($(F_CPU),)
122 BASE_CC_FLAGS += -DF_CPU=$(F_CPU)UL
123 endif
124 ifeq ($(LINKER_RELAXATIONS), Y)
125 BASE_CC_FLAGS += -mrelax
126 endif
127 ifeq ($(JUMP_TABLES), N)
128 # This flag is required for bootloaders as GCC will emit invalid jump table
129 # assembly code for devices with large amounts of flash; the jump table target
130 # is extracted from FLASH without using the correct ELPM instruction, resulting
131 # in a pseudo-random jump target.
132 BASE_CC_FLAGS += -fno-jump-tables
133 endif
134 ifeq ($(LTO), Y)
135 # Enable link time optimization to reduce overall flash size.
136 BASE_CC_FLAGS += -flto -fuse-linker-plugin
137 BASE_LD_FLAGS += -flto -fuse-linker-plugin
138 endif
139
140 # Additional language specific compiler flags
141 BASE_C_FLAGS := -x c -O$(OPTIMIZATION) -std=$(C_STANDARD) -Wstrict-prototypes
142 BASE_CPP_FLAGS := -x c++ -O$(OPTIMIZATION) -std=$(CPP_STANDARD) -fno-exceptions -fno-threadsafe-statics
143 BASE_ASM_FLAGS := -x assembler-with-cpp
144
145 # Create a list of flags to pass to the linker
146 BASE_LD_FLAGS := -lm -Wl,-Map=$(TARGET).map,--cref -Wl,--gc-sections
147 ifeq ($(LINKER_RELAXATIONS), Y)
148 BASE_LD_FLAGS += -Wl,--relax
149 endif
150 ifneq ($(findstring $(ARCH), AVR8 XMEGA),)
151 BASE_LD_FLAGS += -mmcu=$(MCU)
152 else ifneq ($(findstring $(ARCH), UC3),)
153 BASE_LD_FLAGS += -mpart=$(MCU:at32%=%) --rodata-writable --direct-data
154 endif
155
156 # Determine flags to pass to the size utility based on its reported features (only invoke if size target required)
157 # and on an architecture where this non-standard patch is available
158 ifneq ($(ARCH), UC3)
159 size: SIZE_MCU_FLAG := $(shell $(CROSS)-size --help | grep -- --mcu > /dev/null && echo --mcu=$(MCU) )
160 size: SIZE_FORMAT_FLAG := $(shell $(CROSS)-size --help | grep -- --format=.*avr > /dev/null && echo --format=avr )
161 endif
162
163 # Pre-build informational target, to give compiler and project name information when building
164 build_begin:
165 @echo $(MSG_INFO_MESSAGE) Begin compilation of project \"$(TARGET)\"...
166 @echo ""
167 @$(CROSS)-gcc --version
168
169 # Post-build informational target, to project name information when building has completed
170 build_end:
171 @echo $(MSG_INFO_MESSAGE) Finished building project \"$(TARGET)\".
172
173 # Prints size information of a compiled application (FLASH, RAM and EEPROM usages)
174 size: $(TARGET).elf
175 @echo $(MSG_SIZE_CMD) Determining size of \"$<\"
176 @echo ""
177 $(CROSS)-size $(SIZE_MCU_FLAG) $(SIZE_FORMAT_FLAG) $<
178
179 # Prints size information on the symbols within a compiled application in decimal bytes
180 symbol-sizes: $(TARGET).elf
181 @echo $(MSG_NM_CMD) Extracting \"$<\" symbols with decimal byte sizes
182 $(CROSS)-nm --size-sort --demangle --radix=d $<
183
184 # Cleans intermediary build files, leaving only the compiled application files
185 mostlyclean:
186 @echo $(MSG_REMOVE_CMD) Removing object files of \"$(TARGET)\"
187 rm -f $(OBJECT_FILES)
188 @echo $(MSG_REMOVE_CMD) Removing dependency files of \"$(TARGET)\"
189 rm -f $(DEPENDENCY_FILES)
190
191 # Cleans all build files, leaving only the original source code
192 clean: mostlyclean
193 @echo $(MSG_REMOVE_CMD) Removing output files of \"$(TARGET)\"
194 rm -f $(TARGET).elf $(TARGET).hex $(TARGET).bin $(TARGET).eep $(TARGET).map $(TARGET).lss $(TARGET).sym lib$(TARGET).a
195
196 # Performs a complete build of the user application and prints size information afterwards
197 all: build_begin elf hex bin lss sym size build_end
198
199 # Helper targets, to build a specific type of output file without having to know the project target name
200 lib: lib$(TARGET).a
201 elf: $(TARGET).elf
202 hex: $(TARGET).hex $(TARGET).eep
203 bin: $(TARGET).bin
204 lss: $(TARGET).lss
205 sym: $(TARGET).sym
206
207 # Default target to *create* the user application's specified source files; if this rule is executed by
208 # make, the input source file doesn't exist and an error needs to be presented to the user
209 $(SRC):
210 $(error Source file does not exist: $@)
211
212 # Compiles an input C source file and generates an assembly listing for it
213 %.s: %.c $(MAKEFILE_LIST)
214 @echo $(MSG_COMPILE_CMD) Generating assembly from C file \"$(notdir $<)\"
215 $(CROSS)-gcc -S $(BASE_CC_FLAGS) $(BASE_C_FLAGS) $(CC_FLAGS) $(C_FLAGS) $($(notdir $<)_FLAGS) $< -o $@
216
217 # Compiles an input C++ source file and generates an assembly listing for it
218 %.s: %.cpp $(MAKEFILE_LIST)
219 @echo $(MSG_COMPILE_CMD) Generating assembly from C++ file \"$(notdir $<)\"
220 $(CROSS)-gcc -S $(BASE_CC_FLAGS) $(BASE_CPP_FLAGS) $(CC_FLAGS) $(CPP_FLAGS) $($(notdir $<)_FLAGS) $< -o $@
221
222 # Compiles an input C source file and generates a linkable object file for it
223 $(OBJDIR)/%.o: %.c $(MAKEFILE_LIST)
224 @echo $(MSG_COMPILE_CMD) Compiling C file \"$(notdir $<)\"
225 $(CROSS)-gcc -c $(BASE_CC_FLAGS) $(BASE_C_FLAGS) $(CC_FLAGS) $(C_FLAGS) $($(notdir $<)_FLAGS) -MMD -MP -MF $(@:%.o=%.d) $< -o $@
226
227 # Compiles an input C++ source file and generates a linkable object file for it
228 $(OBJDIR)/%.o: %.cpp $(MAKEFILE_LIST)
229 @echo $(MSG_COMPILE_CMD) Compiling C++ file \"$(notdir $<)\"
230 $(CROSS)-gcc -c $(BASE_CC_FLAGS) $(BASE_CPP_FLAGS) $(CC_FLAGS) $(CPP_FLAGS) $($(notdir $<)_FLAGS) -MMD -MP -MF $(@:%.o=%.d) $< -o $@
231
232 # Assembles an input ASM source file and generates a linkable object file for it
233 $(OBJDIR)/%.o: %.S $(MAKEFILE_LIST)
234 @echo $(MSG_ASSEMBLE_CMD) Assembling \"$(notdir $<)\"
235 $(CROSS)-gcc -c $(BASE_CC_FLAGS) $(BASE_ASM_FLAGS) $(CC_FLAGS) $(ASM_FLAGS) $($(notdir $<)_FLAGS) -MMD -MP -MF $(@:%.o=%.d) $< -o $@
236
237 # Generates a library archive file from the user application, which can be linked into other applications
238 .PRECIOUS : $(OBJECT_FILES)
239 .SECONDARY : %.a
240 %.a: $(OBJECT_FILES)
241 @echo $(MSG_ARCHIVE_CMD) Archiving object files into \"$@\"
242 $(CROSS)-ar rcs $@ $(OBJECT_FILES)
243
244 # Generates an ELF debug file from the user application, which can be further processed for FLASH and EEPROM data
245 # files, or used for programming and debugging directly
246 .PRECIOUS : $(OBJECT_FILES)
247 .SECONDARY : %.elf
248 %.elf: $(OBJECT_FILES)
249 @echo $(MSG_LINK_CMD) Linking object files into \"$@\"
250 $(CROSS)-gcc $^ -o $@ $(BASE_LD_FLAGS) $(LD_FLAGS)
251
252 # Extracts out the loadable FLASH memory data from the project ELF file, and creates an Intel HEX format file of it
253 %.hex: %.elf
254 @echo $(MSG_OBJCPY_CMD) Extracting HEX file data from \"$<\"
255 $(CROSS)-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@
256
257 # Extracts out the loadable FLASH memory data from the project ELF file, and creates an Binary format file of it
258 %.bin: %.elf
259 @echo $(MSG_OBJCPY_CMD) Extracting BIN file data from \"$<\"
260 $(CROSS)-objcopy -O binary -R .eeprom -R .fuse -R .lock -R .signature $< $@
261
262 # Extracts out the loadable EEPROM memory data from the project ELF file, and creates an Intel HEX format file of it
263 %.eep: %.elf
264 @echo $(MSG_OBJCPY_CMD) Extracting EEP file data from \"$<\"
265 $(CROSS)-objcopy -O ihex -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings $< $@ || exit 0
266
267 # Creates an assembly listing file from an input project ELF file, containing interleaved assembly and source data
268 %.lss: %.elf
269 @echo $(MSG_OBJDMP_CMD) Extracting LSS file data from \"$<\"
270 $(CROSS)-objdump -h -d -S -z $< > $@
271
272 # Creates a symbol file listing the loadable and discarded symbols from an input project ELF file
273 %.sym: %.elf
274 @echo $(MSG_NM_CMD) Extracting SYM file data from \"$<\"
275 $(CROSS)-nm -n $< > $@
276
277 # Include build dependency files
278 -include $(DEPENDENCY_FILES)
279
280 # Phony build targets for this module
281 .PHONY: build_begin build_end $(DMBS_BUILD_TARGETS)