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