Makefile.sam.in 16 KB

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