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 53 54# Checkpatch ignores 55CHECK_IGNORE = --ignore COMPLEX_MACRO 56 57CHECKPATCH_ARGS = --no-tree --no-signoff ${CHECK_IGNORE} 58CHECKCODE_ARGS = --no-patch --no-tree --no-signoff ${CHECK_IGNORE} 59 60ifeq (${V},0) 61 Q=@ 62 CHECKCODE_ARGS += --no-summary --terse 63else 64 Q= 65endif 66export Q 67 68ifneq (${DEBUG}, 0) 69 BUILD_TYPE := debug 70else 71 BUILD_TYPE := release 72endif 73 74BL_COMMON_SOURCES := common/bl_common.c \ 75 common/debug.c \ 76 lib/aarch64/cache_helpers.S \ 77 lib/aarch64/misc_helpers.S \ 78 lib/aarch64/tlb_helpers.S \ 79 lib/aarch64/xlat_helpers.c \ 80 lib/stdlib/std.c \ 81 lib/io_storage.c \ 82 plat/common/aarch64/platform_helpers.S 83 84BUILD_BASE := ./build 85BUILD_PLAT := ${BUILD_BASE}/${PLAT}/${BUILD_TYPE} 86 87PLATFORMS := $(shell ls -I common plat/) 88SPDS := $(shell ls -I none services/spd) 89HELP_PLATFORMS := $(shell echo ${PLATFORMS} | sed 's/ /|/g') 90 91# Convenience function for adding build definitions 92# $(eval $(call add_define,FOO)) will have: 93# -DFOO if $(FOO) is empty; -DFOO=$(FOO) otherwise 94define add_define 95DEFINES += -D$(1)$(if $(value $(1)),=$(value $(1)),) 96endef 97 98# Convenience function for verifying option has a boolean value 99# $(eval $(call assert_boolean,FOO)) will assert FOO is 0 or 1 100define assert_boolean 101$(and $(patsubst 0,,$(value $(1))),$(patsubst 1,,$(value $(1))),$(error $(1) must be boolean)) 102endef 103 104ifeq (${PLAT},) 105 $(error "Error: Unknown platform. Please use PLAT=<platform name> to specify the platform.") 106endif 107ifeq ($(findstring ${PLAT},${PLATFORMS}),) 108 $(error "Error: Invalid platform. The following platforms are available: ${PLATFORMS}") 109endif 110 111all: msg_start 112 113msg_start: 114 @echo "Building ${PLAT}" 115 116include plat/${PLAT}/platform.mk 117 118ifdef BL1_SOURCES 119NEED_BL1 := yes 120include bl1/bl1.mk 121endif 122 123ifdef BL2_SOURCES 124NEED_BL2 := yes 125include bl2/bl2.mk 126endif 127 128ifdef BL31_SOURCES 129NEED_BL31 := yes 130include bl31/bl31.mk 131endif 132 133# Include SPD Makefile if one has been specified 134ifneq (${SPD},none) 135 # We expect to locate an spd.mk under the specified SPD directory 136 SPD_MAKE := $(shell m="services/spd/${SPD}/${SPD}.mk"; [ -f "$$m" ] && echo "$$m") 137 138 ifeq (${SPD_MAKE},) 139 $(error Error: No services/spd/${SPD}/${SPD}.mk located) 140 endif 141 $(info Including ${SPD_MAKE}) 142 include ${SPD_MAKE} 143 144 # If there's BL32 companion for the chosen SPD, and the SPD wants to build the 145 # BL2 from source, we expect that the SPD's Makefile would set NEED_BL32 146 # variable to "yes" 147endif 148 149.PHONY: all msg_start clean realclean distclean cscope locate-checkpatch checkcodebase checkpatch fiptool fip 150.SUFFIXES: 151 152INCLUDES += -Iinclude/bl1 \ 153 -Iinclude/bl2 \ 154 -Iinclude/bl31 \ 155 -Iinclude/bl31/services \ 156 -Iinclude/bl32 \ 157 -Iinclude/bl32/payloads \ 158 -Iinclude/common \ 159 -Iinclude/drivers \ 160 -Iinclude/drivers/arm \ 161 -Iinclude/lib \ 162 -Iinclude/lib/aarch64 \ 163 -Iinclude/stdlib \ 164 -Iinclude/stdlib/sys \ 165 -Iplat/${PLAT} \ 166 ${PLAT_INCLUDES} \ 167 ${SPD_INCLUDES} 168 169# Process DEBUG flag 170$(eval $(call assert_boolean,DEBUG)) 171$(eval $(call add_define,DEBUG)) 172ifeq (${DEBUG},0) 173 $(eval $(call add_define,NDEBUG)) 174else 175CFLAGS += -g 176ASFLAGS += -g -Wa,--gdwarf-2 177endif 178 179# Process NS_TIMER_SWITCH flag 180$(eval $(call assert_boolean,NS_TIMER_SWITCH)) 181$(eval $(call add_define,NS_TIMER_SWITCH)) 182 183# Process RESET_TO_BL31 flag 184$(eval $(call assert_boolean,RESET_TO_BL31)) 185$(eval $(call add_define,RESET_TO_BL31)) 186 187ASFLAGS += -nostdinc -ffreestanding -Wa,--fatal-warnings \ 188 -mgeneral-regs-only -D__ASSEMBLY__ \ 189 ${DEFINES} ${INCLUDES} 190CFLAGS += -nostdinc -pedantic -ffreestanding -Wall \ 191 -Werror -mgeneral-regs-only -std=c99 -c -Os \ 192 ${DEFINES} ${INCLUDES} 193CFLAGS += -ffunction-sections -fdata-sections 194 195LDFLAGS += --fatal-warnings -O1 196LDFLAGS += --gc-sections 197 198 199CC := ${CROSS_COMPILE}gcc 200CPP := ${CROSS_COMPILE}cpp 201AS := ${CROSS_COMPILE}gcc 202AR := ${CROSS_COMPILE}ar 203LD := ${CROSS_COMPILE}ld 204OC := ${CROSS_COMPILE}objcopy 205OD := ${CROSS_COMPILE}objdump 206NM := ${CROSS_COMPILE}nm 207PP := ${CROSS_COMPILE}gcc -E ${CFLAGS} 208 209# Variables for use with Firmware Image Package 210FIPTOOLPATH ?= tools/fip_create 211FIPTOOL ?= ${FIPTOOLPATH}/fip_create 212fiptool: ${FIPTOOL} 213fip: ${BUILD_PLAT}/fip.bin 214 215locate-checkpatch: 216ifndef CHECKPATCH 217 $(error "Please set CHECKPATCH to point to the Linux checkpatch.pl file, eg: CHECKPATCH=../linux/script/checkpatch.pl") 218else 219ifeq (,$(wildcard ${CHECKPATCH})) 220 $(error "The file CHECKPATCH points to cannot be found, use eg: CHECKPATCH=../linux/script/checkpatch.pl") 221endif 222endif 223 224clean: 225 @echo " CLEAN" 226 ${Q}rm -rf ${BUILD_PLAT} 227 ${Q}${MAKE} --no-print-directory -C ${FIPTOOLPATH} clean 228 229realclean distclean: 230 @echo " REALCLEAN" 231 ${Q}rm -rf ${BUILD_BASE} 232 ${Q}rm -f ${CURDIR}/cscope.* 233 ${Q}${MAKE} --no-print-directory -C ${FIPTOOLPATH} clean 234 235checkcodebase: locate-checkpatch 236 @echo " CHECKING STYLE" 237 @if test -d .git ; then \ 238 git ls-files | while read GIT_FILE ; do ${CHECKPATCH} ${CHECKCODE_ARGS} -f $$GIT_FILE ; done ; \ 239 else \ 240 find . -type f -not -iwholename "*.git*" -not -iwholename "*build*" -exec ${CHECKPATCH} ${CHECKCODE_ARGS} -f {} \; ; \ 241 fi 242 243checkpatch: locate-checkpatch 244 @echo " CHECKING STYLE" 245 @git format-patch --stdout ${BASE_COMMIT} | ${CHECKPATCH} ${CHECKPATCH_ARGS} - || true 246 247${FIPTOOL}: 248 ${Q}${MAKE} --no-print-directory -C ${FIPTOOLPATH} 249 @echo 250 @echo "Built $@ successfully" 251 @echo 252 253define match_goals 254$(strip $(foreach goal,$(1),$(filter $(goal),$(MAKECMDGOALS)))) 255endef 256 257# List of rules that involve building things 258BUILD_TARGETS := all bl1 bl2 bl31 bl32 fip 259 260# Does the list of goals specified on the command line include a build target? 261ifneq ($(call match_goals,${BUILD_TARGETS}),) 262IS_ANYTHING_TO_BUILD := 1 263endif 264 265define MAKE_C 266 267$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2)))) 268$(eval PREREQUISITES := $(patsubst %.o,%.d,$(OBJ))) 269 270$(OBJ) : $(2) 271 @echo " CC $$<" 272 $$(Q)$$(CC) $$(CFLAGS) -c $$< -o $$@ 273 274 275$(PREREQUISITES) : $(2) 276 @echo " DEPS $$@" 277 @mkdir -p $(1) 278 $$(Q)$$(CC) $$(CFLAGS) -M -MT $(OBJ) -MF $$@ $$< 279 280ifdef IS_ANYTHING_TO_BUILD 281-include $(PREREQUISITES) 282endif 283 284endef 285 286 287define MAKE_S 288 289$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2)))) 290$(eval PREREQUISITES := $(patsubst %.o,%.d,$(OBJ))) 291 292$(OBJ) : $(2) 293 @echo " AS $$<" 294 $$(Q)$$(AS) $$(ASFLAGS) -c $$< -o $$@ 295 296$(PREREQUISITES) : $(2) 297 @echo " DEPS $$@" 298 @mkdir -p $(1) 299 $$(Q)$$(AS) $$(ASFLAGS) -M -MT $(OBJ) -MF $$@ $$< 300 301ifdef IS_ANYTHING_TO_BUILD 302-include $(PREREQUISITES) 303endif 304 305endef 306 307 308define MAKE_LD 309 310$(eval PREREQUISITES := $(1).d) 311 312$(1) : $(2) 313 @echo " PP $$<" 314 $$(Q)$$(AS) $$(ASFLAGS) -P -E -o $$@ $$< 315 316$(PREREQUISITES) : $(2) 317 @echo " DEPS $$@" 318 @mkdir -p $$(dir $$@) 319 $$(Q)$$(AS) $$(ASFLAGS) -M -MT $(1) -MF $$@ $$< 320 321ifdef IS_ANYTHING_TO_BUILD 322-include $(PREREQUISITES) 323endif 324 325endef 326 327 328define MAKE_OBJS 329 $(eval C_OBJS := $(filter %.c,$(2))) 330 $(eval REMAIN := $(filter-out %.c,$(2))) 331 $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj)))) 332 333 $(eval S_OBJS := $(filter %.S,$(REMAIN))) 334 $(eval REMAIN := $(filter-out %.S,$(REMAIN))) 335 $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj)))) 336 337 $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN))) 338endef 339 340 341# NOTE: The line continuation '\' is required in the next define otherwise we 342# end up with a line-feed characer at the end of the last c filename. 343# Also bare this issue in mind if extending the list of supported filetypes. 344define SOURCES_TO_OBJS 345 $(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \ 346 $(notdir $(patsubst %.S,%.o,$(filter %.S,$(1)))) 347endef 348 349define MAKE_BL 350 $(eval BUILD_DIR := ${BUILD_PLAT}/bl$(1)) 351 $(eval SOURCES := $(BL$(1)_SOURCES) $(BL_COMMON_SOURCES) $(PLAT_BL_COMMON_SOURCES)) 352 $(eval OBJS := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES)))) 353 $(eval LINKERFILE := $(BUILD_DIR)/bl$(1).ld) 354 $(eval MAPFILE := $(BUILD_DIR)/bl$(1).map) 355 $(eval ELF := $(BUILD_DIR)/bl$(1).elf) 356 $(eval DUMP := $(BUILD_DIR)/bl$(1).dump) 357 $(eval BIN := $(BUILD_PLAT)/bl$(1).bin) 358 359 $(eval $(call MAKE_OBJS,$(BUILD_DIR),$(SOURCES))) 360 $(eval $(call MAKE_LD,$(LINKERFILE),$(BL$(1)_LINKERFILE))) 361 362$(BUILD_DIR) : 363 $$(Q)mkdir -p "$$@" 364 365$(ELF) : $(OBJS) $(LINKERFILE) 366 @echo " LD $$@" 367 @echo 'const char build_message[] = "Built : "__TIME__", "__DATE__;' | \ 368 $$(CC) $$(CFLAGS) -xc - -o $(BUILD_DIR)/build_message.o 369 $$(Q)$$(LD) -o $$@ $$(LDFLAGS) -Map=$(MAPFILE) --script $(LINKERFILE) \ 370 $(BUILD_DIR)/build_message.o $(OBJS) 371 372$(DUMP) : $(ELF) 373 @echo " OD $$@" 374 $${Q}$${OD} -dx $$< > $$@ 375 376$(BIN) : $(ELF) 377 @echo " BIN $$@" 378 $$(Q)$$(OC) -O binary $$< $$@ 379 @echo 380 @echo "Built $$@ successfully" 381 @echo 382 383.PHONY : bl$(1) 384bl$(1) : $(BUILD_DIR) $(BIN) $(DUMP) 385 386all : bl$(1) 387 388$(eval FIP_DEPS += $(if $2,$(BIN),)) 389$(eval FIP_ARGS += $(if $2,--bl$(1) $(BIN),)) 390 391endef 392 393 394ifeq (${NEED_BL1},yes) 395$(eval $(call MAKE_BL,1)) 396endif 397 398ifeq (${NEED_BL2},yes) 399$(eval $(call MAKE_BL,2,in_fip)) 400endif 401 402ifeq (${NEED_BL31},yes) 403BL31_SOURCES += ${SPD_SOURCES} 404$(eval $(call MAKE_BL,31,in_fip)) 405endif 406 407ifeq (${NEED_BL32},yes) 408$(eval $(call MAKE_BL,32,in_fip)) 409endif 410 411${BUILD_PLAT}/fip.bin: ${FIP_DEPS} ${BL33} ${FIPTOOL} 412 $(if ${BL33},,$(error "To build a FIP, please set BL33 to point to the Normal World binary, eg: BL33=../uefi/FVP_AARCH64_EFI.fd")) 413 ${Q}${FIPTOOL} --dump \ 414 ${FIP_ARGS} \ 415 --bl33 ${BL33} \ 416 $@ 417 @echo 418 @echo "Built $@ successfully" 419 @echo 420 421 422cscope: 423 @echo " CSCOPE" 424 ${Q}find ${CURDIR} -name "*.[chsS]" > cscope.files 425 ${Q}cscope -b -q -k 426 427help: 428 @echo "usage: ${MAKE} PLAT=<${HELP_PLATFORMS}> <all|bl1|bl2|bl31|distclean|clean|checkcodebase|checkpatch>" 429 @echo "" 430 @echo "PLAT is used to specify which platform you wish to build." 431 @echo "If no platform is specified, PLAT defaults to: ${DEFAULT_PLAT}" 432 @echo "" 433 @echo "Supported Targets:" 434 @echo " all Build the BL1, BL2 and BL31 binaries" 435 @echo " bl1 Build the BL1 binary" 436 @echo " bl2 Build the BL2 binary" 437 @echo " bl31 Build the BL31 binary" 438 @echo " checkcodebase Check the coding style of the entire source tree" 439 @echo " checkpatch Check the coding style on changes in the current" 440 @echo " branch against BASE_COMMIT (default origin/master)" 441 @echo " clean Clean the build for the selected platform" 442 @echo " cscope Generate cscope index" 443 @echo " distclean Remove all build artifacts for all platforms" 444 @echo " fiptool Build the Firmware Image Package(FIP) creation tool" 445 @echo "" 446 @echo "note: most build targets require PLAT to be set to a specific platform." 447 @echo "" 448 @echo "example: build all targets for the FVP platform:" 449 @echo " CROSS_COMPILE=aarch64-none-elf- make PLAT=fvp all" 450