xref: /rk3399_ARM-atf/make_helpers/build_macros.mk (revision 7623e085cb5396054b72f1ea3f02e8c7a34568b5)
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 := $($(call uppercase,$(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 _V := $(call uppercase,$(1)))
224
225    # $(check_$(1)_cmd) variable is executed in the check_$(1) target and also
226    # is put into the ${CHECK_$(3)FIP_CMD} variable which is executed by the
227    # target ${BUILD_PLAT}/${$(3)FIP_NAME}.
228    $(eval check_$(1)_cmd := \
229        $(if $(value $(_V)),,$$$$(error "Platform '${PLAT}' requires $(_V). Please set $(_V) to point to the right file")) \
230        $(if $(wildcard $(value $(_V))),,$$$$(error '$(_V)=$(value $(_V))' was specified, but '$(value $(_V))' does not exist)) \
231    )
232
233    $(3)CRT_DEPS += check_$(1)
234    CHECK_$(3)FIP_CMD += $$(check_$(1)_cmd)
235ifeq ($(4),1)
236    $(eval ENC_BIN := ${BUILD_PLAT}/$(1)_enc.bin)
237    $(call ENCRYPT_FW,$(value $(_V)),$(ENC_BIN))
238    $(call TOOL_ADD_IMG_PAYLOAD,$(1),$(value $(_V)),$(2),$(ENC_BIN),$(3), \
239		$(ENC_BIN))
240else
241    $(call TOOL_ADD_IMG_PAYLOAD,$(1),$(value $(_V)),$(2),$(if $(wildcard $(value $(_V))),$(value $(_V)),FORCE),$(3))
242endif
243
244.PHONY: check_$(1)
245check_$(1):
246	$(check_$(1)_cmd)
247endef
248
249# SELECT_OPENSSL_API_VERSION selects the OpenSSL API version to be used to
250# build the host tools by checking the version of OpenSSL located under
251# the path defined by the OPENSSL_DIR variable. It receives no parameters.
252define SELECT_OPENSSL_API_VERSION
253    # Set default value for USING_OPENSSL3 macro to 0
254    $(eval USING_OPENSSL3 = 0)
255    # Obtain the OpenSSL version for the build located under OPENSSL_DIR
256    $(eval OPENSSL_INFO := $(shell LD_LIBRARY_PATH=${OPENSSL_DIR}:${OPENSSL_DIR}/lib ${OPENSSL_BIN_PATH}/openssl version))
257    $(eval OPENSSL_CURRENT_VER = $(word 2, ${OPENSSL_INFO}))
258    $(eval OPENSSL_CURRENT_VER_MAJOR = $(firstword $(subst ., ,$(OPENSSL_CURRENT_VER))))
259    # If OpenSSL version is 3.x, then set USING_OPENSSL3 flag to 1
260    $(if $(filter 3,$(OPENSSL_CURRENT_VER_MAJOR)), $(eval USING_OPENSSL3 = 1))
261endef
262
263################################################################################
264# Generic image processing filters
265################################################################################
266
267# GZIP
268define GZIP_RULE
269$(1): $(2)
270	$(s)echo "  GZIP    $$@"
271	$(q)gzip -n -f -9 $$< --stdout > $$@
272endef
273
274GZIP_SUFFIX := .gz
275
276################################################################################
277# Auxiliary macros to build TF images from sources
278################################################################################
279
280MAKE_DEP = -Wp,-MD,$(DEP) -MT $$@ -MP
281
282
283# MAKE_C_LIB builds a C source file and generates the dependency file
284#   $(1) = output directory
285#   $(2) = source file (%.c)
286#   $(3) = library name
287define MAKE_C_LIB
288$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
289$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
290$(eval LIB := $(call uppercase, $(notdir $(1))))
291
292$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
293	$$(s)echo "  CC      $$<"
294	$$(q)$($(ARCH)-cc) $$($(LIB)_CFLAGS) $$(TF_CFLAGS) $$(CFLAGS) $(MAKE_DEP) -c $$< -o $$@
295
296-include $(DEP)
297
298endef
299
300# MAKE_S_LIB builds an assembly source file and generates the dependency file
301#   $(1) = output directory
302#   $(2) = source file (%.S)
303#   $(3) = library name
304define MAKE_S_LIB
305$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
306$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
307
308$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
309	$$(s)echo "  AS      $$<"
310	$$(q)$($(ARCH)-as) -x assembler-with-cpp $$(TF_CFLAGS_$(ARCH)) $$(ASFLAGS) $(MAKE_DEP) -c $$< -o $$@
311
312-include $(DEP)
313
314endef
315
316
317# MAKE_C builds a C source file and generates the dependency file
318#   $(1) = output directory
319#   $(2) = source file (%.c)
320#   $(3) = BL stage
321define MAKE_C
322
323$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
324$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
325
326$(eval BL_DEFINES := IMAGE_$(call uppercase,$(3)) $($(call uppercase,$(3))_DEFINES) $(PLAT_BL_COMMON_DEFINES))
327$(eval BL_INCLUDE_DIRS := $($(call uppercase,$(3))_INCLUDE_DIRS) $(PLAT_BL_COMMON_INCLUDE_DIRS))
328$(eval BL_CPPFLAGS := $($(call uppercase,$(3))_CPPFLAGS) $(addprefix -D,$(BL_DEFINES)) $(addprefix -I,$(BL_INCLUDE_DIRS)) $(PLAT_BL_COMMON_CPPFLAGS))
329$(eval BL_CFLAGS := $($(call uppercase,$(3))_CFLAGS) $(PLAT_BL_COMMON_CFLAGS))
330
331$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
332	$$(s)echo "  CC      $$<"
333	$$(q)$($(ARCH)-cc) $$(LTO_CFLAGS) $$(TF_CFLAGS) $$(CFLAGS) $(BL_CPPFLAGS) $(BL_CFLAGS) $(MAKE_DEP) -c $$< -o $$@
334
335-include $(DEP)
336
337endef
338
339
340# MAKE_S builds an assembly source file and generates the dependency file
341#   $(1) = output directory
342#   $(2) = assembly file (%.S)
343#   $(3) = BL stage
344define MAKE_S
345
346$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
347$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
348
349$(eval BL_DEFINES := IMAGE_$(call uppercase,$(3)) $($(call uppercase,$(3))_DEFINES) $(PLAT_BL_COMMON_DEFINES))
350$(eval BL_INCLUDE_DIRS := $($(call uppercase,$(3))_INCLUDE_DIRS) $(PLAT_BL_COMMON_INCLUDE_DIRS))
351$(eval BL_CPPFLAGS := $($(call uppercase,$(3))_CPPFLAGS) $(addprefix -D,$(BL_DEFINES)) $(addprefix -I,$(BL_INCLUDE_DIRS)) $(PLAT_BL_COMMON_CPPFLAGS))
352$(eval BL_ASFLAGS := $($(call uppercase,$(3))_ASFLAGS) $(PLAT_BL_COMMON_ASFLAGS))
353
354$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
355	$$(s)echo "  AS      $$<"
356	$$(q)$($(ARCH)-as) -x assembler-with-cpp $$(TF_CFLAGS_$(ARCH)) $$(ASFLAGS) $(BL_CPPFLAGS) $(BL_ASFLAGS) $(MAKE_DEP) -c $$< -o $$@
357
358-include $(DEP)
359
360endef
361
362
363# MAKE_LD generate the linker script using the C preprocessor
364#   $(1) = output linker script
365#   $(2) = input template
366#   $(3) = BL stage
367define MAKE_LD
368
369$(eval DEP := $(1).d)
370
371$(eval BL_DEFINES := IMAGE_$(call uppercase,$(3)) $($(call uppercase,$(3))_DEFINES) $(PLAT_BL_COMMON_DEFINES))
372$(eval BL_INCLUDE_DIRS := $($(call uppercase,$(3))_INCLUDE_DIRS) $(PLAT_BL_COMMON_INCLUDE_DIRS))
373$(eval BL_CPPFLAGS := $($(call uppercase,$(3))_CPPFLAGS) $(addprefix -D,$(BL_DEFINES)) $(addprefix -I,$(BL_INCLUDE_DIRS)) $(PLAT_BL_COMMON_CPPFLAGS))
374
375$(1): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
376	$$(s)echo "  PP      $$<"
377	$$(q)$($(ARCH)-cpp) -E $$(CPPFLAGS) $(BL_CPPFLAGS) $(TF_CFLAGS_$(ARCH)) -P -x assembler-with-cpp -D__LINKER__ $(MAKE_DEP) -o $$@ $$<
378
379-include $(DEP)
380
381endef
382
383# MAKE_LIB_OBJS builds both C and assembly source files
384#   $(1) = output directory
385#   $(2) = list of source files
386#   $(3) = name of the library
387define MAKE_LIB_OBJS
388        $(eval C_OBJS := $(filter %.c,$(2)))
389        $(eval REMAIN := $(filter-out %.c,$(2)))
390        $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C_LIB,$(1),$(obj),$(3))))
391
392        $(eval S_OBJS := $(filter %.S,$(REMAIN)))
393        $(eval REMAIN := $(filter-out %.S,$(REMAIN)))
394        $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S_LIB,$(1),$(obj),$(3))))
395
396        $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN)))
397endef
398
399
400# MAKE_OBJS builds both C and assembly source files
401#   $(1) = output directory
402#   $(2) = list of source files (both C and assembly)
403#   $(3) = BL stage
404define MAKE_OBJS
405        $(eval C_OBJS := $(filter %.c,$(2)))
406        $(eval REMAIN := $(filter-out %.c,$(2)))
407        $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj),$(3))))
408
409        $(eval S_OBJS := $(filter %.S,$(REMAIN)))
410        $(eval REMAIN := $(filter-out %.S,$(REMAIN)))
411        $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj),$(3))))
412
413        $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN)))
414endef
415
416
417# NOTE: The line continuation '\' is required in the next define otherwise we
418# end up with a line-feed characer at the end of the last c filename.
419# Also bear this issue in mind if extending the list of supported filetypes.
420define SOURCES_TO_OBJS
421        $(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \
422        $(notdir $(patsubst %.S,%.o,$(filter %.S,$(1))))
423endef
424
425.PHONY: libraries
426
427# MAKE_LIB macro defines the targets and options to build each BL image.
428# Arguments:
429#   $(1) = Library name
430define MAKE_LIB
431        $(eval BUILD_DIR  := ${BUILD_PLAT}/lib$(1))
432        $(eval LIB_DIR    := ${BUILD_PLAT}/lib)
433        $(eval ROMLIB_DIR    := ${BUILD_PLAT}/romlib)
434        $(eval SOURCES    := $(LIB$(call uppercase,$(1))_SRCS))
435        $(eval OBJS       := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES))))
436
437$(eval $(call MAKE_LIB_OBJS,$(BUILD_DIR),$(SOURCES),$(1)))
438
439libraries: ${LIB_DIR}/lib$(1).a
440ifeq ($($(ARCH)-ld-id),arm-link)
441LDPATHS = --userlibpath=${LIB_DIR}
442LDLIBS += --library=$(1)
443else
444LDPATHS = -L${LIB_DIR}
445LDLIBS += -l$(1)
446endif
447
448ifeq ($(USE_ROMLIB),1)
449LIBWRAPPER = -lwrappers
450endif
451
452all: ${LIB_DIR}/lib$(1).a
453
454${LIB_DIR}/lib$(1).a: $(OBJS) | $$$$(@D)/
455	$$(s)echo "  AR      $$@"
456	$$(q)$($(ARCH)-ar) cr $$@ $$?
457endef
458
459# Generate the path to one or more preprocessed linker scripts given the paths
460# of their sources.
461#
462# Arguments:
463#   $(1) = path to one or more linker script sources
464define linker_script_path
465        $(patsubst %.S,$(BUILD_DIR)/%,$(1))
466endef
467
468ifeq ($(USE_ROMLIB),1)
469WRAPPER_FLAGS := @${BUILD_PLAT}/romlib/romlib.ldflags
470endif
471
472# MAKE_BL macro defines the targets and options to build each BL image.
473# Arguments:
474#   $(1) = BL stage
475#   $(2) = FIP command line option (if empty, image will not be included in the FIP)
476#   $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
477#   $(4) = BL encryption flag (optional) (0, 1)
478define MAKE_BL
479        $(eval BUILD_DIR  := ${BUILD_PLAT}/$(1))
480        $(eval BL_SOURCES := $($(call uppercase,$(1))_SOURCES))
481        $(eval SOURCES    := $(sort $(BL_SOURCES) $(BL_COMMON_SOURCES) $(PLAT_BL_COMMON_SOURCES)))
482        $(eval OBJS       := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES))))
483        $(eval MAPFILE    := $(call IMG_MAPFILE,$(1)))
484        $(eval ELF        := $(call IMG_ELF,$(1)))
485        $(eval DUMP       := $(call IMG_DUMP,$(1)))
486        $(eval BIN        := $(call IMG_BIN,$(1)))
487        $(eval ENC_BIN    := $(call IMG_ENC_BIN,$(1)))
488        $(eval BL_LIBS    := $($(call uppercase,$(1))_LIBS))
489
490        $(eval DEFAULT_LINKER_SCRIPT_SOURCE := $($(call uppercase,$(1))_DEFAULT_LINKER_SCRIPT_SOURCE))
491        $(eval DEFAULT_LINKER_SCRIPT := $(call linker_script_path,$(DEFAULT_LINKER_SCRIPT_SOURCE)))
492
493        $(eval LINKER_SCRIPT_SOURCES := $($(call uppercase,$(1))_LINKER_SCRIPT_SOURCES))
494        $(eval LINKER_SCRIPTS := $(call linker_script_path,$(LINKER_SCRIPT_SOURCES)))
495
496$(eval $(call MAKE_OBJS,$(BUILD_DIR),$(SOURCES),$(1)))
497
498# Generate targets to preprocess each required linker script
499$(eval $(foreach source,$(DEFAULT_LINKER_SCRIPT_SOURCE) $(LINKER_SCRIPT_SOURCES), \
500        $(call MAKE_LD,$(call linker_script_path,$(source)),$(source),$(1))))
501
502$(eval BL_LDFLAGS := $($(call uppercase,$(1))_LDFLAGS))
503
504ifeq ($(USE_ROMLIB),1)
505$(ELF): romlib.bin | $$$$(@D)/
506endif
507
508# MODULE_OBJS can be assigned by vendors with different compiled
509# object file path, and prebuilt object file path.
510$(eval OBJS += $(MODULE_OBJS))
511
512$(ELF): $(OBJS) $(DEFAULT_LINKER_SCRIPT) $(LINKER_SCRIPTS) | $$$$(@D)/ libraries $(BL_LIBS)
513	$$(s)echo "  LD      $$@"
514ifeq ($($(ARCH)-ld-id),arm-link)
515	$$(q)$($(ARCH)-ld) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $(BL_LDFLAGS) --entry=${1}_entrypoint \
516		--predefine=$(call escape-shell,-D__LINKER__=$(__LINKER__)) \
517		--predefine=$(call escape-shell,-DTF_CFLAGS=$(TF_CFLAGS)) \
518		--map --list="$(MAPFILE)" --scatter=${PLAT_DIR}/scat/${1}.scat \
519		$(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS) $(OBJS)
520else ifeq ($($(ARCH)-ld-id),gnu-gcc)
521	$$(q)$($(ARCH)-ld) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $$(WRAPPER_FLAGS) $(BL_LDFLAGS) -Wl,-Map=$(MAPFILE) \
522		$(addprefix -Wl$(comma)--script$(comma),$(LINKER_SCRIPTS)) -Wl,--script,$(DEFAULT_LINKER_SCRIPT) \
523		$(OBJS) $(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS)
524else
525	$$(q)$($(ARCH)-ld) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $$(WRAPPER_FLAGS) $(BL_LDFLAGS) -Map=$(MAPFILE) \
526		$(addprefix -T ,$(LINKER_SCRIPTS)) --script $(DEFAULT_LINKER_SCRIPT) \
527		$(OBJS) $(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS)
528endif
529ifeq ($(DISABLE_BIN_GENERATION),1)
530	$(s)echo
531	$(s)echo "Built $$@ successfully"
532	$(s)echo
533endif
534
535$(DUMP): $(ELF) | $$$$(@D)/
536	$$(s)echo "  OD      $$@"
537	$$(q)$($(ARCH)-od) -dx $$< > $$@
538
539$(BIN): $(ELF) | $$$$(@D)/
540	$$(s)echo "  BIN     $$@"
541	$$(q)$($(ARCH)-oc) -O binary $$< $$@
542	$(s)echo
543	$(s)echo "Built $$@ successfully"
544	$(s)echo
545
546.PHONY: $(1)
547ifeq ($(DISABLE_BIN_GENERATION),1)
548$(1): $(ELF) $(DUMP)
549else
550$(1): $(BIN) $(DUMP)
551endif
552
553all: $(1)
554
555ifeq ($(4),1)
556$(call ENCRYPT_FW,$(BIN),$(ENC_BIN))
557$(if $(2),$(call TOOL_ADD_IMG_PAYLOAD,$(1),$(BIN),--$(2),$(ENC_BIN),$(3), \
558		$(ENC_BIN)))
559else
560$(if $(2),$(call TOOL_ADD_IMG_PAYLOAD,$(1),$(BIN),--$(2),$(BIN),$(3)))
561endif
562
563endef
564
565# Convert device tree source file names to matching blobs
566#   $(1) = input dts
567define SOURCES_TO_DTBS
568        $(notdir $(patsubst %.dts,%.dtb,$(filter %.dts,$(1))))
569endef
570
571# MAKE_DTB generate the Flattened device tree binary
572#   $(1) = output directory
573#   $(2) = input dts
574define MAKE_DTB
575
576# List of DTB file(s) to generate, based on DTS file basename list
577$(eval DOBJ := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2))))
578# List of the pre-compiled DTS file(s)
579$(eval DPRE := $(addprefix $(1)/,$(patsubst %.dts,%.pre.dts,$(notdir $(2)))))
580# Dependencies of the pre-compiled DTS file(s) on its source and included files
581$(eval DTSDEP := $(patsubst %.dtb,%.o.d,$(DOBJ)))
582# Dependencies of the DT compilation on its pre-compiled DTS
583$(eval DTBDEP := $(patsubst %.dtb,%.d,$(DOBJ)))
584
585$(DPRE): $(2) | $$$$(@D)/
586	$$(s)echo "  CPP     $$<"
587	$(eval DTBS       := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2))))
588	$$(q)$($(ARCH)-cpp) -E $$(TF_CFLAGS_$(ARCH)) $$(DTC_CPPFLAGS) -MT $(DTBS) -MMD -MF $(DTSDEP) -o $(DPRE) $$<
589
590$(DOBJ): $(DPRE) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
591	$$(s)echo "  DTC     $$<"
592	$$(q)$($(ARCH)-dtc) $$(DTC_FLAGS) -d $(DTBDEP) -o $$@ $$<
593
594-include $(DTBDEP)
595-include $(DTSDEP)
596
597endef
598
599# MAKE_DTBS builds flattened device tree sources
600#   $(1) = output directory
601#   $(2) = list of flattened device tree source files
602define MAKE_DTBS
603        $(eval DOBJS := $(filter %.dts,$(2)))
604        $(eval REMAIN := $(filter-out %.dts,$(2)))
605        $(and $(REMAIN),$(error FDT_SOURCES contain non-DTS files: $(REMAIN)))
606        $(eval $(foreach obj,$(DOBJS),$(call MAKE_DTB,$(1),$(obj))))
607
608dtbs: $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2)))
609all: dtbs
610endef
611