xref: /rk3399_ARM-atf/make_helpers/build_macros.mk (revision b45fc164162d53ee5f658b896951dae75af4ca54)
173c99d4eSJuan Castillo#
2cbd6cec3SBoyan Karatotev# Copyright (c) 2015-2025, 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# A user defined function to recursively search for a filename below a directory
14231c1470SEvan Lloyd#    $1 is the directory root of the recursive search (blank for current directory).
15231c1470SEvan Lloyd#    $2 is the file name to search for.
16231c1470SEvan Lloyddefine rwildcard
17231c1470SEvan Lloyd$(strip $(foreach d,$(wildcard ${1}*),$(call rwildcard,${d}/,${2}) $(filter $(subst *,%,%${2}),${d})))
18231c1470SEvan Lloydendef
19231c1470SEvan Lloyd
20e444763dSBoyan Karatotev# Convenience function for setting a variable to 0 if not previously set
21e444763dSBoyan Karatotev# $(eval $(call default_zero,FOO))
22e444763dSBoyan Karatotevdefine default_zero
23e444763dSBoyan Karatotev	$(eval $(1) ?= 0)
24e444763dSBoyan Karatotevendef
25e444763dSBoyan Karatotev
26e444763dSBoyan Karatotev# Convenience function for setting a list of variables to 0 if not previously set
27e444763dSBoyan Karatotev# $(eval $(call default_zeros,FOO BAR))
28e444763dSBoyan Karatotevdefine default_zeros
29e444763dSBoyan Karatotev	$(foreach var,$1,$(eval $(call default_zero,$(var))))
30e444763dSBoyan Karatotevendef
31e444763dSBoyan Karatotev
322a71f163SGovindraj Raja# Convenience function for setting a variable to 1 if not previously set
332a71f163SGovindraj Raja# $(eval $(call default_one,FOO))
342a71f163SGovindraj Rajadefine default_one
352a71f163SGovindraj Raja	$(eval $(1) ?= 1)
362a71f163SGovindraj Rajaendef
372a71f163SGovindraj Raja
382a71f163SGovindraj Raja# Convenience function for setting a list of variables to 1 if not previously set
392a71f163SGovindraj Raja# $(eval $(call default_ones,FOO BAR))
402a71f163SGovindraj Rajadefine default_ones
412a71f163SGovindraj Raja	$(foreach var,$1,$(eval $(call default_one,$(var))))
422a71f163SGovindraj Rajaendef
432a71f163SGovindraj Raja
4473c99d4eSJuan Castillo# Convenience function for adding build definitions
4573c99d4eSJuan Castillo# $(eval $(call add_define,FOO)) will have:
4673c99d4eSJuan Castillo# -DFOO if $(FOO) is empty; -DFOO=$(FOO) otherwise
4773c99d4eSJuan Castillodefine add_define
4873c99d4eSJuan Castillo    DEFINES			+=	-D$(1)$(if $(value $(1)),=$(value $(1)),)
4973c99d4eSJuan Castilloendef
5073c99d4eSJuan Castillo
51327131c4SLeonardo Sandoval# Convenience function for addding multiple build definitions
52327131c4SLeonardo Sandoval# $(eval $(call add_defines,FOO BOO))
53327131c4SLeonardo Sandovaldefine add_defines
54327131c4SLeonardo Sandoval    $(foreach def,$1,$(eval $(call add_define,$(def))))
55327131c4SLeonardo Sandovalendef
56327131c4SLeonardo Sandoval
578eadeb4aSSoren Brinkmann# Convenience function for adding build definitions
588eadeb4aSSoren Brinkmann# $(eval $(call add_define_val,FOO,BAR)) will have:
598eadeb4aSSoren Brinkmann# -DFOO=BAR
608eadeb4aSSoren Brinkmanndefine add_define_val
618eadeb4aSSoren Brinkmann    DEFINES			+=	-D$(1)=$(2)
628eadeb4aSSoren Brinkmannendef
638eadeb4aSSoren Brinkmann
6473c99d4eSJuan Castillo# Convenience function for verifying option has a boolean value
6573c99d4eSJuan Castillo# $(eval $(call assert_boolean,FOO)) will assert FOO is 0 or 1
6673c99d4eSJuan Castillodefine assert_boolean
671369fb82SYann Gautier    $(if $($(1)),,$(error $(1) must not be empty))
68be4cd40eSMasahiro Yamada    $(if $(filter-out 0 1,$($1)),$(error $1 must be boolean))
6973c99d4eSJuan Castilloendef
7073c99d4eSJuan Castillo
71327131c4SLeonardo Sandoval# Convenience function for verifying options have boolean values
72327131c4SLeonardo Sandoval# $(eval $(call assert_booleans,FOO BOO)) will assert FOO and BOO for 0 or 1 values
73327131c4SLeonardo Sandovaldefine assert_booleans
74327131c4SLeonardo Sandoval    $(foreach bool,$1,$(eval $(call assert_boolean,$(bool))))
75327131c4SLeonardo Sandovalendef
76327131c4SLeonardo Sandoval
77c877b414SJeenu Viswambharan0-9 := 0 1 2 3 4 5 6 7 8 9
78c877b414SJeenu Viswambharan
79c877b414SJeenu Viswambharan# Function to verify that a given option $(1) contains a numeric value
80c877b414SJeenu Viswambharandefine assert_numeric
81c877b414SJeenu Viswambharan$(if $($(1)),,$(error $(1) must not be empty))
82c877b414SJeenu Viswambharan$(eval __numeric := $($(1)))
83c877b414SJeenu Viswambharan$(foreach d,$(0-9),$(eval __numeric := $(subst $(d),,$(__numeric))))
84c877b414SJeenu Viswambharan$(if $(__numeric),$(error $(1) must be numeric))
85c877b414SJeenu Viswambharanendef
86c877b414SJeenu Viswambharan
87327131c4SLeonardo Sandoval# Convenience function for verifying options have numeric values
88327131c4SLeonardo Sandoval# $(eval $(call assert_numerics,FOO BOO)) will assert FOO and BOO contain numeric values
89327131c4SLeonardo Sandovaldefine assert_numerics
90327131c4SLeonardo Sandoval    $(foreach num,$1,$(eval $(call assert_numeric,$(num))))
91327131c4SLeonardo Sandovalendef
92327131c4SLeonardo Sandoval
93*b45fc164SBoyan Karatotev# Convenience function to check for a given linker option. A call to
94*b45fc164SBoyan Karatotev# $(call ld_option, --no-XYZ) will return --no-XYZ if supported by the linker,
95*b45fc164SBoyan Karatotev# prefixed appropriately for the linker in use
96*b45fc164SBoyan Karatotevld_option = $(call toolchain-ld-option,$(ARCH),$(1))
97a5f09cf7SMarco Felsch
98dea23e24SGovindraj Raja# Convenience function to check for a given compiler option. A call to
99dea23e24SGovindraj Raja# $(call cc_option, --no-XYZ) will return --no-XYZ if supported by the compiler
100316f5c97SBoyan Karatotev# NOTE: consider assigning to an immediately expanded temporary variable before
101316f5c97SBoyan Karatotev# assigning. This is because variables like TF_CFLAGS are recursively expanded
102316f5c97SBoyan Karatotev# and assigning this directly will cause it to be expanded every time the
103316f5c97SBoyan Karatotev# variable is used, potentially thrashing multicore performance.
104dea23e24SGovindraj Rajadefine cc_option
105ffb77421SChris Kay	$(shell if $($(ARCH)-cc) $(1) -c -x c /dev/null -o /dev/null >/dev/null 2>&1; then echo $(1); fi )
106dea23e24SGovindraj Rajaendef
107dea23e24SGovindraj Raja
1088c7b944aSVijayenthiran Subramaniam# CREATE_SEQ is a recursive function to create sequence of numbers from 1 to
1098c7b944aSVijayenthiran Subramaniam# $(2) and assign the sequence to $(1)
1108c7b944aSVijayenthiran Subramaniamdefine CREATE_SEQ
1118c7b944aSVijayenthiran Subramaniam$(if $(word $(2), $($(1))),\
1128c7b944aSVijayenthiran Subramaniam  $(eval $(1) += $(words $($(1))))\
1138c7b944aSVijayenthiran Subramaniam  $(eval $(1) := $(filter-out 0,$($(1)))),\
1148c7b944aSVijayenthiran Subramaniam  $(eval $(1) += $(words $($(1))))\
1158c7b944aSVijayenthiran Subramaniam  $(call CREATE_SEQ,$(1),$(2))\
1168c7b944aSVijayenthiran Subramaniam)
1178c7b944aSVijayenthiran Subramaniamendef
1188c7b944aSVijayenthiran Subramaniam
11973c99d4eSJuan Castillo# IMG_MAPFILE defines the output file describing the memory map corresponding
12073c99d4eSJuan Castillo# to a BL stage
121434d0491SZelalem Aweke#   $(1) = BL stage
12273c99d4eSJuan Castillodefine IMG_MAPFILE
123434d0491SZelalem Aweke    ${BUILD_DIR}/$(1).map
12473c99d4eSJuan Castilloendef
12573c99d4eSJuan Castillo
12673c99d4eSJuan Castillo# IMG_ELF defines the elf file corresponding to a BL stage
127434d0491SZelalem Aweke#   $(1) = BL stage
12873c99d4eSJuan Castillodefine IMG_ELF
129434d0491SZelalem Aweke    ${BUILD_DIR}/$(1).elf
13073c99d4eSJuan Castilloendef
13173c99d4eSJuan Castillo
13273c99d4eSJuan Castillo# IMG_DUMP defines the symbols dump file corresponding to a BL stage
133434d0491SZelalem Aweke#   $(1) = BL stage
13473c99d4eSJuan Castillodefine IMG_DUMP
135434d0491SZelalem Aweke    ${BUILD_DIR}/$(1).dump
13673c99d4eSJuan Castilloendef
13773c99d4eSJuan Castillo
13873c99d4eSJuan Castillo# IMG_BIN defines the default image file corresponding to a BL stage
139434d0491SZelalem Aweke#   $(1) = BL stage
14073c99d4eSJuan Castillodefine IMG_BIN
141434d0491SZelalem Aweke    ${BUILD_PLAT}/$(1).bin
14273c99d4eSJuan Castilloendef
14373c99d4eSJuan Castillo
144c6ba9b45SSumit Garg# IMG_ENC_BIN defines the default encrypted image file corresponding to a
145c6ba9b45SSumit Garg# BL stage
146434d0491SZelalem Aweke#   $(1) = BL stage
147c6ba9b45SSumit Gargdefine IMG_ENC_BIN
148434d0491SZelalem Aweke    ${BUILD_PLAT}/$(1)_enc.bin
149c6ba9b45SSumit Gargendef
150c6ba9b45SSumit Garg
151c6ba9b45SSumit Garg# ENCRYPT_FW invokes enctool to encrypt firmware binary
152c6ba9b45SSumit Garg#   $(1) = input firmware binary
153c6ba9b45SSumit Garg#   $(2) = output encrypted firmware binary
154c6ba9b45SSumit Gargdefine ENCRYPT_FW
155c6ba9b45SSumit Garg$(2): $(1) enctool
1567c4e1eeaSChris Kay	$$(s)echo "  ENC     $$<"
1577c4e1eeaSChris Kay	$$(q)$$(ENCTOOL) $$(ENC_ARGS) -i $$< -o $$@
158c6ba9b45SSumit Gargendef
159c6ba9b45SSumit Garg
16010cea934SMasahiro Yamada# TOOL_ADD_PAYLOAD appends the command line arguments required by fiptool to
16110cea934SMasahiro Yamada# package a new payload and/or by cert_create to generate certificate.
16210cea934SMasahiro Yamada# Optionally, it adds the dependency on this payload
16373c99d4eSJuan Castillo#   $(1) = payload filename (i.e. bl31.bin)
1641e0c0784SMasahiro Yamada#   $(2) = command line option for the specified payload (i.e. --soc-fw)
16536af3455SMasahiro Yamada#   $(3) = tool target dependency (optional) (ex. build/fvp/release/bl31.bin)
1661dc0714fSMasahiro Yamada#   $(4) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
167c6ba9b45SSumit Garg#   $(5) = encrypted payload (optional) (ex. build/fvp/release/bl31_enc.bin)
16810cea934SMasahiro Yamadadefine TOOL_ADD_PAYLOAD
169c6ba9b45SSumit Gargifneq ($(5),)
170c6ba9b45SSumit Garg    $(4)FIP_ARGS += $(2) $(5)
171c6ba9b45SSumit Garg    $(if $(3),$(4)CRT_DEPS += $(1))
172c6ba9b45SSumit Gargelse
173945b316fSMasahiro Yamada    $(4)FIP_ARGS += $(2) $(1)
174c6ba9b45SSumit Garg    $(if $(3),$(4)CRT_DEPS += $(3))
175c6ba9b45SSumit Gargendif
176945b316fSMasahiro Yamada    $(if $(3),$(4)FIP_DEPS += $(3))
177f30ee0b9SMasahiro Yamada    $(4)CRT_ARGS += $(2) $(1)
17873c99d4eSJuan Castilloendef
17973c99d4eSJuan Castillo
1802da522bbSMasahiro Yamada# TOOL_ADD_IMG_PAYLOAD works like TOOL_ADD_PAYLOAD, but applies image filters
1812da522bbSMasahiro Yamada# before passing them to host tools if BL*_PRE_TOOL_FILTER is defined.
1822da522bbSMasahiro Yamada#   $(1) = image_type (scp_bl2, bl33, etc.)
1832da522bbSMasahiro Yamada#   $(2) = payload filepath (ex. build/fvp/release/bl31.bin)
1842da522bbSMasahiro Yamada#   $(3) = command line option for the specified payload (ex. --soc-fw)
1852da522bbSMasahiro Yamada#   $(4) = tool target dependency (optional) (ex. build/fvp/release/bl31.bin)
1862da522bbSMasahiro Yamada#   $(5) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
187c6ba9b45SSumit Garg#   $(6) = encrypted payload (optional) (ex. build/fvp/release/bl31_enc.bin)
1882da522bbSMasahiro Yamada
1892da522bbSMasahiro Yamadadefine TOOL_ADD_IMG_PAYLOAD
1902da522bbSMasahiro Yamada
191f7a41fb4SBoyan Karatotev$(eval PRE_TOOL_FILTER := $($(1)_PRE_TOOL_FILTER))
1922da522bbSMasahiro Yamada
1932da522bbSMasahiro Yamadaifneq ($(PRE_TOOL_FILTER),)
1942da522bbSMasahiro Yamada
1952da522bbSMasahiro Yamada$(eval PROCESSED_PATH := $(BUILD_PLAT)/$(1).bin$($(PRE_TOOL_FILTER)_SUFFIX))
1962da522bbSMasahiro Yamada
1972da522bbSMasahiro Yamada$(call $(PRE_TOOL_FILTER)_RULE,$(PROCESSED_PATH),$(2))
1982da522bbSMasahiro Yamada
1992da522bbSMasahiro Yamada$(PROCESSED_PATH): $(4)
2002da522bbSMasahiro Yamada
201c6ba9b45SSumit Garg$(call TOOL_ADD_PAYLOAD,$(PROCESSED_PATH),$(3),$(PROCESSED_PATH),$(5),$(6))
2022da522bbSMasahiro Yamada
2032da522bbSMasahiro Yamadaelse
204c6ba9b45SSumit Garg$(call TOOL_ADD_PAYLOAD,$(2),$(3),$(4),$(5),$(6))
2052da522bbSMasahiro Yamadaendif
2062da522bbSMasahiro Yamadaendef
2072da522bbSMasahiro Yamada
2080191262dSYatharth Kochar# CERT_ADD_CMD_OPT adds a new command line option to the cert_create invocation
20973c99d4eSJuan Castillo#   $(1) = parameter filename
21073c99d4eSJuan Castillo#   $(2) = cert_create command line option for the specified parameter
21191704d9dSMasahiro Yamada#   $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
21273c99d4eSJuan Castillodefine CERT_ADD_CMD_OPT
21391704d9dSMasahiro Yamada    $(3)CRT_ARGS += $(2) $(1)
21473c99d4eSJuan Castilloendef
21573c99d4eSJuan Castillo
216c939d13aSMasahiro Yamada# TOOL_ADD_IMG allows the platform to specify an external image to be packed
217c939d13aSMasahiro Yamada# in the FIP and/or for which certificate is generated. It also adds a
218c939d13aSMasahiro Yamada# dependency on the image file, aborting the build if the file does not exist.
21933950dd8SMasahiro Yamada#   $(1) = image_type (scp_bl2, bl33, etc.)
2201e0c0784SMasahiro Yamada#   $(2) = command line option for fiptool (--scp-fw, --nt-fw, etc)
2211dc0714fSMasahiro Yamada#   $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
222c6ba9b45SSumit Garg#   $(4) = Image encryption flag (optional) (0, 1)
22373c99d4eSJuan Castillo# Example:
22433950dd8SMasahiro Yamada#   $(eval $(call TOOL_ADD_IMG,bl33,--nt-fw))
225c939d13aSMasahiro Yamadadefine TOOL_ADD_IMG
22633950dd8SMasahiro Yamada    # Build option to specify the image filename (SCP_BL2, BL33, etc)
22733950dd8SMasahiro Yamada    # This is the uppercase form of the first parameter
228f7a41fb4SBoyan Karatotev    $(eval BL := $(call uppercase,$(1)))
229f7a41fb4SBoyan Karatotev    $(eval _V := $(BL))
23033950dd8SMasahiro Yamada
2314727fd13SPali Rohár    # $(check_$(1)_cmd) variable is executed in the check_$(1) target and also
2324727fd13SPali Rohár    # is put into the ${CHECK_$(3)FIP_CMD} variable which is executed by the
2334727fd13SPali Rohár    # target ${BUILD_PLAT}/${$(3)FIP_NAME}.
2344727fd13SPali Rohár    $(eval check_$(1)_cmd := \
2354727fd13SPali Rohár        $(if $(value $(_V)),,$$$$(error "Platform '${PLAT}' requires $(_V). Please set $(_V) to point to the right file")) \
2364727fd13SPali Rohár        $(if $(wildcard $(value $(_V))),,$$$$(error '$(_V)=$(value $(_V))' was specified, but '$(value $(_V))' does not exist)) \
2374727fd13SPali Rohár    )
2384727fd13SPali Rohár
239945b316fSMasahiro Yamada    $(3)CRT_DEPS += check_$(1)
2404727fd13SPali Rohár    CHECK_$(3)FIP_CMD += $$(check_$(1)_cmd)
241c6ba9b45SSumit Gargifeq ($(4),1)
242c6ba9b45SSumit Garg    $(eval ENC_BIN := ${BUILD_PLAT}/$(1)_enc.bin)
243c6ba9b45SSumit Garg    $(call ENCRYPT_FW,$(value $(_V)),$(ENC_BIN))
244f7a41fb4SBoyan Karatotev    $(call TOOL_ADD_IMG_PAYLOAD,$(BL),$(value $(_V)),$(2),$(ENC_BIN),$(3), \
245c6ba9b45SSumit Garg		$(ENC_BIN))
246c6ba9b45SSumit Gargelse
247f7a41fb4SBoyan Karatotev    $(call TOOL_ADD_IMG_PAYLOAD,$(BL),$(value $(_V)),$(2),$(if $(wildcard $(value $(_V))),$(value $(_V)),FORCE),$(3))
248c6ba9b45SSumit Gargendif
2490191262dSYatharth Kochar
25087ebd20dSMasahiro Yamada.PHONY: check_$(1)
2510191262dSYatharth Kocharcheck_$(1):
2524727fd13SPali Rohár	$(check_$(1)_cmd)
2530191262dSYatharth Kocharendef
25473c99d4eSJuan Castillo
255cf2dd17dSJuan Pablo Conde# SELECT_OPENSSL_API_VERSION selects the OpenSSL API version to be used to
256cf2dd17dSJuan Pablo Conde# build the host tools by checking the version of OpenSSL located under
257cf2dd17dSJuan Pablo Conde# the path defined by the OPENSSL_DIR variable. It receives no parameters.
258cf2dd17dSJuan Pablo Condedefine SELECT_OPENSSL_API_VERSION
259cf2dd17dSJuan Pablo Conde    # Set default value for USING_OPENSSL3 macro to 0
260cf2dd17dSJuan Pablo Conde    $(eval USING_OPENSSL3 = 0)
261cf2dd17dSJuan Pablo Conde    # Obtain the OpenSSL version for the build located under OPENSSL_DIR
262cf2dd17dSJuan Pablo Conde    $(eval OPENSSL_INFO := $(shell LD_LIBRARY_PATH=${OPENSSL_DIR}:${OPENSSL_DIR}/lib ${OPENSSL_BIN_PATH}/openssl version))
263cf2dd17dSJuan Pablo Conde    $(eval OPENSSL_CURRENT_VER = $(word 2, ${OPENSSL_INFO}))
264cf2dd17dSJuan Pablo Conde    $(eval OPENSSL_CURRENT_VER_MAJOR = $(firstword $(subst ., ,$(OPENSSL_CURRENT_VER))))
265cf2dd17dSJuan Pablo Conde    # If OpenSSL version is 3.x, then set USING_OPENSSL3 flag to 1
266cf2dd17dSJuan Pablo Conde    $(if $(filter 3,$(OPENSSL_CURRENT_VER_MAJOR)), $(eval USING_OPENSSL3 = 1))
267cf2dd17dSJuan Pablo Condeendef
268cf2dd17dSJuan Pablo Conde
26973c99d4eSJuan Castillo################################################################################
27014db8908SMasahiro Yamada# Generic image processing filters
27114db8908SMasahiro Yamada################################################################################
27214db8908SMasahiro Yamada
27314db8908SMasahiro Yamada# GZIP
27414db8908SMasahiro Yamadadefine GZIP_RULE
27514db8908SMasahiro Yamada$(1): $(2)
2767c4e1eeaSChris Kay	$(s)echo "  GZIP    $$@"
2777c4e1eeaSChris Kay	$(q)gzip -n -f -9 $$< --stdout > $$@
27814db8908SMasahiro Yamadaendef
27914db8908SMasahiro Yamada
28014db8908SMasahiro YamadaGZIP_SUFFIX := .gz
28114db8908SMasahiro Yamada
28214db8908SMasahiro Yamada################################################################################
28373c99d4eSJuan Castillo# Auxiliary macros to build TF images from sources
28473c99d4eSJuan Castillo################################################################################
28573c99d4eSJuan Castillo
286a7bbd8e7SChris KayMAKE_DEP = -Wp,-MD,$1 -MT $2 -MP
28773c99d4eSJuan Castillo
288cbd6cec3SBoyan Karatotev# MAKE_TOOL_C builds a C source file and generates the dependency file
289cbd6cec3SBoyan Karatotev#   $(1) = output directory
290cbd6cec3SBoyan Karatotev#   $(2) = source file (%.c)
291cbd6cec3SBoyan Karatotev#   $(3) = lowercase name of the tool
292cbd6cec3SBoyan Karatotev#   $(4) = uppercase name of the tool
293cbd6cec3SBoyan Karatotevdefine MAKE_TOOL_C
294cbd6cec3SBoyan Karatotev
295cbd6cec3SBoyan Karatotev$(eval SRC := $(2))
296cbd6cec3SBoyan Karatotev$(eval OBJ := $(patsubst %.c,$(1)/$(3)/%.o,$(SRC)))
297cbd6cec3SBoyan Karatotev$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
298cbd6cec3SBoyan Karatotev
299cbd6cec3SBoyan Karatotev$(eval TOOL_DEFINES := $($(4)_DEFINES))
300cbd6cec3SBoyan Karatotev$(eval TOOL_INCLUDE_DIRS := $($(4)_INCLUDE_DIRS))
301cbd6cec3SBoyan Karatotev$(eval TOOL_CPPFLAGS := $($(4)_CPPFLAGS) $(addprefix -D,$(TOOL_DEFINES)) $(addprefix -I,$(TOOL_INCLUDE_DIRS)))
302cbd6cec3SBoyan Karatotev$(eval TOOL_CFLAGS := $($(4)_CFLAGS))
303cbd6cec3SBoyan Karatotev
304cbd6cec3SBoyan Karatotev$(OBJ): $(SRC) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
305cbd6cec3SBoyan Karatotev	$$(s)echo "  HOSTCC      $$<"
306cbd6cec3SBoyan Karatotev	$$(q)$(host-cc) $$(HOSTCCFLAGS) $(TOOL_CPPFLAGS) $(TOOL_CFLAGS) $(call MAKE_DEP,$(DEP),$(OBJ)) -c $$< -o $$@
307cbd6cec3SBoyan Karatotev
308cbd6cec3SBoyan Karatotev-include $(DEP)
309cbd6cec3SBoyan Karatotev
310cbd6cec3SBoyan Karatotevendef
311cbd6cec3SBoyan Karatotev
312cbd6cec3SBoyan Karatotev# MAKE_TOOL
313cbd6cec3SBoyan Karatotev#   $(1) = output directory
314cbd6cec3SBoyan Karatotev#   $(2) = lowercase name of the tool
315cbd6cec3SBoyan Karatotev#   $(3) = uppercase name of the tool
316cbd6cec3SBoyan Karatotevdefine MAKE_TOOL
317cbd6cec3SBoyan Karatotev$(eval SRCS := $($(3)_SOURCES))
318cbd6cec3SBoyan Karatotev$(eval OBJS := $(patsubst %.c,$(1)/$(2)/%.o,$(SRCS)))
319cbd6cec3SBoyan Karatotev$(eval DST := $(1)/$(2)/$(2)$(.exe))
320cbd6cec3SBoyan Karatotev$(eval $(foreach src,$(SRCS),$(call MAKE_TOOL_C,$(1),$(src),$(2),$(3))))
321cbd6cec3SBoyan Karatotev
322cbd6cec3SBoyan Karatotev$(DST): $(OBJS) $(filter-out %.d,$(MAKEFILE_LIST))
323cbd6cec3SBoyan Karatotev	$$(s)echo "  HOSTLD  $$@"
324cbd6cec3SBoyan Karatotev	$$(q)$(host-cc) $${OBJS} -o $$@ $($(3)_LDFLAGS)
325cbd6cec3SBoyan Karatotev	$$(s)echo
326cbd6cec3SBoyan Karatotev	$$(s)echo "Built $$@ successfully"
327cbd6cec3SBoyan Karatotev	$$(s)echo
328cbd6cec3SBoyan Karatotev
329cbd6cec3SBoyan Karatotevall: $(DST)
330cbd6cec3SBoyan Karatotevendef
3315fee0287SRoberto Vargas
3325fee0287SRoberto Vargas# MAKE_C_LIB builds a C source file and generates the dependency file
3335fee0287SRoberto Vargas#   $(1) = output directory
3345fee0287SRoberto Vargas#   $(2) = source file (%.c)
3355fee0287SRoberto Vargas#   $(3) = library name
336f7a41fb4SBoyan Karatotev#   $(4) = uppercase name of the library
3375fee0287SRoberto Vargasdefine MAKE_C_LIB
3385fee0287SRoberto Vargas$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
3395fee0287SRoberto Vargas$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
340f7a41fb4SBoyan Karatotev$(eval LIB := $(notdir $(1)))
3415fee0287SRoberto Vargas
342f4dd18c2SChris Kay$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
3437c4e1eeaSChris Kay	$$(s)echo "  CC      $$<"
344d8a23ecdSSlava Andrianov	$$(q)$($(ARCH)-cc) $$(LIB$(4)_CFLAGS) $$(TF_CFLAGS) $(call MAKE_DEP,$(DEP),$(OBJ)) -c $$< -o $$@
3455fee0287SRoberto Vargas
3465fee0287SRoberto Vargas-include $(DEP)
3475fee0287SRoberto Vargas
3485fee0287SRoberto Vargasendef
3495fee0287SRoberto Vargas
35070b0f278SAntonio Nino Diaz# MAKE_S_LIB builds an assembly source file and generates the dependency file
35170b0f278SAntonio Nino Diaz#   $(1) = output directory
35270b0f278SAntonio Nino Diaz#   $(2) = source file (%.S)
35370b0f278SAntonio Nino Diaz#   $(3) = library name
354f7a41fb4SBoyan Karatotev#   $(4) = uppercase name of the library
35570b0f278SAntonio Nino Diazdefine MAKE_S_LIB
35670b0f278SAntonio Nino Diaz$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
35770b0f278SAntonio Nino Diaz$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
35870b0f278SAntonio Nino Diaz
359f4dd18c2SChris Kay$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
3607c4e1eeaSChris Kay	$$(s)echo "  AS      $$<"
3617416eb2fSBoyan Karatotev	$$(q)$($(ARCH)-as) -x assembler-with-cpp $$(TF_CFLAGS) $$(ASFLAGS) $(call MAKE_DEP,$(DEP),$(OBJ)) -c $$< -o $$@
36270b0f278SAntonio Nino Diaz
36370b0f278SAntonio Nino Diaz-include $(DEP)
36470b0f278SAntonio Nino Diaz
36570b0f278SAntonio Nino Diazendef
36670b0f278SAntonio Nino Diaz
3675fee0287SRoberto Vargas
36873c99d4eSJuan Castillo# MAKE_C builds a C source file and generates the dependency file
36973c99d4eSJuan Castillo#   $(1) = output directory
37073c99d4eSJuan Castillo#   $(2) = source file (%.c)
371434d0491SZelalem Aweke#   $(3) = BL stage
372f7a41fb4SBoyan Karatotev#   $(4) = uppercase BL stage
37373c99d4eSJuan Castillodefine MAKE_C
37473c99d4eSJuan Castillo
37573c99d4eSJuan Castillo$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
376710ea1d0SMasahiro Yamada$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
377a123cb14SChris Kay
378500927efSBoyan Karatotev$(eval BL_DEFINES := IMAGE_$(4) $($(4)_DEFINES))
379500927efSBoyan Karatotev$(eval BL_INCLUDE_DIRS := $($(4)_INCLUDE_DIRS))
380500927efSBoyan Karatotev$(eval BL_CPPFLAGS := $($(4)_CPPFLAGS) $(addprefix -D,$(BL_DEFINES)) $(addprefix -I,$(BL_INCLUDE_DIRS)))
381500927efSBoyan Karatotev$(eval BL_CFLAGS := $($(4)_CFLAGS))
38273c99d4eSJuan Castillo
383f4dd18c2SChris Kay$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
3847c4e1eeaSChris Kay	$$(s)echo "  CC      $$<"
3856bb9f053SBoyan Karatotev	$$(q)$($(ARCH)-cc) $$(LTO_CFLAGS) $$(TF_CFLAGS) $(BL_CPPFLAGS) $(BL_CFLAGS) $(call MAKE_DEP,$(DEP),$(OBJ)) -c $$< -o $$@
38673c99d4eSJuan Castillo
387710ea1d0SMasahiro Yamada-include $(DEP)
38873c99d4eSJuan Castillo
38973c99d4eSJuan Castilloendef
39073c99d4eSJuan Castillo
39173c99d4eSJuan Castillo
39273c99d4eSJuan Castillo# MAKE_S builds an assembly source file and generates the dependency file
39373c99d4eSJuan Castillo#   $(1) = output directory
39473c99d4eSJuan Castillo#   $(2) = assembly file (%.S)
395434d0491SZelalem Aweke#   $(3) = BL stage
396f7a41fb4SBoyan Karatotev#   $(4) = uppercase BL stage
39773c99d4eSJuan Castillodefine MAKE_S
39873c99d4eSJuan Castillo
39973c99d4eSJuan Castillo$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
400710ea1d0SMasahiro Yamada$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
401a123cb14SChris Kay
402500927efSBoyan Karatotev$(eval BL_DEFINES := IMAGE_$(4) $($(4)_DEFINES))
403500927efSBoyan Karatotev$(eval BL_INCLUDE_DIRS := $($(4)_INCLUDE_DIRS))
404500927efSBoyan Karatotev$(eval BL_CPPFLAGS := $($(4)_CPPFLAGS) $(addprefix -D,$(BL_DEFINES)) $(addprefix -I,$(BL_INCLUDE_DIRS)))
405500927efSBoyan Karatotev$(eval BL_ASFLAGS := $($(4)_ASFLAGS))
40673c99d4eSJuan Castillo
407f4dd18c2SChris Kay$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
4087c4e1eeaSChris Kay	$$(s)echo "  AS      $$<"
4097416eb2fSBoyan Karatotev	$$(q)$($(ARCH)-as) -x assembler-with-cpp $$(TF_CFLAGS) $$(ASFLAGS) $(BL_CPPFLAGS) $(BL_ASFLAGS) $(call MAKE_DEP,$(DEP),$(OBJ)) -c $$< -o $$@
41073c99d4eSJuan Castillo
411710ea1d0SMasahiro Yamada-include $(DEP)
41273c99d4eSJuan Castillo
41373c99d4eSJuan Castilloendef
41473c99d4eSJuan Castillo
4151e8b5354SBoyan Karatotev# MAKE_PRE run the C preprocessor on a file
4161e8b5354SBoyan Karatotev#   $(1) = output file
4171e8b5354SBoyan Karatotev#   $(2) = list of input files
4181e8b5354SBoyan Karatotev#   $(3) = dep file
4191e8b5354SBoyan Karatotev#   $(4) = list of rule-specific flags to pass
4201e8b5354SBoyan Karatotevdefine MAKE_PRE
4211e8b5354SBoyan Karatotev$(eval OUT := $(1))
4221e8b5354SBoyan Karatotev$(eval SRC := $(2))
4231e8b5354SBoyan Karatotev$(eval DEP := $(3))
4241e8b5354SBoyan Karatotev$(eval CUSTOM_FLAGS := $(4))
4251e8b5354SBoyan Karatotev$(OUT): $(SRC) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
4261e8b5354SBoyan Karatotev	$$(s)echo "  CPP     $$<"
4271e8b5354SBoyan Karatotev	$$(q)$($(ARCH)-cpp) -E -P -x assembler-with-cpp $$(TF_CFLAGS) $(CUSTOM_FLAGS) $(call MAKE_DEP,$(DEP),$(OUT)) -o $$@ $$<
4281e8b5354SBoyan Karatotevendef
42973c99d4eSJuan Castillo
43073c99d4eSJuan Castillo# MAKE_LD generate the linker script using the C preprocessor
43173c99d4eSJuan Castillo#   $(1) = output linker script
43273c99d4eSJuan Castillo#   $(2) = input template
433434d0491SZelalem Aweke#   $(3) = BL stage
434f7a41fb4SBoyan Karatotev#   $(4) = uppercase BL stage
43573c99d4eSJuan Castillodefine MAKE_LD
43673c99d4eSJuan Castillo
437710ea1d0SMasahiro Yamada$(eval DEP := $(1).d)
438a123cb14SChris Kay
439500927efSBoyan Karatotev$(eval BL_DEFINES := IMAGE_$(4) $($(4)_DEFINES))
440500927efSBoyan Karatotev$(eval BL_INCLUDE_DIRS := $($(4)_INCLUDE_DIRS))
441500927efSBoyan Karatotev$(eval BL_CPPFLAGS := $($(4)_CPPFLAGS) $(addprefix -D,$(BL_DEFINES)) $(addprefix -I,$(BL_INCLUDE_DIRS)))
4421e8b5354SBoyan Karatotev$(eval FLAGS := -D__LINKER__ $(BL_CPPFLAGS))
44373c99d4eSJuan Castillo
4441e8b5354SBoyan Karatotev$(eval $(call MAKE_PRE,$(1),$(2),$(DEP),$(FLAGS)))
445710ea1d0SMasahiro Yamada-include $(DEP)
44673c99d4eSJuan Castillo
44773c99d4eSJuan Castilloendef
44873c99d4eSJuan Castillo
44970b0f278SAntonio Nino Diaz# MAKE_LIB_OBJS builds both C and assembly source files
4505fee0287SRoberto Vargas#   $(1) = output directory
4515fee0287SRoberto Vargas#   $(2) = list of source files
4525fee0287SRoberto Vargas#   $(3) = name of the library
453f7a41fb4SBoyan Karatotev#   $(4) = uppercase name of the library
4545fee0287SRoberto Vargasdefine MAKE_LIB_OBJS
4555fee0287SRoberto Vargas        $(eval C_OBJS := $(filter %.c,$(2)))
4565fee0287SRoberto Vargas        $(eval REMAIN := $(filter-out %.c,$(2)))
457f7a41fb4SBoyan Karatotev        $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C_LIB,$(1),$(obj),$(3),$(4))))
4585fee0287SRoberto Vargas
45970b0f278SAntonio Nino Diaz        $(eval S_OBJS := $(filter %.S,$(REMAIN)))
46070b0f278SAntonio Nino Diaz        $(eval REMAIN := $(filter-out %.S,$(REMAIN)))
461f7a41fb4SBoyan Karatotev        $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S_LIB,$(1),$(obj),$(3),$(4))))
46270b0f278SAntonio Nino Diaz
4635fee0287SRoberto Vargas        $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN)))
4645fee0287SRoberto Vargasendef
4655fee0287SRoberto Vargas
46673c99d4eSJuan Castillo
46773c99d4eSJuan Castillo# MAKE_OBJS builds both C and assembly source files
46873c99d4eSJuan Castillo#   $(1) = output directory
46973c99d4eSJuan Castillo#   $(2) = list of source files (both C and assembly)
470434d0491SZelalem Aweke#   $(3) = BL stage
471f7a41fb4SBoyan Karatotev#   $(4) = uppercase BL stage
47273c99d4eSJuan Castillodefine MAKE_OBJS
47373c99d4eSJuan Castillo        $(eval C_OBJS := $(filter %.c,$(2)))
47473c99d4eSJuan Castillo        $(eval REMAIN := $(filter-out %.c,$(2)))
475f7a41fb4SBoyan Karatotev        $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj),$(3),$(4))))
47673c99d4eSJuan Castillo
47773c99d4eSJuan Castillo        $(eval S_OBJS := $(filter %.S,$(REMAIN)))
47873c99d4eSJuan Castillo        $(eval REMAIN := $(filter-out %.S,$(REMAIN)))
479f7a41fb4SBoyan Karatotev        $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj),$(3),$(4))))
48073c99d4eSJuan Castillo
48173c99d4eSJuan Castillo        $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN)))
48273c99d4eSJuan Castilloendef
48373c99d4eSJuan Castillo
48473c99d4eSJuan Castillo
48573c99d4eSJuan Castillo# NOTE: The line continuation '\' is required in the next define otherwise we
48673c99d4eSJuan Castillo# end up with a line-feed characer at the end of the last c filename.
487e7f54dbdSEvan Lloyd# Also bear this issue in mind if extending the list of supported filetypes.
48873c99d4eSJuan Castillodefine SOURCES_TO_OBJS
48973c99d4eSJuan Castillo        $(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \
49073c99d4eSJuan Castillo        $(notdir $(patsubst %.S,%.o,$(filter %.S,$(1))))
49173c99d4eSJuan Castilloendef
49273c99d4eSJuan Castillo
4935fee0287SRoberto Vargas.PHONY: libraries
4945fee0287SRoberto Vargas
4955fee0287SRoberto Vargas# MAKE_LIB macro defines the targets and options to build each BL image.
4965fee0287SRoberto Vargas# Arguments:
4975fee0287SRoberto Vargas#   $(1) = Library name
4985fee0287SRoberto Vargasdefine MAKE_LIB
499f7a41fb4SBoyan Karatotev        $(eval BL         := $(call uppercase,$(1)))
5005fee0287SRoberto Vargas        $(eval BUILD_DIR  := ${BUILD_PLAT}/lib$(1))
5015fee0287SRoberto Vargas        $(eval LIB_DIR    := ${BUILD_PLAT}/lib)
5025accce5bSRoberto Vargas        $(eval ROMLIB_DIR    := ${BUILD_PLAT}/romlib)
503f7a41fb4SBoyan Karatotev        $(eval SOURCES    := $(LIB$(BL)_SRCS))
5045fee0287SRoberto Vargas        $(eval OBJS       := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES))))
5055fee0287SRoberto Vargas
506f7a41fb4SBoyan Karatotev$(eval $(call MAKE_LIB_OBJS,$(BUILD_DIR),$(SOURCES),$(1),$(BL)))
5075fee0287SRoberto Vargas
5085fee0287SRoberto Vargaslibraries: ${LIB_DIR}/lib$(1).a
5098620bd0bSChris Kayifeq ($($(ARCH)-ld-id),arm-link)
510c2ad38ceSVarun WadekarLDPATHS = --userlibpath=${LIB_DIR}
511c2ad38ceSVarun WadekarLDLIBS += --library=$(1)
512c2ad38ceSVarun Wadekarelse
5135fee0287SRoberto VargasLDPATHS = -L${LIB_DIR}
5145fee0287SRoberto VargasLDLIBS += -l$(1)
515c2ad38ceSVarun Wadekarendif
5165fee0287SRoberto Vargas
5175accce5bSRoberto Vargasifeq ($(USE_ROMLIB),1)
5186baf85b3SSathees BalyaLIBWRAPPER = -lwrappers
5195accce5bSRoberto Vargasendif
5205accce5bSRoberto Vargas
5215fee0287SRoberto Vargasall: ${LIB_DIR}/lib$(1).a
5225fee0287SRoberto Vargas
523f4dd18c2SChris Kay${LIB_DIR}/lib$(1).a: $(OBJS) | $$$$(@D)/
5247c4e1eeaSChris Kay	$$(s)echo "  AR      $$@"
5257c4e1eeaSChris Kay	$$(q)$($(ARCH)-ar) cr $$@ $$?
5265fee0287SRoberto Vargasendef
5275fee0287SRoberto Vargas
52882274936SChris Kay# Generate the path to one or more preprocessed linker scripts given the paths
52982274936SChris Kay# of their sources.
53082274936SChris Kay#
53182274936SChris Kay# Arguments:
53282274936SChris Kay#   $(1) = path to one or more linker script sources
53382274936SChris Kaydefine linker_script_path
53482274936SChris Kay        $(patsubst %.S,$(BUILD_DIR)/%,$(1))
53582274936SChris Kayendef
53682274936SChris Kay
537d95d56bdSJimmy Brissonifeq ($(USE_ROMLIB),1)
538d95d56bdSJimmy BrissonWRAPPER_FLAGS := @${BUILD_PLAT}/romlib/romlib.ldflags
539d95d56bdSJimmy Brissonendif
540d95d56bdSJimmy Brisson
54173c99d4eSJuan Castillo# MAKE_BL macro defines the targets and options to build each BL image.
54273c99d4eSJuan Castillo# Arguments:
543434d0491SZelalem Aweke#   $(1) = BL stage
5448f0617efSJuan Castillo#   $(2) = FIP command line option (if empty, image will not be included in the FIP)
5451dc0714fSMasahiro Yamada#   $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
546c6ba9b45SSumit Garg#   $(4) = BL encryption flag (optional) (0, 1)
54773c99d4eSJuan Castillodefine MAKE_BL
548f7a41fb4SBoyan Karatotev        $(eval BL         := $(call uppercase,$(1)))
549434d0491SZelalem Aweke        $(eval BUILD_DIR  := ${BUILD_PLAT}/$(1))
550f7a41fb4SBoyan Karatotev        $(eval BL_SOURCES := $($(BL)_SOURCES))
551bb22fb84SChris Kay        $(eval SOURCES    := $(sort $(BL_SOURCES) $(BL_COMMON_SOURCES) $(PLAT_BL_COMMON_SOURCES)))
55273c99d4eSJuan Castillo        $(eval OBJS       := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES))))
55373c99d4eSJuan Castillo        $(eval MAPFILE    := $(call IMG_MAPFILE,$(1)))
55473c99d4eSJuan Castillo        $(eval ELF        := $(call IMG_ELF,$(1)))
55573c99d4eSJuan Castillo        $(eval DUMP       := $(call IMG_DUMP,$(1)))
55673c99d4eSJuan Castillo        $(eval BIN        := $(call IMG_BIN,$(1)))
557c6ba9b45SSumit Garg        $(eval ENC_BIN    := $(call IMG_ENC_BIN,$(1)))
558f7a41fb4SBoyan Karatotev        $(eval BL_LIBS    := $($(BL)_LIBS))
55982274936SChris Kay
560f7a41fb4SBoyan Karatotev        $(eval DEFAULT_LINKER_SCRIPT_SOURCE := $($(BL)_DEFAULT_LINKER_SCRIPT_SOURCE))
56182274936SChris Kay        $(eval DEFAULT_LINKER_SCRIPT := $(call linker_script_path,$(DEFAULT_LINKER_SCRIPT_SOURCE)))
56282274936SChris Kay
563f7a41fb4SBoyan Karatotev        $(eval LINKER_SCRIPT_SOURCES := $($(BL)_LINKER_SCRIPT_SOURCES))
564a6ff0067SChris Kay        $(eval LINKER_SCRIPTS := $(call linker_script_path,$(LINKER_SCRIPT_SOURCES)))
565a6ff0067SChris Kay
566f7a41fb4SBoyan Karatotev$(eval $(call MAKE_OBJS,$(BUILD_DIR),$(SOURCES),$(1),$(BL)))
567a6ff0067SChris Kay
568a6ff0067SChris Kay# Generate targets to preprocess each required linker script
569a6ff0067SChris Kay$(eval $(foreach source,$(DEFAULT_LINKER_SCRIPT_SOURCE) $(LINKER_SCRIPT_SOURCES), \
570f7a41fb4SBoyan Karatotev        $(call MAKE_LD,$(call linker_script_path,$(source)),$(source),$(1),$(BL))))
571a6ff0067SChris Kay
572f7a41fb4SBoyan Karatotev$(eval BL_LDFLAGS := $($(BL)_LDFLAGS))
57373c99d4eSJuan Castillo
5745accce5bSRoberto Vargasifeq ($(USE_ROMLIB),1)
5756e622818SChris Kay$(ELF): $(BUILD_PLAT)/romlib/romlib.bin | $$$$(@D)/
5765accce5bSRoberto Vargasendif
5775accce5bSRoberto Vargas
5788dccddc7SLeon Chen# MODULE_OBJS can be assigned by vendors with different compiled
5798dccddc7SLeon Chen# object file path, and prebuilt object file path.
5808dccddc7SLeon Chen$(eval OBJS += $(MODULE_OBJS))
5818dccddc7SLeon Chen
582f4dd18c2SChris Kay$(ELF): $(OBJS) $(DEFAULT_LINKER_SCRIPT) $(LINKER_SCRIPTS) | $$$$(@D)/ libraries $(BL_LIBS)
5837c4e1eeaSChris Kay	$$(s)echo "  LD      $$@"
5848620bd0bSChris Kayifeq ($($(ARCH)-ld-id),arm-link)
5857c4e1eeaSChris Kay	$$(q)$($(ARCH)-ld) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $(BL_LDFLAGS) --entry=${1}_entrypoint \
586df52e260SChris Kay		--predefine=$(call escape-shell,-D__LINKER__=$(__LINKER__)) \
587df52e260SChris Kay		--predefine=$(call escape-shell,-DTF_CFLAGS=$(TF_CFLAGS)) \
588434d0491SZelalem Aweke		--map --list="$(MAPFILE)" --scatter=${PLAT_DIR}/scat/${1}.scat \
589758ccb80SChris Kay		$(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS) $(OBJS)
5908620bd0bSChris Kayelse ifeq ($($(ARCH)-ld-id),gnu-gcc)
591d95d56bdSJimmy Brisson	$$(q)$($(ARCH)-ld) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $$(WRAPPER_FLAGS) $(BL_LDFLAGS) -Wl,-Map=$(MAPFILE) \
592a6ff0067SChris Kay		$(addprefix -Wl$(comma)--script$(comma),$(LINKER_SCRIPTS)) -Wl,--script,$(DEFAULT_LINKER_SCRIPT) \
593edbce9aaSzelalem-aweke		$(OBJS) $(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS)
594c2ad38ceSVarun Wadekarelse
595d95d56bdSJimmy Brisson	$$(q)$($(ARCH)-ld) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $$(WRAPPER_FLAGS) $(BL_LDFLAGS) -Map=$(MAPFILE) \
596a6ff0067SChris Kay		$(addprefix -T ,$(LINKER_SCRIPTS)) --script $(DEFAULT_LINKER_SCRIPT) \
5976baf85b3SSathees Balya		$(OBJS) $(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS)
598c2ad38ceSVarun Wadekarendif
5999e4609f1SChristoph Müllnerifeq ($(DISABLE_BIN_GENERATION),1)
6007c4e1eeaSChris Kay	$(s)echo
6017c4e1eeaSChris Kay	$(s)echo "Built $$@ successfully"
6027c4e1eeaSChris Kay	$(s)echo
6039e4609f1SChristoph Müllnerendif
60473c99d4eSJuan Castillo
605f4dd18c2SChris Kay$(DUMP): $(ELF) | $$$$(@D)/
6067c4e1eeaSChris Kay	$$(s)echo "  OD      $$@"
6077c4e1eeaSChris Kay	$$(q)$($(ARCH)-od) -dx $$< > $$@
60873c99d4eSJuan Castillo
609f4dd18c2SChris Kay$(BIN): $(ELF) | $$$$(@D)/
6107c4e1eeaSChris Kay	$$(s)echo "  BIN     $$@"
6117c4e1eeaSChris Kay	$$(q)$($(ARCH)-oc) -O binary $$< $$@
6127c4e1eeaSChris Kay	$(s)echo
6137c4e1eeaSChris Kay	$(s)echo "Built $$@ successfully"
6147c4e1eeaSChris Kay	$(s)echo
61573c99d4eSJuan Castillo
616434d0491SZelalem Aweke.PHONY: $(1)
6179e4609f1SChristoph Müllnerifeq ($(DISABLE_BIN_GENERATION),1)
618434d0491SZelalem Aweke$(1): $(ELF) $(DUMP)
6199e4609f1SChristoph Müllnerelse
620434d0491SZelalem Aweke$(1): $(BIN) $(DUMP)
6219e4609f1SChristoph Müllnerendif
62273c99d4eSJuan Castillo
623434d0491SZelalem Awekeall: $(1)
62473c99d4eSJuan Castillo
625c6ba9b45SSumit Gargifeq ($(4),1)
626c6ba9b45SSumit Garg$(call ENCRYPT_FW,$(BIN),$(ENC_BIN))
627f7a41fb4SBoyan Karatotev$(if $(2),$(call TOOL_ADD_IMG_PAYLOAD,$(BL),$(BIN),--$(2),$(ENC_BIN),$(3), \
628c6ba9b45SSumit Garg		$(ENC_BIN)))
629c6ba9b45SSumit Gargelse
630f7a41fb4SBoyan Karatotev$(if $(2),$(call TOOL_ADD_IMG_PAYLOAD,$(BL),$(BIN),--$(2),$(BIN),$(3)))
631c6ba9b45SSumit Gargendif
63273c99d4eSJuan Castillo
63373c99d4eSJuan Castilloendef
63473c99d4eSJuan Castillo
63538c14d88SSoby Mathew# Convert device tree source file names to matching blobs
63638c14d88SSoby Mathew#   $(1) = input dts
63703b397a8SNishanth Menondefine SOURCES_TO_DTBS
63803b397a8SNishanth Menon        $(notdir $(patsubst %.dts,%.dtb,$(filter %.dts,$(1))))
63903b397a8SNishanth Menonendef
64003b397a8SNishanth Menon
64138c14d88SSoby Mathew# MAKE_DTB generate the Flattened device tree binary
64203b397a8SNishanth Menon#   $(1) = output directory
64303b397a8SNishanth Menon#   $(2) = input dts
64403b397a8SNishanth Menondefine MAKE_DTB
64503b397a8SNishanth Menon
646077b3e9aSYann Gautier# List of DTB file(s) to generate, based on DTS file basename list
64738c14d88SSoby Mathew$(eval DOBJ := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2))))
648077b3e9aSYann Gautier# List of the pre-compiled DTS file(s)
64901d237cbSYann Gautier$(eval DPRE := $(addprefix $(1)/,$(patsubst %.dts,%.pre.dts,$(notdir $(2)))))
650077b3e9aSYann Gautier# Dependencies of the pre-compiled DTS file(s) on its source and included files
651077b3e9aSYann Gautier$(eval DTSDEP := $(patsubst %.dtb,%.o.d,$(DOBJ)))
652077b3e9aSYann Gautier# Dependencies of the DT compilation on its pre-compiled DTS
653077b3e9aSYann Gautier$(eval DTBDEP := $(patsubst %.dtb,%.d,$(DOBJ)))
65403b397a8SNishanth Menon
6551e8b5354SBoyan Karatotev$(eval $(call MAKE_PRE,$(DPRE),$(2),$(DTSDEP),$(DTC_CPPFLAGS)))
6567b453526SChris Kay
657f4dd18c2SChris Kay$(DOBJ): $(DPRE) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
6587c4e1eeaSChris Kay	$$(s)echo "  DTC     $$<"
6597c4e1eeaSChris Kay	$$(q)$($(ARCH)-dtc) $$(DTC_FLAGS) -d $(DTBDEP) -o $$@ $$<
6601c08ff32SSalman Nabi	$$($$@-after)
66103b397a8SNishanth Menon
662077b3e9aSYann Gautier-include $(DTBDEP)
663077b3e9aSYann Gautier-include $(DTSDEP)
66403b397a8SNishanth Menon
66503b397a8SNishanth Menonendef
66603b397a8SNishanth Menon
66703b397a8SNishanth Menon# MAKE_DTBS builds flattened device tree sources
66803b397a8SNishanth Menon#   $(1) = output directory
66903b397a8SNishanth Menon#   $(2) = list of flattened device tree source files
67003b397a8SNishanth Menondefine MAKE_DTBS
67103b397a8SNishanth Menon        $(eval DOBJS := $(filter %.dts,$(2)))
67203b397a8SNishanth Menon        $(eval REMAIN := $(filter-out %.dts,$(2)))
67338c14d88SSoby Mathew        $(and $(REMAIN),$(error FDT_SOURCES contain non-DTS files: $(REMAIN)))
67403b397a8SNishanth Menon        $(eval $(foreach obj,$(DOBJS),$(call MAKE_DTB,$(1),$(obj))))
67503b397a8SNishanth Menon
676f4dd18c2SChris Kaydtbs: $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2)))
67738c14d88SSoby Mathewall: dtbs
67803b397a8SNishanth Menonendef
679