gcc.mk 11 KB

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