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