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