xref: /rk3399_ARM-atf/make_helpers/build_macros.mk (revision 2a71f1633c7cc84c90c3c343b09153201ee6ebaa)
173c99d4eSJuan Castillo#
2781cb314SChris Kay# Copyright (c) 2015-2024, 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}
16a6ff0067SChris Kaycomma			:= ,
17231c1470SEvan Lloyd
18231c1470SEvan Lloyd# A user defined function to recursively search for a filename below a directory
19231c1470SEvan Lloyd#    $1 is the directory root of the recursive search (blank for current directory).
20231c1470SEvan Lloyd#    $2 is the file name to search for.
21231c1470SEvan Lloyddefine rwildcard
22231c1470SEvan Lloyd$(strip $(foreach d,$(wildcard ${1}*),$(call rwildcard,${d}/,${2}) $(filter $(subst *,%,%${2}),${d})))
23231c1470SEvan Lloydendef
24231c1470SEvan Lloyd
255ba8f669SYatharth Kochar# This table is used in converting lower case to upper case.
265ba8f669SYatharth 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
275ba8f669SYatharth Kochar
285ba8f669SYatharth Kochar# Internal macro used for converting lower case to upper case.
295ba8f669SYatharth Kochar#   $(1) = upper case table
305ba8f669SYatharth Kochar#   $(2) = String to convert
315ba8f669SYatharth Kochardefine uppercase_internal
325ba8f669SYatharth Kochar$(if $(1),$$(subst $(firstword $(1)),$(call uppercase_internal,$(wordlist 2,$(words $(1)),$(1)),$(2))),$(2))
335ba8f669SYatharth Kocharendef
345ba8f669SYatharth Kochar
355ba8f669SYatharth Kochar# A macro for converting a string to upper case
365ba8f669SYatharth Kochar#   $(1) = String to convert
375ba8f669SYatharth Kochardefine uppercase
385ba8f669SYatharth Kochar$(eval uppercase_result:=$(call uppercase_internal,$(uppercase_table),$(1)))$(uppercase_result)
395ba8f669SYatharth Kocharendef
405ba8f669SYatharth Kochar
41e444763dSBoyan Karatotev# Convenience function for setting a variable to 0 if not previously set
42e444763dSBoyan Karatotev# $(eval $(call default_zero,FOO))
43e444763dSBoyan Karatotevdefine default_zero
44e444763dSBoyan Karatotev	$(eval $(1) ?= 0)
45e444763dSBoyan Karatotevendef
46e444763dSBoyan Karatotev
47e444763dSBoyan Karatotev# Convenience function for setting a list of variables to 0 if not previously set
48e444763dSBoyan Karatotev# $(eval $(call default_zeros,FOO BAR))
49e444763dSBoyan Karatotevdefine default_zeros
50e444763dSBoyan Karatotev	$(foreach var,$1,$(eval $(call default_zero,$(var))))
51e444763dSBoyan Karatotevendef
52e444763dSBoyan Karatotev
53*2a71f163SGovindraj Raja# Convenience function for setting a variable to 1 if not previously set
54*2a71f163SGovindraj Raja# $(eval $(call default_one,FOO))
55*2a71f163SGovindraj Rajadefine default_one
56*2a71f163SGovindraj Raja	$(eval $(1) ?= 1)
57*2a71f163SGovindraj Rajaendef
58*2a71f163SGovindraj Raja
59*2a71f163SGovindraj Raja# Convenience function for setting a list of variables to 1 if not previously set
60*2a71f163SGovindraj Raja# $(eval $(call default_ones,FOO BAR))
61*2a71f163SGovindraj Rajadefine default_ones
62*2a71f163SGovindraj Raja	$(foreach var,$1,$(eval $(call default_one,$(var))))
63*2a71f163SGovindraj Rajaendef
64*2a71f163SGovindraj Raja
6573c99d4eSJuan Castillo# Convenience function for adding build definitions
6673c99d4eSJuan Castillo# $(eval $(call add_define,FOO)) will have:
6773c99d4eSJuan Castillo# -DFOO if $(FOO) is empty; -DFOO=$(FOO) otherwise
6873c99d4eSJuan Castillodefine add_define
6973c99d4eSJuan Castillo    DEFINES			+=	-D$(1)$(if $(value $(1)),=$(value $(1)),)
7073c99d4eSJuan Castilloendef
7173c99d4eSJuan Castillo
72327131c4SLeonardo Sandoval# Convenience function for addding multiple build definitions
73327131c4SLeonardo Sandoval# $(eval $(call add_defines,FOO BOO))
74327131c4SLeonardo Sandovaldefine add_defines
75327131c4SLeonardo Sandoval    $(foreach def,$1,$(eval $(call add_define,$(def))))
76327131c4SLeonardo Sandovalendef
77327131c4SLeonardo Sandoval
788eadeb4aSSoren Brinkmann# Convenience function for adding build definitions
798eadeb4aSSoren Brinkmann# $(eval $(call add_define_val,FOO,BAR)) will have:
808eadeb4aSSoren Brinkmann# -DFOO=BAR
818eadeb4aSSoren Brinkmanndefine add_define_val
828eadeb4aSSoren Brinkmann    DEFINES			+=	-D$(1)=$(2)
838eadeb4aSSoren Brinkmannendef
848eadeb4aSSoren Brinkmann
8573c99d4eSJuan Castillo# Convenience function for verifying option has a boolean value
8673c99d4eSJuan Castillo# $(eval $(call assert_boolean,FOO)) will assert FOO is 0 or 1
8773c99d4eSJuan Castillodefine assert_boolean
881369fb82SYann Gautier    $(if $($(1)),,$(error $(1) must not be empty))
89be4cd40eSMasahiro Yamada    $(if $(filter-out 0 1,$($1)),$(error $1 must be boolean))
9073c99d4eSJuan Castilloendef
9173c99d4eSJuan Castillo
92327131c4SLeonardo Sandoval# Convenience function for verifying options have boolean values
93327131c4SLeonardo Sandoval# $(eval $(call assert_booleans,FOO BOO)) will assert FOO and BOO for 0 or 1 values
94327131c4SLeonardo Sandovaldefine assert_booleans
95327131c4SLeonardo Sandoval    $(foreach bool,$1,$(eval $(call assert_boolean,$(bool))))
96327131c4SLeonardo Sandovalendef
97327131c4SLeonardo Sandoval
98c877b414SJeenu Viswambharan0-9 := 0 1 2 3 4 5 6 7 8 9
99c877b414SJeenu Viswambharan
100c877b414SJeenu Viswambharan# Function to verify that a given option $(1) contains a numeric value
101c877b414SJeenu Viswambharandefine assert_numeric
102c877b414SJeenu Viswambharan$(if $($(1)),,$(error $(1) must not be empty))
103c877b414SJeenu Viswambharan$(eval __numeric := $($(1)))
104c877b414SJeenu Viswambharan$(foreach d,$(0-9),$(eval __numeric := $(subst $(d),,$(__numeric))))
105c877b414SJeenu Viswambharan$(if $(__numeric),$(error $(1) must be numeric))
106c877b414SJeenu Viswambharanendef
107c877b414SJeenu Viswambharan
108327131c4SLeonardo Sandoval# Convenience function for verifying options have numeric values
109327131c4SLeonardo Sandoval# $(eval $(call assert_numerics,FOO BOO)) will assert FOO and BOO contain numeric values
110327131c4SLeonardo Sandovaldefine assert_numerics
111327131c4SLeonardo Sandoval    $(foreach num,$1,$(eval $(call assert_numeric,$(num))))
112327131c4SLeonardo Sandovalendef
113327131c4SLeonardo Sandoval
114a5f09cf7SMarco Felsch# Convenience function to check for a given linker option. An call to
115a5f09cf7SMarco Felsch# $(call ld_option, --no-XYZ) will return --no-XYZ if supported by the linker
11686e489c1SChris Kayld_option = $(shell $(LD) $(1) -Wl,--version >/dev/null 2>&1 || $(LD) $(1) -v >/dev/null 2>&1 && echo $(1))
117a5f09cf7SMarco Felsch
118dea23e24SGovindraj Raja# Convenience function to check for a given compiler option. A call to
119dea23e24SGovindraj Raja# $(call cc_option, --no-XYZ) will return --no-XYZ if supported by the compiler
120dea23e24SGovindraj Rajadefine cc_option
121dea23e24SGovindraj Raja	$(shell if $(CC) $(1) -c -x c /dev/null -o /dev/null >/dev/null 2>&1; then echo $(1); fi )
122dea23e24SGovindraj Rajaendef
123dea23e24SGovindraj Raja
1248c7b944aSVijayenthiran Subramaniam# CREATE_SEQ is a recursive function to create sequence of numbers from 1 to
1258c7b944aSVijayenthiran Subramaniam# $(2) and assign the sequence to $(1)
1268c7b944aSVijayenthiran Subramaniamdefine CREATE_SEQ
1278c7b944aSVijayenthiran Subramaniam$(if $(word $(2), $($(1))),\
1288c7b944aSVijayenthiran Subramaniam  $(eval $(1) += $(words $($(1))))\
1298c7b944aSVijayenthiran Subramaniam  $(eval $(1) := $(filter-out 0,$($(1)))),\
1308c7b944aSVijayenthiran Subramaniam  $(eval $(1) += $(words $($(1))))\
1318c7b944aSVijayenthiran Subramaniam  $(call CREATE_SEQ,$(1),$(2))\
1328c7b944aSVijayenthiran Subramaniam)
1338c7b944aSVijayenthiran Subramaniamendef
1348c7b944aSVijayenthiran Subramaniam
13573c99d4eSJuan Castillo# IMG_MAPFILE defines the output file describing the memory map corresponding
13673c99d4eSJuan Castillo# to a BL stage
137434d0491SZelalem Aweke#   $(1) = BL stage
13873c99d4eSJuan Castillodefine IMG_MAPFILE
139434d0491SZelalem Aweke    ${BUILD_DIR}/$(1).map
14073c99d4eSJuan Castilloendef
14173c99d4eSJuan Castillo
14273c99d4eSJuan Castillo# IMG_ELF defines the elf file corresponding to a BL stage
143434d0491SZelalem Aweke#   $(1) = BL stage
14473c99d4eSJuan Castillodefine IMG_ELF
145434d0491SZelalem Aweke    ${BUILD_DIR}/$(1).elf
14673c99d4eSJuan Castilloendef
14773c99d4eSJuan Castillo
14873c99d4eSJuan Castillo# IMG_DUMP defines the symbols dump file corresponding to a BL stage
149434d0491SZelalem Aweke#   $(1) = BL stage
15073c99d4eSJuan Castillodefine IMG_DUMP
151434d0491SZelalem Aweke    ${BUILD_DIR}/$(1).dump
15273c99d4eSJuan Castilloendef
15373c99d4eSJuan Castillo
15473c99d4eSJuan Castillo# IMG_BIN defines the default image file corresponding to a BL stage
155434d0491SZelalem Aweke#   $(1) = BL stage
15673c99d4eSJuan Castillodefine IMG_BIN
157434d0491SZelalem Aweke    ${BUILD_PLAT}/$(1).bin
15873c99d4eSJuan Castilloendef
15973c99d4eSJuan Castillo
160c6ba9b45SSumit Garg# IMG_ENC_BIN defines the default encrypted image file corresponding to a
161c6ba9b45SSumit Garg# BL stage
162434d0491SZelalem Aweke#   $(1) = BL stage
163c6ba9b45SSumit Gargdefine IMG_ENC_BIN
164434d0491SZelalem Aweke    ${BUILD_PLAT}/$(1)_enc.bin
165c6ba9b45SSumit Gargendef
166c6ba9b45SSumit Garg
167c6ba9b45SSumit Garg# ENCRYPT_FW invokes enctool to encrypt firmware binary
168c6ba9b45SSumit Garg#   $(1) = input firmware binary
169c6ba9b45SSumit Garg#   $(2) = output encrypted firmware binary
170c6ba9b45SSumit Gargdefine ENCRYPT_FW
171c6ba9b45SSumit Garg$(2): $(1) enctool
172c6ba9b45SSumit Garg	$$(ECHO) "  ENC     $$<"
173c6ba9b45SSumit Garg	$$(Q)$$(ENCTOOL) $$(ENC_ARGS) -i $$< -o $$@
174c6ba9b45SSumit Gargendef
175c6ba9b45SSumit Garg
17610cea934SMasahiro Yamada# TOOL_ADD_PAYLOAD appends the command line arguments required by fiptool to
17710cea934SMasahiro Yamada# package a new payload and/or by cert_create to generate certificate.
17810cea934SMasahiro Yamada# Optionally, it adds the dependency on this payload
17973c99d4eSJuan Castillo#   $(1) = payload filename (i.e. bl31.bin)
1801e0c0784SMasahiro Yamada#   $(2) = command line option for the specified payload (i.e. --soc-fw)
18136af3455SMasahiro Yamada#   $(3) = tool target dependency (optional) (ex. build/fvp/release/bl31.bin)
1821dc0714fSMasahiro Yamada#   $(4) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
183c6ba9b45SSumit Garg#   $(5) = encrypted payload (optional) (ex. build/fvp/release/bl31_enc.bin)
18410cea934SMasahiro Yamadadefine TOOL_ADD_PAYLOAD
185c6ba9b45SSumit Gargifneq ($(5),)
186c6ba9b45SSumit Garg    $(4)FIP_ARGS += $(2) $(5)
187c6ba9b45SSumit Garg    $(if $(3),$(4)CRT_DEPS += $(1))
188c6ba9b45SSumit Gargelse
189945b316fSMasahiro Yamada    $(4)FIP_ARGS += $(2) $(1)
190c6ba9b45SSumit Garg    $(if $(3),$(4)CRT_DEPS += $(3))
191c6ba9b45SSumit Gargendif
192945b316fSMasahiro Yamada    $(if $(3),$(4)FIP_DEPS += $(3))
193f30ee0b9SMasahiro Yamada    $(4)CRT_ARGS += $(2) $(1)
19473c99d4eSJuan Castilloendef
19573c99d4eSJuan Castillo
1962da522bbSMasahiro Yamada# TOOL_ADD_IMG_PAYLOAD works like TOOL_ADD_PAYLOAD, but applies image filters
1972da522bbSMasahiro Yamada# before passing them to host tools if BL*_PRE_TOOL_FILTER is defined.
1982da522bbSMasahiro Yamada#   $(1) = image_type (scp_bl2, bl33, etc.)
1992da522bbSMasahiro Yamada#   $(2) = payload filepath (ex. build/fvp/release/bl31.bin)
2002da522bbSMasahiro Yamada#   $(3) = command line option for the specified payload (ex. --soc-fw)
2012da522bbSMasahiro Yamada#   $(4) = tool target dependency (optional) (ex. build/fvp/release/bl31.bin)
2022da522bbSMasahiro Yamada#   $(5) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
203c6ba9b45SSumit Garg#   $(6) = encrypted payload (optional) (ex. build/fvp/release/bl31_enc.bin)
2042da522bbSMasahiro Yamada
2052da522bbSMasahiro Yamadadefine TOOL_ADD_IMG_PAYLOAD
2062da522bbSMasahiro Yamada
2072da522bbSMasahiro Yamada$(eval PRE_TOOL_FILTER := $($(call uppercase,$(1))_PRE_TOOL_FILTER))
2082da522bbSMasahiro Yamada
2092da522bbSMasahiro Yamadaifneq ($(PRE_TOOL_FILTER),)
2102da522bbSMasahiro Yamada
2112da522bbSMasahiro Yamada$(eval PROCESSED_PATH := $(BUILD_PLAT)/$(1).bin$($(PRE_TOOL_FILTER)_SUFFIX))
2122da522bbSMasahiro Yamada
2132da522bbSMasahiro Yamada$(call $(PRE_TOOL_FILTER)_RULE,$(PROCESSED_PATH),$(2))
2142da522bbSMasahiro Yamada
2152da522bbSMasahiro Yamada$(PROCESSED_PATH): $(4)
2162da522bbSMasahiro Yamada
217c6ba9b45SSumit Garg$(call TOOL_ADD_PAYLOAD,$(PROCESSED_PATH),$(3),$(PROCESSED_PATH),$(5),$(6))
2182da522bbSMasahiro Yamada
2192da522bbSMasahiro Yamadaelse
220c6ba9b45SSumit Garg$(call TOOL_ADD_PAYLOAD,$(2),$(3),$(4),$(5),$(6))
2212da522bbSMasahiro Yamadaendif
2222da522bbSMasahiro Yamadaendef
2232da522bbSMasahiro Yamada
2240191262dSYatharth Kochar# CERT_ADD_CMD_OPT adds a new command line option to the cert_create invocation
22573c99d4eSJuan Castillo#   $(1) = parameter filename
22673c99d4eSJuan Castillo#   $(2) = cert_create command line option for the specified parameter
22791704d9dSMasahiro Yamada#   $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
22873c99d4eSJuan Castillodefine CERT_ADD_CMD_OPT
22991704d9dSMasahiro Yamada    $(3)CRT_ARGS += $(2) $(1)
23073c99d4eSJuan Castilloendef
23173c99d4eSJuan Castillo
232c939d13aSMasahiro Yamada# TOOL_ADD_IMG allows the platform to specify an external image to be packed
233c939d13aSMasahiro Yamada# in the FIP and/or for which certificate is generated. It also adds a
234c939d13aSMasahiro Yamada# dependency on the image file, aborting the build if the file does not exist.
23533950dd8SMasahiro Yamada#   $(1) = image_type (scp_bl2, bl33, etc.)
2361e0c0784SMasahiro Yamada#   $(2) = command line option for fiptool (--scp-fw, --nt-fw, etc)
2371dc0714fSMasahiro Yamada#   $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
238c6ba9b45SSumit Garg#   $(4) = Image encryption flag (optional) (0, 1)
23973c99d4eSJuan Castillo# Example:
24033950dd8SMasahiro Yamada#   $(eval $(call TOOL_ADD_IMG,bl33,--nt-fw))
241c939d13aSMasahiro Yamadadefine TOOL_ADD_IMG
24233950dd8SMasahiro Yamada    # Build option to specify the image filename (SCP_BL2, BL33, etc)
24333950dd8SMasahiro Yamada    # This is the uppercase form of the first parameter
24433950dd8SMasahiro Yamada    $(eval _V := $(call uppercase,$(1)))
24533950dd8SMasahiro Yamada
2464727fd13SPali Rohár    # $(check_$(1)_cmd) variable is executed in the check_$(1) target and also
2474727fd13SPali Rohár    # is put into the ${CHECK_$(3)FIP_CMD} variable which is executed by the
2484727fd13SPali Rohár    # target ${BUILD_PLAT}/${$(3)FIP_NAME}.
2494727fd13SPali Rohár    $(eval check_$(1)_cmd := \
2504727fd13SPali Rohár        $(if $(value $(_V)),,$$$$(error "Platform '${PLAT}' requires $(_V). Please set $(_V) to point to the right file")) \
2514727fd13SPali Rohár        $(if $(wildcard $(value $(_V))),,$$$$(error '$(_V)=$(value $(_V))' was specified, but '$(value $(_V))' does not exist)) \
2524727fd13SPali Rohár    )
2534727fd13SPali Rohár
254945b316fSMasahiro Yamada    $(3)CRT_DEPS += check_$(1)
2554727fd13SPali Rohár    CHECK_$(3)FIP_CMD += $$(check_$(1)_cmd)
256c6ba9b45SSumit Gargifeq ($(4),1)
257c6ba9b45SSumit Garg    $(eval ENC_BIN := ${BUILD_PLAT}/$(1)_enc.bin)
258c6ba9b45SSumit Garg    $(call ENCRYPT_FW,$(value $(_V)),$(ENC_BIN))
259c6ba9b45SSumit Garg    $(call TOOL_ADD_IMG_PAYLOAD,$(1),$(value $(_V)),$(2),$(ENC_BIN),$(3), \
260c6ba9b45SSumit Garg		$(ENC_BIN))
261c6ba9b45SSumit Gargelse
2624727fd13SPali Rohár    $(call TOOL_ADD_IMG_PAYLOAD,$(1),$(value $(_V)),$(2),$(if $(wildcard $(value $(_V))),$(value $(_V)),FORCE),$(3))
263c6ba9b45SSumit Gargendif
2640191262dSYatharth Kochar
26587ebd20dSMasahiro Yamada.PHONY: check_$(1)
2660191262dSYatharth Kocharcheck_$(1):
2674727fd13SPali Rohár	$(check_$(1)_cmd)
2680191262dSYatharth Kocharendef
26973c99d4eSJuan Castillo
270cf2dd17dSJuan Pablo Conde# SELECT_OPENSSL_API_VERSION selects the OpenSSL API version to be used to
271cf2dd17dSJuan Pablo Conde# build the host tools by checking the version of OpenSSL located under
272cf2dd17dSJuan Pablo Conde# the path defined by the OPENSSL_DIR variable. It receives no parameters.
273cf2dd17dSJuan Pablo Condedefine SELECT_OPENSSL_API_VERSION
274cf2dd17dSJuan Pablo Conde    # Set default value for USING_OPENSSL3 macro to 0
275cf2dd17dSJuan Pablo Conde    $(eval USING_OPENSSL3 = 0)
276cf2dd17dSJuan Pablo Conde    # Obtain the OpenSSL version for the build located under OPENSSL_DIR
277cf2dd17dSJuan Pablo Conde    $(eval OPENSSL_INFO := $(shell LD_LIBRARY_PATH=${OPENSSL_DIR}:${OPENSSL_DIR}/lib ${OPENSSL_BIN_PATH}/openssl version))
278cf2dd17dSJuan Pablo Conde    $(eval OPENSSL_CURRENT_VER = $(word 2, ${OPENSSL_INFO}))
279cf2dd17dSJuan Pablo Conde    $(eval OPENSSL_CURRENT_VER_MAJOR = $(firstword $(subst ., ,$(OPENSSL_CURRENT_VER))))
280cf2dd17dSJuan Pablo Conde    # If OpenSSL version is 3.x, then set USING_OPENSSL3 flag to 1
281cf2dd17dSJuan Pablo Conde    $(if $(filter 3,$(OPENSSL_CURRENT_VER_MAJOR)), $(eval USING_OPENSSL3 = 1))
282cf2dd17dSJuan Pablo Condeendef
283cf2dd17dSJuan Pablo Conde
28473c99d4eSJuan Castillo################################################################################
28514db8908SMasahiro Yamada# Generic image processing filters
28614db8908SMasahiro Yamada################################################################################
28714db8908SMasahiro Yamada
28814db8908SMasahiro Yamada# GZIP
28914db8908SMasahiro Yamadadefine GZIP_RULE
29014db8908SMasahiro Yamada$(1): $(2)
291ee1ba6d4SAndre Przywara	$(ECHO) "  GZIP    $$@"
29214db8908SMasahiro Yamada	$(Q)gzip -n -f -9 $$< --stdout > $$@
29314db8908SMasahiro Yamadaendef
29414db8908SMasahiro Yamada
29514db8908SMasahiro YamadaGZIP_SUFFIX := .gz
29614db8908SMasahiro Yamada
29714db8908SMasahiro Yamada################################################################################
29873c99d4eSJuan Castillo# Auxiliary macros to build TF images from sources
29973c99d4eSJuan Castillo################################################################################
30073c99d4eSJuan Castillo
3011d274ab0SMasahiro YamadaMAKE_DEP = -Wp,-MD,$(DEP) -MT $$@ -MP
30273c99d4eSJuan Castillo
3035fee0287SRoberto Vargas
3045fee0287SRoberto Vargas# MAKE_C_LIB builds a C source file and generates the dependency file
3055fee0287SRoberto Vargas#   $(1) = output directory
3065fee0287SRoberto Vargas#   $(2) = source file (%.c)
3075fee0287SRoberto Vargas#   $(3) = library name
3085fee0287SRoberto Vargasdefine MAKE_C_LIB
3095fee0287SRoberto Vargas$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
3105fee0287SRoberto Vargas$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
3115a65fcd5SGovindraj Raja$(eval LIB := $(call uppercase, $(notdir $(1))))
3125fee0287SRoberto Vargas
3135fee0287SRoberto Vargas$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | lib$(3)_dirs
314ee1ba6d4SAndre Przywara	$$(ECHO) "  CC      $$<"
3155a65fcd5SGovindraj Raja	$$(Q)$$(CC) $$($(LIB)_CFLAGS) $$(TF_CFLAGS) $$(CFLAGS) $(MAKE_DEP) -c $$< -o $$@
3165fee0287SRoberto Vargas
3175fee0287SRoberto Vargas-include $(DEP)
3185fee0287SRoberto Vargas
3195fee0287SRoberto Vargasendef
3205fee0287SRoberto Vargas
32170b0f278SAntonio Nino Diaz# MAKE_S_LIB builds an assembly source file and generates the dependency file
32270b0f278SAntonio Nino Diaz#   $(1) = output directory
32370b0f278SAntonio Nino Diaz#   $(2) = source file (%.S)
32470b0f278SAntonio Nino Diaz#   $(3) = library name
32570b0f278SAntonio Nino Diazdefine MAKE_S_LIB
32670b0f278SAntonio Nino Diaz$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
32770b0f278SAntonio Nino Diaz$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
32870b0f278SAntonio Nino Diaz
32970b0f278SAntonio Nino Diaz$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | lib$(3)_dirs
33070b0f278SAntonio Nino Diaz	$$(ECHO) "  AS      $$<"
33170b0f278SAntonio Nino Diaz	$$(Q)$$(AS) $$(ASFLAGS) $(MAKE_DEP) -c $$< -o $$@
33270b0f278SAntonio Nino Diaz
33370b0f278SAntonio Nino Diaz-include $(DEP)
33470b0f278SAntonio Nino Diaz
33570b0f278SAntonio Nino Diazendef
33670b0f278SAntonio Nino Diaz
3375fee0287SRoberto Vargas
33873c99d4eSJuan Castillo# MAKE_C builds a C source file and generates the dependency file
33973c99d4eSJuan Castillo#   $(1) = output directory
34073c99d4eSJuan Castillo#   $(2) = source file (%.c)
341434d0491SZelalem Aweke#   $(3) = BL stage
34273c99d4eSJuan Castillodefine MAKE_C
34373c99d4eSJuan Castillo
34473c99d4eSJuan Castillo$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
345710ea1d0SMasahiro Yamada$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
346a123cb14SChris Kay
3471ab8c109SChris Kay$(eval BL_DEFINES := IMAGE_$(call uppercase,$(3)) $($(call uppercase,$(3))_DEFINES) $(PLAT_BL_COMMON_DEFINES))
3481ab8c109SChris Kay$(eval BL_INCLUDE_DIRS := $($(call uppercase,$(3))_INCLUDE_DIRS) $(PLAT_BL_COMMON_INCLUDE_DIRS))
3491ab8c109SChris Kay$(eval BL_CPPFLAGS := $($(call uppercase,$(3))_CPPFLAGS) $(addprefix -D,$(BL_DEFINES)) $(addprefix -I,$(BL_INCLUDE_DIRS)) $(PLAT_BL_COMMON_CPPFLAGS))
3501ab8c109SChris Kay$(eval BL_CFLAGS := $($(call uppercase,$(3))_CFLAGS) $(PLAT_BL_COMMON_CFLAGS))
35173c99d4eSJuan Castillo
352434d0491SZelalem Aweke$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $(3)_dirs
353ee1ba6d4SAndre Przywara	$$(ECHO) "  CC      $$<"
354848a7e8cSMasahiro Yamada	$$(Q)$$(CC) $$(LTO_CFLAGS) $$(TF_CFLAGS) $$(CFLAGS) $(BL_CPPFLAGS) $(BL_CFLAGS) $(MAKE_DEP) -c $$< -o $$@
35573c99d4eSJuan Castillo
356710ea1d0SMasahiro Yamada-include $(DEP)
35773c99d4eSJuan Castillo
35873c99d4eSJuan Castilloendef
35973c99d4eSJuan Castillo
36073c99d4eSJuan Castillo
36173c99d4eSJuan Castillo# MAKE_S builds an assembly source file and generates the dependency file
36273c99d4eSJuan Castillo#   $(1) = output directory
36373c99d4eSJuan Castillo#   $(2) = assembly file (%.S)
364434d0491SZelalem Aweke#   $(3) = BL stage
36573c99d4eSJuan Castillodefine MAKE_S
36673c99d4eSJuan Castillo
36773c99d4eSJuan Castillo$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
368710ea1d0SMasahiro Yamada$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
369a123cb14SChris Kay
3701ab8c109SChris Kay$(eval BL_DEFINES := IMAGE_$(call uppercase,$(3)) $($(call uppercase,$(3))_DEFINES) $(PLAT_BL_COMMON_DEFINES))
3711ab8c109SChris Kay$(eval BL_INCLUDE_DIRS := $($(call uppercase,$(3))_INCLUDE_DIRS) $(PLAT_BL_COMMON_INCLUDE_DIRS))
3721ab8c109SChris Kay$(eval BL_CPPFLAGS := $($(call uppercase,$(3))_CPPFLAGS) $(addprefix -D,$(BL_DEFINES)) $(addprefix -I,$(BL_INCLUDE_DIRS)) $(PLAT_BL_COMMON_CPPFLAGS))
3731ab8c109SChris Kay$(eval BL_ASFLAGS := $($(call uppercase,$(3))_ASFLAGS) $(PLAT_BL_COMMON_ASFLAGS))
37473c99d4eSJuan Castillo
375434d0491SZelalem Aweke$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $(3)_dirs
376ee1ba6d4SAndre Przywara	$$(ECHO) "  AS      $$<"
377848a7e8cSMasahiro Yamada	$$(Q)$$(AS) $$(ASFLAGS) $(BL_CPPFLAGS) $(BL_ASFLAGS) $(MAKE_DEP) -c $$< -o $$@
37873c99d4eSJuan Castillo
379710ea1d0SMasahiro Yamada-include $(DEP)
38073c99d4eSJuan Castillo
38173c99d4eSJuan Castilloendef
38273c99d4eSJuan Castillo
38373c99d4eSJuan Castillo
38473c99d4eSJuan Castillo# MAKE_LD generate the linker script using the C preprocessor
38573c99d4eSJuan Castillo#   $(1) = output linker script
38673c99d4eSJuan Castillo#   $(2) = input template
387434d0491SZelalem Aweke#   $(3) = BL stage
38873c99d4eSJuan Castillodefine MAKE_LD
38973c99d4eSJuan Castillo
390710ea1d0SMasahiro Yamada$(eval DEP := $(1).d)
391a123cb14SChris Kay
3921ab8c109SChris Kay$(eval BL_DEFINES := IMAGE_$(call uppercase,$(3)) $($(call uppercase,$(3))_DEFINES) $(PLAT_BL_COMMON_DEFINES))
3931ab8c109SChris Kay$(eval BL_INCLUDE_DIRS := $($(call uppercase,$(3))_INCLUDE_DIRS) $(PLAT_BL_COMMON_INCLUDE_DIRS))
3941ab8c109SChris Kay$(eval BL_CPPFLAGS := $($(call uppercase,$(3))_CPPFLAGS) $(addprefix -D,$(BL_DEFINES)) $(addprefix -I,$(BL_INCLUDE_DIRS)) $(PLAT_BL_COMMON_CPPFLAGS))
39573c99d4eSJuan Castillo
396434d0491SZelalem Aweke$(1): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $(3)_dirs
397ee1ba6d4SAndre Przywara	$$(ECHO) "  PP      $$<"
398848a7e8cSMasahiro Yamada	$$(Q)$$(CPP) $$(CPPFLAGS) $(BL_CPPFLAGS) $(TF_CFLAGS_$(ARCH)) -P -x assembler-with-cpp -D__LINKER__ $(MAKE_DEP) -o $$@ $$<
39973c99d4eSJuan Castillo
400710ea1d0SMasahiro Yamada-include $(DEP)
40173c99d4eSJuan Castillo
40273c99d4eSJuan Castilloendef
40373c99d4eSJuan Castillo
40470b0f278SAntonio Nino Diaz# MAKE_LIB_OBJS builds both C and assembly source files
4055fee0287SRoberto Vargas#   $(1) = output directory
4065fee0287SRoberto Vargas#   $(2) = list of source files
4075fee0287SRoberto Vargas#   $(3) = name of the library
4085fee0287SRoberto Vargasdefine MAKE_LIB_OBJS
4095fee0287SRoberto Vargas        $(eval C_OBJS := $(filter %.c,$(2)))
4105fee0287SRoberto Vargas        $(eval REMAIN := $(filter-out %.c,$(2)))
4115fee0287SRoberto Vargas        $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C_LIB,$(1),$(obj),$(3))))
4125fee0287SRoberto Vargas
41370b0f278SAntonio Nino Diaz        $(eval S_OBJS := $(filter %.S,$(REMAIN)))
41470b0f278SAntonio Nino Diaz        $(eval REMAIN := $(filter-out %.S,$(REMAIN)))
41570b0f278SAntonio Nino Diaz        $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S_LIB,$(1),$(obj),$(3))))
41670b0f278SAntonio Nino Diaz
4175fee0287SRoberto Vargas        $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN)))
4185fee0287SRoberto Vargasendef
4195fee0287SRoberto Vargas
42073c99d4eSJuan Castillo
42173c99d4eSJuan Castillo# MAKE_OBJS builds both C and assembly source files
42273c99d4eSJuan Castillo#   $(1) = output directory
42373c99d4eSJuan Castillo#   $(2) = list of source files (both C and assembly)
424434d0491SZelalem Aweke#   $(3) = BL stage
42573c99d4eSJuan Castillodefine MAKE_OBJS
42673c99d4eSJuan Castillo        $(eval C_OBJS := $(filter %.c,$(2)))
42773c99d4eSJuan Castillo        $(eval REMAIN := $(filter-out %.c,$(2)))
42873c99d4eSJuan Castillo        $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj),$(3))))
42973c99d4eSJuan Castillo
43073c99d4eSJuan Castillo        $(eval S_OBJS := $(filter %.S,$(REMAIN)))
43173c99d4eSJuan Castillo        $(eval REMAIN := $(filter-out %.S,$(REMAIN)))
43273c99d4eSJuan Castillo        $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj),$(3))))
43373c99d4eSJuan Castillo
43473c99d4eSJuan Castillo        $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN)))
43573c99d4eSJuan Castilloendef
43673c99d4eSJuan Castillo
43773c99d4eSJuan Castillo
43873c99d4eSJuan Castillo# NOTE: The line continuation '\' is required in the next define otherwise we
43973c99d4eSJuan Castillo# end up with a line-feed characer at the end of the last c filename.
440e7f54dbdSEvan Lloyd# Also bear this issue in mind if extending the list of supported filetypes.
44173c99d4eSJuan Castillodefine SOURCES_TO_OBJS
44273c99d4eSJuan Castillo        $(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \
44373c99d4eSJuan Castillo        $(notdir $(patsubst %.S,%.o,$(filter %.S,$(1))))
44473c99d4eSJuan Castilloendef
44573c99d4eSJuan Castillo
4462f5d4a48SPatrick Georgi# Allow overriding the timestamp, for example for reproducible builds, or to
4472f5d4a48SPatrick Georgi# synchronize timestamps across multiple projects.
4482f5d4a48SPatrick Georgi# This must be set to a C string (including quotes where applicable).
4492f5d4a48SPatrick GeorgiBUILD_MESSAGE_TIMESTAMP ?= __TIME__", "__DATE__
45073c99d4eSJuan Castillo
4515fee0287SRoberto Vargas.PHONY: libraries
4525fee0287SRoberto Vargas
4535accce5bSRoberto Vargas# MAKE_LIB_DIRS macro defines the target for the directory where
4545fee0287SRoberto Vargas# libraries are created
4555accce5bSRoberto Vargasdefine MAKE_LIB_DIRS
4565fee0287SRoberto Vargas        $(eval LIB_DIR    := ${BUILD_PLAT}/lib)
4575accce5bSRoberto Vargas        $(eval ROMLIB_DIR    := ${BUILD_PLAT}/romlib)
4585accce5bSRoberto Vargas        $(eval LIBWRAPPER_DIR := ${BUILD_PLAT}/libwrapper)
4595fee0287SRoberto Vargas        $(eval $(call MAKE_PREREQ_DIR,${LIB_DIR},${BUILD_PLAT}))
4605accce5bSRoberto Vargas        $(eval $(call MAKE_PREREQ_DIR,${ROMLIB_DIR},${BUILD_PLAT}))
4615accce5bSRoberto Vargas        $(eval $(call MAKE_PREREQ_DIR,${LIBWRAPPER_DIR},${BUILD_PLAT}))
4625fee0287SRoberto Vargasendef
4635fee0287SRoberto Vargas
4645fee0287SRoberto Vargas# MAKE_LIB macro defines the targets and options to build each BL image.
4655fee0287SRoberto Vargas# Arguments:
4665fee0287SRoberto Vargas#   $(1) = Library name
4675fee0287SRoberto Vargasdefine MAKE_LIB
4685fee0287SRoberto Vargas        $(eval BUILD_DIR  := ${BUILD_PLAT}/lib$(1))
4695fee0287SRoberto Vargas        $(eval LIB_DIR    := ${BUILD_PLAT}/lib)
4705accce5bSRoberto Vargas        $(eval ROMLIB_DIR    := ${BUILD_PLAT}/romlib)
4715fee0287SRoberto Vargas        $(eval SOURCES    := $(LIB$(call uppercase,$(1))_SRCS))
4725fee0287SRoberto Vargas        $(eval OBJS       := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES))))
4735fee0287SRoberto Vargas
4745fee0287SRoberto Vargas$(eval $(call MAKE_PREREQ_DIR,${BUILD_DIR},${BUILD_PLAT}))
4755fee0287SRoberto Vargas$(eval $(call MAKE_LIB_OBJS,$(BUILD_DIR),$(SOURCES),$(1)))
4765fee0287SRoberto Vargas
4775fee0287SRoberto Vargas.PHONY : lib${1}_dirs
4785accce5bSRoberto Vargaslib${1}_dirs: | ${BUILD_DIR} ${LIB_DIR}  ${ROMLIB_DIR} ${LIBWRAPPER_DIR}
4795fee0287SRoberto Vargaslibraries: ${LIB_DIR}/lib$(1).a
480c2ad38ceSVarun Wadekarifneq ($(findstring armlink,$(notdir $(LD))),)
481c2ad38ceSVarun WadekarLDPATHS = --userlibpath=${LIB_DIR}
482c2ad38ceSVarun WadekarLDLIBS += --library=$(1)
483c2ad38ceSVarun Wadekarelse
4845fee0287SRoberto VargasLDPATHS = -L${LIB_DIR}
4855fee0287SRoberto VargasLDLIBS += -l$(1)
486c2ad38ceSVarun Wadekarendif
4875fee0287SRoberto Vargas
4885accce5bSRoberto Vargasifeq ($(USE_ROMLIB),1)
4896baf85b3SSathees BalyaLIBWRAPPER = -lwrappers
4905accce5bSRoberto Vargasendif
4915accce5bSRoberto Vargas
4925fee0287SRoberto Vargasall: ${LIB_DIR}/lib$(1).a
4935fee0287SRoberto Vargas
4945fee0287SRoberto Vargas${LIB_DIR}/lib$(1).a: $(OBJS)
495ee1ba6d4SAndre Przywara	$$(ECHO) "  AR      $$@"
4965fee0287SRoberto Vargas	$$(Q)$$(AR) cr $$@ $$?
4975fee0287SRoberto Vargasendef
4985fee0287SRoberto Vargas
49982274936SChris Kay# Generate the path to one or more preprocessed linker scripts given the paths
50082274936SChris Kay# of their sources.
50182274936SChris Kay#
50282274936SChris Kay# Arguments:
50382274936SChris Kay#   $(1) = path to one or more linker script sources
50482274936SChris Kaydefine linker_script_path
50582274936SChris Kay        $(patsubst %.S,$(BUILD_DIR)/%,$(1))
50682274936SChris Kayendef
50782274936SChris Kay
50873c99d4eSJuan Castillo# MAKE_BL macro defines the targets and options to build each BL image.
50973c99d4eSJuan Castillo# Arguments:
510434d0491SZelalem Aweke#   $(1) = BL stage
5118f0617efSJuan Castillo#   $(2) = FIP command line option (if empty, image will not be included in the FIP)
5121dc0714fSMasahiro Yamada#   $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
513c6ba9b45SSumit Garg#   $(4) = BL encryption flag (optional) (0, 1)
51473c99d4eSJuan Castillodefine MAKE_BL
515434d0491SZelalem Aweke        $(eval BUILD_DIR  := ${BUILD_PLAT}/$(1))
516434d0491SZelalem Aweke        $(eval BL_SOURCES := $($(call uppercase,$(1))_SOURCES))
517bb22fb84SChris Kay        $(eval SOURCES    := $(sort $(BL_SOURCES) $(BL_COMMON_SOURCES) $(PLAT_BL_COMMON_SOURCES)))
51873c99d4eSJuan Castillo        $(eval OBJS       := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES))))
51973c99d4eSJuan Castillo        $(eval MAPFILE    := $(call IMG_MAPFILE,$(1)))
52073c99d4eSJuan Castillo        $(eval ELF        := $(call IMG_ELF,$(1)))
52173c99d4eSJuan Castillo        $(eval DUMP       := $(call IMG_DUMP,$(1)))
52273c99d4eSJuan Castillo        $(eval BIN        := $(call IMG_BIN,$(1)))
523c6ba9b45SSumit Garg        $(eval ENC_BIN    := $(call IMG_ENC_BIN,$(1)))
524434d0491SZelalem Aweke        $(eval BL_LIBS    := $($(call uppercase,$(1))_LIBS))
52582274936SChris Kay
52682274936SChris Kay        $(eval DEFAULT_LINKER_SCRIPT_SOURCE := $($(call uppercase,$(1))_DEFAULT_LINKER_SCRIPT_SOURCE))
52782274936SChris Kay        $(eval DEFAULT_LINKER_SCRIPT := $(call linker_script_path,$(DEFAULT_LINKER_SCRIPT_SOURCE)))
52882274936SChris Kay
529a6ff0067SChris Kay        $(eval LINKER_SCRIPT_SOURCES := $($(call uppercase,$(1))_LINKER_SCRIPT_SOURCES))
530a6ff0067SChris Kay        $(eval LINKER_SCRIPTS := $(call linker_script_path,$(LINKER_SCRIPT_SOURCES)))
531a6ff0067SChris Kay
53251b27702SEvan Lloyd        # We use sort only to get a list of unique object directory names.
53351b27702SEvan Lloyd        # ordering is not relevant but sort removes duplicates.
534a6ff0067SChris Kay        $(eval TEMP_OBJ_DIRS := $(sort $(dir ${OBJS} ${DEFAULT_LINKER_SCRIPT} ${LINKER_SCRIPTS})))
53551b27702SEvan Lloyd        # The $(dir ) function leaves a trailing / on the directory names
536d014ea6cSMasahiro Yamada        # Rip off the / to match directory names with make rule targets.
537d014ea6cSMasahiro Yamada        $(eval OBJ_DIRS   := $(patsubst %/,%,$(TEMP_OBJ_DIRS)))
53851b27702SEvan Lloyd
53951b27702SEvan Lloyd# Create generators for object directory structure
54051b27702SEvan Lloyd
5418012cc5cSMasahiro Yamada$(eval $(call MAKE_PREREQ_DIR,${BUILD_DIR},${BUILD_PLAT}))
5426ba7d274SEvan Lloyd
54382274936SChris Kay$(eval $(foreach objd,${OBJ_DIRS},
54482274936SChris Kay        $(call MAKE_PREREQ_DIR,${objd},${BUILD_DIR})))
54551b27702SEvan Lloyd
546434d0491SZelalem Aweke.PHONY : ${1}_dirs
54751b27702SEvan Lloyd
54851b27702SEvan Lloyd# We use order-only prerequisites to ensure that directories are created,
54951b27702SEvan Lloyd# but do not cause re-builds every time a file is written.
550434d0491SZelalem Aweke${1}_dirs: | ${OBJ_DIRS}
55173c99d4eSJuan Castillo
55273c99d4eSJuan Castillo$(eval $(call MAKE_OBJS,$(BUILD_DIR),$(SOURCES),$(1)))
553a6ff0067SChris Kay
554a6ff0067SChris Kay# Generate targets to preprocess each required linker script
555a6ff0067SChris Kay$(eval $(foreach source,$(DEFAULT_LINKER_SCRIPT_SOURCE) $(LINKER_SCRIPT_SOURCES), \
556a6ff0067SChris Kay        $(call MAKE_LD,$(call linker_script_path,$(source)),$(source),$(1))))
557a6ff0067SChris Kay
558434d0491SZelalem Aweke$(eval BL_LDFLAGS := $($(call uppercase,$(1))_LDFLAGS))
55973c99d4eSJuan Castillo
5605accce5bSRoberto Vargasifeq ($(USE_ROMLIB),1)
5615accce5bSRoberto Vargas$(ELF): romlib.bin
5625accce5bSRoberto Vargasendif
5635accce5bSRoberto Vargas
5648dccddc7SLeon Chen# MODULE_OBJS can be assigned by vendors with different compiled
5658dccddc7SLeon Chen# object file path, and prebuilt object file path.
5668dccddc7SLeon Chen$(eval OBJS += $(MODULE_OBJS))
5678dccddc7SLeon Chen
568a6ff0067SChris Kay$(ELF): $(OBJS) $(DEFAULT_LINKER_SCRIPT) $(LINKER_SCRIPTS) | $(1)_dirs libraries $(BL_LIBS)
569ee1ba6d4SAndre Przywara	$$(ECHO) "  LD      $$@"
570414ab853SEvan Lloydifdef MAKE_BUILD_STRINGS
571414ab853SEvan Lloyd	$(call MAKE_BUILD_STRINGS,$(BUILD_DIR)/build_message.o)
572414ab853SEvan Lloydelse
5732f5d4a48SPatrick Georgi	@echo 'const char build_message[] = "Built : "$(BUILD_MESSAGE_TIMESTAMP); \
574dddf4283Slaurenw-arm	       const char version_string[] = "${VERSION_STRING}"; \
575dddf4283Slaurenw-arm	       const char version[] = "${VERSION}";' | \
576f2e1d57eSMasahiro Yamada		$$(CC) $$(TF_CFLAGS) $$(CFLAGS) -xc -c - -o $(BUILD_DIR)/build_message.o
577414ab853SEvan Lloydendif
578c2ad38ceSVarun Wadekarifneq ($(findstring armlink,$(notdir $(LD))),)
579434d0491SZelalem Aweke	$$(Q)$$(LD) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $(BL_LDFLAGS) --entry=${1}_entrypoint \
580c2ad38ceSVarun Wadekar		--predefine="-D__LINKER__=$(__LINKER__)" \
581c2ad38ceSVarun Wadekar		--predefine="-DTF_CFLAGS=$(TF_CFLAGS)" \
582434d0491SZelalem Aweke		--map --list="$(MAPFILE)" --scatter=${PLAT_DIR}/scat/${1}.scat \
583c2ad38ceSVarun Wadekar		$(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS) \
584c2ad38ceSVarun Wadekar		$(BUILD_DIR)/build_message.o $(OBJS)
585edbce9aaSzelalem-awekeelse ifneq ($(findstring gcc,$(notdir $(LD))),)
58649ba1df5SAndrey Skvortsov	$$(Q)$$(LD) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $(BL_LDFLAGS) -Wl,-Map=$(MAPFILE) \
587a6ff0067SChris Kay		$(addprefix -Wl$(comma)--script$(comma),$(LINKER_SCRIPTS)) -Wl,--script,$(DEFAULT_LINKER_SCRIPT) \
588a6ff0067SChris Kay		$(BUILD_DIR)/build_message.o \
589edbce9aaSzelalem-aweke		$(OBJS) $(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS)
590c2ad38ceSVarun Wadekarelse
591d986bae4SMasahiro Yamada	$$(Q)$$(LD) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $(BL_LDFLAGS) -Map=$(MAPFILE) \
592a6ff0067SChris Kay		$(addprefix -T ,$(LINKER_SCRIPTS)) --script $(DEFAULT_LINKER_SCRIPT) \
593a6ff0067SChris Kay		$(BUILD_DIR)/build_message.o \
5946baf85b3SSathees Balya		$(OBJS) $(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS)
595c2ad38ceSVarun Wadekarendif
5969e4609f1SChristoph Müllnerifeq ($(DISABLE_BIN_GENERATION),1)
5979e4609f1SChristoph Müllner	@${ECHO_BLANK_LINE}
5989e4609f1SChristoph Müllner	@echo "Built $$@ successfully"
5999e4609f1SChristoph Müllner	@${ECHO_BLANK_LINE}
6009e4609f1SChristoph Müllnerendif
60173c99d4eSJuan Castillo
60273c99d4eSJuan Castillo$(DUMP): $(ELF)
603ee1ba6d4SAndre Przywara	$${ECHO} "  OD      $$@"
60473c99d4eSJuan Castillo	$${Q}$${OD} -dx $$< > $$@
60573c99d4eSJuan Castillo
60673c99d4eSJuan Castillo$(BIN): $(ELF)
607ee1ba6d4SAndre Przywara	$${ECHO} "  BIN     $$@"
60873c99d4eSJuan Castillo	$$(Q)$$(OC) -O binary $$< $$@
609052ab529SEvan Lloyd	@${ECHO_BLANK_LINE}
61073c99d4eSJuan Castillo	@echo "Built $$@ successfully"
611052ab529SEvan Lloyd	@${ECHO_BLANK_LINE}
61273c99d4eSJuan Castillo
613434d0491SZelalem Aweke.PHONY: $(1)
6149e4609f1SChristoph Müllnerifeq ($(DISABLE_BIN_GENERATION),1)
615434d0491SZelalem Aweke$(1): $(ELF) $(DUMP)
6169e4609f1SChristoph Müllnerelse
617434d0491SZelalem Aweke$(1): $(BIN) $(DUMP)
6189e4609f1SChristoph Müllnerendif
61973c99d4eSJuan Castillo
620434d0491SZelalem Awekeall: $(1)
62173c99d4eSJuan Castillo
622c6ba9b45SSumit Gargifeq ($(4),1)
623c6ba9b45SSumit Garg$(call ENCRYPT_FW,$(BIN),$(ENC_BIN))
624434d0491SZelalem Aweke$(if $(2),$(call TOOL_ADD_IMG_PAYLOAD,$(1),$(BIN),--$(2),$(ENC_BIN),$(3), \
625c6ba9b45SSumit Garg		$(ENC_BIN)))
626c6ba9b45SSumit Gargelse
627434d0491SZelalem Aweke$(if $(2),$(call TOOL_ADD_IMG_PAYLOAD,$(1),$(BIN),--$(2),$(BIN),$(3)))
628c6ba9b45SSumit Gargendif
62973c99d4eSJuan Castillo
63073c99d4eSJuan Castilloendef
63173c99d4eSJuan Castillo
63238c14d88SSoby Mathew# Convert device tree source file names to matching blobs
63338c14d88SSoby Mathew#   $(1) = input dts
63403b397a8SNishanth Menondefine SOURCES_TO_DTBS
63503b397a8SNishanth Menon        $(notdir $(patsubst %.dts,%.dtb,$(filter %.dts,$(1))))
63603b397a8SNishanth Menonendef
63703b397a8SNishanth Menon
63838c14d88SSoby Mathew# MAKE_FDT_DIRS macro creates the prerequisite directories that host the
63938c14d88SSoby Mathew# FDT binaries
64038c14d88SSoby Mathew#   $(1) = output directory
64138c14d88SSoby Mathew#   $(2) = input dts
64238c14d88SSoby Mathewdefine MAKE_FDT_DIRS
64338c14d88SSoby Mathew        $(eval DTBS       := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2))))
64403b397a8SNishanth Menon        $(eval TEMP_DTB_DIRS := $(sort $(dir ${DTBS})))
64503b397a8SNishanth Menon        # The $(dir ) function leaves a trailing / on the directory names
64603b397a8SNishanth Menon        # Rip off the / to match directory names with make rule targets.
64703b397a8SNishanth Menon        $(eval DTB_DIRS   := $(patsubst %/,%,$(TEMP_DTB_DIRS)))
64803b397a8SNishanth Menon
64903b397a8SNishanth Menon$(eval $(foreach objd,${DTB_DIRS},$(call MAKE_PREREQ_DIR,${objd},${BUILD_DIR})))
65003b397a8SNishanth Menon
65103b397a8SNishanth Menonfdt_dirs: ${DTB_DIRS}
65203b397a8SNishanth Menonendef
65303b397a8SNishanth Menon
65438c14d88SSoby Mathew# MAKE_DTB generate the Flattened device tree binary
65503b397a8SNishanth Menon#   $(1) = output directory
65603b397a8SNishanth Menon#   $(2) = input dts
65703b397a8SNishanth Menondefine MAKE_DTB
65803b397a8SNishanth Menon
659077b3e9aSYann Gautier# List of DTB file(s) to generate, based on DTS file basename list
66038c14d88SSoby Mathew$(eval DOBJ := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2))))
661077b3e9aSYann Gautier# List of the pre-compiled DTS file(s)
66201d237cbSYann Gautier$(eval DPRE := $(addprefix $(1)/,$(patsubst %.dts,%.pre.dts,$(notdir $(2)))))
663077b3e9aSYann Gautier# Dependencies of the pre-compiled DTS file(s) on its source and included files
664077b3e9aSYann Gautier$(eval DTSDEP := $(patsubst %.dtb,%.o.d,$(DOBJ)))
665077b3e9aSYann Gautier# Dependencies of the DT compilation on its pre-compiled DTS
666077b3e9aSYann Gautier$(eval DTBDEP := $(patsubst %.dtb,%.d,$(DOBJ)))
66703b397a8SNishanth Menon
6686bc1a30cSStephen Warren$(DOBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | fdt_dirs
669ee1ba6d4SAndre Przywara	$${ECHO} "  CPP     $$<"
670077b3e9aSYann Gautier	$(eval DTBS       := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2))))
671781cb314SChris Kay	$$(Q)$$(CPP) $$(DTC_CPPFLAGS) -MT $(DTBS) -MMD -MF $(DTSDEP) -o $(DPRE) $$<
672ee1ba6d4SAndre Przywara	$${ECHO} "  DTC     $$<"
6732d51b55eSBalint Dobszay	$$(Q)$$(DTC) $$(DTC_FLAGS) -d $(DTBDEP) -o $$@ $(DPRE)
67403b397a8SNishanth Menon
675077b3e9aSYann Gautier-include $(DTBDEP)
676077b3e9aSYann Gautier-include $(DTSDEP)
67703b397a8SNishanth Menon
67803b397a8SNishanth Menonendef
67903b397a8SNishanth Menon
68003b397a8SNishanth Menon# MAKE_DTBS builds flattened device tree sources
68103b397a8SNishanth Menon#   $(1) = output directory
68203b397a8SNishanth Menon#   $(2) = list of flattened device tree source files
68303b397a8SNishanth Menondefine MAKE_DTBS
68403b397a8SNishanth Menon        $(eval DOBJS := $(filter %.dts,$(2)))
68503b397a8SNishanth Menon        $(eval REMAIN := $(filter-out %.dts,$(2)))
68638c14d88SSoby Mathew        $(and $(REMAIN),$(error FDT_SOURCES contain non-DTS files: $(REMAIN)))
68703b397a8SNishanth Menon        $(eval $(foreach obj,$(DOBJS),$(call MAKE_DTB,$(1),$(obj))))
68803b397a8SNishanth Menon
68938c14d88SSoby Mathew        $(eval $(call MAKE_FDT_DIRS,$(1),$(2)))
69038c14d88SSoby Mathew
69138c14d88SSoby Mathewdtbs: $(DTBS)
69238c14d88SSoby Mathewall: dtbs
69303b397a8SNishanth Menonendef
694