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