173c99d4eSJuan Castillo# 22d51b55eSBalint Dobszay# Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved. 373c99d4eSJuan Castillo# 482cb2c1aSdp-arm# SPDX-License-Identifier: BSD-3-Clause 573c99d4eSJuan Castillo# 673c99d4eSJuan Castillo 71670d9dfSEvan Lloyd# Report an error if the eval make function is not available. 81670d9dfSEvan Lloyd$(eval eval_available := T) 91670d9dfSEvan Lloydifneq (${eval_available},T) 101670d9dfSEvan Lloyd $(error This makefile only works with a Make program that supports $$(eval)) 111670d9dfSEvan Lloydendif 121670d9dfSEvan Lloyd 13231c1470SEvan Lloyd# Some utility macros for manipulating awkward (whitespace) characters. 14231c1470SEvan Lloydblank := 15231c1470SEvan Lloydspace :=${blank} ${blank} 16231c1470SEvan Lloyd 17231c1470SEvan Lloyd# A user defined function to recursively search for a filename below a directory 18231c1470SEvan Lloyd# $1 is the directory root of the recursive search (blank for current directory). 19231c1470SEvan Lloyd# $2 is the file name to search for. 20231c1470SEvan Lloyddefine rwildcard 21231c1470SEvan Lloyd$(strip $(foreach d,$(wildcard ${1}*),$(call rwildcard,${d}/,${2}) $(filter $(subst *,%,%${2}),${d}))) 22231c1470SEvan Lloydendef 23231c1470SEvan Lloyd 245ba8f669SYatharth Kochar# This table is used in converting lower case to upper case. 255ba8f669SYatharth Kocharuppercase_table:=a,A b,B c,C d,D e,E f,F g,G h,H i,I j,J k,K l,L m,M n,N o,O p,P q,Q r,R s,S t,T u,U v,V w,W x,X y,Y z,Z 265ba8f669SYatharth Kochar 275ba8f669SYatharth Kochar# Internal macro used for converting lower case to upper case. 285ba8f669SYatharth Kochar# $(1) = upper case table 295ba8f669SYatharth Kochar# $(2) = String to convert 305ba8f669SYatharth Kochardefine uppercase_internal 315ba8f669SYatharth Kochar$(if $(1),$$(subst $(firstword $(1)),$(call uppercase_internal,$(wordlist 2,$(words $(1)),$(1)),$(2))),$(2)) 325ba8f669SYatharth Kocharendef 335ba8f669SYatharth Kochar 345ba8f669SYatharth Kochar# A macro for converting a string to upper case 355ba8f669SYatharth Kochar# $(1) = String to convert 365ba8f669SYatharth Kochardefine uppercase 375ba8f669SYatharth Kochar$(eval uppercase_result:=$(call uppercase_internal,$(uppercase_table),$(1)))$(uppercase_result) 385ba8f669SYatharth Kocharendef 395ba8f669SYatharth Kochar 4073c99d4eSJuan Castillo# Convenience function for adding build definitions 4173c99d4eSJuan Castillo# $(eval $(call add_define,FOO)) will have: 4273c99d4eSJuan Castillo# -DFOO if $(FOO) is empty; -DFOO=$(FOO) otherwise 4373c99d4eSJuan Castillodefine add_define 4473c99d4eSJuan Castillo DEFINES += -D$(1)$(if $(value $(1)),=$(value $(1)),) 4573c99d4eSJuan Castilloendef 4673c99d4eSJuan Castillo 478eadeb4aSSoren Brinkmann# Convenience function for adding build definitions 488eadeb4aSSoren Brinkmann# $(eval $(call add_define_val,FOO,BAR)) will have: 498eadeb4aSSoren Brinkmann# -DFOO=BAR 508eadeb4aSSoren Brinkmanndefine add_define_val 518eadeb4aSSoren Brinkmann DEFINES += -D$(1)=$(2) 528eadeb4aSSoren Brinkmannendef 538eadeb4aSSoren Brinkmann 5473c99d4eSJuan Castillo# Convenience function for verifying option has a boolean value 5573c99d4eSJuan Castillo# $(eval $(call assert_boolean,FOO)) will assert FOO is 0 or 1 5673c99d4eSJuan Castillodefine assert_boolean 57be4cd40eSMasahiro Yamada $(if $(filter-out 0 1,$($1)),$(error $1 must be boolean)) 5873c99d4eSJuan Castilloendef 5973c99d4eSJuan Castillo 60c877b414SJeenu Viswambharan0-9 := 0 1 2 3 4 5 6 7 8 9 61c877b414SJeenu Viswambharan 62c877b414SJeenu Viswambharan# Function to verify that a given option $(1) contains a numeric value 63c877b414SJeenu Viswambharandefine assert_numeric 64c877b414SJeenu Viswambharan$(if $($(1)),,$(error $(1) must not be empty)) 65c877b414SJeenu Viswambharan$(eval __numeric := $($(1))) 66c877b414SJeenu Viswambharan$(foreach d,$(0-9),$(eval __numeric := $(subst $(d),,$(__numeric)))) 67c877b414SJeenu Viswambharan$(if $(__numeric),$(error $(1) must be numeric)) 68c877b414SJeenu Viswambharanendef 69c877b414SJeenu Viswambharan 708c7b944aSVijayenthiran Subramaniam# CREATE_SEQ is a recursive function to create sequence of numbers from 1 to 718c7b944aSVijayenthiran Subramaniam# $(2) and assign the sequence to $(1) 728c7b944aSVijayenthiran Subramaniamdefine CREATE_SEQ 738c7b944aSVijayenthiran Subramaniam$(if $(word $(2), $($(1))),\ 748c7b944aSVijayenthiran Subramaniam $(eval $(1) += $(words $($(1))))\ 758c7b944aSVijayenthiran Subramaniam $(eval $(1) := $(filter-out 0,$($(1)))),\ 768c7b944aSVijayenthiran Subramaniam $(eval $(1) += $(words $($(1))))\ 778c7b944aSVijayenthiran Subramaniam $(call CREATE_SEQ,$(1),$(2))\ 788c7b944aSVijayenthiran Subramaniam) 798c7b944aSVijayenthiran Subramaniamendef 808c7b944aSVijayenthiran Subramaniam 8173c99d4eSJuan Castillo# IMG_LINKERFILE defines the linker script corresponding to a BL stage 82d7db9a6aSMasahiro Yamada# $(1) = BL stage (1, 2, 2u, 31, 32) 8373c99d4eSJuan Castillodefine IMG_LINKERFILE 8473c99d4eSJuan Castillo ${BUILD_DIR}/bl$(1).ld 8573c99d4eSJuan Castilloendef 8673c99d4eSJuan Castillo 8773c99d4eSJuan Castillo# IMG_MAPFILE defines the output file describing the memory map corresponding 8873c99d4eSJuan Castillo# to a BL stage 89d7db9a6aSMasahiro Yamada# $(1) = BL stage (1, 2, 2u, 31, 32) 9073c99d4eSJuan Castillodefine IMG_MAPFILE 9173c99d4eSJuan Castillo ${BUILD_DIR}/bl$(1).map 9273c99d4eSJuan Castilloendef 9373c99d4eSJuan Castillo 9473c99d4eSJuan Castillo# IMG_ELF defines the elf file corresponding to a BL stage 95d7db9a6aSMasahiro Yamada# $(1) = BL stage (1, 2, 2u, 31, 32) 9673c99d4eSJuan Castillodefine IMG_ELF 9773c99d4eSJuan Castillo ${BUILD_DIR}/bl$(1).elf 9873c99d4eSJuan Castilloendef 9973c99d4eSJuan Castillo 10073c99d4eSJuan Castillo# IMG_DUMP defines the symbols dump file corresponding to a BL stage 101d7db9a6aSMasahiro Yamada# $(1) = BL stage (1, 2, 2u, 31, 32) 10273c99d4eSJuan Castillodefine IMG_DUMP 10373c99d4eSJuan Castillo ${BUILD_DIR}/bl$(1).dump 10473c99d4eSJuan Castilloendef 10573c99d4eSJuan Castillo 10673c99d4eSJuan Castillo# IMG_BIN defines the default image file corresponding to a BL stage 107d7db9a6aSMasahiro Yamada# $(1) = BL stage (1, 2, 2u, 31, 32) 10873c99d4eSJuan Castillodefine IMG_BIN 10973c99d4eSJuan Castillo ${BUILD_PLAT}/bl$(1).bin 11073c99d4eSJuan Castilloendef 11173c99d4eSJuan Castillo 112*c6ba9b45SSumit Garg# IMG_ENC_BIN defines the default encrypted image file corresponding to a 113*c6ba9b45SSumit Garg# BL stage 114*c6ba9b45SSumit Garg# $(1) = BL stage (2, 30, 31, 32, 33) 115*c6ba9b45SSumit Gargdefine IMG_ENC_BIN 116*c6ba9b45SSumit Garg ${BUILD_PLAT}/bl$(1)_enc.bin 117*c6ba9b45SSumit Gargendef 118*c6ba9b45SSumit Garg 119*c6ba9b45SSumit Garg# ENCRYPT_FW invokes enctool to encrypt firmware binary 120*c6ba9b45SSumit Garg# $(1) = input firmware binary 121*c6ba9b45SSumit Garg# $(2) = output encrypted firmware binary 122*c6ba9b45SSumit Gargdefine ENCRYPT_FW 123*c6ba9b45SSumit Garg$(2): $(1) enctool 124*c6ba9b45SSumit Garg $$(ECHO) " ENC $$<" 125*c6ba9b45SSumit Garg $$(Q)$$(ENCTOOL) $$(ENC_ARGS) -i $$< -o $$@ 126*c6ba9b45SSumit Gargendef 127*c6ba9b45SSumit Garg 12810cea934SMasahiro Yamada# TOOL_ADD_PAYLOAD appends the command line arguments required by fiptool to 12910cea934SMasahiro Yamada# package a new payload and/or by cert_create to generate certificate. 13010cea934SMasahiro Yamada# Optionally, it adds the dependency on this payload 13173c99d4eSJuan Castillo# $(1) = payload filename (i.e. bl31.bin) 1321e0c0784SMasahiro Yamada# $(2) = command line option for the specified payload (i.e. --soc-fw) 13336af3455SMasahiro Yamada# $(3) = tool target dependency (optional) (ex. build/fvp/release/bl31.bin) 1341dc0714fSMasahiro Yamada# $(4) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip) 135*c6ba9b45SSumit Garg# $(5) = encrypted payload (optional) (ex. build/fvp/release/bl31_enc.bin) 13610cea934SMasahiro Yamadadefine TOOL_ADD_PAYLOAD 137*c6ba9b45SSumit Gargifneq ($(5),) 138*c6ba9b45SSumit Garg $(4)FIP_ARGS += $(2) $(5) 139*c6ba9b45SSumit Garg $(if $(3),$(4)CRT_DEPS += $(1)) 140*c6ba9b45SSumit Gargelse 141945b316fSMasahiro Yamada $(4)FIP_ARGS += $(2) $(1) 142*c6ba9b45SSumit Garg $(if $(3),$(4)CRT_DEPS += $(3)) 143*c6ba9b45SSumit Gargendif 144945b316fSMasahiro Yamada $(if $(3),$(4)FIP_DEPS += $(3)) 145f30ee0b9SMasahiro Yamada $(4)CRT_ARGS += $(2) $(1) 14673c99d4eSJuan Castilloendef 14773c99d4eSJuan Castillo 1482da522bbSMasahiro Yamada# TOOL_ADD_IMG_PAYLOAD works like TOOL_ADD_PAYLOAD, but applies image filters 1492da522bbSMasahiro Yamada# before passing them to host tools if BL*_PRE_TOOL_FILTER is defined. 1502da522bbSMasahiro Yamada# $(1) = image_type (scp_bl2, bl33, etc.) 1512da522bbSMasahiro Yamada# $(2) = payload filepath (ex. build/fvp/release/bl31.bin) 1522da522bbSMasahiro Yamada# $(3) = command line option for the specified payload (ex. --soc-fw) 1532da522bbSMasahiro Yamada# $(4) = tool target dependency (optional) (ex. build/fvp/release/bl31.bin) 1542da522bbSMasahiro Yamada# $(5) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip) 155*c6ba9b45SSumit Garg# $(6) = encrypted payload (optional) (ex. build/fvp/release/bl31_enc.bin) 1562da522bbSMasahiro Yamada 1572da522bbSMasahiro Yamadadefine TOOL_ADD_IMG_PAYLOAD 1582da522bbSMasahiro Yamada 1592da522bbSMasahiro Yamada$(eval PRE_TOOL_FILTER := $($(call uppercase,$(1))_PRE_TOOL_FILTER)) 1602da522bbSMasahiro Yamada 1612da522bbSMasahiro Yamadaifneq ($(PRE_TOOL_FILTER),) 1622da522bbSMasahiro Yamada 1632da522bbSMasahiro Yamada$(eval PROCESSED_PATH := $(BUILD_PLAT)/$(1).bin$($(PRE_TOOL_FILTER)_SUFFIX)) 1642da522bbSMasahiro Yamada 1652da522bbSMasahiro Yamada$(call $(PRE_TOOL_FILTER)_RULE,$(PROCESSED_PATH),$(2)) 1662da522bbSMasahiro Yamada 1672da522bbSMasahiro Yamada$(PROCESSED_PATH): $(4) 1682da522bbSMasahiro Yamada 169*c6ba9b45SSumit Garg$(call TOOL_ADD_PAYLOAD,$(PROCESSED_PATH),$(3),$(PROCESSED_PATH),$(5),$(6)) 1702da522bbSMasahiro Yamada 1712da522bbSMasahiro Yamadaelse 172*c6ba9b45SSumit Garg$(call TOOL_ADD_PAYLOAD,$(2),$(3),$(4),$(5),$(6)) 1732da522bbSMasahiro Yamadaendif 1742da522bbSMasahiro Yamadaendef 1752da522bbSMasahiro Yamada 1760191262dSYatharth Kochar# CERT_ADD_CMD_OPT adds a new command line option to the cert_create invocation 17773c99d4eSJuan Castillo# $(1) = parameter filename 17873c99d4eSJuan Castillo# $(2) = cert_create command line option for the specified parameter 17991704d9dSMasahiro Yamada# $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip) 18073c99d4eSJuan Castillodefine CERT_ADD_CMD_OPT 18191704d9dSMasahiro Yamada $(3)CRT_ARGS += $(2) $(1) 18273c99d4eSJuan Castilloendef 18373c99d4eSJuan Castillo 184c939d13aSMasahiro Yamada# TOOL_ADD_IMG allows the platform to specify an external image to be packed 185c939d13aSMasahiro Yamada# in the FIP and/or for which certificate is generated. It also adds a 186c939d13aSMasahiro Yamada# dependency on the image file, aborting the build if the file does not exist. 18733950dd8SMasahiro Yamada# $(1) = image_type (scp_bl2, bl33, etc.) 1881e0c0784SMasahiro Yamada# $(2) = command line option for fiptool (--scp-fw, --nt-fw, etc) 1891dc0714fSMasahiro Yamada# $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip) 190*c6ba9b45SSumit Garg# $(4) = Image encryption flag (optional) (0, 1) 19173c99d4eSJuan Castillo# Example: 19233950dd8SMasahiro Yamada# $(eval $(call TOOL_ADD_IMG,bl33,--nt-fw)) 193c939d13aSMasahiro Yamadadefine TOOL_ADD_IMG 19433950dd8SMasahiro Yamada # Build option to specify the image filename (SCP_BL2, BL33, etc) 19533950dd8SMasahiro Yamada # This is the uppercase form of the first parameter 19633950dd8SMasahiro Yamada $(eval _V := $(call uppercase,$(1))) 19733950dd8SMasahiro Yamada 198945b316fSMasahiro Yamada $(3)CRT_DEPS += check_$(1) 199945b316fSMasahiro Yamada $(3)FIP_DEPS += check_$(1) 200*c6ba9b45SSumit Gargifeq ($(4),1) 201*c6ba9b45SSumit Garg $(eval ENC_BIN := ${BUILD_PLAT}/$(1)_enc.bin) 202*c6ba9b45SSumit Garg $(call ENCRYPT_FW,$(value $(_V)),$(ENC_BIN)) 203*c6ba9b45SSumit Garg $(call TOOL_ADD_IMG_PAYLOAD,$(1),$(value $(_V)),$(2),$(ENC_BIN),$(3), \ 204*c6ba9b45SSumit Garg $(ENC_BIN)) 205*c6ba9b45SSumit Gargelse 2062da522bbSMasahiro Yamada $(call TOOL_ADD_IMG_PAYLOAD,$(1),$(value $(_V)),$(2),,$(3)) 207*c6ba9b45SSumit Gargendif 2080191262dSYatharth Kochar 20987ebd20dSMasahiro Yamada.PHONY: check_$(1) 2100191262dSYatharth Kocharcheck_$(1): 21133950dd8SMasahiro Yamada $$(if $(value $(_V)),,$$(error "Platform '${PLAT}' requires $(_V). Please set $(_V) to point to the right file")) 21233950dd8SMasahiro Yamada $$(if $(wildcard $(value $(_V))),,$$(error '$(_V)=$(value $(_V))' was specified, but '$(value $(_V))' does not exist)) 2130191262dSYatharth Kocharendef 21473c99d4eSJuan Castillo 21573c99d4eSJuan Castillo################################################################################ 21614db8908SMasahiro Yamada# Generic image processing filters 21714db8908SMasahiro Yamada################################################################################ 21814db8908SMasahiro Yamada 21914db8908SMasahiro Yamada# GZIP 22014db8908SMasahiro Yamadadefine GZIP_RULE 22114db8908SMasahiro Yamada$(1): $(2) 222ee1ba6d4SAndre Przywara $(ECHO) " GZIP $$@" 22314db8908SMasahiro Yamada $(Q)gzip -n -f -9 $$< --stdout > $$@ 22414db8908SMasahiro Yamadaendef 22514db8908SMasahiro Yamada 22614db8908SMasahiro YamadaGZIP_SUFFIX := .gz 22714db8908SMasahiro Yamada 22814db8908SMasahiro Yamada################################################################################ 22973c99d4eSJuan Castillo# Auxiliary macros to build TF images from sources 23073c99d4eSJuan Castillo################################################################################ 23173c99d4eSJuan Castillo 2321d274ab0SMasahiro YamadaMAKE_DEP = -Wp,-MD,$(DEP) -MT $$@ -MP 23373c99d4eSJuan Castillo 2345fee0287SRoberto Vargas 2355fee0287SRoberto Vargas# MAKE_C_LIB builds a C source file and generates the dependency file 2365fee0287SRoberto Vargas# $(1) = output directory 2375fee0287SRoberto Vargas# $(2) = source file (%.c) 2385fee0287SRoberto Vargas# $(3) = library name 2395fee0287SRoberto Vargasdefine MAKE_C_LIB 2405fee0287SRoberto Vargas$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2)))) 2415fee0287SRoberto Vargas$(eval DEP := $(patsubst %.o,%.d,$(OBJ))) 2425fee0287SRoberto Vargas 2435fee0287SRoberto Vargas$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | lib$(3)_dirs 244ee1ba6d4SAndre Przywara $$(ECHO) " CC $$<" 2455fee0287SRoberto Vargas $$(Q)$$(CC) $$(TF_CFLAGS) $$(CFLAGS) $(MAKE_DEP) -c $$< -o $$@ 2465fee0287SRoberto Vargas 2475fee0287SRoberto Vargas-include $(DEP) 2485fee0287SRoberto Vargas 2495fee0287SRoberto Vargasendef 2505fee0287SRoberto Vargas 25170b0f278SAntonio Nino Diaz# MAKE_S_LIB builds an assembly source file and generates the dependency file 25270b0f278SAntonio Nino Diaz# $(1) = output directory 25370b0f278SAntonio Nino Diaz# $(2) = source file (%.S) 25470b0f278SAntonio Nino Diaz# $(3) = library name 25570b0f278SAntonio Nino Diazdefine MAKE_S_LIB 25670b0f278SAntonio Nino Diaz$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2)))) 25770b0f278SAntonio Nino Diaz$(eval DEP := $(patsubst %.o,%.d,$(OBJ))) 25870b0f278SAntonio Nino Diaz 25970b0f278SAntonio Nino Diaz$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | lib$(3)_dirs 26070b0f278SAntonio Nino Diaz $$(ECHO) " AS $$<" 26170b0f278SAntonio Nino Diaz $$(Q)$$(AS) $$(ASFLAGS) $(MAKE_DEP) -c $$< -o $$@ 26270b0f278SAntonio Nino Diaz 26370b0f278SAntonio Nino Diaz-include $(DEP) 26470b0f278SAntonio Nino Diaz 26570b0f278SAntonio Nino Diazendef 26670b0f278SAntonio Nino Diaz 2675fee0287SRoberto Vargas 26873c99d4eSJuan Castillo# MAKE_C builds a C source file and generates the dependency file 26973c99d4eSJuan Castillo# $(1) = output directory 27073c99d4eSJuan Castillo# $(2) = source file (%.c) 271d7db9a6aSMasahiro Yamada# $(3) = BL stage (1, 2, 2u, 31, 32) 27273c99d4eSJuan Castillodefine MAKE_C 27373c99d4eSJuan Castillo 27473c99d4eSJuan Castillo$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2)))) 275710ea1d0SMasahiro Yamada$(eval DEP := $(patsubst %.o,%.d,$(OBJ))) 2765ba8f669SYatharth Kochar$(eval IMAGE := IMAGE_BL$(call uppercase,$(3))) 2773661f16cSJeenu Viswambharan$(eval BL_CFLAGS := $(BL$(call uppercase,$(3))_CFLAGS)) 27873c99d4eSJuan Castillo 2796bc1a30cSStephen Warren$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | bl$(3)_dirs 280ee1ba6d4SAndre Przywara $$(ECHO) " CC $$<" 281edbce9aaSzelalem-aweke $$(Q)$$(CC) $$(LTO_CFLAGS) $$(TF_CFLAGS) $$(CFLAGS) $(BL_CFLAGS) -D$(IMAGE) $(MAKE_DEP) -c $$< -o $$@ 28273c99d4eSJuan Castillo 283710ea1d0SMasahiro Yamada-include $(DEP) 28473c99d4eSJuan Castillo 28573c99d4eSJuan Castilloendef 28673c99d4eSJuan Castillo 28773c99d4eSJuan Castillo 28873c99d4eSJuan Castillo# MAKE_S builds an assembly source file and generates the dependency file 28973c99d4eSJuan Castillo# $(1) = output directory 29073c99d4eSJuan Castillo# $(2) = assembly file (%.S) 291d7db9a6aSMasahiro Yamada# $(3) = BL stage (1, 2, 2u, 31, 32) 29273c99d4eSJuan Castillodefine MAKE_S 29373c99d4eSJuan Castillo 29473c99d4eSJuan Castillo$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2)))) 295710ea1d0SMasahiro Yamada$(eval DEP := $(patsubst %.o,%.d,$(OBJ))) 2965ba8f669SYatharth Kochar$(eval IMAGE := IMAGE_BL$(call uppercase,$(3))) 29773c99d4eSJuan Castillo 2986bc1a30cSStephen Warren$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | bl$(3)_dirs 299ee1ba6d4SAndre Przywara $$(ECHO) " AS $$<" 300710ea1d0SMasahiro Yamada $$(Q)$$(AS) $$(ASFLAGS) -D$(IMAGE) $(MAKE_DEP) -c $$< -o $$@ 30173c99d4eSJuan Castillo 302710ea1d0SMasahiro Yamada-include $(DEP) 30373c99d4eSJuan Castillo 30473c99d4eSJuan Castilloendef 30573c99d4eSJuan Castillo 30673c99d4eSJuan Castillo 30773c99d4eSJuan Castillo# MAKE_LD generate the linker script using the C preprocessor 30873c99d4eSJuan Castillo# $(1) = output linker script 30973c99d4eSJuan Castillo# $(2) = input template 310d7db9a6aSMasahiro Yamada# $(3) = BL stage (1, 2, 2u, 31, 32) 31173c99d4eSJuan Castillodefine MAKE_LD 31273c99d4eSJuan Castillo 313710ea1d0SMasahiro Yamada$(eval DEP := $(1).d) 3141a4b46d5SDaniel Boulby$(eval IMAGE := IMAGE_BL$(call uppercase,$(3))) 31573c99d4eSJuan Castillo 3166bc1a30cSStephen Warren$(1): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | bl$(3)_dirs 317ee1ba6d4SAndre Przywara $$(ECHO) " PP $$<" 318402b3cf8SJulius Werner $$(Q)$$(CPP) $$(CPPFLAGS) $(TF_CFLAGS_$(ARCH)) -P -x assembler-with-cpp -D__LINKER__ $(MAKE_DEP) -D$(IMAGE) -o $$@ $$< 31973c99d4eSJuan Castillo 320710ea1d0SMasahiro Yamada-include $(DEP) 32173c99d4eSJuan Castillo 32273c99d4eSJuan Castilloendef 32373c99d4eSJuan Castillo 32470b0f278SAntonio Nino Diaz# MAKE_LIB_OBJS builds both C and assembly source files 3255fee0287SRoberto Vargas# $(1) = output directory 3265fee0287SRoberto Vargas# $(2) = list of source files 3275fee0287SRoberto Vargas# $(3) = name of the library 3285fee0287SRoberto Vargasdefine MAKE_LIB_OBJS 3295fee0287SRoberto Vargas $(eval C_OBJS := $(filter %.c,$(2))) 3305fee0287SRoberto Vargas $(eval REMAIN := $(filter-out %.c,$(2))) 3315fee0287SRoberto Vargas $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C_LIB,$(1),$(obj),$(3)))) 3325fee0287SRoberto Vargas 33370b0f278SAntonio Nino Diaz $(eval S_OBJS := $(filter %.S,$(REMAIN))) 33470b0f278SAntonio Nino Diaz $(eval REMAIN := $(filter-out %.S,$(REMAIN))) 33570b0f278SAntonio Nino Diaz $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S_LIB,$(1),$(obj),$(3)))) 33670b0f278SAntonio Nino Diaz 3375fee0287SRoberto Vargas $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN))) 3385fee0287SRoberto Vargasendef 3395fee0287SRoberto Vargas 34073c99d4eSJuan Castillo 34173c99d4eSJuan Castillo# MAKE_OBJS builds both C and assembly source files 34273c99d4eSJuan Castillo# $(1) = output directory 34373c99d4eSJuan Castillo# $(2) = list of source files (both C and assembly) 344d7db9a6aSMasahiro Yamada# $(3) = BL stage (1, 2, 2u, 31, 32) 34573c99d4eSJuan Castillodefine MAKE_OBJS 34673c99d4eSJuan Castillo $(eval C_OBJS := $(filter %.c,$(2))) 34773c99d4eSJuan Castillo $(eval REMAIN := $(filter-out %.c,$(2))) 34873c99d4eSJuan Castillo $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj),$(3)))) 34973c99d4eSJuan Castillo 35073c99d4eSJuan Castillo $(eval S_OBJS := $(filter %.S,$(REMAIN))) 35173c99d4eSJuan Castillo $(eval REMAIN := $(filter-out %.S,$(REMAIN))) 35273c99d4eSJuan Castillo $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj),$(3)))) 35373c99d4eSJuan Castillo 35473c99d4eSJuan Castillo $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN))) 35573c99d4eSJuan Castilloendef 35673c99d4eSJuan Castillo 35773c99d4eSJuan Castillo 35873c99d4eSJuan Castillo# NOTE: The line continuation '\' is required in the next define otherwise we 35973c99d4eSJuan Castillo# end up with a line-feed characer at the end of the last c filename. 360e7f54dbdSEvan Lloyd# Also bear this issue in mind if extending the list of supported filetypes. 36173c99d4eSJuan Castillodefine SOURCES_TO_OBJS 36273c99d4eSJuan Castillo $(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \ 36373c99d4eSJuan Castillo $(notdir $(patsubst %.S,%.o,$(filter %.S,$(1)))) 36473c99d4eSJuan Castilloendef 36573c99d4eSJuan Castillo 3662f5d4a48SPatrick Georgi# Allow overriding the timestamp, for example for reproducible builds, or to 3672f5d4a48SPatrick Georgi# synchronize timestamps across multiple projects. 3682f5d4a48SPatrick Georgi# This must be set to a C string (including quotes where applicable). 3692f5d4a48SPatrick GeorgiBUILD_MESSAGE_TIMESTAMP ?= __TIME__", "__DATE__ 37073c99d4eSJuan Castillo 3715fee0287SRoberto Vargas.PHONY: libraries 3725fee0287SRoberto Vargas 3735accce5bSRoberto Vargas# MAKE_LIB_DIRS macro defines the target for the directory where 3745fee0287SRoberto Vargas# libraries are created 3755accce5bSRoberto Vargasdefine MAKE_LIB_DIRS 3765fee0287SRoberto Vargas $(eval LIB_DIR := ${BUILD_PLAT}/lib) 3775accce5bSRoberto Vargas $(eval ROMLIB_DIR := ${BUILD_PLAT}/romlib) 3785accce5bSRoberto Vargas $(eval LIBWRAPPER_DIR := ${BUILD_PLAT}/libwrapper) 3795fee0287SRoberto Vargas $(eval $(call MAKE_PREREQ_DIR,${LIB_DIR},${BUILD_PLAT})) 3805accce5bSRoberto Vargas $(eval $(call MAKE_PREREQ_DIR,${ROMLIB_DIR},${BUILD_PLAT})) 3815accce5bSRoberto Vargas $(eval $(call MAKE_PREREQ_DIR,${LIBWRAPPER_DIR},${BUILD_PLAT})) 3825fee0287SRoberto Vargasendef 3835fee0287SRoberto Vargas 3845fee0287SRoberto Vargas# MAKE_LIB macro defines the targets and options to build each BL image. 3855fee0287SRoberto Vargas# Arguments: 3865fee0287SRoberto Vargas# $(1) = Library name 3875fee0287SRoberto Vargasdefine MAKE_LIB 3885fee0287SRoberto Vargas $(eval BUILD_DIR := ${BUILD_PLAT}/lib$(1)) 3895fee0287SRoberto Vargas $(eval LIB_DIR := ${BUILD_PLAT}/lib) 3905accce5bSRoberto Vargas $(eval ROMLIB_DIR := ${BUILD_PLAT}/romlib) 3915fee0287SRoberto Vargas $(eval SOURCES := $(LIB$(call uppercase,$(1))_SRCS)) 3925fee0287SRoberto Vargas $(eval OBJS := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES)))) 3935fee0287SRoberto Vargas 3945fee0287SRoberto Vargas$(eval $(call MAKE_PREREQ_DIR,${BUILD_DIR},${BUILD_PLAT})) 3955fee0287SRoberto Vargas$(eval $(call MAKE_LIB_OBJS,$(BUILD_DIR),$(SOURCES),$(1))) 3965fee0287SRoberto Vargas 3975fee0287SRoberto Vargas.PHONY : lib${1}_dirs 3985accce5bSRoberto Vargaslib${1}_dirs: | ${BUILD_DIR} ${LIB_DIR} ${ROMLIB_DIR} ${LIBWRAPPER_DIR} 3995fee0287SRoberto Vargaslibraries: ${LIB_DIR}/lib$(1).a 400c2ad38ceSVarun Wadekarifneq ($(findstring armlink,$(notdir $(LD))),) 401c2ad38ceSVarun WadekarLDPATHS = --userlibpath=${LIB_DIR} 402c2ad38ceSVarun WadekarLDLIBS += --library=$(1) 403c2ad38ceSVarun Wadekarelse 4045fee0287SRoberto VargasLDPATHS = -L${LIB_DIR} 4055fee0287SRoberto VargasLDLIBS += -l$(1) 406c2ad38ceSVarun Wadekarendif 4075fee0287SRoberto Vargas 4085accce5bSRoberto Vargasifeq ($(USE_ROMLIB),1) 4096baf85b3SSathees BalyaLIBWRAPPER = -lwrappers 4105accce5bSRoberto Vargasendif 4115accce5bSRoberto Vargas 4125fee0287SRoberto Vargasall: ${LIB_DIR}/lib$(1).a 4135fee0287SRoberto Vargas 4145fee0287SRoberto Vargas${LIB_DIR}/lib$(1).a: $(OBJS) 415ee1ba6d4SAndre Przywara $$(ECHO) " AR $$@" 4165fee0287SRoberto Vargas $$(Q)$$(AR) cr $$@ $$? 4175fee0287SRoberto Vargasendef 4185fee0287SRoberto Vargas 41973c99d4eSJuan Castillo# MAKE_BL macro defines the targets and options to build each BL image. 42073c99d4eSJuan Castillo# Arguments: 421d7db9a6aSMasahiro Yamada# $(1) = BL stage (1, 2, 2u, 31, 32) 4228f0617efSJuan Castillo# $(2) = FIP command line option (if empty, image will not be included in the FIP) 4231dc0714fSMasahiro Yamada# $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip) 424*c6ba9b45SSumit Garg# $(4) = BL encryption flag (optional) (0, 1) 42573c99d4eSJuan Castillodefine MAKE_BL 42673c99d4eSJuan Castillo $(eval BUILD_DIR := ${BUILD_PLAT}/bl$(1)) 4275ba8f669SYatharth Kochar $(eval BL_SOURCES := $(BL$(call uppercase,$(1))_SOURCES)) 4285ba8f669SYatharth Kochar $(eval SOURCES := $(BL_SOURCES) $(BL_COMMON_SOURCES) $(PLAT_BL_COMMON_SOURCES)) 42973c99d4eSJuan Castillo $(eval OBJS := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES)))) 43073c99d4eSJuan Castillo $(eval LINKERFILE := $(call IMG_LINKERFILE,$(1))) 43173c99d4eSJuan Castillo $(eval MAPFILE := $(call IMG_MAPFILE,$(1))) 43273c99d4eSJuan Castillo $(eval ELF := $(call IMG_ELF,$(1))) 43373c99d4eSJuan Castillo $(eval DUMP := $(call IMG_DUMP,$(1))) 43473c99d4eSJuan Castillo $(eval BIN := $(call IMG_BIN,$(1))) 435*c6ba9b45SSumit Garg $(eval ENC_BIN := $(call IMG_ENC_BIN,$(1))) 4365ba8f669SYatharth Kochar $(eval BL_LINKERFILE := $(BL$(call uppercase,$(1))_LINKERFILE)) 43723e0fe52SKonstantin Porotchkin $(eval BL_LIBS := $(BL$(call uppercase,$(1))_LIBS)) 43851b27702SEvan Lloyd # We use sort only to get a list of unique object directory names. 43951b27702SEvan Lloyd # ordering is not relevant but sort removes duplicates. 4406ba7d274SEvan Lloyd $(eval TEMP_OBJ_DIRS := $(sort $(dir ${OBJS} ${LINKERFILE}))) 44151b27702SEvan Lloyd # The $(dir ) function leaves a trailing / on the directory names 442d014ea6cSMasahiro Yamada # Rip off the / to match directory names with make rule targets. 443d014ea6cSMasahiro Yamada $(eval OBJ_DIRS := $(patsubst %/,%,$(TEMP_OBJ_DIRS))) 44451b27702SEvan Lloyd 44551b27702SEvan Lloyd# Create generators for object directory structure 44651b27702SEvan Lloyd 4478012cc5cSMasahiro Yamada$(eval $(call MAKE_PREREQ_DIR,${BUILD_DIR},${BUILD_PLAT})) 4486ba7d274SEvan Lloyd 4496ba7d274SEvan Lloyd$(eval $(foreach objd,${OBJ_DIRS},$(call MAKE_PREREQ_DIR,${objd},${BUILD_DIR}))) 45051b27702SEvan Lloyd 45151b27702SEvan Lloyd.PHONY : bl${1}_dirs 45251b27702SEvan Lloyd 45351b27702SEvan Lloyd# We use order-only prerequisites to ensure that directories are created, 45451b27702SEvan Lloyd# but do not cause re-builds every time a file is written. 45551b27702SEvan Lloydbl${1}_dirs: | ${OBJ_DIRS} 45673c99d4eSJuan Castillo 45773c99d4eSJuan Castillo$(eval $(call MAKE_OBJS,$(BUILD_DIR),$(SOURCES),$(1))) 458a6ca7888SMasahiro Yamada$(eval $(call MAKE_LD,$(LINKERFILE),$(BL_LINKERFILE),$(1))) 459d986bae4SMasahiro Yamada$(eval BL_LDFLAGS := $(BL$(call uppercase,$(1))_LDFLAGS)) 46073c99d4eSJuan Castillo 4615accce5bSRoberto Vargasifeq ($(USE_ROMLIB),1) 4625accce5bSRoberto Vargas$(ELF): romlib.bin 4635accce5bSRoberto Vargasendif 4645accce5bSRoberto Vargas 4655fee0287SRoberto Vargas$(ELF): $(OBJS) $(LINKERFILE) | bl$(1)_dirs libraries $(BL_LIBS) 466ee1ba6d4SAndre Przywara $$(ECHO) " LD $$@" 467414ab853SEvan Lloydifdef MAKE_BUILD_STRINGS 468414ab853SEvan Lloyd $(call MAKE_BUILD_STRINGS, $(BUILD_DIR)/build_message.o) 469414ab853SEvan Lloydelse 4702f5d4a48SPatrick Georgi @echo 'const char build_message[] = "Built : "$(BUILD_MESSAGE_TIMESTAMP); \ 47173c99d4eSJuan Castillo const char version_string[] = "${VERSION_STRING}";' | \ 472f2e1d57eSMasahiro Yamada $$(CC) $$(TF_CFLAGS) $$(CFLAGS) -xc -c - -o $(BUILD_DIR)/build_message.o 473414ab853SEvan Lloydendif 474c2ad38ceSVarun Wadekarifneq ($(findstring armlink,$(notdir $(LD))),) 475d986bae4SMasahiro Yamada $$(Q)$$(LD) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $(BL_LDFLAGS) --entry=bl${1}_entrypoint \ 476c2ad38ceSVarun Wadekar --predefine="-D__LINKER__=$(__LINKER__)" \ 477c2ad38ceSVarun Wadekar --predefine="-DTF_CFLAGS=$(TF_CFLAGS)" \ 478c2ad38ceSVarun Wadekar --map --list="$(MAPFILE)" --scatter=${PLAT_DIR}/scat/bl${1}.scat \ 479c2ad38ceSVarun Wadekar $(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS) \ 480c2ad38ceSVarun Wadekar $(BUILD_DIR)/build_message.o $(OBJS) 481edbce9aaSzelalem-awekeelse ifneq ($(findstring gcc,$(notdir $(LD))),) 482edbce9aaSzelalem-aweke $$(Q)$$(LD) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) -Wl,-Map=$(MAPFILE) \ 483edbce9aaSzelalem-aweke -Wl,-T$(LINKERFILE) $(BUILD_DIR)/build_message.o \ 484edbce9aaSzelalem-aweke $(OBJS) $(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS) 485c2ad38ceSVarun Wadekarelse 486d986bae4SMasahiro Yamada $$(Q)$$(LD) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $(BL_LDFLAGS) -Map=$(MAPFILE) \ 4875fee0287SRoberto Vargas --script $(LINKERFILE) $(BUILD_DIR)/build_message.o \ 4886baf85b3SSathees Balya $(OBJS) $(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS) 489c2ad38ceSVarun Wadekarendif 4909e4609f1SChristoph Müllnerifeq ($(DISABLE_BIN_GENERATION),1) 4919e4609f1SChristoph Müllner @${ECHO_BLANK_LINE} 4929e4609f1SChristoph Müllner @echo "Built $$@ successfully" 4939e4609f1SChristoph Müllner @${ECHO_BLANK_LINE} 4949e4609f1SChristoph Müllnerendif 49573c99d4eSJuan Castillo 49673c99d4eSJuan Castillo$(DUMP): $(ELF) 497ee1ba6d4SAndre Przywara $${ECHO} " OD $$@" 49873c99d4eSJuan Castillo $${Q}$${OD} -dx $$< > $$@ 49973c99d4eSJuan Castillo 50073c99d4eSJuan Castillo$(BIN): $(ELF) 501ee1ba6d4SAndre Przywara $${ECHO} " BIN $$@" 50273c99d4eSJuan Castillo $$(Q)$$(OC) -O binary $$< $$@ 503052ab529SEvan Lloyd @${ECHO_BLANK_LINE} 50473c99d4eSJuan Castillo @echo "Built $$@ successfully" 505052ab529SEvan Lloyd @${ECHO_BLANK_LINE} 50673c99d4eSJuan Castillo 50773c99d4eSJuan Castillo.PHONY: bl$(1) 5089e4609f1SChristoph Müllnerifeq ($(DISABLE_BIN_GENERATION),1) 5099e4609f1SChristoph Müllnerbl$(1): $(ELF) $(DUMP) 5109e4609f1SChristoph Müllnerelse 51151b27702SEvan Lloydbl$(1): $(BIN) $(DUMP) 5129e4609f1SChristoph Müllnerendif 51373c99d4eSJuan Castillo 51473c99d4eSJuan Castilloall: bl$(1) 51573c99d4eSJuan Castillo 516*c6ba9b45SSumit Gargifeq ($(4),1) 517*c6ba9b45SSumit Garg$(call ENCRYPT_FW,$(BIN),$(ENC_BIN)) 518*c6ba9b45SSumit Garg$(if $(2),$(call TOOL_ADD_IMG_PAYLOAD,bl$(1),$(BIN),--$(2),$(ENC_BIN),$(3), \ 519*c6ba9b45SSumit Garg $(ENC_BIN))) 520*c6ba9b45SSumit Gargelse 5212da522bbSMasahiro Yamada$(if $(2),$(call TOOL_ADD_IMG_PAYLOAD,bl$(1),$(BIN),--$(2),$(BIN),$(3))) 522*c6ba9b45SSumit Gargendif 52373c99d4eSJuan Castillo 52473c99d4eSJuan Castilloendef 52573c99d4eSJuan Castillo 52638c14d88SSoby Mathew# Convert device tree source file names to matching blobs 52738c14d88SSoby Mathew# $(1) = input dts 52803b397a8SNishanth Menondefine SOURCES_TO_DTBS 52903b397a8SNishanth Menon $(notdir $(patsubst %.dts,%.dtb,$(filter %.dts,$(1)))) 53003b397a8SNishanth Menonendef 53103b397a8SNishanth Menon 53238c14d88SSoby Mathew# MAKE_FDT_DIRS macro creates the prerequisite directories that host the 53338c14d88SSoby Mathew# FDT binaries 53438c14d88SSoby Mathew# $(1) = output directory 53538c14d88SSoby Mathew# $(2) = input dts 53638c14d88SSoby Mathewdefine MAKE_FDT_DIRS 53738c14d88SSoby Mathew $(eval DTBS := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2)))) 53803b397a8SNishanth Menon $(eval TEMP_DTB_DIRS := $(sort $(dir ${DTBS}))) 53903b397a8SNishanth Menon # The $(dir ) function leaves a trailing / on the directory names 54003b397a8SNishanth Menon # Rip off the / to match directory names with make rule targets. 54103b397a8SNishanth Menon $(eval DTB_DIRS := $(patsubst %/,%,$(TEMP_DTB_DIRS))) 54203b397a8SNishanth Menon 54303b397a8SNishanth Menon$(eval $(foreach objd,${DTB_DIRS},$(call MAKE_PREREQ_DIR,${objd},${BUILD_DIR}))) 54403b397a8SNishanth Menon 54503b397a8SNishanth Menonfdt_dirs: ${DTB_DIRS} 54603b397a8SNishanth Menonendef 54703b397a8SNishanth Menon 54838c14d88SSoby Mathew# MAKE_DTB generate the Flattened device tree binary 54903b397a8SNishanth Menon# $(1) = output directory 55003b397a8SNishanth Menon# $(2) = input dts 55103b397a8SNishanth Menondefine MAKE_DTB 55203b397a8SNishanth Menon 553077b3e9aSYann Gautier# List of DTB file(s) to generate, based on DTS file basename list 55438c14d88SSoby Mathew$(eval DOBJ := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2)))) 555077b3e9aSYann Gautier# List of the pre-compiled DTS file(s) 55601d237cbSYann Gautier$(eval DPRE := $(addprefix $(1)/,$(patsubst %.dts,%.pre.dts,$(notdir $(2))))) 557077b3e9aSYann Gautier# Dependencies of the pre-compiled DTS file(s) on its source and included files 558077b3e9aSYann Gautier$(eval DTSDEP := $(patsubst %.dtb,%.o.d,$(DOBJ))) 559077b3e9aSYann Gautier# Dependencies of the DT compilation on its pre-compiled DTS 560077b3e9aSYann Gautier$(eval DTBDEP := $(patsubst %.dtb,%.d,$(DOBJ))) 56103b397a8SNishanth Menon 5626bc1a30cSStephen Warren$(DOBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | fdt_dirs 563ee1ba6d4SAndre Przywara $${ECHO} " CPP $$<" 564077b3e9aSYann Gautier $(eval DTBS := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2)))) 5657e94a699SManish Pandey $$(Q)$$(PP) $$(DTC_CPPFLAGS) -MT $(DTBS) -MMD -MF $(DTSDEP) -o $(DPRE) $$< 566ee1ba6d4SAndre Przywara $${ECHO} " DTC $$<" 5672d51b55eSBalint Dobszay $$(Q)$$(DTC) $$(DTC_FLAGS) -d $(DTBDEP) -o $$@ $(DPRE) 56803b397a8SNishanth Menon 569077b3e9aSYann Gautier-include $(DTBDEP) 570077b3e9aSYann Gautier-include $(DTSDEP) 57103b397a8SNishanth Menon 57203b397a8SNishanth Menonendef 57303b397a8SNishanth Menon 57403b397a8SNishanth Menon# MAKE_DTBS builds flattened device tree sources 57503b397a8SNishanth Menon# $(1) = output directory 57603b397a8SNishanth Menon# $(2) = list of flattened device tree source files 57703b397a8SNishanth Menondefine MAKE_DTBS 57803b397a8SNishanth Menon $(eval DOBJS := $(filter %.dts,$(2))) 57903b397a8SNishanth Menon $(eval REMAIN := $(filter-out %.dts,$(2))) 58038c14d88SSoby Mathew $(and $(REMAIN),$(error FDT_SOURCES contain non-DTS files: $(REMAIN))) 58103b397a8SNishanth Menon $(eval $(foreach obj,$(DOBJS),$(call MAKE_DTB,$(1),$(obj)))) 58203b397a8SNishanth Menon 58338c14d88SSoby Mathew $(eval $(call MAKE_FDT_DIRS,$(1),$(2))) 58438c14d88SSoby Mathew 58538c14d88SSoby Mathewdtbs: $(DTBS) 58638c14d88SSoby Mathewall: dtbs 58703b397a8SNishanth Menonendef 588