upload
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

lufa_build.mk 15KB

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