xref: /rk3399_ARM-atf/make_helpers/build_macros.mk (revision a6ff0067ab57d848d3fb28a3eb2b47e6cf2a6092)
173c99d4eSJuan Castillo#
282274936SChris Kay# Copyright (c) 2015-2023, 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}
16*a6ff0067SChris 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
4173c99d4eSJuan Castillo# Convenience function for adding build definitions
4273c99d4eSJuan Castillo# $(eval $(call add_define,FOO)) will have:
4373c99d4eSJuan Castillo# -DFOO if $(FOO) is empty; -DFOO=$(FOO) otherwise
4473c99d4eSJuan Castillodefine add_define
4573c99d4eSJuan Castillo    DEFINES			+=	-D$(1)$(if $(value $(1)),=$(value $(1)),)
4673c99d4eSJuan Castilloendef
4773c99d4eSJuan Castillo
48327131c4SLeonardo Sandoval
49327131c4SLeonardo Sandoval# Convenience function for addding multiple build definitions
50327131c4SLeonardo Sandoval# $(eval $(call add_defines,FOO BOO))
51327131c4SLeonardo Sandovaldefine add_defines
52327131c4SLeonardo Sandoval    $(foreach def,$1,$(eval $(call add_define,$(def))))
53327131c4SLeonardo Sandovalendef
54327131c4SLeonardo Sandoval
558eadeb4aSSoren Brinkmann# Convenience function for adding build definitions
568eadeb4aSSoren Brinkmann# $(eval $(call add_define_val,FOO,BAR)) will have:
578eadeb4aSSoren Brinkmann# -DFOO=BAR
588eadeb4aSSoren Brinkmanndefine add_define_val
598eadeb4aSSoren Brinkmann    DEFINES			+=	-D$(1)=$(2)
608eadeb4aSSoren Brinkmannendef
618eadeb4aSSoren Brinkmann
6273c99d4eSJuan Castillo# Convenience function for verifying option has a boolean value
6373c99d4eSJuan Castillo# $(eval $(call assert_boolean,FOO)) will assert FOO is 0 or 1
6473c99d4eSJuan Castillodefine assert_boolean
65be4cd40eSMasahiro Yamada    $(if $(filter-out 0 1,$($1)),$(error $1 must be boolean))
6673c99d4eSJuan Castilloendef
6773c99d4eSJuan Castillo
68327131c4SLeonardo Sandoval# Convenience function for verifying options have boolean values
69327131c4SLeonardo Sandoval# $(eval $(call assert_booleans,FOO BOO)) will assert FOO and BOO for 0 or 1 values
70327131c4SLeonardo Sandovaldefine assert_booleans
71327131c4SLeonardo Sandoval    $(foreach bool,$1,$(eval $(call assert_boolean,$(bool))))
72327131c4SLeonardo Sandovalendef
73327131c4SLeonardo Sandoval
74c877b414SJeenu Viswambharan0-9 := 0 1 2 3 4 5 6 7 8 9
75c877b414SJeenu Viswambharan
76c877b414SJeenu Viswambharan# Function to verify that a given option $(1) contains a numeric value
77c877b414SJeenu Viswambharandefine assert_numeric
78c877b414SJeenu Viswambharan$(if $($(1)),,$(error $(1) must not be empty))
79c877b414SJeenu Viswambharan$(eval __numeric := $($(1)))
80c877b414SJeenu Viswambharan$(foreach d,$(0-9),$(eval __numeric := $(subst $(d),,$(__numeric))))
81c877b414SJeenu Viswambharan$(if $(__numeric),$(error $(1) must be numeric))
82c877b414SJeenu Viswambharanendef
83c877b414SJeenu Viswambharan
84327131c4SLeonardo Sandoval# Convenience function for verifying options have numeric values
85327131c4SLeonardo Sandoval# $(eval $(call assert_numerics,FOO BOO)) will assert FOO and BOO contain numeric values
86327131c4SLeonardo Sandovaldefine assert_numerics
87327131c4SLeonardo Sandoval    $(foreach num,$1,$(eval $(call assert_numeric,$(num))))
88327131c4SLeonardo Sandovalendef
89327131c4SLeonardo Sandoval
908c7b944aSVijayenthiran Subramaniam# CREATE_SEQ is a recursive function to create sequence of numbers from 1 to
918c7b944aSVijayenthiran Subramaniam# $(2) and assign the sequence to $(1)
928c7b944aSVijayenthiran Subramaniamdefine CREATE_SEQ
938c7b944aSVijayenthiran Subramaniam$(if $(word $(2), $($(1))),\
948c7b944aSVijayenthiran Subramaniam  $(eval $(1) += $(words $($(1))))\
958c7b944aSVijayenthiran Subramaniam  $(eval $(1) := $(filter-out 0,$($(1)))),\
968c7b944aSVijayenthiran Subramaniam  $(eval $(1) += $(words $($(1))))\
978c7b944aSVijayenthiran Subramaniam  $(call CREATE_SEQ,$(1),$(2))\
988c7b944aSVijayenthiran Subramaniam)
998c7b944aSVijayenthiran Subramaniamendef
1008c7b944aSVijayenthiran Subramaniam
10173c99d4eSJuan Castillo# IMG_MAPFILE defines the output file describing the memory map corresponding
10273c99d4eSJuan Castillo# to a BL stage
103434d0491SZelalem Aweke#   $(1) = BL stage
10473c99d4eSJuan Castillodefine IMG_MAPFILE
105434d0491SZelalem Aweke    ${BUILD_DIR}/$(1).map
10673c99d4eSJuan Castilloendef
10773c99d4eSJuan Castillo
10873c99d4eSJuan Castillo# IMG_ELF defines the elf file corresponding to a BL stage
109434d0491SZelalem Aweke#   $(1) = BL stage
11073c99d4eSJuan Castillodefine IMG_ELF
111434d0491SZelalem Aweke    ${BUILD_DIR}/$(1).elf
11273c99d4eSJuan Castilloendef
11373c99d4eSJuan Castillo
11473c99d4eSJuan Castillo# IMG_DUMP defines the symbols dump file corresponding to a BL stage
115434d0491SZelalem Aweke#   $(1) = BL stage
11673c99d4eSJuan Castillodefine IMG_DUMP
117434d0491SZelalem Aweke    ${BUILD_DIR}/$(1).dump
11873c99d4eSJuan Castilloendef
11973c99d4eSJuan Castillo
12073c99d4eSJuan Castillo# IMG_BIN defines the default image file corresponding to a BL stage
121434d0491SZelalem Aweke#   $(1) = BL stage
12273c99d4eSJuan Castillodefine IMG_BIN
123434d0491SZelalem Aweke    ${BUILD_PLAT}/$(1).bin
12473c99d4eSJuan Castilloendef
12573c99d4eSJuan Castillo
126c6ba9b45SSumit Garg# IMG_ENC_BIN defines the default encrypted image file corresponding to a
127c6ba9b45SSumit Garg# BL stage
128434d0491SZelalem Aweke#   $(1) = BL stage
129c6ba9b45SSumit Gargdefine IMG_ENC_BIN
130434d0491SZelalem Aweke    ${BUILD_PLAT}/$(1)_enc.bin
131c6ba9b45SSumit Gargendef
132c6ba9b45SSumit Garg
133c6ba9b45SSumit Garg# ENCRYPT_FW invokes enctool to encrypt firmware binary
134c6ba9b45SSumit Garg#   $(1) = input firmware binary
135c6ba9b45SSumit Garg#   $(2) = output encrypted firmware binary
136c6ba9b45SSumit Gargdefine ENCRYPT_FW
137c6ba9b45SSumit Garg$(2): $(1) enctool
138c6ba9b45SSumit Garg	$$(ECHO) "  ENC     $$<"
139c6ba9b45SSumit Garg	$$(Q)$$(ENCTOOL) $$(ENC_ARGS) -i $$< -o $$@
140c6ba9b45SSumit Gargendef
141c6ba9b45SSumit Garg
14210cea934SMasahiro Yamada# TOOL_ADD_PAYLOAD appends the command line arguments required by fiptool to
14310cea934SMasahiro Yamada# package a new payload and/or by cert_create to generate certificate.
14410cea934SMasahiro Yamada# Optionally, it adds the dependency on this payload
14573c99d4eSJuan Castillo#   $(1) = payload filename (i.e. bl31.bin)
1461e0c0784SMasahiro Yamada#   $(2) = command line option for the specified payload (i.e. --soc-fw)
14736af3455SMasahiro Yamada#   $(3) = tool target dependency (optional) (ex. build/fvp/release/bl31.bin)
1481dc0714fSMasahiro Yamada#   $(4) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
149c6ba9b45SSumit Garg#   $(5) = encrypted payload (optional) (ex. build/fvp/release/bl31_enc.bin)
15010cea934SMasahiro Yamadadefine TOOL_ADD_PAYLOAD
151c6ba9b45SSumit Gargifneq ($(5),)
152c6ba9b45SSumit Garg    $(4)FIP_ARGS += $(2) $(5)
153c6ba9b45SSumit Garg    $(if $(3),$(4)CRT_DEPS += $(1))
154c6ba9b45SSumit Gargelse
155945b316fSMasahiro Yamada    $(4)FIP_ARGS += $(2) $(1)
156c6ba9b45SSumit Garg    $(if $(3),$(4)CRT_DEPS += $(3))
157c6ba9b45SSumit Gargendif
158945b316fSMasahiro Yamada    $(if $(3),$(4)FIP_DEPS += $(3))
159f30ee0b9SMasahiro Yamada    $(4)CRT_ARGS += $(2) $(1)
16073c99d4eSJuan Castilloendef
16173c99d4eSJuan Castillo
1622da522bbSMasahiro Yamada# TOOL_ADD_IMG_PAYLOAD works like TOOL_ADD_PAYLOAD, but applies image filters
1632da522bbSMasahiro Yamada# before passing them to host tools if BL*_PRE_TOOL_FILTER is defined.
1642da522bbSMasahiro Yamada#   $(1) = image_type (scp_bl2, bl33, etc.)
1652da522bbSMasahiro Yamada#   $(2) = payload filepath (ex. build/fvp/release/bl31.bin)
1662da522bbSMasahiro Yamada#   $(3) = command line option for the specified payload (ex. --soc-fw)
1672da522bbSMasahiro Yamada#   $(4) = tool target dependency (optional) (ex. build/fvp/release/bl31.bin)
1682da522bbSMasahiro Yamada#   $(5) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
169c6ba9b45SSumit Garg#   $(6) = encrypted payload (optional) (ex. build/fvp/release/bl31_enc.bin)
1702da522bbSMasahiro Yamada
1712da522bbSMasahiro Yamadadefine TOOL_ADD_IMG_PAYLOAD
1722da522bbSMasahiro Yamada
1732da522bbSMasahiro Yamada$(eval PRE_TOOL_FILTER := $($(call uppercase,$(1))_PRE_TOOL_FILTER))
1742da522bbSMasahiro Yamada
1752da522bbSMasahiro Yamadaifneq ($(PRE_TOOL_FILTER),)
1762da522bbSMasahiro Yamada
1772da522bbSMasahiro Yamada$(eval PROCESSED_PATH := $(BUILD_PLAT)/$(1).bin$($(PRE_TOOL_FILTER)_SUFFIX))
1782da522bbSMasahiro Yamada
1792da522bbSMasahiro Yamada$(call $(PRE_TOOL_FILTER)_RULE,$(PROCESSED_PATH),$(2))
1802da522bbSMasahiro Yamada
1812da522bbSMasahiro Yamada$(PROCESSED_PATH): $(4)
1822da522bbSMasahiro Yamada
183c6ba9b45SSumit Garg$(call TOOL_ADD_PAYLOAD,$(PROCESSED_PATH),$(3),$(PROCESSED_PATH),$(5),$(6))
1842da522bbSMasahiro Yamada
1852da522bbSMasahiro Yamadaelse
186c6ba9b45SSumit Garg$(call TOOL_ADD_PAYLOAD,$(2),$(3),$(4),$(5),$(6))
1872da522bbSMasahiro Yamadaendif
1882da522bbSMasahiro Yamadaendef
1892da522bbSMasahiro Yamada
1900191262dSYatharth Kochar# CERT_ADD_CMD_OPT adds a new command line option to the cert_create invocation
19173c99d4eSJuan Castillo#   $(1) = parameter filename
19273c99d4eSJuan Castillo#   $(2) = cert_create command line option for the specified parameter
19391704d9dSMasahiro Yamada#   $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
19473c99d4eSJuan Castillodefine CERT_ADD_CMD_OPT
19591704d9dSMasahiro Yamada    $(3)CRT_ARGS += $(2) $(1)
19673c99d4eSJuan Castilloendef
19773c99d4eSJuan Castillo
198c939d13aSMasahiro Yamada# TOOL_ADD_IMG allows the platform to specify an external image to be packed
199c939d13aSMasahiro Yamada# in the FIP and/or for which certificate is generated. It also adds a
200c939d13aSMasahiro Yamada# dependency on the image file, aborting the build if the file does not exist.
20133950dd8SMasahiro Yamada#   $(1) = image_type (scp_bl2, bl33, etc.)
2021e0c0784SMasahiro Yamada#   $(2) = command line option for fiptool (--scp-fw, --nt-fw, etc)
2031dc0714fSMasahiro Yamada#   $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
204c6ba9b45SSumit Garg#   $(4) = Image encryption flag (optional) (0, 1)
20573c99d4eSJuan Castillo# Example:
20633950dd8SMasahiro Yamada#   $(eval $(call TOOL_ADD_IMG,bl33,--nt-fw))
207c939d13aSMasahiro Yamadadefine TOOL_ADD_IMG
20833950dd8SMasahiro Yamada    # Build option to specify the image filename (SCP_BL2, BL33, etc)
20933950dd8SMasahiro Yamada    # This is the uppercase form of the first parameter
21033950dd8SMasahiro Yamada    $(eval _V := $(call uppercase,$(1)))
21133950dd8SMasahiro Yamada
2124727fd13SPali Rohár    # $(check_$(1)_cmd) variable is executed in the check_$(1) target and also
2134727fd13SPali Rohár    # is put into the ${CHECK_$(3)FIP_CMD} variable which is executed by the
2144727fd13SPali Rohár    # target ${BUILD_PLAT}/${$(3)FIP_NAME}.
2154727fd13SPali Rohár    $(eval check_$(1)_cmd := \
2164727fd13SPali Rohár        $(if $(value $(_V)),,$$$$(error "Platform '${PLAT}' requires $(_V). Please set $(_V) to point to the right file")) \
2174727fd13SPali Rohár        $(if $(wildcard $(value $(_V))),,$$$$(error '$(_V)=$(value $(_V))' was specified, but '$(value $(_V))' does not exist)) \
2184727fd13SPali Rohár    )
2194727fd13SPali Rohár
220945b316fSMasahiro Yamada    $(3)CRT_DEPS += check_$(1)
2214727fd13SPali Rohár    CHECK_$(3)FIP_CMD += $$(check_$(1)_cmd)
222c6ba9b45SSumit Gargifeq ($(4),1)
223c6ba9b45SSumit Garg    $(eval ENC_BIN := ${BUILD_PLAT}/$(1)_enc.bin)
224c6ba9b45SSumit Garg    $(call ENCRYPT_FW,$(value $(_V)),$(ENC_BIN))
225c6ba9b45SSumit Garg    $(call TOOL_ADD_IMG_PAYLOAD,$(1),$(value $(_V)),$(2),$(ENC_BIN),$(3), \
226c6ba9b45SSumit Garg		$(ENC_BIN))
227c6ba9b45SSumit Gargelse
2284727fd13SPali Rohár    $(call TOOL_ADD_IMG_PAYLOAD,$(1),$(value $(_V)),$(2),$(if $(wildcard $(value $(_V))),$(value $(_V)),FORCE),$(3))
229c6ba9b45SSumit Gargendif
2300191262dSYatharth Kochar
23187ebd20dSMasahiro Yamada.PHONY: check_$(1)
2320191262dSYatharth Kocharcheck_$(1):
2334727fd13SPali Rohár	$(check_$(1)_cmd)
2340191262dSYatharth Kocharendef
23573c99d4eSJuan Castillo
236cf2dd17dSJuan Pablo Conde# SELECT_OPENSSL_API_VERSION selects the OpenSSL API version to be used to
237cf2dd17dSJuan Pablo Conde# build the host tools by checking the version of OpenSSL located under
238cf2dd17dSJuan Pablo Conde# the path defined by the OPENSSL_DIR variable. It receives no parameters.
239cf2dd17dSJuan Pablo Condedefine SELECT_OPENSSL_API_VERSION
240cf2dd17dSJuan Pablo Conde    # Set default value for USING_OPENSSL3 macro to 0
241cf2dd17dSJuan Pablo Conde    $(eval USING_OPENSSL3 = 0)
242cf2dd17dSJuan Pablo Conde    # Obtain the OpenSSL version for the build located under OPENSSL_DIR
243cf2dd17dSJuan Pablo Conde    $(eval OPENSSL_INFO := $(shell LD_LIBRARY_PATH=${OPENSSL_DIR}:${OPENSSL_DIR}/lib ${OPENSSL_BIN_PATH}/openssl version))
244cf2dd17dSJuan Pablo Conde    $(eval OPENSSL_CURRENT_VER = $(word 2, ${OPENSSL_INFO}))
245cf2dd17dSJuan Pablo Conde    $(eval OPENSSL_CURRENT_VER_MAJOR = $(firstword $(subst ., ,$(OPENSSL_CURRENT_VER))))
246cf2dd17dSJuan Pablo Conde    # If OpenSSL version is 3.x, then set USING_OPENSSL3 flag to 1
247cf2dd17dSJuan Pablo Conde    $(if $(filter 3,$(OPENSSL_CURRENT_VER_MAJOR)), $(eval USING_OPENSSL3 = 1))
248cf2dd17dSJuan Pablo Condeendef
249cf2dd17dSJuan Pablo Conde
25073c99d4eSJuan Castillo################################################################################
25114db8908SMasahiro Yamada# Generic image processing filters
25214db8908SMasahiro Yamada################################################################################
25314db8908SMasahiro Yamada
25414db8908SMasahiro Yamada# GZIP
25514db8908SMasahiro Yamadadefine GZIP_RULE
25614db8908SMasahiro Yamada$(1): $(2)
257ee1ba6d4SAndre Przywara	$(ECHO) "  GZIP    $$@"
25814db8908SMasahiro Yamada	$(Q)gzip -n -f -9 $$< --stdout > $$@
25914db8908SMasahiro Yamadaendef
26014db8908SMasahiro Yamada
26114db8908SMasahiro YamadaGZIP_SUFFIX := .gz
26214db8908SMasahiro Yamada
26314db8908SMasahiro Yamada################################################################################
26473c99d4eSJuan Castillo# Auxiliary macros to build TF images from sources
26573c99d4eSJuan Castillo################################################################################
26673c99d4eSJuan Castillo
2671d274ab0SMasahiro YamadaMAKE_DEP = -Wp,-MD,$(DEP) -MT $$@ -MP
26873c99d4eSJuan Castillo
2695fee0287SRoberto Vargas
2705fee0287SRoberto Vargas# MAKE_C_LIB builds a C source file and generates the dependency file
2715fee0287SRoberto Vargas#   $(1) = output directory
2725fee0287SRoberto Vargas#   $(2) = source file (%.c)
2735fee0287SRoberto Vargas#   $(3) = library name
2745fee0287SRoberto Vargasdefine MAKE_C_LIB
2755fee0287SRoberto Vargas$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
2765fee0287SRoberto Vargas$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
2775fee0287SRoberto Vargas
2785fee0287SRoberto Vargas$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | lib$(3)_dirs
279ee1ba6d4SAndre Przywara	$$(ECHO) "  CC      $$<"
2805fee0287SRoberto Vargas	$$(Q)$$(CC) $$(TF_CFLAGS) $$(CFLAGS) $(MAKE_DEP) -c $$< -o $$@
2815fee0287SRoberto Vargas
2825fee0287SRoberto Vargas-include $(DEP)
2835fee0287SRoberto Vargas
2845fee0287SRoberto Vargasendef
2855fee0287SRoberto Vargas
28670b0f278SAntonio Nino Diaz# MAKE_S_LIB builds an assembly source file and generates the dependency file
28770b0f278SAntonio Nino Diaz#   $(1) = output directory
28870b0f278SAntonio Nino Diaz#   $(2) = source file (%.S)
28970b0f278SAntonio Nino Diaz#   $(3) = library name
29070b0f278SAntonio Nino Diazdefine MAKE_S_LIB
29170b0f278SAntonio Nino Diaz$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
29270b0f278SAntonio Nino Diaz$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
29370b0f278SAntonio Nino Diaz
29470b0f278SAntonio Nino Diaz$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | lib$(3)_dirs
29570b0f278SAntonio Nino Diaz	$$(ECHO) "  AS      $$<"
29670b0f278SAntonio Nino Diaz	$$(Q)$$(AS) $$(ASFLAGS) $(MAKE_DEP) -c $$< -o $$@
29770b0f278SAntonio Nino Diaz
29870b0f278SAntonio Nino Diaz-include $(DEP)
29970b0f278SAntonio Nino Diaz
30070b0f278SAntonio Nino Diazendef
30170b0f278SAntonio Nino Diaz
3025fee0287SRoberto Vargas
30373c99d4eSJuan Castillo# MAKE_C builds a C source file and generates the dependency file
30473c99d4eSJuan Castillo#   $(1) = output directory
30573c99d4eSJuan Castillo#   $(2) = source file (%.c)
306434d0491SZelalem Aweke#   $(3) = BL stage
30773c99d4eSJuan Castillodefine MAKE_C
30873c99d4eSJuan Castillo
30973c99d4eSJuan Castillo$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
310710ea1d0SMasahiro Yamada$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
311434d0491SZelalem Aweke$(eval BL_CPPFLAGS := $($(call uppercase,$(3))_CPPFLAGS) -DIMAGE_$(call uppercase,$(3)))
312434d0491SZelalem Aweke$(eval BL_CFLAGS := $($(call uppercase,$(3))_CFLAGS))
31373c99d4eSJuan Castillo
314434d0491SZelalem Aweke$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $(3)_dirs
315ee1ba6d4SAndre Przywara	$$(ECHO) "  CC      $$<"
316848a7e8cSMasahiro Yamada	$$(Q)$$(CC) $$(LTO_CFLAGS) $$(TF_CFLAGS) $$(CFLAGS) $(BL_CPPFLAGS) $(BL_CFLAGS) $(MAKE_DEP) -c $$< -o $$@
31773c99d4eSJuan Castillo
318710ea1d0SMasahiro Yamada-include $(DEP)
31973c99d4eSJuan Castillo
32073c99d4eSJuan Castilloendef
32173c99d4eSJuan Castillo
32273c99d4eSJuan Castillo
32373c99d4eSJuan Castillo# MAKE_S builds an assembly source file and generates the dependency file
32473c99d4eSJuan Castillo#   $(1) = output directory
32573c99d4eSJuan Castillo#   $(2) = assembly file (%.S)
326434d0491SZelalem Aweke#   $(3) = BL stage
32773c99d4eSJuan Castillodefine MAKE_S
32873c99d4eSJuan Castillo
32973c99d4eSJuan Castillo$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
330710ea1d0SMasahiro Yamada$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
331434d0491SZelalem Aweke$(eval BL_CPPFLAGS := $($(call uppercase,$(3))_CPPFLAGS) -DIMAGE_$(call uppercase,$(3)))
332434d0491SZelalem Aweke$(eval BL_ASFLAGS := $($(call uppercase,$(3))_ASFLAGS))
33373c99d4eSJuan Castillo
334434d0491SZelalem Aweke$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $(3)_dirs
335ee1ba6d4SAndre Przywara	$$(ECHO) "  AS      $$<"
336848a7e8cSMasahiro Yamada	$$(Q)$$(AS) $$(ASFLAGS) $(BL_CPPFLAGS) $(BL_ASFLAGS) $(MAKE_DEP) -c $$< -o $$@
33773c99d4eSJuan Castillo
338710ea1d0SMasahiro Yamada-include $(DEP)
33973c99d4eSJuan Castillo
34073c99d4eSJuan Castilloendef
34173c99d4eSJuan Castillo
34273c99d4eSJuan Castillo
34373c99d4eSJuan Castillo# MAKE_LD generate the linker script using the C preprocessor
34473c99d4eSJuan Castillo#   $(1) = output linker script
34573c99d4eSJuan Castillo#   $(2) = input template
346434d0491SZelalem Aweke#   $(3) = BL stage
34773c99d4eSJuan Castillodefine MAKE_LD
34873c99d4eSJuan Castillo
349710ea1d0SMasahiro Yamada$(eval DEP := $(1).d)
350434d0491SZelalem Aweke$(eval BL_CPPFLAGS := $($(call uppercase,$(3))_CPPFLAGS) -DIMAGE_$(call uppercase,$(3)))
35173c99d4eSJuan Castillo
352434d0491SZelalem Aweke$(1): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $(3)_dirs
353ee1ba6d4SAndre Przywara	$$(ECHO) "  PP      $$<"
354848a7e8cSMasahiro Yamada	$$(Q)$$(CPP) $$(CPPFLAGS) $(BL_CPPFLAGS) $(TF_CFLAGS_$(ARCH)) -P -x assembler-with-cpp -D__LINKER__ $(MAKE_DEP) -o $$@ $$<
35573c99d4eSJuan Castillo
356710ea1d0SMasahiro Yamada-include $(DEP)
35773c99d4eSJuan Castillo
35873c99d4eSJuan Castilloendef
35973c99d4eSJuan Castillo
36070b0f278SAntonio Nino Diaz# MAKE_LIB_OBJS builds both C and assembly source files
3615fee0287SRoberto Vargas#   $(1) = output directory
3625fee0287SRoberto Vargas#   $(2) = list of source files
3635fee0287SRoberto Vargas#   $(3) = name of the library
3645fee0287SRoberto Vargasdefine MAKE_LIB_OBJS
3655fee0287SRoberto Vargas        $(eval C_OBJS := $(filter %.c,$(2)))
3665fee0287SRoberto Vargas        $(eval REMAIN := $(filter-out %.c,$(2)))
3675fee0287SRoberto Vargas        $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C_LIB,$(1),$(obj),$(3))))
3685fee0287SRoberto Vargas
36970b0f278SAntonio Nino Diaz        $(eval S_OBJS := $(filter %.S,$(REMAIN)))
37070b0f278SAntonio Nino Diaz        $(eval REMAIN := $(filter-out %.S,$(REMAIN)))
37170b0f278SAntonio Nino Diaz        $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S_LIB,$(1),$(obj),$(3))))
37270b0f278SAntonio Nino Diaz
3735fee0287SRoberto Vargas        $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN)))
3745fee0287SRoberto Vargasendef
3755fee0287SRoberto Vargas
37673c99d4eSJuan Castillo
37773c99d4eSJuan Castillo# MAKE_OBJS builds both C and assembly source files
37873c99d4eSJuan Castillo#   $(1) = output directory
37973c99d4eSJuan Castillo#   $(2) = list of source files (both C and assembly)
380434d0491SZelalem Aweke#   $(3) = BL stage
38173c99d4eSJuan Castillodefine MAKE_OBJS
38273c99d4eSJuan Castillo        $(eval C_OBJS := $(filter %.c,$(2)))
38373c99d4eSJuan Castillo        $(eval REMAIN := $(filter-out %.c,$(2)))
38473c99d4eSJuan Castillo        $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj),$(3))))
38573c99d4eSJuan Castillo
38673c99d4eSJuan Castillo        $(eval S_OBJS := $(filter %.S,$(REMAIN)))
38773c99d4eSJuan Castillo        $(eval REMAIN := $(filter-out %.S,$(REMAIN)))
38873c99d4eSJuan Castillo        $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj),$(3))))
38973c99d4eSJuan Castillo
39073c99d4eSJuan Castillo        $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN)))
39173c99d4eSJuan Castilloendef
39273c99d4eSJuan Castillo
39373c99d4eSJuan Castillo
39473c99d4eSJuan Castillo# NOTE: The line continuation '\' is required in the next define otherwise we
39573c99d4eSJuan Castillo# end up with a line-feed characer at the end of the last c filename.
396e7f54dbdSEvan Lloyd# Also bear this issue in mind if extending the list of supported filetypes.
39773c99d4eSJuan Castillodefine SOURCES_TO_OBJS
39873c99d4eSJuan Castillo        $(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \
39973c99d4eSJuan Castillo        $(notdir $(patsubst %.S,%.o,$(filter %.S,$(1))))
40073c99d4eSJuan Castilloendef
40173c99d4eSJuan Castillo
4022f5d4a48SPatrick Georgi# Allow overriding the timestamp, for example for reproducible builds, or to
4032f5d4a48SPatrick Georgi# synchronize timestamps across multiple projects.
4042f5d4a48SPatrick Georgi# This must be set to a C string (including quotes where applicable).
4052f5d4a48SPatrick GeorgiBUILD_MESSAGE_TIMESTAMP ?= __TIME__", "__DATE__
40673c99d4eSJuan Castillo
4075fee0287SRoberto Vargas.PHONY: libraries
4085fee0287SRoberto Vargas
4095accce5bSRoberto Vargas# MAKE_LIB_DIRS macro defines the target for the directory where
4105fee0287SRoberto Vargas# libraries are created
4115accce5bSRoberto Vargasdefine MAKE_LIB_DIRS
4125fee0287SRoberto Vargas        $(eval LIB_DIR    := ${BUILD_PLAT}/lib)
4135accce5bSRoberto Vargas        $(eval ROMLIB_DIR    := ${BUILD_PLAT}/romlib)
4145accce5bSRoberto Vargas        $(eval LIBWRAPPER_DIR := ${BUILD_PLAT}/libwrapper)
4155fee0287SRoberto Vargas        $(eval $(call MAKE_PREREQ_DIR,${LIB_DIR},${BUILD_PLAT}))
4165accce5bSRoberto Vargas        $(eval $(call MAKE_PREREQ_DIR,${ROMLIB_DIR},${BUILD_PLAT}))
4175accce5bSRoberto Vargas        $(eval $(call MAKE_PREREQ_DIR,${LIBWRAPPER_DIR},${BUILD_PLAT}))
4185fee0287SRoberto Vargasendef
4195fee0287SRoberto Vargas
4205fee0287SRoberto Vargas# MAKE_LIB macro defines the targets and options to build each BL image.
4215fee0287SRoberto Vargas# Arguments:
4225fee0287SRoberto Vargas#   $(1) = Library name
4235fee0287SRoberto Vargasdefine MAKE_LIB
4245fee0287SRoberto Vargas        $(eval BUILD_DIR  := ${BUILD_PLAT}/lib$(1))
4255fee0287SRoberto Vargas        $(eval LIB_DIR    := ${BUILD_PLAT}/lib)
4265accce5bSRoberto Vargas        $(eval ROMLIB_DIR    := ${BUILD_PLAT}/romlib)
4275fee0287SRoberto Vargas        $(eval SOURCES    := $(LIB$(call uppercase,$(1))_SRCS))
4285fee0287SRoberto Vargas        $(eval OBJS       := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES))))
4295fee0287SRoberto Vargas
4305fee0287SRoberto Vargas$(eval $(call MAKE_PREREQ_DIR,${BUILD_DIR},${BUILD_PLAT}))
4315fee0287SRoberto Vargas$(eval $(call MAKE_LIB_OBJS,$(BUILD_DIR),$(SOURCES),$(1)))
4325fee0287SRoberto Vargas
4335fee0287SRoberto Vargas.PHONY : lib${1}_dirs
4345accce5bSRoberto Vargaslib${1}_dirs: | ${BUILD_DIR} ${LIB_DIR}  ${ROMLIB_DIR} ${LIBWRAPPER_DIR}
4355fee0287SRoberto Vargaslibraries: ${LIB_DIR}/lib$(1).a
436c2ad38ceSVarun Wadekarifneq ($(findstring armlink,$(notdir $(LD))),)
437c2ad38ceSVarun WadekarLDPATHS = --userlibpath=${LIB_DIR}
438c2ad38ceSVarun WadekarLDLIBS += --library=$(1)
439c2ad38ceSVarun Wadekarelse
4405fee0287SRoberto VargasLDPATHS = -L${LIB_DIR}
4415fee0287SRoberto VargasLDLIBS += -l$(1)
442c2ad38ceSVarun Wadekarendif
4435fee0287SRoberto Vargas
4445accce5bSRoberto Vargasifeq ($(USE_ROMLIB),1)
4456baf85b3SSathees BalyaLIBWRAPPER = -lwrappers
4465accce5bSRoberto Vargasendif
4475accce5bSRoberto Vargas
4485fee0287SRoberto Vargasall: ${LIB_DIR}/lib$(1).a
4495fee0287SRoberto Vargas
4505fee0287SRoberto Vargas${LIB_DIR}/lib$(1).a: $(OBJS)
451ee1ba6d4SAndre Przywara	$$(ECHO) "  AR      $$@"
4525fee0287SRoberto Vargas	$$(Q)$$(AR) cr $$@ $$?
4535fee0287SRoberto Vargasendef
4545fee0287SRoberto Vargas
45582274936SChris Kay# Generate the path to one or more preprocessed linker scripts given the paths
45682274936SChris Kay# of their sources.
45782274936SChris Kay#
45882274936SChris Kay# Arguments:
45982274936SChris Kay#   $(1) = path to one or more linker script sources
46082274936SChris Kaydefine linker_script_path
46182274936SChris Kay        $(patsubst %.S,$(BUILD_DIR)/%,$(1))
46282274936SChris Kayendef
46382274936SChris Kay
46473c99d4eSJuan Castillo# MAKE_BL macro defines the targets and options to build each BL image.
46573c99d4eSJuan Castillo# Arguments:
466434d0491SZelalem Aweke#   $(1) = BL stage
4678f0617efSJuan Castillo#   $(2) = FIP command line option (if empty, image will not be included in the FIP)
4681dc0714fSMasahiro Yamada#   $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
469c6ba9b45SSumit Garg#   $(4) = BL encryption flag (optional) (0, 1)
47073c99d4eSJuan Castillodefine MAKE_BL
471434d0491SZelalem Aweke        $(eval BUILD_DIR  := ${BUILD_PLAT}/$(1))
472434d0491SZelalem Aweke        $(eval BL_SOURCES := $($(call uppercase,$(1))_SOURCES))
4735ba8f669SYatharth Kochar        $(eval SOURCES    := $(BL_SOURCES) $(BL_COMMON_SOURCES) $(PLAT_BL_COMMON_SOURCES))
47473c99d4eSJuan Castillo        $(eval OBJS       := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES))))
47573c99d4eSJuan Castillo        $(eval MAPFILE    := $(call IMG_MAPFILE,$(1)))
47673c99d4eSJuan Castillo        $(eval ELF        := $(call IMG_ELF,$(1)))
47773c99d4eSJuan Castillo        $(eval DUMP       := $(call IMG_DUMP,$(1)))
47873c99d4eSJuan Castillo        $(eval BIN        := $(call IMG_BIN,$(1)))
479c6ba9b45SSumit Garg        $(eval ENC_BIN    := $(call IMG_ENC_BIN,$(1)))
480434d0491SZelalem Aweke        $(eval BL_LIBS    := $($(call uppercase,$(1))_LIBS))
48182274936SChris Kay
48282274936SChris Kay        $(eval DEFAULT_LINKER_SCRIPT_SOURCE := $($(call uppercase,$(1))_DEFAULT_LINKER_SCRIPT_SOURCE))
48382274936SChris Kay        $(eval DEFAULT_LINKER_SCRIPT := $(call linker_script_path,$(DEFAULT_LINKER_SCRIPT_SOURCE)))
48482274936SChris Kay
485*a6ff0067SChris Kay        $(eval LINKER_SCRIPT_SOURCES := $($(call uppercase,$(1))_LINKER_SCRIPT_SOURCES))
486*a6ff0067SChris Kay        $(eval LINKER_SCRIPTS := $(call linker_script_path,$(LINKER_SCRIPT_SOURCES)))
487*a6ff0067SChris Kay
48851b27702SEvan Lloyd        # We use sort only to get a list of unique object directory names.
48951b27702SEvan Lloyd        # ordering is not relevant but sort removes duplicates.
490*a6ff0067SChris Kay        $(eval TEMP_OBJ_DIRS := $(sort $(dir ${OBJS} ${DEFAULT_LINKER_SCRIPT} ${LINKER_SCRIPTS})))
49151b27702SEvan Lloyd        # The $(dir ) function leaves a trailing / on the directory names
492d014ea6cSMasahiro Yamada        # Rip off the / to match directory names with make rule targets.
493d014ea6cSMasahiro Yamada        $(eval OBJ_DIRS   := $(patsubst %/,%,$(TEMP_OBJ_DIRS)))
49451b27702SEvan Lloyd
49551b27702SEvan Lloyd# Create generators for object directory structure
49651b27702SEvan Lloyd
4978012cc5cSMasahiro Yamada$(eval $(call MAKE_PREREQ_DIR,${BUILD_DIR},${BUILD_PLAT}))
4986ba7d274SEvan Lloyd
49982274936SChris Kay$(eval $(foreach objd,${OBJ_DIRS},
50082274936SChris Kay        $(call MAKE_PREREQ_DIR,${objd},${BUILD_DIR})))
50151b27702SEvan Lloyd
502434d0491SZelalem Aweke.PHONY : ${1}_dirs
50351b27702SEvan Lloyd
50451b27702SEvan Lloyd# We use order-only prerequisites to ensure that directories are created,
50551b27702SEvan Lloyd# but do not cause re-builds every time a file is written.
506434d0491SZelalem Aweke${1}_dirs: | ${OBJ_DIRS}
50773c99d4eSJuan Castillo
50873c99d4eSJuan Castillo$(eval $(call MAKE_OBJS,$(BUILD_DIR),$(SOURCES),$(1)))
509*a6ff0067SChris Kay
510*a6ff0067SChris Kay# Generate targets to preprocess each required linker script
511*a6ff0067SChris Kay$(eval $(foreach source,$(DEFAULT_LINKER_SCRIPT_SOURCE) $(LINKER_SCRIPT_SOURCES), \
512*a6ff0067SChris Kay        $(call MAKE_LD,$(call linker_script_path,$(source)),$(source),$(1))))
513*a6ff0067SChris Kay
514434d0491SZelalem Aweke$(eval BL_LDFLAGS := $($(call uppercase,$(1))_LDFLAGS))
51573c99d4eSJuan Castillo
5165accce5bSRoberto Vargasifeq ($(USE_ROMLIB),1)
5175accce5bSRoberto Vargas$(ELF): romlib.bin
5185accce5bSRoberto Vargasendif
5195accce5bSRoberto Vargas
5208dccddc7SLeon Chen# MODULE_OBJS can be assigned by vendors with different compiled
5218dccddc7SLeon Chen# object file path, and prebuilt object file path.
5228dccddc7SLeon Chen$(eval OBJS += $(MODULE_OBJS))
5238dccddc7SLeon Chen
524*a6ff0067SChris Kay$(ELF): $(OBJS) $(DEFAULT_LINKER_SCRIPT) $(LINKER_SCRIPTS) | $(1)_dirs libraries $(BL_LIBS)
525ee1ba6d4SAndre Przywara	$$(ECHO) "  LD      $$@"
526414ab853SEvan Lloydifdef MAKE_BUILD_STRINGS
527414ab853SEvan Lloyd	$(call MAKE_BUILD_STRINGS, $(BUILD_DIR)/build_message.o)
528414ab853SEvan Lloydelse
5292f5d4a48SPatrick Georgi	@echo 'const char build_message[] = "Built : "$(BUILD_MESSAGE_TIMESTAMP); \
530dddf4283Slaurenw-arm	       const char version_string[] = "${VERSION_STRING}"; \
531dddf4283Slaurenw-arm	       const char version[] = "${VERSION}";' | \
532f2e1d57eSMasahiro Yamada		$$(CC) $$(TF_CFLAGS) $$(CFLAGS) -xc -c - -o $(BUILD_DIR)/build_message.o
533414ab853SEvan Lloydendif
534c2ad38ceSVarun Wadekarifneq ($(findstring armlink,$(notdir $(LD))),)
535434d0491SZelalem Aweke	$$(Q)$$(LD) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $(BL_LDFLAGS) --entry=${1}_entrypoint \
536c2ad38ceSVarun Wadekar		--predefine="-D__LINKER__=$(__LINKER__)" \
537c2ad38ceSVarun Wadekar		--predefine="-DTF_CFLAGS=$(TF_CFLAGS)" \
538434d0491SZelalem Aweke		--map --list="$(MAPFILE)" --scatter=${PLAT_DIR}/scat/${1}.scat \
539c2ad38ceSVarun Wadekar		$(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS) \
540c2ad38ceSVarun Wadekar		$(BUILD_DIR)/build_message.o $(OBJS)
541edbce9aaSzelalem-awekeelse ifneq ($(findstring gcc,$(notdir $(LD))),)
542edbce9aaSzelalem-aweke	$$(Q)$$(LD) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) -Wl,-Map=$(MAPFILE) \
543*a6ff0067SChris Kay		$(addprefix -Wl$(comma)--script$(comma),$(LINKER_SCRIPTS)) -Wl,--script,$(DEFAULT_LINKER_SCRIPT) \
544*a6ff0067SChris Kay		$(BUILD_DIR)/build_message.o \
545edbce9aaSzelalem-aweke		$(OBJS) $(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS)
546c2ad38ceSVarun Wadekarelse
547d986bae4SMasahiro Yamada	$$(Q)$$(LD) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $(BL_LDFLAGS) -Map=$(MAPFILE) \
548*a6ff0067SChris Kay		$(addprefix -T ,$(LINKER_SCRIPTS)) --script $(DEFAULT_LINKER_SCRIPT) \
549*a6ff0067SChris Kay		$(BUILD_DIR)/build_message.o \
5506baf85b3SSathees Balya		$(OBJS) $(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS)
551c2ad38ceSVarun Wadekarendif
5529e4609f1SChristoph Müllnerifeq ($(DISABLE_BIN_GENERATION),1)
5539e4609f1SChristoph Müllner	@${ECHO_BLANK_LINE}
5549e4609f1SChristoph Müllner	@echo "Built $$@ successfully"
5559e4609f1SChristoph Müllner	@${ECHO_BLANK_LINE}
5569e4609f1SChristoph Müllnerendif
55773c99d4eSJuan Castillo
55873c99d4eSJuan Castillo$(DUMP): $(ELF)
559ee1ba6d4SAndre Przywara	$${ECHO} "  OD      $$@"
56073c99d4eSJuan Castillo	$${Q}$${OD} -dx $$< > $$@
56173c99d4eSJuan Castillo
56273c99d4eSJuan Castillo$(BIN): $(ELF)
563ee1ba6d4SAndre Przywara	$${ECHO} "  BIN     $$@"
56473c99d4eSJuan Castillo	$$(Q)$$(OC) -O binary $$< $$@
565052ab529SEvan Lloyd	@${ECHO_BLANK_LINE}
56673c99d4eSJuan Castillo	@echo "Built $$@ successfully"
567052ab529SEvan Lloyd	@${ECHO_BLANK_LINE}
56873c99d4eSJuan Castillo
569434d0491SZelalem Aweke.PHONY: $(1)
5709e4609f1SChristoph Müllnerifeq ($(DISABLE_BIN_GENERATION),1)
571434d0491SZelalem Aweke$(1): $(ELF) $(DUMP)
5729e4609f1SChristoph Müllnerelse
573434d0491SZelalem Aweke$(1): $(BIN) $(DUMP)
5749e4609f1SChristoph Müllnerendif
57573c99d4eSJuan Castillo
576434d0491SZelalem Awekeall: $(1)
57773c99d4eSJuan Castillo
578c6ba9b45SSumit Gargifeq ($(4),1)
579c6ba9b45SSumit Garg$(call ENCRYPT_FW,$(BIN),$(ENC_BIN))
580434d0491SZelalem Aweke$(if $(2),$(call TOOL_ADD_IMG_PAYLOAD,$(1),$(BIN),--$(2),$(ENC_BIN),$(3), \
581c6ba9b45SSumit Garg		$(ENC_BIN)))
582c6ba9b45SSumit Gargelse
583434d0491SZelalem Aweke$(if $(2),$(call TOOL_ADD_IMG_PAYLOAD,$(1),$(BIN),--$(2),$(BIN),$(3)))
584c6ba9b45SSumit Gargendif
58573c99d4eSJuan Castillo
58673c99d4eSJuan Castilloendef
58773c99d4eSJuan Castillo
58838c14d88SSoby Mathew# Convert device tree source file names to matching blobs
58938c14d88SSoby Mathew#   $(1) = input dts
59003b397a8SNishanth Menondefine SOURCES_TO_DTBS
59103b397a8SNishanth Menon        $(notdir $(patsubst %.dts,%.dtb,$(filter %.dts,$(1))))
59203b397a8SNishanth Menonendef
59303b397a8SNishanth Menon
59438c14d88SSoby Mathew# MAKE_FDT_DIRS macro creates the prerequisite directories that host the
59538c14d88SSoby Mathew# FDT binaries
59638c14d88SSoby Mathew#   $(1) = output directory
59738c14d88SSoby Mathew#   $(2) = input dts
59838c14d88SSoby Mathewdefine MAKE_FDT_DIRS
59938c14d88SSoby Mathew        $(eval DTBS       := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2))))
60003b397a8SNishanth Menon        $(eval TEMP_DTB_DIRS := $(sort $(dir ${DTBS})))
60103b397a8SNishanth Menon        # The $(dir ) function leaves a trailing / on the directory names
60203b397a8SNishanth Menon        # Rip off the / to match directory names with make rule targets.
60303b397a8SNishanth Menon        $(eval DTB_DIRS   := $(patsubst %/,%,$(TEMP_DTB_DIRS)))
60403b397a8SNishanth Menon
60503b397a8SNishanth Menon$(eval $(foreach objd,${DTB_DIRS},$(call MAKE_PREREQ_DIR,${objd},${BUILD_DIR})))
60603b397a8SNishanth Menon
60703b397a8SNishanth Menonfdt_dirs: ${DTB_DIRS}
60803b397a8SNishanth Menonendef
60903b397a8SNishanth Menon
61038c14d88SSoby Mathew# MAKE_DTB generate the Flattened device tree binary
61103b397a8SNishanth Menon#   $(1) = output directory
61203b397a8SNishanth Menon#   $(2) = input dts
61303b397a8SNishanth Menondefine MAKE_DTB
61403b397a8SNishanth Menon
615077b3e9aSYann Gautier# List of DTB file(s) to generate, based on DTS file basename list
61638c14d88SSoby Mathew$(eval DOBJ := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2))))
617077b3e9aSYann Gautier# List of the pre-compiled DTS file(s)
61801d237cbSYann Gautier$(eval DPRE := $(addprefix $(1)/,$(patsubst %.dts,%.pre.dts,$(notdir $(2)))))
619077b3e9aSYann Gautier# Dependencies of the pre-compiled DTS file(s) on its source and included files
620077b3e9aSYann Gautier$(eval DTSDEP := $(patsubst %.dtb,%.o.d,$(DOBJ)))
621077b3e9aSYann Gautier# Dependencies of the DT compilation on its pre-compiled DTS
622077b3e9aSYann Gautier$(eval DTBDEP := $(patsubst %.dtb,%.d,$(DOBJ)))
62303b397a8SNishanth Menon
6246bc1a30cSStephen Warren$(DOBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | fdt_dirs
625ee1ba6d4SAndre Przywara	$${ECHO} "  CPP     $$<"
626077b3e9aSYann Gautier	$(eval DTBS       := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2))))
6277e94a699SManish Pandey	$$(Q)$$(PP) $$(DTC_CPPFLAGS) -MT $(DTBS) -MMD -MF $(DTSDEP) -o $(DPRE) $$<
628ee1ba6d4SAndre Przywara	$${ECHO} "  DTC     $$<"
6292d51b55eSBalint Dobszay	$$(Q)$$(DTC) $$(DTC_FLAGS) -d $(DTBDEP) -o $$@ $(DPRE)
63003b397a8SNishanth Menon
631077b3e9aSYann Gautier-include $(DTBDEP)
632077b3e9aSYann Gautier-include $(DTSDEP)
63303b397a8SNishanth Menon
63403b397a8SNishanth Menonendef
63503b397a8SNishanth Menon
63603b397a8SNishanth Menon# MAKE_DTBS builds flattened device tree sources
63703b397a8SNishanth Menon#   $(1) = output directory
63803b397a8SNishanth Menon#   $(2) = list of flattened device tree source files
63903b397a8SNishanth Menondefine MAKE_DTBS
64003b397a8SNishanth Menon        $(eval DOBJS := $(filter %.dts,$(2)))
64103b397a8SNishanth Menon        $(eval REMAIN := $(filter-out %.dts,$(2)))
64238c14d88SSoby Mathew        $(and $(REMAIN),$(error FDT_SOURCES contain non-DTS files: $(REMAIN)))
64303b397a8SNishanth Menon        $(eval $(foreach obj,$(DOBJS),$(call MAKE_DTB,$(1),$(obj))))
64403b397a8SNishanth Menon
64538c14d88SSoby Mathew        $(eval $(call MAKE_FDT_DIRS,$(1),$(2)))
64638c14d88SSoby Mathew
64738c14d88SSoby Mathewdtbs: $(DTBS)
64838c14d88SSoby Mathewall: dtbs
64903b397a8SNishanth Menonendef
650