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