Makefile.avr.in 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. # List of available make goals:
  2. #
  3. # all Default target, builds the project
  4. # clean Clean up the project
  5. # rebuild Rebuild the project
  6. #
  7. # doc Build the documentation
  8. # cleandoc Clean up the documentation
  9. # rebuilddoc Rebuild the documentation
  10. #
  11. #
  12. # Copyright (c) 2009 - 2013 Atmel Corporation. All rights reserved.
  13. #
  14. # \asf_license_start
  15. #
  16. # \page License
  17. #
  18. # Redistribution and use in source and binary forms, with or without
  19. # modification, are permitted provided that the following conditions are met:
  20. #
  21. # 1. Redistributions of source code must retain the above copyright notice,
  22. # this list of conditions and the following disclaimer.
  23. #
  24. # 2. Redistributions in binary form must reproduce the above copyright notice,
  25. # this list of conditions and the following disclaimer in the documentation
  26. # and/or other materials provided with the distribution.
  27. #
  28. # 3. The name of Atmel may not be used to endorse or promote products derived
  29. # from this software without specific prior written permission.
  30. #
  31. # 4. This software may only be redistributed and used in connection with an
  32. # Atmel microcontroller product.
  33. #
  34. # THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  35. # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  36. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  37. # EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  38. # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  39. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  40. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  41. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  43. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  44. # POSSIBILITY OF SUCH DAMAGE.
  45. #
  46. # \asf_license_stop
  47. #
  48. # Include the config.mk file from the current working path, e.g., where the
  49. # user called make.
  50. include config.mk
  51. # Tool to use to generate documentation from the source code
  52. DOCGEN ?= doxygen
  53. # Look for source files relative to the top-level source directory
  54. VPATH := $(PRJ_PATH)
  55. # Output target file
  56. target := $(TARGET)
  57. # Output project name (target name minus suffix)
  58. project := $(basename $(target))
  59. # Output target file (typically ELF or static library)
  60. ifeq ($(suffix $(target)),.a)
  61. target_type := lib
  62. else
  63. ifeq ($(suffix $(target)),.elf)
  64. target_type := elf
  65. else
  66. $(error "Target type $(target_type) is not supported")
  67. endif
  68. endif
  69. # Allow override of operating system detection. The user can add OS=Linux or
  70. # OS=Windows on the command line to explicit set the host OS.
  71. #
  72. # This allows to work around broken uname utility on certain systems.
  73. ifdef OS
  74. ifeq ($(strip $(OS)), Linux)
  75. os_type := Linux
  76. endif
  77. ifeq ($(strip $(OS)), Windows)
  78. os_type := windows32_64
  79. endif
  80. endif
  81. os_type ?= $(strip $(shell uname))
  82. ifeq ($(os_type),windows32)
  83. os := Windows
  84. else
  85. ifeq ($(os_type),windows64)
  86. os := Windows
  87. else
  88. ifeq ($(os_type),windows32_64)
  89. os ?= Windows
  90. else
  91. ifeq ($(os_type),)
  92. os := Windows
  93. else
  94. # Default to Linux style operating system. Both Cygwin and mingw are fully
  95. # compatible (for this Makefile) with Linux.
  96. os := Linux
  97. endif
  98. endif
  99. endif
  100. endif
  101. # Output documentation directory and configuration file.
  102. docdir := ../doxygen/html
  103. doccfg := ../doxygen/doxyfile.doxygen
  104. CROSS ?= avr-
  105. AR := $(CROSS)ar
  106. AS := $(CROSS)as
  107. CC := $(CROSS)gcc
  108. CPP := $(CROSS)gcc -E
  109. CXX := $(CROSS)g++
  110. LD := $(CROSS)gcc
  111. NM := $(CROSS)nm
  112. OBJCOPY := $(CROSS)objcopy
  113. OBJDUMP := $(CROSS)objdump
  114. SIZE := $(CROSS)size
  115. RM := rm
  116. ifeq ($(os),Windows)
  117. RMDIR := rmdir /S /Q
  118. else
  119. RMDIR := rmdir -p --ignore-fail-on-non-empty
  120. endif
  121. # On Windows, we need to override the shell to force the use of cmd.exe
  122. ifeq ($(os),Windows)
  123. SHELL := cmd
  124. endif
  125. # Strings for beautifying output
  126. MSG_CLEAN_FILES = "RM *.o *.d"
  127. MSG_CLEAN_DIRS = "RMDIR $(strip $(clean-dirs))"
  128. MSG_CLEAN_DOC = "RMDIR $(docdir)"
  129. MSG_MKDIR = "MKDIR $(dir $@)"
  130. MSG_INFO = "INFO "
  131. MSG_PREBUILD = "PREBUILD $(PREBUILD_CMD)"
  132. MSG_POSTBUILD = "POSTBUILD $(POSTBUILD_CMD)"
  133. MSG_ARCHIVING = "AR $@"
  134. MSG_ASSEMBLING = "AS $@"
  135. MSG_BINARY_IMAGE = "OBJCOPY $@"
  136. MSG_COMPILING = "CC $@"
  137. MSG_COMPILING_CXX = "CXX $@"
  138. MSG_EEPROM_IMAGE = "OBJCOPY $@"
  139. MSG_EXTENDED_LISTING = "OBJDUMP $@"
  140. MSG_IHEX_IMAGE = "OBJCOPY $@"
  141. MSG_LINKING = "LN $@"
  142. MSG_PREPROCESSING = "CPP $@"
  143. MSG_SIZE = "SIZE $@"
  144. MSG_SYMBOL_TABLE = "NM $@"
  145. MSG_GENERATING_DOC = "DOXYGEN $(docdir)"
  146. # Don't use make's built-in rules and variables
  147. MAKEFLAGS += -rR
  148. # Don't print 'Entering directory ...'
  149. MAKEFLAGS += --no-print-directory
  150. # Function for reversing the order of a list
  151. reverse = $(if $(1),$(call reverse,$(wordlist 2,$(words $(1)),$(1)))) $(firstword $(1))
  152. # Hide command output by default, but allow the user to override this
  153. # by adding V=1 on the command line.
  154. #
  155. # This is inspired by the Kbuild system used by the Linux kernel.
  156. ifdef V
  157. ifeq ("$(origin V)", "command line")
  158. VERBOSE = $(V)
  159. endif
  160. endif
  161. ifndef VERBOSE
  162. VERBOSE = 0
  163. endif
  164. ifeq ($(VERBOSE), 1)
  165. Q =
  166. else
  167. Q = @
  168. endif
  169. arflags-gnu-y := $(ARFLAGS)
  170. asflags-gnu-y := $(ASFLAGS)
  171. cflags-gnu-y := $(CFLAGS)
  172. cxxflags-gnu-y := $(CXXFLAGS)
  173. cppflags-gnu-y := $(CPPFLAGS)
  174. cpuflags-gnu-y :=
  175. dbgflags-gnu-y := $(DBGFLAGS)
  176. libflags-gnu-y := $(foreach LIB,$(LIBS),-l$(LIB))
  177. ldflags-gnu-y := $(LDFLAGS)
  178. flashflags-gnu-y := $(FLASHFLAGS)
  179. eepromflags-gnu-y := $(EEPROMFLAGS)
  180. clean-files :=
  181. clean-dirs :=
  182. clean-files += $(wildcard $(target) $(project).map)
  183. clean-files += $(wildcard $(project).hex $(project).eep)
  184. clean-files += $(wildcard $(project).lss $(project).sym)
  185. clean-files += $(wildcard $(build))
  186. # Use pipes instead of temporary files for communication between processes
  187. cflags-gnu-y += -pipe
  188. asflags-gnu-y += -pipe
  189. ldflags-gnu-y += -pipe
  190. # Archiver flags.
  191. arflags-gnu-y += rcs
  192. # Always enable warnings. And be very careful about implicit
  193. # declarations.
  194. cflags-gnu-y += -Wall -Wstrict-prototypes -Wmissing-prototypes
  195. cflags-gnu-y += -Werror-implicit-function-declaration
  196. cxxflags-gnu-y += -Wall
  197. # IAR doesn't allow arithmetic on void pointers, so warn about that.
  198. cflags-gnu-y += -Wpointer-arith
  199. cxxflags-gnu-y += -Wpointer-arith
  200. # Preprocessor flags.
  201. cppflags-gnu-y += $(foreach INC,$(addprefix $(PRJ_PATH)/,$(INC_PATH)),-I$(INC))
  202. asflags-gnu-y += $(foreach INC,$(addprefix $(PRJ_PATH)/,$(INC_PATH)),'-Wa,-I$(INC)')
  203. # CPU specific flags.
  204. cpuflags-gnu-y += -mmcu=$(MCU)
  205. # Dependency file flags.
  206. depflags = -MD -MP -MQ $@
  207. # Debug specific flags.
  208. ifdef BUILD_DEBUG_LEVEL
  209. dbgflags-gnu-y += -g$(BUILD_DEBUG_LEVEL)
  210. else
  211. dbgflags-gnu-y += -gdwarf-2
  212. endif
  213. # Optimization specific flags.
  214. ifdef BUILD_OPTIMIZATION
  215. optflags-gnu-y = -O$(BUILD_OPTIMIZATION)
  216. else
  217. optflags-gnu-y = $(OPTIMIZATION)
  218. endif
  219. # Relax compilation and linking.
  220. cflags-gnu-y += -mrelax
  221. cxxflags-gnu-y += -mrelax
  222. asflags-gnu-y += -mrelax
  223. ldflags-gnu-y += -Wl,--relax
  224. # Always preprocess assembler files.
  225. asflags-gnu-y += -x assembler-with-cpp
  226. # Compile C files using the GNU99 standard.
  227. cflags-gnu-y += -std=gnu99
  228. # Compile C++ files using the GNU++98 standard.
  229. cxxflags-gnu-y += -std=gnu++98
  230. # Use unsigned character type when compiling.
  231. cflags-gnu-y += -funsigned-char
  232. cxxflags-gnu-y += -funsigned-char
  233. # Don't use strict aliasing (very common in embedded applications).
  234. cflags-gnu-y += -fno-strict-aliasing
  235. cxxflags-gnu-y += -fno-strict-aliasing
  236. # Separate each function and data into its own separate section to allow
  237. # garbage collection of unused sections.
  238. cflags-gnu-y += -ffunction-sections -fdata-sections
  239. cxxflags-gnu-y += -ffunction-sections -fdata-sections
  240. # Garbage collect unreferred sections when linking.
  241. ldflags-gnu-y += -Wl,--gc-sections
  242. # Output a link map file and a cross reference table
  243. ldflags-gnu-y += -Wl,-Map=$(project).map,--cref
  244. # Add library search paths relative to the top level directory.
  245. ldflags-gnu-y += $(foreach _LIB_PATH,$(addprefix $(PRJ_PATH)/,$(LIB_PATH)),-L$(_LIB_PATH))
  246. a_flags = $(cpuflags-gnu-y) $(depflags) $(cppflags-gnu-y) $(asflags-gnu-y) -D__ASSEMBLY__
  247. c_flags = $(cpuflags-gnu-y) $(dbgflags-gnu-y) $(depflags) $(optflags-gnu-y) $(cppflags-gnu-y) $(cflags-gnu-y)
  248. cxx_flags= $(cpuflags-gnu-y) $(dbgflags-gnu-y) $(depflags) $(optflags-gnu-y) $(cppflags-gnu-y) $(cxxflags-gnu-y)
  249. l_flags = $(cpuflags-gnu-y) $(optflags-gnu-y) $(ldflags-gnu-y)
  250. ar_flags = $(arflags-gnu-y)
  251. # Intel Hex file production flags
  252. flashflags-gnu-y += -R .eeprom -R .usb_descriptor_table
  253. # Eeprom file production flags
  254. eepromflags-gnu-y += -j .eeprom
  255. eepromflags-gnu-y += --set-section-flags=.eeprom="alloc,load"
  256. eepromflags-gnu-y += --change-section-lma .eeprom=0
  257. # Source files list and part informations must already be included before
  258. # running this makefile
  259. # If a custom build directory is specified, use it -- force trailing / in directory name.
  260. ifdef BUILD_DIR
  261. build-dir := $(dir $(BUILD_DIR))$(if $(notdir $(BUILD_DIR)),$(notdir $(BUILD_DIR))/)
  262. else
  263. build-dir =
  264. endif
  265. # Create object files list from source files list.
  266. obj-y := $(addprefix $(build-dir), $(addsuffix .o,$(basename $(CSRCS) $(ASSRCS))))
  267. # Create dependency files list from source files list.
  268. dep-files := $(wildcard $(foreach f,$(obj-y),$(basename $(f)).d))
  269. clean-files += $(wildcard $(obj-y))
  270. clean-files += $(dep-files)
  271. clean-dirs += $(call reverse,$(sort $(wildcard $(dir $(obj-y)))))
  272. # Default target.
  273. .PHONY: all
  274. ifeq ($(target_type),lib)
  275. all: $(target) $(project).lss $(project).sym
  276. else
  277. ifeq ($(target_type),elf)
  278. all: prebuild $(target) $(project).lss $(project).sym $(project).hex $(project).bin postbuild
  279. endif
  280. endif
  281. prebuild:
  282. ifneq ($(strip $(PREBUILD_CMD)),)
  283. @echo $(MSG_PREBUILD)
  284. $(Q)$(PREBUILD_CMD)
  285. endif
  286. postbuild:
  287. ifneq ($(strip $(POSTBUILD_CMD)),)
  288. @echo $(MSG_POSTBUILD)
  289. $(Q)$(POSTBUILD_CMD)
  290. endif
  291. # Clean up the project.
  292. .PHONY: clean
  293. clean:
  294. @$(if $(strip $(clean-files)),echo $(MSG_CLEAN_FILES))
  295. $(if $(strip $(clean-files)),$(Q)$(RM) $(clean-files),)
  296. @$(if $(strip $(clean-dirs)),echo $(MSG_CLEAN_DIRS))
  297. # Remove created directories, and make sure we only remove existing
  298. # directories, since recursive rmdir might help us a bit on the way.
  299. ifeq ($(os),Windows)
  300. $(Q)$(if $(strip $(clean-dirs)), \
  301. $(RMDIR) $(strip $(subst /,\,$(clean-dirs))))
  302. else
  303. $(Q)$(if $(strip $(clean-dirs)), \
  304. for directory in $(strip $(clean-dirs)); do \
  305. if [ -d "$$directory" ]; then \
  306. $(RMDIR) $$directory; \
  307. fi \
  308. done \
  309. )
  310. endif
  311. # Rebuild the project.
  312. .PHONY: rebuild
  313. rebuild: clean all
  314. .PHONY: objfiles
  315. objfiles: $(obj-y)
  316. # Create object files from C source files.
  317. $(build-dir)%.o: %.c $(MAKEFILE_PATH) config.mk
  318. $(Q)test -d $(dir $@) || echo $(MSG_MKDIR)
  319. ifeq ($(os),Windows)
  320. $(Q)test -d $(patsubst %/,%,$(dir $@)) || mkdir $(subst /,\,$(dir $@))
  321. else
  322. $(Q)test -d $(dir $@) || mkdir -p $(dir $@)
  323. endif
  324. @echo $(MSG_COMPILING)
  325. $(Q)$(CC) $(c_flags) -c $< -o $@
  326. # Create object files from C++ source files.
  327. $(build-dir)%.o: %.cpp $(MAKEFILE_PATH) config.mk
  328. $(Q)test -d $(dir $@) || echo $(MSG_MKDIR)
  329. ifeq ($(os),Windows)
  330. $(Q)test -d $(patsubst %/,%,$(dir $@)) || mkdir $(subst /,\,$(dir $@))
  331. else
  332. $(Q)test -d $(dir $@) || mkdir -p $(dir $@)
  333. endif
  334. @echo $(MSG_COMPILING_CXX)
  335. $(Q)$(CXX) $(cxx_flags) -c $< -o $@
  336. # Preprocess and assemble: create object files from assembler source files.
  337. $(build-dir)%.o: %.s $(MAKEFILE_PATH) config.mk
  338. $(Q)test -d $(dir $@) || echo $(MSG_MKDIR)
  339. ifeq ($(os),Windows)
  340. $(Q)test -d $(patsubst %/,%,$(dir $@)) || mkdir $(subst /,\,$(dir $@))
  341. else
  342. $(Q)test -d $(dir $@) || mkdir -p $(dir $@)
  343. endif
  344. @echo $(MSG_ASSEMBLING)
  345. $(Q)$(CC) $(a_flags) -c $< -o $@
  346. # Preprocess and assemble: create object files from assembler source files.
  347. $(build-dir)%.o: %.S $(MAKEFILE_PATH) config.mk
  348. $(Q)test -d $(dir $@) || echo $(MSG_MKDIR)
  349. ifeq ($(os),Windows)
  350. $(Q)test -d $(patsubst %/,%,$(dir $@)) || mkdir $(subst /,\,$(dir $@))
  351. else
  352. $(Q)test -d $(dir $@) || mkdir -p $(dir $@)
  353. endif
  354. @echo $(MSG_ASSEMBLING)
  355. $(Q)$(CC) $(a_flags) -c $< -o $@
  356. # Include all dependency files to add depedency to all header files in use.
  357. include $(dep-files)
  358. ifeq ($(target_type),lib)
  359. # Archive object files into an archive
  360. $(target): $(MAKEFILE_PATH) config.mk $(obj-y)
  361. @echo $(MSG_ARCHIVING)
  362. $(Q)$(AR) $(ar_flags) $@ $(obj-y)
  363. @echo $(MSG_SIZE)
  364. $(Q)$(SIZE) -Bxt $@
  365. else
  366. ifeq ($(target_type),elf)
  367. # Link the object files into an ELF file. Also make sure the target is rebuilt
  368. # if the common Makefile.avr.in or project config.mk is changed.
  369. $(target): $(MAKEFILE_PATH) config.mk $(obj-y)
  370. @echo $(MSG_LINKING)
  371. $(Q)$(LD) $(l_flags) $(obj-y) $(libflags-gnu-y) -o $@
  372. @echo $(MSG_SIZE)
  373. $(Q)$(SIZE) -Ax $@
  374. $(Q)$(SIZE) -Bx $@
  375. endif
  376. endif
  377. # Create extended function listing from target output file.
  378. %.lss: $(target)
  379. @echo $(MSG_EXTENDED_LISTING)
  380. $(Q)$(OBJDUMP) -h -S $< > $@
  381. # Create symbol table from target output file.
  382. %.sym: $(target)
  383. @echo $(MSG_SYMBOL_TABLE)
  384. $(Q)$(NM) -n $< > $@
  385. # Create Intel HEX image from ELF output file.
  386. %.hex: $(target)
  387. @echo $(MSG_IHEX_IMAGE)
  388. $(Q)$(OBJCOPY) -O ihex $(flashflags-gnu-y) $< $@
  389. # Create EEPROM Intel HEX image from ELF output file.
  390. %.eep: $(target)
  391. @echo $(MSG_EEPROM_IMAGE)
  392. $(Q)$(OBJCOPY) $(eepromflags-gnu-y) -O ihex $< $@ || exit 0
  393. # Create binary image from ELF output file.
  394. %.bin: $(target)
  395. @echo $(MSG_BINARY_IMAGE)
  396. $(Q)$(OBJCOPY) -O binary $< $@
  397. # Provide information about the detected host operating system.
  398. .SECONDARY: info-os
  399. info-os:
  400. @echo $(MSG_INFO)$(os) build host detected
  401. # Build Doxygen generated documentation.
  402. .PHONY: doc
  403. doc:
  404. @echo $(MSG_GENERATING_DOC)
  405. $(Q)cd $(dir $(doccfg)) && $(DOCGEN) $(notdir $(doccfg))
  406. # Clean Doxygen generated documentation.
  407. .PHONY: cleandoc
  408. cleandoc:
  409. @$(if $(wildcard $(docdir)),echo $(MSG_CLEAN_DOC))
  410. $(Q)$(if $(wildcard $(docdir)),$(RM) --recursive $(docdir))
  411. # Rebuild the Doxygen generated documentation.
  412. .PHONY: rebuilddoc
  413. rebuilddoc: cleandoc doc