1# 2# Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions are met: 6# 7# Redistributions of source code must retain the above copyright notice, this 8# list of conditions and the following disclaimer. 9# 10# Redistributions in binary form must reproduce the above copyright notice, 11# this list of conditions and the following disclaimer in the documentation 12# and/or other materials provided with the distribution. 13# 14# Neither the name of ARM nor the names of its contributors may be used 15# to endorse or promote products derived from this software without specific 16# prior written permission. 17# 18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28# POSSIBILITY OF SUCH DAMAGE. 29# 30 31# 32# Default values for build configurations 33# 34 35# Build verbosity 36V := 0 37# Debug build 38DEBUG := 0 39# Build architecture 40ARCH := aarch64 41# Build platform 42DEFAULT_PLAT := fvp 43PLAT := ${DEFAULT_PLAT} 44# SPD choice 45SPD := none 46# Base commit to perform code check on 47BASE_COMMIT := origin/master 48# NS timer register save and restore 49NS_TIMER_SWITCH := 0 50# By default, Bl1 acts as the reset handler, not BL31 51RESET_TO_BL31 := 0 52# Include FP registers in cpu context 53CTX_INCLUDE_FPREGS := 0 54# Determine the version of ARM GIC architecture to use for interrupt management 55# in EL3. The platform port can change this value if needed. 56ARM_GIC_ARCH := 2 57 58 59# Checkpatch ignores 60CHECK_IGNORE = --ignore COMPLEX_MACRO 61 62CHECKPATCH_ARGS = --no-tree --no-signoff ${CHECK_IGNORE} 63CHECKCODE_ARGS = --no-patch --no-tree --no-signoff ${CHECK_IGNORE} 64 65ifeq (${V},0) 66 Q=@ 67 CHECKCODE_ARGS += --no-summary --terse 68else 69 Q= 70endif 71export Q 72 73ifneq (${DEBUG}, 0) 74 BUILD_TYPE := debug 75else 76 BUILD_TYPE := release 77endif 78 79BL_COMMON_SOURCES := common/bl_common.c \ 80 common/debug.c \ 81 common/tf_printf.c \ 82 lib/aarch64/cache_helpers.S \ 83 lib/aarch64/misc_helpers.S \ 84 lib/aarch64/xlat_helpers.c \ 85 lib/stdlib/std.c \ 86 lib/io_storage.c \ 87 plat/common/aarch64/platform_helpers.S 88 89BUILD_BASE := ./build 90BUILD_PLAT := ${BUILD_BASE}/${PLAT}/${BUILD_TYPE} 91 92PLATFORMS := $(shell ls -I common plat/) 93SPDS := $(shell ls -I none services/spd) 94HELP_PLATFORMS := $(shell echo ${PLATFORMS} | sed 's/ /|/g') 95 96# Convenience function for adding build definitions 97# $(eval $(call add_define,FOO)) will have: 98# -DFOO if $(FOO) is empty; -DFOO=$(FOO) otherwise 99define add_define 100DEFINES += -D$(1)$(if $(value $(1)),=$(value $(1)),) 101endef 102 103# Convenience function for verifying option has a boolean value 104# $(eval $(call assert_boolean,FOO)) will assert FOO is 0 or 1 105define assert_boolean 106$(and $(patsubst 0,,$(value $(1))),$(patsubst 1,,$(value $(1))),$(error $(1) must be boolean)) 107endef 108 109ifeq (${PLAT},) 110 $(error "Error: Unknown platform. Please use PLAT=<platform name> to specify the platform.") 111endif 112ifeq ($(findstring ${PLAT},${PLATFORMS}),) 113 $(error "Error: Invalid platform. The following platforms are available: ${PLATFORMS}") 114endif 115 116all: msg_start 117 118msg_start: 119 @echo "Building ${PLAT}" 120 121include plat/${PLAT}/platform.mk 122 123ifdef BL1_SOURCES 124NEED_BL1 := yes 125include bl1/bl1.mk 126endif 127 128ifdef BL2_SOURCES 129NEED_BL2 := yes 130include bl2/bl2.mk 131endif 132 133ifdef BL31_SOURCES 134NEED_BL31 := yes 135include bl31/bl31.mk 136endif 137 138# Include SPD Makefile if one has been specified 139ifneq (${SPD},none) 140 # We expect to locate an spd.mk under the specified SPD directory 141 SPD_MAKE := $(shell m="services/spd/${SPD}/${SPD}.mk"; [ -f "$$m" ] && echo "$$m") 142 143 ifeq (${SPD_MAKE},) 144 $(error Error: No services/spd/${SPD}/${SPD}.mk located) 145 endif 146 $(info Including ${SPD_MAKE}) 147 include ${SPD_MAKE} 148 149 # If there's BL32 companion for the chosen SPD, and the SPD wants to build the 150 # BL2 from source, we expect that the SPD's Makefile would set NEED_BL32 151 # variable to "yes" 152endif 153 154.PHONY: all msg_start clean realclean distclean cscope locate-checkpatch checkcodebase checkpatch fiptool fip 155.SUFFIXES: 156 157INCLUDES += -Iinclude/bl31 \ 158 -Iinclude/bl31/services \ 159 -Iinclude/bl32 \ 160 -Iinclude/bl32/payloads \ 161 -Iinclude/common \ 162 -Iinclude/drivers \ 163 -Iinclude/drivers/arm \ 164 -Iinclude/lib \ 165 -Iinclude/lib/aarch64 \ 166 -Iinclude/plat/common \ 167 -Iinclude/stdlib \ 168 -Iinclude/stdlib/sys \ 169 ${PLAT_INCLUDES} \ 170 ${SPD_INCLUDES} 171 172# Process DEBUG flag 173$(eval $(call assert_boolean,DEBUG)) 174$(eval $(call add_define,DEBUG)) 175ifeq (${DEBUG},0) 176 $(eval $(call add_define,NDEBUG)) 177else 178CFLAGS += -g 179ASFLAGS += -g -Wa,--gdwarf-2 180endif 181 182# Process NS_TIMER_SWITCH flag 183$(eval $(call assert_boolean,NS_TIMER_SWITCH)) 184$(eval $(call add_define,NS_TIMER_SWITCH)) 185 186# Process RESET_TO_BL31 flag 187$(eval $(call assert_boolean,RESET_TO_BL31)) 188$(eval $(call add_define,RESET_TO_BL31)) 189 190# Process CTX_INCLUDE_FPREGS flag 191$(eval $(call assert_boolean,CTX_INCLUDE_FPREGS)) 192$(eval $(call add_define,CTX_INCLUDE_FPREGS)) 193 194# Process ARM_GIC_ARCH flag 195$(eval $(call add_define,ARM_GIC_ARCH)) 196 197 198ASFLAGS += -nostdinc -ffreestanding -Wa,--fatal-warnings \ 199 -Werror -Wmissing-include-dirs \ 200 -mgeneral-regs-only -D__ASSEMBLY__ \ 201 ${DEFINES} ${INCLUDES} 202CFLAGS += -nostdinc -pedantic -ffreestanding -Wall \ 203 -Werror -Wmissing-include-dirs \ 204 -mgeneral-regs-only -std=c99 -c -Os \ 205 ${DEFINES} ${INCLUDES} 206CFLAGS += -ffunction-sections -fdata-sections 207 208LDFLAGS += --fatal-warnings -O1 209LDFLAGS += --gc-sections 210 211 212CC := ${CROSS_COMPILE}gcc 213CPP := ${CROSS_COMPILE}cpp 214AS := ${CROSS_COMPILE}gcc 215AR := ${CROSS_COMPILE}ar 216LD := ${CROSS_COMPILE}ld 217OC := ${CROSS_COMPILE}objcopy 218OD := ${CROSS_COMPILE}objdump 219NM := ${CROSS_COMPILE}nm 220PP := ${CROSS_COMPILE}gcc -E ${CFLAGS} 221 222# Variables for use with Firmware Image Package 223FIPTOOLPATH ?= tools/fip_create 224FIPTOOL ?= ${FIPTOOLPATH}/fip_create 225fiptool: ${FIPTOOL} 226fip: ${BUILD_PLAT}/fip.bin 227 228locate-checkpatch: 229ifndef CHECKPATCH 230 $(error "Please set CHECKPATCH to point to the Linux checkpatch.pl file, eg: CHECKPATCH=../linux/script/checkpatch.pl") 231else 232ifeq (,$(wildcard ${CHECKPATCH})) 233 $(error "The file CHECKPATCH points to cannot be found, use eg: CHECKPATCH=../linux/script/checkpatch.pl") 234endif 235endif 236 237clean: 238 @echo " CLEAN" 239 ${Q}rm -rf ${BUILD_PLAT} 240 ${Q}${MAKE} --no-print-directory -C ${FIPTOOLPATH} clean 241 242realclean distclean: 243 @echo " REALCLEAN" 244 ${Q}rm -rf ${BUILD_BASE} 245 ${Q}rm -f ${CURDIR}/cscope.* 246 ${Q}${MAKE} --no-print-directory -C ${FIPTOOLPATH} clean 247 248checkcodebase: locate-checkpatch 249 @echo " CHECKING STYLE" 250 @if test -d .git ; then \ 251 git ls-files | grep -v stdlib | while read GIT_FILE ; do ${CHECKPATCH} ${CHECKCODE_ARGS} -f $$GIT_FILE ; done ; \ 252 else \ 253 find . -type f -not -iwholename "*.git*" -not -iwholename "*build*" -not -iwholename "*stdlib*" -exec ${CHECKPATCH} ${CHECKCODE_ARGS} -f {} \; ; \ 254 fi 255 256checkpatch: locate-checkpatch 257 @echo " CHECKING STYLE" 258 @git format-patch --stdout ${BASE_COMMIT} | ${CHECKPATCH} ${CHECKPATCH_ARGS} - || true 259 260${FIPTOOL}: 261 ${Q}${MAKE} --no-print-directory -C ${FIPTOOLPATH} 262 @echo 263 @echo "Built $@ successfully" 264 @echo 265 266define match_goals 267$(strip $(foreach goal,$(1),$(filter $(goal),$(MAKECMDGOALS)))) 268endef 269 270# List of rules that involve building things 271BUILD_TARGETS := all bl1 bl2 bl31 bl32 fip 272 273# Does the list of goals specified on the command line include a build target? 274ifneq ($(call match_goals,${BUILD_TARGETS}),) 275IS_ANYTHING_TO_BUILD := 1 276endif 277 278define MAKE_C 279 280$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2)))) 281$(eval PREREQUISITES := $(patsubst %.o,%.d,$(OBJ))) 282 283$(OBJ) : $(2) 284 @echo " CC $$<" 285 $$(Q)$$(CC) $$(CFLAGS) -c $$< -o $$@ 286 287 288$(PREREQUISITES) : $(2) 289 @echo " DEPS $$@" 290 @mkdir -p $(1) 291 $$(Q)$$(CC) $$(CFLAGS) -M -MT $(OBJ) -MF $$@ $$< 292 293ifdef IS_ANYTHING_TO_BUILD 294-include $(PREREQUISITES) 295endif 296 297endef 298 299 300define MAKE_S 301 302$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2)))) 303$(eval PREREQUISITES := $(patsubst %.o,%.d,$(OBJ))) 304 305$(OBJ) : $(2) 306 @echo " AS $$<" 307 $$(Q)$$(AS) $$(ASFLAGS) -c $$< -o $$@ 308 309$(PREREQUISITES) : $(2) 310 @echo " DEPS $$@" 311 @mkdir -p $(1) 312 $$(Q)$$(AS) $$(ASFLAGS) -M -MT $(OBJ) -MF $$@ $$< 313 314ifdef IS_ANYTHING_TO_BUILD 315-include $(PREREQUISITES) 316endif 317 318endef 319 320 321define MAKE_LD 322 323$(eval PREREQUISITES := $(1).d) 324 325$(1) : $(2) 326 @echo " PP $$<" 327 $$(Q)$$(AS) $$(ASFLAGS) -P -E -o $$@ $$< 328 329$(PREREQUISITES) : $(2) 330 @echo " DEPS $$@" 331 @mkdir -p $$(dir $$@) 332 $$(Q)$$(AS) $$(ASFLAGS) -M -MT $(1) -MF $$@ $$< 333 334ifdef IS_ANYTHING_TO_BUILD 335-include $(PREREQUISITES) 336endif 337 338endef 339 340 341define MAKE_OBJS 342 $(eval C_OBJS := $(filter %.c,$(2))) 343 $(eval REMAIN := $(filter-out %.c,$(2))) 344 $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj)))) 345 346 $(eval S_OBJS := $(filter %.S,$(REMAIN))) 347 $(eval REMAIN := $(filter-out %.S,$(REMAIN))) 348 $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj)))) 349 350 $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN))) 351endef 352 353 354# NOTE: The line continuation '\' is required in the next define otherwise we 355# end up with a line-feed characer at the end of the last c filename. 356# Also bare this issue in mind if extending the list of supported filetypes. 357define SOURCES_TO_OBJS 358 $(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \ 359 $(notdir $(patsubst %.S,%.o,$(filter %.S,$(1)))) 360endef 361 362define MAKE_BL 363 $(eval BUILD_DIR := ${BUILD_PLAT}/bl$(1)) 364 $(eval SOURCES := $(BL$(1)_SOURCES) $(BL_COMMON_SOURCES) $(PLAT_BL_COMMON_SOURCES)) 365 $(eval OBJS := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES)))) 366 $(eval LINKERFILE := $(BUILD_DIR)/bl$(1).ld) 367 $(eval MAPFILE := $(BUILD_DIR)/bl$(1).map) 368 $(eval ELF := $(BUILD_DIR)/bl$(1).elf) 369 $(eval DUMP := $(BUILD_DIR)/bl$(1).dump) 370 $(eval BIN := $(BUILD_PLAT)/bl$(1).bin) 371 372 $(eval $(call MAKE_OBJS,$(BUILD_DIR),$(SOURCES))) 373 $(eval $(call MAKE_LD,$(LINKERFILE),$(BL$(1)_LINKERFILE))) 374 375$(BUILD_DIR) : 376 $$(Q)mkdir -p "$$@" 377 378$(ELF) : $(OBJS) $(LINKERFILE) 379 @echo " LD $$@" 380 @echo 'const char build_message[] = "Built : "__TIME__", "__DATE__;' | \ 381 $$(CC) $$(CFLAGS) -xc - -o $(BUILD_DIR)/build_message.o 382 $$(Q)$$(LD) -o $$@ $$(LDFLAGS) -Map=$(MAPFILE) --script $(LINKERFILE) \ 383 $(BUILD_DIR)/build_message.o $(OBJS) 384 385$(DUMP) : $(ELF) 386 @echo " OD $$@" 387 $${Q}$${OD} -dx $$< > $$@ 388 389$(BIN) : $(ELF) 390 @echo " BIN $$@" 391 $$(Q)$$(OC) -O binary $$< $$@ 392 @echo 393 @echo "Built $$@ successfully" 394 @echo 395 396.PHONY : bl$(1) 397bl$(1) : $(BUILD_DIR) $(BIN) $(DUMP) 398 399all : bl$(1) 400 401$(eval FIP_DEPS += $(if $2,$(BIN),)) 402$(eval FIP_ARGS += $(if $2,--bl$(1) $(BIN),)) 403 404endef 405 406 407ifeq (${NEED_BL1},yes) 408$(eval $(call MAKE_BL,1)) 409endif 410 411ifeq (${NEED_BL2},yes) 412$(eval $(call MAKE_BL,2,in_fip)) 413endif 414 415ifeq (${NEED_BL31},yes) 416BL31_SOURCES += ${SPD_SOURCES} 417$(eval $(call MAKE_BL,31,in_fip)) 418endif 419 420ifeq (${NEED_BL32},yes) 421$(eval $(call MAKE_BL,32,in_fip)) 422endif 423 424ifeq (${NEED_BL30},yes) 425FIP_DEPS += ${BL30} 426FIP_ARGS += --bl30 ${BL30} 427endif 428 429ifeq (${NEED_BL30},yes) 430# If BL3-0 is needed by the platform then 'BL30' variable must be defined. 431check_bl30: 432 $(if ${BL30},,$(error "To build a FIP for platform ${PLAT}, please set BL30 to point to the SCP firmware")) 433else 434# If BL3-0 is not needed by the platform but the user still specified the path 435# to a BL3-0 image then warn him that it will be ignored. 436check_bl30: 437 $(if ${BL30},$(warning "BL3-0 is not supported on platform ${PLAT}, it will just be ignored"),) 438endif 439 440${BUILD_PLAT}/fip.bin: ${FIP_DEPS} ${BL33} ${FIPTOOL} check_bl30 441 $(if ${BL33},,$(error "To build a FIP, please set BL33 to point to the Normal World binary, eg: BL33=../uefi/FVP_AARCH64_EFI.fd")) 442 ${Q}${FIPTOOL} --dump \ 443 ${FIP_ARGS} \ 444 --bl33 ${BL33} \ 445 $@ 446 @echo 447 @echo "Built $@ successfully" 448 @echo 449 450 451cscope: 452 @echo " CSCOPE" 453 ${Q}find ${CURDIR} -name "*.[chsS]" > cscope.files 454 ${Q}cscope -b -q -k 455 456help: 457 @echo "usage: ${MAKE} PLAT=<${HELP_PLATFORMS}> <all|bl1|bl2|bl31|distclean|clean|checkcodebase|checkpatch>" 458 @echo "" 459 @echo "PLAT is used to specify which platform you wish to build." 460 @echo "If no platform is specified, PLAT defaults to: ${DEFAULT_PLAT}" 461 @echo "" 462 @echo "Supported Targets:" 463 @echo " all Build the BL1, BL2 and BL31 binaries" 464 @echo " bl1 Build the BL1 binary" 465 @echo " bl2 Build the BL2 binary" 466 @echo " bl31 Build the BL31 binary" 467 @echo " checkcodebase Check the coding style of the entire source tree" 468 @echo " checkpatch Check the coding style on changes in the current" 469 @echo " branch against BASE_COMMIT (default origin/master)" 470 @echo " clean Clean the build for the selected platform" 471 @echo " cscope Generate cscope index" 472 @echo " distclean Remove all build artifacts for all platforms" 473 @echo " fiptool Build the Firmware Image Package(FIP) creation tool" 474 @echo "" 475 @echo "note: most build targets require PLAT to be set to a specific platform." 476 @echo "" 477 @echo "example: build all targets for the FVP platform:" 478 @echo " CROSS_COMPILE=aarch64-none-elf- make PLAT=fvp all" 479