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 - 2013 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 52# Include the config.mk file from the current working path, e.g., where the 53# user called make. 54include config.mk 55 56# Tool to use to generate documentation from the source code 57DOCGEN ?= doxygen 58 59# Look for source files relative to the top-level source directory 60VPATH := $(PRJ_PATH) 61 62# Output target file 63project_type := $(PROJECT_TYPE) 64 65# Output target file 66ifeq ($(project_type),flash) 67target := $(TARGET_FLASH) 68linker_script := $(PRJ_PATH)/$(LINKER_SCRIPT_FLASH) 69debug_script := $(PRJ_PATH)/$(DEBUG_SCRIPT_FLASH) 70else 71target := $(TARGET_SRAM) 72linker_script := $(PRJ_PATH)/$(LINKER_SCRIPT_SRAM) 73debug_script := $(PRJ_PATH)/$(DEBUG_SCRIPT_SRAM) 74endif 75 76# Output project name (target name minus suffix) 77project := $(basename $(target)) 78 79# Output target file (typically ELF or static library) 80ifeq ($(suffix $(target)),.a) 81target_type := lib 82else 83ifeq ($(suffix $(target)),.elf) 84target_type := elf 85else 86$(error "Target type $(target_type) is not supported") 87endif 88endif 89 90# Allow override of operating system detection. The user can add OS=Linux or 91# OS=Windows on the command line to explicit set the host OS. 92# 93# This allows to work around broken uname utility on certain systems. 94ifdef OS 95 ifeq ($(strip $(OS)), Linux) 96 os_type := Linux 97 endif 98 ifeq ($(strip $(OS)), Windows) 99 os_type := windows32_64 100 endif 101endif 102 103os_type ?= $(strip $(shell uname)) 104 105ifeq ($(os_type),windows32) 106os := Windows 107else 108ifeq ($(os_type),windows64) 109os := Windows 110else 111ifeq ($(os_type),windows32_64) 112os ?= Windows 113else 114ifeq ($(os_type),) 115os := Windows 116else 117# Default to Linux style operating system. Both Cygwin and mingw are fully 118# compatible (for this Makefile) with Linux. 119os := Linux 120endif 121endif 122endif 123endif 124 125# Output documentation directory and configuration file. 126docdir := ../doxygen/html 127doccfg := ../doxygen/doxyfile.doxygen 128 129CROSS ?= arm-none-eabi- 130AR := $(CROSS)ar 131AS := $(CROSS)as 132CC := $(CROSS)gcc 133CPP := $(CROSS)gcc -E 134CXX := $(CROSS)g++ 135LD := $(CROSS)g++ 136NM := $(CROSS)nm 137OBJCOPY := $(CROSS)objcopy 138OBJDUMP := $(CROSS)objdump 139SIZE := $(CROSS)size 140GDB := $(CROSS)gdb 141 142RM := rm 143ifeq ($(os),Windows) 144RMDIR := rmdir /S /Q 145else 146RMDIR := rmdir -p --ignore-fail-on-non-empty 147endif 148 149# On Windows, we need to override the shell to force the use of cmd.exe 150ifeq ($(os),Windows) 151SHELL := cmd 152endif 153 154# Strings for beautifying output 155MSG_CLEAN_FILES = "RM *.o *.d" 156MSG_CLEAN_DIRS = "RMDIR $(strip $(clean-dirs))" 157MSG_CLEAN_DOC = "RMDIR $(docdir)" 158MSG_MKDIR = "MKDIR $(dir $@)" 159 160MSG_INFO = "INFO " 161MSG_PREBUILD = "PREBUILD $(PREBUILD_CMD)" 162MSG_POSTBUILD = "POSTBUILD $(POSTBUILD_CMD)" 163 164MSG_ARCHIVING = "AR $@" 165MSG_ASSEMBLING = "AS $@" 166MSG_BINARY_IMAGE = "OBJCOPY $@" 167MSG_COMPILING = "CC $@" 168MSG_COMPILING_CXX = "CXX $@" 169MSG_EXTENDED_LISTING = "OBJDUMP $@" 170MSG_IHEX_IMAGE = "OBJCOPY $@" 171MSG_LINKING = "LN $@" 172MSG_PREPROCESSING = "CPP $@" 173MSG_SIZE = "SIZE $@" 174MSG_SYMBOL_TABLE = "NM $@" 175 176MSG_GENERATING_DOC = "DOXYGEN $(docdir)" 177 178# Don't use make's built-in rules and variables 179MAKEFLAGS += -rR 180 181# Don't print 'Entering directory ...' 182MAKEFLAGS += --no-print-directory 183 184# Function for reversing the order of a list 185reverse = $(if $(1),$(call reverse,$(wordlist 2,$(words $(1)),$(1)))) $(firstword $(1)) 186 187# Hide command output by default, but allow the user to override this 188# by adding V=1 on the command line. 189# 190# This is inspired by the Kbuild system used by the Linux kernel. 191ifdef V 192 ifeq ("$(origin V)", "command line") 193 VERBOSE = $(V) 194 endif 195endif 196ifndef VERBOSE 197 VERBOSE = 0 198endif 199 200ifeq ($(VERBOSE), 1) 201 Q = 202else 203 Q = @ 204endif 205 206arflags-gnu-y := $(ARFLAGS) 207asflags-gnu-y := $(ASFLAGS) 208cflags-gnu-y := $(CFLAGS) 209cxxflags-gnu-y := $(CXXFLAGS) 210cppflags-gnu-y := $(CPPFLAGS) 211cpuflags-gnu-y := 212dbgflags-gnu-y := $(DBGFLAGS) 213libflags-gnu-y := $(foreach LIB,$(LIBS),-l$(LIB)) 214ldflags-gnu-y := $(LDFLAGS) 215flashflags-gnu-y := 216clean-files := 217clean-dirs := 218 219clean-files += $(wildcard $(target) $(project).map) 220clean-files += $(wildcard $(project).hex $(project).bin) 221clean-files += $(wildcard $(project).lss $(project).sym) 222clean-files += $(wildcard $(build)) 223 224# Use pipes instead of temporary files for communication between processes 225cflags-gnu-y += -pipe 226asflags-gnu-y += -pipe 227ldflags-gnu-y += -pipe 228 229# Archiver flags. 230arflags-gnu-y += rcs 231 232# Always enable warnings. And be very careful about implicit 233# declarations. 234cflags-gnu-y += -Wall -Wstrict-prototypes -Wmissing-prototypes 235cflags-gnu-y += -Werror-implicit-function-declaration 236cxxflags-gnu-y += -Wall 237# IAR doesn't allow arithmetic on void pointers, so warn about that. 238cflags-gnu-y += -Wpointer-arith 239cxxflags-gnu-y += -Wpointer-arith 240 241# Preprocessor flags. 242cppflags-gnu-y += $(foreach INC,$(addprefix $(PRJ_PATH)/,$(INC_PATH)),-I$(INC)) 243asflags-gnu-y += $(foreach INC,$(addprefix $(PRJ_PATH)/,$(INC_PATH)),'-Wa,-I$(INC)') 244 245# CPU specific flags. 246cpuflags-gnu-y += -mcpu=$(ARCH) -mthumb -D=__$(PART)__ 247 248# Dependency file flags. 249depflags = -MD -MP -MQ $@ 250 251# Debug specific flags. 252ifdef BUILD_DEBUG_LEVEL 253dbgflags-gnu-y += -g$(BUILD_DEBUG_LEVEL) 254else 255dbgflags-gnu-y += -g3 256endif 257 258# Optimization specific flags. 259ifdef BUILD_OPTIMIZATION 260optflags-gnu-y = -O$(BUILD_OPTIMIZATION) 261else 262optflags-gnu-y = $(OPTIMIZATION) 263endif 264 265# Always preprocess assembler files. 266asflags-gnu-y += -x assembler-with-cpp 267# Compile C files using the GNU99 standard. 268cflags-gnu-y += -std=gnu99 269# Compile C++ files using the GNU++98 standard. 270cxxflags-gnu-y += -std=gnu++98 271 272# Don't use strict aliasing (very common in embedded applications). 273cflags-gnu-y += -fno-strict-aliasing 274cxxflags-gnu-y += -fno-strict-aliasing 275 276# Separate each function and data into its own separate section to allow 277# garbage collection of unused sections. 278cflags-gnu-y += -ffunction-sections -fdata-sections 279cxxflags-gnu-y += -ffunction-sections -fdata-sections 280 281# Various cflags. 282cflags-gnu-y += -Wchar-subscripts -Wcomment -Wformat=2 -Wimplicit-int 283cflags-gnu-y += -Wmain -Wparentheses 284cflags-gnu-y += -Wsequence-point -Wreturn-type -Wswitch -Wtrigraphs -Wunused 285cflags-gnu-y += -Wuninitialized -Wunknown-pragmas -Wfloat-equal -Wundef 286cflags-gnu-y += -Wshadow -Wbad-function-cast -Wwrite-strings 287cflags-gnu-y += -Wsign-compare -Waggregate-return 288cflags-gnu-y += -Wmissing-declarations 289cflags-gnu-y += -Wformat -Wmissing-format-attribute -Wno-deprecated-declarations 290cflags-gnu-y += -Wpacked -Wredundant-decls -Wnested-externs -Winline -Wlong-long 291cflags-gnu-y += -Wunreachable-code 292cflags-gnu-y += -Wcast-align 293cflags-gnu-y += --param max-inline-insns-single=500 294 295# Garbage collect unreferred sections when linking. 296ldflags-gnu-y += -Wl,--gc-sections 297 298# Use the linker script if provided by the project. 299ifneq ($(strip $(linker_script)),) 300ldflags-gnu-y += -Wl,-T $(linker_script) 301endif 302 303# Output a link map file and a cross reference table 304ldflags-gnu-y += -Wl,-Map=$(project).map,--cref 305 306# Add library search paths relative to the top level directory. 307ldflags-gnu-y += $(foreach _LIB_PATH,$(addprefix $(PRJ_PATH)/,$(LIB_PATH)),-L$(_LIB_PATH)) 308 309a_flags = $(cpuflags-gnu-y) $(depflags) $(cppflags-gnu-y) $(asflags-gnu-y) -D__ASSEMBLY__ 310c_flags = $(cpuflags-gnu-y) $(dbgflags-gnu-y) $(depflags) $(optflags-gnu-y) $(cppflags-gnu-y) $(cflags-gnu-y) 311cxx_flags= $(cpuflags-gnu-y) $(dbgflags-gnu-y) $(depflags) $(optflags-gnu-y) $(cppflags-gnu-y) $(cxxflags-gnu-y) 312l_flags = -Wl,--entry=Reset_Handler -Wl,--cref $(cpuflags-gnu-y) $(optflags-gnu-y) $(ldflags-gnu-y) 313ar_flags = $(arflags-gnu-y) 314 315# Source files list and part informations must already be included before 316# running this makefile 317 318# If a custom build directory is specified, use it -- force trailing / in directory name. 319ifdef BUILD_DIR 320 build-dir := $(dir $(BUILD_DIR))$(if $(notdir $(BUILD_DIR)),$(notdir $(BUILD_DIR))/) 321else 322 build-dir = 323endif 324 325# Create object files list from source files list. 326obj-y := $(addprefix $(build-dir), $(addsuffix .o,$(basename $(CSRCS) $(ASSRCS)))) 327# Create dependency files list from source files list. 328dep-files := $(wildcard $(foreach f,$(obj-y),$(basename $(f)).d)) 329 330clean-files += $(wildcard $(obj-y)) 331clean-files += $(dep-files) 332 333clean-dirs += $(call reverse,$(sort $(wildcard $(dir $(obj-y))))) 334 335# Default target. 336.PHONY: all 337ifeq ($(project_type),all) 338all: 339 $(MAKE) all PROJECT_TYPE=flash 340 $(MAKE) all PROJECT_TYPE=sram 341else 342ifeq ($(target_type),lib) 343all: $(target) $(project).lss $(project).sym 344else 345ifeq ($(target_type),elf) 346all: prebuild $(target) $(project).lss $(project).sym $(project).hex $(project).bin postbuild 347endif 348endif 349endif 350 351prebuild: 352ifneq ($(strip $(PREBUILD_CMD)),) 353 @echo $(MSG_PREBUILD) 354 $(Q)$(PREBUILD_CMD) 355endif 356 357postbuild: 358ifneq ($(strip $(POSTBUILD_CMD)),) 359 @echo $(MSG_POSTBUILD) 360 $(Q)$(POSTBUILD_CMD) 361endif 362 363# Clean up the project. 364.PHONY: clean 365clean: 366 @$(if $(strip $(clean-files)),echo $(MSG_CLEAN_FILES)) 367 $(if $(strip $(clean-files)),$(Q)$(RM) $(clean-files),) 368 @$(if $(strip $(clean-dirs)),echo $(MSG_CLEAN_DIRS)) 369# Remove created directories, and make sure we only remove existing 370# directories, since recursive rmdir might help us a bit on the way. 371ifeq ($(os),Windows) 372 $(Q)$(if $(strip $(clean-dirs)), \ 373 $(RMDIR) $(strip $(subst /,\,$(clean-dirs)))) 374else 375 $(Q)$(if $(strip $(clean-dirs)), \ 376 for directory in $(strip $(clean-dirs)); do \ 377 if [ -d "$$directory" ]; then \ 378 $(RMDIR) $$directory; \ 379 fi \ 380 done \ 381 ) 382endif 383 384# Rebuild the project. 385.PHONY: rebuild 386rebuild: clean all 387 388# Debug the project in flash. 389.PHONY: debug_flash 390debug_flash: all 391 $(GDB) -x "$(PRJ_PATH)/$(DEBUG_SCRIPT_FLASH)" -ex "reset" -readnow -se $(TARGET_FLASH) 392 393# Debug the project in sram. 394.PHONY: debug_sram 395debug_sram: all 396 $(GDB) -x "$(PRJ_PATH)/$(DEBUG_SCRIPT_SRAM)" -ex "reset" -readnow -se $(TARGET_SRAM) 397 398.PHONY: objfiles 399objfiles: $(obj-y) 400 401# Create object files from C source files. 402$(build-dir)%.o: %.c $(MAKEFILE_PATH) config.mk 403 $(Q)test -d $(dir $@) || echo $(MSG_MKDIR) 404ifeq ($(os),Windows) 405 $(Q)test -d $(patsubst %/,%,$(dir $@)) || mkdir $(subst /,\,$(dir $@)) 406else 407 $(Q)test -d $(dir $@) || mkdir -p $(dir $@) 408endif 409 @echo $(MSG_COMPILING) 410 $(Q)$(CC) $(c_flags) -c $< -o $@ 411 412# Create object files from C++ source files. 413$(build-dir)%.o: %.cpp $(MAKEFILE_PATH) config.mk 414 $(Q)test -d $(dir $@) || echo $(MSG_MKDIR) 415ifeq ($(os),Windows) 416 $(Q)test -d $(patsubst %/,%,$(dir $@)) || mkdir $(subst /,\,$(dir $@)) 417else 418 $(Q)test -d $(dir $@) || mkdir -p $(dir $@) 419endif 420 @echo $(MSG_COMPILING_CXX) 421 $(Q)$(CXX) $(cxx_flags) -c $< -o $@ 422 423# Preprocess and assemble: create object files from assembler source files. 424$(build-dir)%.o: %.S $(MAKEFILE_PATH) config.mk 425 $(Q)test -d $(dir $@) || echo $(MSG_MKDIR) 426ifeq ($(os),Windows) 427 $(Q)test -d $(patsubst %/,%,$(dir $@)) || mkdir $(subst /,\,$(dir $@)) 428else 429 $(Q)test -d $(dir $@) || mkdir -p $(dir $@) 430endif 431 @echo $(MSG_ASSEMBLING) 432 $(Q)$(CC) $(a_flags) -c $< -o $@ 433 434# Include all dependency files to add depedency to all header files in use. 435include $(dep-files) 436 437ifeq ($(target_type),lib) 438# Archive object files into an archive 439$(target): $(MAKEFILE_PATH) config.mk $(obj-y) 440 @echo $(MSG_ARCHIVING) 441 $(Q)$(AR) $(ar_flags) $@ $(obj-y) 442 @echo $(MSG_SIZE) 443 $(Q)$(SIZE) -Bxt $@ 444else 445ifeq ($(target_type),elf) 446# Link the object files into an ELF file. Also make sure the target is rebuilt 447# if the common Makefile.sam.in or project config.mk is changed. 448$(target): $(linker_script) $(MAKEFILE_PATH) config.mk $(obj-y) 449 @echo $(MSG_LINKING) 450 $(Q)$(LD) $(l_flags) $(obj-y) $(libflags-gnu-y) -o $@ 451 @echo $(MSG_SIZE) 452 $(Q)$(SIZE) -Ax $@ 453 $(Q)$(SIZE) -Bx $@ 454endif 455endif 456 457# Create extended function listing from target output file. 458%.lss: $(target) 459 @echo $(MSG_EXTENDED_LISTING) 460 $(Q)$(OBJDUMP) -h -S $< > $@ 461 462# Create symbol table from target output file. 463%.sym: $(target) 464 @echo $(MSG_SYMBOL_TABLE) 465 $(Q)$(NM) -n $< > $@ 466 467# Create Intel HEX image from ELF output file. 468%.hex: $(target) 469 @echo $(MSG_IHEX_IMAGE) 470 $(Q)$(OBJCOPY) -O ihex $(flashflags-gnu-y) $< $@ 471 472# Create binary image from ELF output file. 473%.bin: $(target) 474 @echo $(MSG_BINARY_IMAGE) 475 $(Q)$(OBJCOPY) -O binary $< $@ 476 477# Provide information about the detected host operating system. 478.SECONDARY: info-os 479info-os: 480 @echo $(MSG_INFO)$(os) build host detected 481 482# Build Doxygen generated documentation. 483.PHONY: doc 484doc: 485 @echo $(MSG_GENERATING_DOC) 486 $(Q)cd $(dir $(doccfg)) && $(DOCGEN) $(notdir $(doccfg)) 487 488# Clean Doxygen generated documentation. 489.PHONY: cleandoc 490cleandoc: 491 @$(if $(wildcard $(docdir)),echo $(MSG_CLEAN_DOC)) 492 $(Q)$(if $(wildcard $(docdir)),$(RM) --recursive $(docdir)) 493 494# Rebuild the Doxygen generated documentation. 495.PHONY: rebuilddoc 496rebuilddoc: cleandoc doc 497