1# 2# Copyright (c) 2015-2026, Arm Limited and Contributors. All rights reserved. 3# 4# SPDX-License-Identifier: BSD-3-Clause 5# 6 7# Report an error if the eval make function is not available. 8$(eval eval_available := T) 9ifneq (${eval_available},T) 10 $(error This makefile only works with a Make program that supports $$(eval)) 11endif 12 13# A user defined function to recursively search for a filename below a directory 14# $1 is the directory root of the recursive search (blank for current directory). 15# $2 is the file name to search for. 16define rwildcard 17$(strip $(foreach d,$(wildcard ${1}*),$(call rwildcard,${d}/,${2}) $(filter $(subst *,%,%${2}),${d}))) 18endef 19 20# Convenience function for setting a variable to 0 if not previously set 21# $(eval $(call default_zero,FOO)) 22define default_zero 23 $(eval $(1) ?= 0) 24endef 25 26# Convenience function for setting a list of variables to 0 if not previously set 27# $(eval $(call default_zeros,FOO BAR)) 28define default_zeros 29 $(foreach var,$1,$(eval $(call default_zero,$(var)))) 30endef 31 32# Convenience function for setting a variable to 1 if not previously set 33# $(eval $(call default_one,FOO)) 34define default_one 35 $(eval $(1) ?= 1) 36endef 37 38# Convenience function for setting a list of variables to 1 if not previously set 39# $(eval $(call default_ones,FOO BAR)) 40define default_ones 41 $(foreach var,$1,$(eval $(call default_one,$(var)))) 42endef 43 44# Convenience function for setting CRYPTO_SUPPORT per component based on build flags 45# and set MBEDTLS_LIB based on CRYPTO_SUPPORT 46# $(eval $(call set_crypto_support,NEED_AUTH,NEED_HASH)) 47# $(1) = NEED_AUTH, determines need for authentication verification support 48# $(2) = NEED_HASH, determines need for hash calculation support 49# CRYPTO_SUPPORT is set to 0 (default), 1 (authentication only), 2 (hash only), or 50# 3 (both) based on what support is required. 51define set_crypto_support 52 ifeq ($($1)-$($2),1-1) 53 CRYPTO_SUPPORT := 3 54 else ifeq ($($2),1) 55 CRYPTO_SUPPORT := 2 56 else ifeq ($($1),1) 57 CRYPTO_SUPPORT := 1 58 else 59 CRYPTO_SUPPORT := 0 60 endif 61 MBEDTLS_LIB ?= $(BUILD_PLAT)/lib/libmbedtls.a 62 CRYPTO_LIB := $(if $(filter-out 0,$(CRYPTO_SUPPORT)),$(MBEDTLS_LIB),) 63endef 64 65# Convenience function for creating a build definition 66# $(call make_define,FOO) will have: 67# -DFOO if $(FOO) is empty; -DFOO=$(FOO) otherwise 68make_define = -D$(1)$(if $($(1)),=$($(1))) 69 70# Convenience function for creating multiple build definitions 71# For BL1, BL1_CPPFLAGS += $(call make_defines,FOO BOO) 72make_defines = $(foreach def,$(1),$(call make_define,$(def))) 73 74# Convenience function for adding build definitions 75# $(eval $(call add_define,FOO)) will have: 76# -DFOO if $(FOO) is empty; -DFOO=$(FOO) otherwise 77define add_define 78 DEFINES += -D$(1)$(if $(value $(1)),=$(value $(1)),) 79endef 80 81# Convenience function for addding multiple build definitions 82# $(eval $(call add_defines,FOO BOO)) 83define add_defines 84 $(foreach def,$1,$(eval $(call add_define,$(def)))) 85endef 86 87# Convenience function for adding build definitions 88# $(eval $(call add_define_val,FOO,BAR)) will have: 89# -DFOO=BAR 90define add_define_val 91 DEFINES += -D$(1)=$(2) 92endef 93 94# Convenience function for verifying option has a boolean value 95# $(eval $(call assert_boolean,FOO)) will assert FOO is 0 or 1 96define assert_boolean 97 $(if $($(1)),,$(error $(1) must not be empty)) 98 $(if $(filter-out 0 1,$($1)),$(error $1 must be boolean)) 99endef 100 101# Convenience function for verifying options have boolean values 102# $(eval $(call assert_booleans,FOO BOO)) will assert FOO and BOO for 0 or 1 values 103define assert_booleans 104 $(foreach bool,$1,$(eval $(call assert_boolean,$(bool)))) 105endef 106 1070-9 := 0 1 2 3 4 5 6 7 8 9 108 109# Function to verify that a given option $(1) contains a numeric value 110define assert_numeric 111$(if $($(1)),,$(error $(1) must not be empty)) 112$(eval __numeric := $($(1))) 113$(foreach d,$(0-9),$(eval __numeric := $(subst $(d),,$(__numeric)))) 114$(if $(__numeric),$(error $(1) must be numeric)) 115endef 116 117# Convenience function for verifying options have numeric values 118# $(eval $(call assert_numerics,FOO BOO)) will assert FOO and BOO contain numeric values 119define assert_numerics 120 $(foreach num,$1,$(eval $(call assert_numeric,$(num)))) 121endef 122 123# Convenience function to check for a given linker option. A call to 124# $(call ld_option, --no-XYZ) will return --no-XYZ if supported by the linker, 125# prefixed appropriately for the linker in use 126ld_option = $(call toolchain-ld-option,$(ARCH),$(1)) 127 128# Convenience function to add a prefix to a linker flag if necessary. Useful 129# when the flag is known to be supported and it just needs to be prefixed 130# correctly. 131ld_prefix = $(toolchain-ld-prefix-$($(ARCH)-ld-id)) 132 133# Convenience function to check for a given compiler option. A call to 134# $(call cc_option, --no-XYZ) will return --no-XYZ if supported by the compiler 135# NOTE: consider assigning to an immediately expanded temporary variable before 136# assigning. This is because variables like TF_CFLAGS are recursively expanded 137# and assigning this directly will cause it to be expanded every time the 138# variable is used, potentially thrashing multicore performance. 139define cc_option 140 $(shell if $($(ARCH)-cc) $(1) -c -x c /dev/null -o /dev/null >/dev/null 2>&1; then echo $(1); fi ) 141endef 142 143# CREATE_SEQ is a recursive function to create sequence of numbers from 1 to 144# $(2) and assign the sequence to $(1) 145define CREATE_SEQ 146$(if $(word $(2), $($(1))),\ 147 $(eval $(1) += $(words $($(1))))\ 148 $(eval $(1) := $(filter-out 0,$($(1)))),\ 149 $(eval $(1) += $(words $($(1))))\ 150 $(call CREATE_SEQ,$(1),$(2))\ 151) 152endef 153 154# IMG_MAPFILE defines the output file describing the memory map corresponding 155# to a BL stage 156# $(1) = BL stage 157define IMG_MAPFILE 158 ${BUILD_DIR}/$(1).map 159endef 160 161# IMG_ELF defines the elf file corresponding to a BL stage 162# $(1) = BL stage 163define IMG_ELF 164 ${BUILD_DIR}/$(1).elf 165endef 166 167# IMG_DUMP defines the symbols dump file corresponding to a BL stage 168# $(1) = BL stage 169define IMG_DUMP 170 ${BUILD_DIR}/$(1).dump 171endef 172 173# IMG_BIN defines the default image file corresponding to a BL stage 174# $(1) = BL stage 175define IMG_BIN 176 ${BUILD_PLAT}/$(1).bin 177endef 178 179# IMG_ENC_BIN defines the default encrypted image file corresponding to a 180# BL stage 181# $(1) = BL stage 182define IMG_ENC_BIN 183 ${BUILD_PLAT}/$(1)_enc.bin 184endef 185 186# ENCRYPT_FW invokes enctool to encrypt firmware binary 187# $(1) = input firmware binary 188# $(2) = output encrypted firmware binary 189define ENCRYPT_FW 190$(2): $(1) enctool 191 $$(s)echo " ENC $$<" 192 $$(q)$$(ENCTOOL) $$(ENC_ARGS) -i $$< -o $$@ 193endef 194 195# TOOL_ADD_PAYLOAD appends the command line arguments required by fiptool to 196# package a new payload and/or by cert_create to generate certificate. 197# Optionally, it adds the dependency on this payload 198# $(1) = payload filename (i.e. bl31.bin) 199# $(2) = command line option for the specified payload (i.e. --soc-fw) 200# $(3) = tool target dependency (optional) (ex. build/fvp/release/bl31.bin) 201# $(4) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip) 202# $(5) = encrypted payload (optional) (ex. build/fvp/release/bl31_enc.bin) 203define TOOL_ADD_PAYLOAD 204ifneq ($(5),) 205 $(4)FIP_ARGS += $(2) $(5) 206 $(if $(3),$(4)CRT_DEPS += $(1)) 207else 208 $(4)FIP_ARGS += $(2) $(1) 209 $(if $(3),$(4)CRT_DEPS += $(3)) 210endif 211 $(if $(3),$(4)FIP_DEPS += $(3)) 212 $(4)CRT_ARGS += $(2) $(1) 213endef 214 215# TOOL_ADD_IMG_PAYLOAD works like TOOL_ADD_PAYLOAD, but applies image filters 216# before passing them to host tools if BL*_PRE_TOOL_FILTER is defined. 217# $(1) = image_type (scp_bl2, bl33, etc.) 218# $(2) = payload filepath (ex. build/fvp/release/bl31.bin) 219# $(3) = command line option for the specified payload (ex. --soc-fw) 220# $(4) = tool target dependency (optional) (ex. build/fvp/release/bl31.bin) 221# $(5) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip) 222# $(6) = encrypted payload (optional) (ex. build/fvp/release/bl31_enc.bin) 223 224define TOOL_ADD_IMG_PAYLOAD 225 226$(eval PRE_TOOL_FILTER := $($(1)_PRE_TOOL_FILTER)) 227 228ifneq ($(PRE_TOOL_FILTER),) 229 230$(eval PROCESSED_PATH := $(BUILD_PLAT)/$(1).bin$($(PRE_TOOL_FILTER)_SUFFIX)) 231 232$(call $(PRE_TOOL_FILTER)_RULE,$(PROCESSED_PATH),$(2)) 233 234$(PROCESSED_PATH): $(4) 235 236$(call TOOL_ADD_PAYLOAD,$(PROCESSED_PATH),$(3),$(PROCESSED_PATH),$(5),$(6)) 237 238else 239$(call TOOL_ADD_PAYLOAD,$(2),$(3),$(4),$(5),$(6)) 240endif 241endef 242 243# CERT_ADD_CMD_OPT adds a new command line option to the cert_create invocation 244# $(1) = parameter filename 245# $(2) = cert_create command line option for the specified parameter 246# $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip) 247define CERT_ADD_CMD_OPT 248 $(3)CRT_ARGS += $(2) $(1) 249endef 250 251# TOOL_ADD_IMG allows the platform to specify an external image to be packed 252# in the FIP and/or for which certificate is generated. It also adds a 253# dependency on the image file, aborting the build if the file does not exist. 254# $(1) = image_type (scp_bl2, bl33, etc.) 255# $(2) = command line option for fiptool (--scp-fw, --nt-fw, etc) 256# $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip) 257# $(4) = Image encryption flag (optional) (0, 1) 258# Example: 259# $(eval $(call TOOL_ADD_IMG,bl33,--nt-fw)) 260define TOOL_ADD_IMG 261 # Build option to specify the image filename (SCP_BL2, BL33, etc) 262 # This is the uppercase form of the first parameter 263 $(eval BL := $(call uppercase,$(1))) 264 $(eval _V := $(BL)) 265 266 # $(check_$(1)_cmd) variable is executed in the check_$(1) target and also 267 # is put into the ${CHECK_$(3)FIP_CMD} variable which is executed by the 268 # target ${BUILD_PLAT}/${$(3)FIP_NAME}. 269 $(eval check_$(1)_cmd := \ 270 $(if $(value $(_V)),,$$$$(error "Platform '${PLAT}' requires $(_V). Please set $(_V) to point to the right file")) \ 271 $(if $(wildcard $(value $(_V))),,$$$$(error '$(_V)=$(value $(_V))' was specified, but '$(value $(_V))' does not exist)) \ 272 ) 273 274 $(3)CRT_DEPS += check_$(1) 275 CHECK_$(3)FIP_CMD += $$(check_$(1)_cmd) 276ifeq ($(4),1) 277 $(eval ENC_BIN := ${BUILD_PLAT}/$(1)_enc.bin) 278 $(call ENCRYPT_FW,$(value $(_V)),$(ENC_BIN)) 279 $(call TOOL_ADD_IMG_PAYLOAD,$(BL),$(value $(_V)),$(2),$(ENC_BIN),$(3), \ 280 $(ENC_BIN)) 281else 282 $(call TOOL_ADD_IMG_PAYLOAD,$(BL),$(value $(_V)),$(2),$(if $(wildcard $(value $(_V))),$(value $(_V)),FORCE),$(3)) 283endif 284 285.PHONY: check_$(1) 286check_$(1): 287 $(check_$(1)_cmd) 288endef 289 290# SELECT_OPENSSL_API_VERSION selects the OpenSSL API version to be used to 291# build the host tools by checking the version of OpenSSL located under 292# the path defined by the OPENSSL_DIR variable. It receives no parameters. 293define SELECT_OPENSSL_API_VERSION 294 # Set default value for USING_OPENSSL3 macro to 0 295 $(eval USING_OPENSSL3 = 0) 296 # Obtain the OpenSSL version for the build located under OPENSSL_DIR 297 $(eval OPENSSL_INFO := $(shell LD_LIBRARY_PATH=${OPENSSL_DIR}:${OPENSSL_DIR}/lib ${OPENSSL_BIN_PATH}/openssl version)) 298 $(eval OPENSSL_CURRENT_VER = $(word 2, ${OPENSSL_INFO})) 299 $(eval OPENSSL_CURRENT_VER_MAJOR = $(firstword $(subst ., ,$(OPENSSL_CURRENT_VER)))) 300 # If OpenSSL version is 3.x, then set USING_OPENSSL3 flag to 1 301 $(if $(filter 3,$(OPENSSL_CURRENT_VER_MAJOR)), $(eval USING_OPENSSL3 = 1)) 302endef 303 304################################################################################ 305# Generic image processing filters 306################################################################################ 307 308# GZIP 309define GZIP_RULE 310$(1): $(2) 311 $(s)echo " GZIP $$@" 312 $(q)gzip -n -f -9 $$< --stdout > $$@ 313endef 314 315GZIP_SUFFIX := .gz 316 317################################################################################ 318# Auxiliary macros to build TF images from sources 319################################################################################ 320 321MAKE_DEP = -Wp,-MD,$1 -MT $2 -MP 322 323# MAKE_TOOL_C builds a C source file and generates the dependency file 324# $(1) = output directory 325# $(2) = source file (%.c) 326# $(3) = lowercase name of the tool 327# $(4) = uppercase name of the tool 328define MAKE_TOOL_C 329 330$(eval SRC := $(2)) 331$(eval OBJ := $(patsubst %.c,$(1)/$(3)/%.o,$(SRC))) 332$(eval DEP := $(patsubst %.o,%.d,$(OBJ))) 333 334$(eval TOOL_DEFINES := $($(4)_DEFINES)) 335$(eval TOOL_INCLUDE_DIRS := $($(4)_INCLUDE_DIRS)) 336$(eval TOOL_CPPFLAGS := $($(4)_CPPFLAGS) $(addprefix -D,$(TOOL_DEFINES)) $(addprefix -I,$(TOOL_INCLUDE_DIRS))) 337$(eval TOOL_CFLAGS := $($(4)_CFLAGS)) 338 339$(OBJ): $(SRC) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/ 340 $$(s)echo " HOSTCC $$<" 341 $$(q)$(host-cc) $$(HOSTCCFLAGS) $(TOOL_CPPFLAGS) $(TOOL_CFLAGS) $(call MAKE_DEP,$(DEP),$(OBJ)) -c $$< -o $$@ 342 343-include $(DEP) 344 345endef 346 347# MAKE_TOOL 348# $(1) = output directory 349# $(2) = lowercase name of the tool 350# $(3) = uppercase name of the tool 351define MAKE_TOOL 352$(eval SRCS := $($(3)_SOURCES)) 353$(eval OBJS := $(patsubst %.c,$(1)/$(2)/%.o,$(SRCS))) 354$(eval DST := $(1)/$(2)/$(2)$(.exe)) 355$(eval $(foreach src,$(SRCS),$(call MAKE_TOOL_C,$(1),$(src),$(2),$(3)))) 356 357$(DST): $(OBJS) $(filter-out %.d,$(MAKEFILE_LIST)) | $(1) 358 $$(s)echo " HOSTLD $$@" 359 $$(q)$(host-ld) $$(OBJS) -o $$@ $$(HOSTLDFLAGS) $($(3)_LDFLAGS) 360 $$(s)echo 361 $$(s)echo "Built $$@ successfully" 362 $$(s)echo 363 364all: $(DST) 365endef 366 367# MAKE_C_LIB builds a C source file and generates the dependency file 368# $(1) = output directory 369# $(2) = source file (%.c) 370# $(3) = library name 371# $(4) = uppercase name of the library 372# $(5) = optional list of cflags that needed to be removed 373define MAKE_C_LIB 374$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2)))) 375$(eval DEP := $(patsubst %.o,%.d,$(OBJ))) 376$(eval LIB := $(notdir $(1))) 377 378$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/ 379 $$(s)echo " CC $$<" 380 $$(q)$($(ARCH)-cc) $$(LIB$(4)_CFLAGS) $$(filter-out $(5),$$(TF_CFLAGS)) $(call MAKE_DEP,$(DEP),$(OBJ)) -c $$< -o $$@ 381 382-include $(DEP) 383 384endef 385 386# MAKE_S_LIB builds an assembly source file and generates the dependency file 387# $(1) = output directory 388# $(2) = source file (%.S) 389# $(3) = library name 390# $(4) = uppercase name of the library 391define MAKE_S_LIB 392$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2)))) 393$(eval DEP := $(patsubst %.o,%.d,$(OBJ))) 394 395$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/ 396 $$(s)echo " AS $$<" 397 $$(q)$($(ARCH)-as) -x assembler-with-cpp $$(TF_CFLAGS) $$(ASFLAGS) $(call MAKE_DEP,$(DEP),$(OBJ)) -c $$< -o $$@ 398 399-include $(DEP) 400 401endef 402 403 404# MAKE_C builds a C source file and generates the dependency file 405# $(1) = output directory 406# $(2) = source file (%.c) 407# $(3) = BL stage 408# $(4) = uppercase BL stage 409define MAKE_C 410 411$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2)))) 412$(eval DEP := $(patsubst %.o,%.d,$(OBJ))) 413 414$(eval BL_DEFINES := IMAGE_$(4) $($(4)_DEFINES)) 415$(eval BL_INCLUDE_DIRS := $($(4)_INCLUDE_DIRS)) 416$(eval BL_CPPFLAGS := $($(4)_CPPFLAGS) $(addprefix -D,$(BL_DEFINES)) $(addprefix -I,$(BL_INCLUDE_DIRS))) 417$(eval BL_CFLAGS := $($(4)_CFLAGS)) 418 419$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/ $(BL_INCLUDE_DIRS:%=%/) 420 $$(s)echo " CC $$<" 421 $$(q)$($(ARCH)-cc) $$(LTO_CFLAGS) $$(TF_CFLAGS) $(BL_CPPFLAGS) $(BL_CFLAGS) $(call MAKE_DEP,$(DEP),$(OBJ)) -c $$< -o $$@ 422 423-include $(DEP) 424 425endef 426 427 428# MAKE_S builds an assembly source file and generates the dependency file 429# $(1) = output directory 430# $(2) = assembly file (%.S) 431# $(3) = BL stage 432# $(4) = uppercase BL stage 433define MAKE_S 434 435$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2)))) 436$(eval DEP := $(patsubst %.o,%.d,$(OBJ))) 437 438$(eval BL_DEFINES := IMAGE_$(4) $($(4)_DEFINES)) 439$(eval BL_INCLUDE_DIRS := $($(4)_INCLUDE_DIRS)) 440$(eval BL_CPPFLAGS := $($(4)_CPPFLAGS) $(addprefix -D,$(BL_DEFINES)) $(addprefix -I,$(BL_INCLUDE_DIRS))) 441$(eval BL_ASFLAGS := $($(4)_ASFLAGS)) 442 443$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/ $(BL_INCLUDE_DIRS:%=%/) 444 $$(s)echo " AS $$<" 445 $$(q)$($(ARCH)-as) -x assembler-with-cpp $$(TF_CFLAGS) $$(ASFLAGS) $(BL_CPPFLAGS) $(BL_ASFLAGS) $(call MAKE_DEP,$(DEP),$(OBJ)) -c $$< -o $$@ 446 447-include $(DEP) 448 449endef 450 451# MAKE_PRE run the C preprocessor on a file 452# $(1) = output file 453# $(2) = list of input files 454# $(3) = dep file 455# $(4) = list of rule-specific flags to pass 456define MAKE_PRE 457$(eval OUT := $(1)) 458$(eval SRC := $(2)) 459$(eval DEP := $(3)) 460$(eval CUSTOM_FLAGS := $(4)) 461$(OUT): $(SRC) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/ 462 $$(s)echo " CPP $$<" 463 $$(q)$($(ARCH)-cpp) -E -P -x assembler-with-cpp $$(TF_CFLAGS) $(CUSTOM_FLAGS) $(call MAKE_DEP,$(DEP),$(OUT)) -o $$@ $$< 464endef 465 466# MAKE_LD generate the linker script using the C preprocessor 467# $(1) = output linker script 468# $(2) = input template 469# $(3) = BL stage 470# $(4) = uppercase BL stage 471define MAKE_LD 472 473$(eval DEP := $(1).d) 474 475$(eval BL_DEFINES := IMAGE_$(4) $($(4)_DEFINES)) 476$(eval BL_INCLUDE_DIRS := $($(4)_INCLUDE_DIRS)) 477$(eval BL_CPPFLAGS := $($(4)_CPPFLAGS) $(addprefix -D,$(BL_DEFINES)) $(addprefix -I,$(BL_INCLUDE_DIRS))) 478$(eval FLAGS := -D__LINKER__ $(BL_CPPFLAGS)) 479 480$(1): | $(BL_INCLUDE_DIRS:%=%/) 481 482$(eval $(call MAKE_PRE,$(1),$(2),$(DEP),$(FLAGS))) 483-include $(DEP) 484 485endef 486 487# MAKE_LIB_OBJS builds both C and assembly source files 488# $(1) = output directory 489# $(2) = list of source files 490# $(3) = name of the library 491# $(4) = uppercase name of the library 492# $(5) = optional list of cflags that needed to be removed 493define MAKE_LIB_OBJS 494 $(eval C_OBJS := $(filter %.c,$(2))) 495 $(eval REMAIN := $(filter-out %.c,$(2))) 496 $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C_LIB,$(1),$(obj),$(3),$(4),$(5)))) 497 498 $(eval S_OBJS := $(filter %.S,$(REMAIN))) 499 $(eval REMAIN := $(filter-out %.S,$(REMAIN))) 500 $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S_LIB,$(1),$(obj),$(3),$(4)))) 501 502 $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN))) 503endef 504 505 506# MAKE_OBJS builds both C and assembly source files 507# $(1) = output directory 508# $(2) = list of source files (both C and assembly) 509# $(3) = BL stage 510# $(4) = uppercase BL stage 511define MAKE_OBJS 512 $(eval C_OBJS := $(filter %.c,$(2))) 513 $(eval REMAIN := $(filter-out %.c,$(2))) 514 $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj),$(3),$(4)))) 515 516 $(eval S_OBJS := $(filter %.S,$(REMAIN))) 517 $(eval REMAIN := $(filter-out %.S,$(REMAIN))) 518 $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj),$(3),$(4)))) 519 520 $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN))) 521endef 522 523 524# NOTE: The line continuation '\' is required in the next define otherwise we 525# end up with a line-feed characer at the end of the last c filename. 526# Also bear this issue in mind if extending the list of supported filetypes. 527define SOURCES_TO_OBJS 528 $(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \ 529 $(notdir $(patsubst %.S,%.o,$(filter %.S,$(1)))) 530endef 531 532.PHONY: libraries 533 534# MAKE_LIB macro defines the targets and options to build each BL image. 535# Arguments: 536# $(1) = Library name 537# $(2) = Optinal list of cflags needed to removed 538define MAKE_LIB 539 $(eval BL := $(call uppercase,$(1))) 540 $(eval BUILD_DIR := ${BUILD_PLAT}/lib$(1)) 541 $(eval LIB_DIR := ${BUILD_PLAT}/lib) 542 $(eval ROMLIB_DIR := ${BUILD_PLAT}/romlib) 543 $(eval SOURCES := $(LIB$(BL)_SRCS)) 544 $(eval OBJS := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES)))) 545 546$(eval $(call MAKE_LIB_OBJS,$(BUILD_DIR),$(SOURCES),$(1),$(BL),$(2))) 547 548libraries: ${LIB_DIR}/lib$(1).a 549ifeq ($($(ARCH)-ld-id),arm-link) 550LDPATHS = --userlibpath=${LIB_DIR} 551LDLIBS += --library=$(1) 552else 553LDPATHS = -L${LIB_DIR} 554LDLIBS += -l$(1) 555endif 556 557ifeq ($(USE_ROMLIB),1) 558LIBWRAPPER = -lwrappers 559endif 560 561all: ${LIB_DIR}/lib$(1).a 562 563${LIB_DIR}/lib$(1).a: $(OBJS) | $$$$(@D)/ 564 $$(s)echo " AR $$@" 565 $$(q)$($(ARCH)-ar) cr $$@ $$? 566endef 567 568# Generate the path to one or more preprocessed linker scripts given the paths 569# of their sources. 570# 571# Arguments: 572# $(1) = path to one or more linker script sources 573define linker_script_path 574 $(patsubst %.S,$(BUILD_DIR)/%,$(1)) 575endef 576 577# MAKE_BL macro defines the targets and options to build each BL image. 578# Arguments: 579# $(1) = BL stage 580# $(2) = FIP command line option (if empty, image will not be included in the FIP) 581# $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip) 582# $(4) = BL encryption flag (optional) (0, 1) 583define MAKE_BL 584 $(eval BL := $(call uppercase,$(1))) 585 $(eval BUILD_DIR := ${BUILD_PLAT}/$(1)) 586 $(eval BL_SOURCES := $($(BL)_SOURCES)) 587 $(eval SOURCES := $(sort $(BL_SOURCES) $(BL_COMMON_SOURCES) $(PLAT_BL_COMMON_SOURCES))) 588 $(eval OBJS := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES)))) 589 $(eval MAPFILE := $(call IMG_MAPFILE,$(1))) 590 $(eval ELF := $(call IMG_ELF,$(1))) 591 $(eval DUMP := $(call IMG_DUMP,$(1))) 592 $(eval BIN := $(call IMG_BIN,$(1))) 593 $(eval ENC_BIN := $(call IMG_ENC_BIN,$(1))) 594 $(eval BL_LIBS := $($(BL)_LIBS)) 595 596 $(eval DEFAULT_LINKER_SCRIPT_SOURCE := $($(BL)_DEFAULT_LINKER_SCRIPT_SOURCE)) 597 $(eval DEFAULT_LINKER_SCRIPT := $(call linker_script_path,$(DEFAULT_LINKER_SCRIPT_SOURCE))) 598 599 $(eval LINKER_SCRIPT_SOURCES := $($(BL)_LINKER_SCRIPT_SOURCES)) 600 $(eval LINKER_SCRIPTS := $(call linker_script_path,$(LINKER_SCRIPT_SOURCES))) 601 $(eval GNU_LINKER_ARGS := $(call ld_prefix,-Map=$(MAPFILE)) $(foreach script,$(LINKER_SCRIPTS) $(DEFAULT_LINKER_SCRIPT), $(call ld_prefix,--script $(script)))) 602 603$(eval $(call MAKE_OBJS,$(BUILD_DIR),$(SOURCES),$(1),$(BL))) 604 605# Generate targets to preprocess each required linker script 606$(eval $(foreach source,$(DEFAULT_LINKER_SCRIPT_SOURCE) $(LINKER_SCRIPT_SOURCES), \ 607 $(call MAKE_LD,$(call linker_script_path,$(source)),$(source),$(1),$(BL)))) 608 609$(eval BL_LDFLAGS := $($(BL)_LDFLAGS)) 610 611ifeq ($(USE_ROMLIB),1) 612$(ELF): $(BUILD_PLAT)/romlib/romlib.bin | $$$$(@D)/ 613endif 614 615# MODULE_OBJS can be assigned by vendors with different compiled 616# object file path, and prebuilt object file path. 617$(eval OBJS += $(MODULE_OBJS)) 618 619$(ELF): $(OBJS) $(DEFAULT_LINKER_SCRIPT) $(LINKER_SCRIPTS) | $$$$(@D)/ libraries $(BL_LIBS) 620 $$(s)echo " LD $$@" 621ifeq ($($(ARCH)-ld-id),arm-link) 622 $$(q)$($(ARCH)-ld) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $(BL_LDFLAGS) --entry=${1}_entrypoint \ 623 --predefine=$(call escape-shell,-D__LINKER__=$(__LINKER__)) \ 624 --predefine=$(call escape-shell,-DTF_CFLAGS=$(TF_CFLAGS)) \ 625 --map --list="$(MAPFILE)" --scatter=${PLAT_DIR}/scat/${1}.scat \ 626 $(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS) $(OBJS) 627else 628 $$(q)$($(ARCH)-ld) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $(BL_LDFLAGS) \ 629 $(GNU_LINKER_ARGS) $(LDPATHS) \ 630 $(call ld_prefix,--start-group) \ 631 $(OBJS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS) \ 632 $(call ld_prefix,--end-group) 633endif 634ifeq ($(DISABLE_BIN_GENERATION),1) 635 $(s)echo 636 $(s)echo "Built $$@ successfully" 637 $(s)echo 638endif 639 640$(DUMP): $(ELF) | $$$$(@D)/ 641 $$(s)echo " OD $$@" 642 $$(q)$($(ARCH)-od) -dx $$< > $$@ 643 644$(BIN): $(ELF) | $$$$(@D)/ 645 $$(s)echo " BIN $$@" 646 $$(q)$($(ARCH)-oc) -O binary $$< $$@ 647 $(s)echo 648 $(s)echo "Built $$@ successfully" 649 $(s)echo 650 651.PHONY: $(1) 652ifeq ($(DISABLE_BIN_GENERATION),1) 653$(1): $(ELF) $(DUMP) 654else 655$(1): $(BIN) $(DUMP) 656endif 657 658all: $(1) 659 660ifeq ($(4),1) 661$(call ENCRYPT_FW,$(BIN),$(ENC_BIN)) 662$(if $(2),$(call TOOL_ADD_IMG_PAYLOAD,$(BL),$(BIN),--$(2),$(ENC_BIN),$(3), \ 663 $(ENC_BIN))) 664else 665$(if $(2),$(call TOOL_ADD_IMG_PAYLOAD,$(BL),$(BIN),--$(2),$(BIN),$(3))) 666endif 667 668endef 669 670# Convert device tree source file names to matching blobs 671# $(1) = input dts 672define SOURCES_TO_DTBS 673 $(notdir $(patsubst %.dts,%.dtb,$(filter %.dts,$(1)))) 674endef 675 676# MAKE_DTB generate the Flattened device tree binary 677# $(1) = output directory 678# $(2) = input dts 679define MAKE_DTB 680 681# List of DTB file(s) to generate, based on DTS file basename list 682$(eval DOBJ := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2)))) 683# List of the pre-compiled DTS file(s) 684$(eval DPRE := $(addprefix $(1)/,$(patsubst %.dts,%.pre.dts,$(notdir $(2))))) 685# Dependencies of the pre-compiled DTS file(s) on its source and included files 686$(eval DTSDEP := $(patsubst %.dtb,%.o.d,$(DOBJ))) 687# Dependencies of the DT compilation on its pre-compiled DTS 688$(eval DTBDEP := $(patsubst %.dtb,%.d,$(DOBJ))) 689 690$(eval $(call MAKE_PRE,$(DPRE),$(2),$(DTSDEP),$(DTC_CPPFLAGS))) 691 692$(DOBJ): $(DPRE) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/ 693 $$(s)echo " DTC $$<" 694 $$(q)$($(ARCH)-dtc) $$(DTC_FLAGS) -d $(DTBDEP) -o $$@ $$< 695 $$($$@-after) 696 697-include $(DTBDEP) 698-include $(DTSDEP) 699 700endef 701 702# MAKE_DTBS builds flattened device tree sources 703# $(1) = output directory 704# $(2) = list of flattened device tree source files 705define MAKE_DTBS 706 $(eval DOBJS := $(filter %.dts,$(2))) 707 $(eval REMAIN := $(filter-out %.dts,$(2))) 708 $(and $(REMAIN),$(error FDT_SOURCES contain non-DTS files: $(REMAIN))) 709 $(eval $(foreach obj,$(DOBJS),$(call MAKE_DTB,$(1),$(obj)))) 710 711dtbs: $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2))) 712all: dtbs 713endef 714