xref: /rk3399_ARM-atf/make_helpers/build_macros.mk (revision 73c99d4eb377e0e25f7951be53087bf92e7b4b18)
1#
2# Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are met:
6#
7# Redistributions of source code must retain the above copyright notice, this
8# list of conditions and the following disclaimer.
9#
10# Redistributions in binary form must reproduce the above copyright notice,
11# this list of conditions and the following disclaimer in the documentation
12# and/or other materials provided with the distribution.
13#
14# Neither the name of ARM nor the names of its contributors may be used
15# to endorse or promote products derived from this software without specific
16# prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29#
30
31# Convenience function for adding build definitions
32# $(eval $(call add_define,FOO)) will have:
33# -DFOO if $(FOO) is empty; -DFOO=$(FOO) otherwise
34define add_define
35    DEFINES			+=	-D$(1)$(if $(value $(1)),=$(value $(1)),)
36endef
37
38# Convenience function for verifying option has a boolean value
39# $(eval $(call assert_boolean,FOO)) will assert FOO is 0 or 1
40define assert_boolean
41    $(and $(patsubst 0,,$(value $(1))),$(patsubst 1,,$(value $(1))),$(error $(1) must be boolean))
42endef
43
44# IMG_LINKERFILE defines the linker script corresponding to a BL stage
45#   $(1) = BL stage (2, 30, 31, 32, 33)
46define IMG_LINKERFILE
47    ${BUILD_DIR}/bl$(1).ld
48endef
49
50# IMG_MAPFILE defines the output file describing the memory map corresponding
51# to a BL stage
52#   $(1) = BL stage (2, 30, 31, 32, 33)
53define IMG_MAPFILE
54    ${BUILD_DIR}/bl$(1).map
55endef
56
57# IMG_ELF defines the elf file corresponding to a BL stage
58#   $(1) = BL stage (2, 30, 31, 32, 33)
59define IMG_ELF
60    ${BUILD_DIR}/bl$(1).elf
61endef
62
63# IMG_DUMP defines the symbols dump file corresponding to a BL stage
64#   $(1) = BL stage (2, 30, 31, 32, 33)
65define IMG_DUMP
66    ${BUILD_DIR}/bl$(1).dump
67endef
68
69# IMG_BIN defines the default image file corresponding to a BL stage
70#   $(1) = BL stage (2, 30, 31, 32, 33)
71define IMG_BIN
72    ${BUILD_PLAT}/bl$(1).bin
73endef
74
75# FIP_ADD_PAYLOAD appends the command line arguments required by the FIP tool
76# to package a new payload. Optionally, it adds the dependency on this payload
77#   $(1) = payload filename (i.e. bl31.bin)
78#   $(2) = command line option for the specified payload (i.e. --bl31)
79#   $(3) = fip target dependency (optional) (i.e. bl31)
80define FIP_ADD_PAYLOAD
81    $(eval FIP_ARGS += $(2) $(1))
82    $(eval $(if $(3),FIP_DEPS += $(3)))
83endef
84
85# CERT_ADD_CMD_OPT adds a new command line option to the cert_create invokation
86#   $(1) = parameter filename
87#   $(2) = cert_create command line option for the specified parameter
88#   $(3) = input parameter (false if empty)
89define CERT_ADD_CMD_OPT
90    $(eval $(if $(3),CRT_DEPS += $(1)))
91    $(eval CRT_ARGS += $(2) $(1))
92endef
93
94# FIP_ADD_IMG allows the platform to specify an image to be packed in the FIP
95# using a build option. It also adds a dependency on the image file, aborting
96# the build if the file does not exist.
97#   $(1) = build option to specify the image filename (BL30, BL33, etc)
98#   $(2) = command line option for the fip_create tool (bl30, bl33, etc)
99# Example:
100#   $(eval $(call FIP_ADD_IMG,BL33,--bl33))
101define FIP_ADD_IMG
102    CRT_DEPS += check_$(1)
103    FIP_DEPS += check_$(1)
104    $(call FIP_ADD_PAYLOAD,$(value $(1)),$(2))
105
106check_$(1):
107	$$(if $(value $(1)),,$$(error "Platform '${PLAT}' requires $(1). Please set $(1) to point to the right file"))
108endef
109
110
111################################################################################
112# Auxiliary macros to build TF images from sources
113################################################################################
114
115define match_goals
116$(strip $(foreach goal,$(1),$(filter $(goal),$(MAKECMDGOALS))))
117endef
118
119# List of rules that involve building things
120BUILD_TARGETS := all bl1 bl2 bl31 bl32 certificates fip
121
122# Does the list of goals specified on the command line include a build target?
123ifneq ($(call match_goals,${BUILD_TARGETS}),)
124IS_ANYTHING_TO_BUILD := 1
125endif
126
127
128# MAKE_C builds a C source file and generates the dependency file
129#   $(1) = output directory
130#   $(2) = source file (%.c)
131#   $(3) = BL stage (2, 30, 31, 32, 33)
132define MAKE_C
133
134$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
135$(eval PREREQUISITES := $(patsubst %.o,%.d,$(OBJ)))
136
137$(OBJ): $(2)
138	@echo "  CC      $$<"
139	$$(Q)$$(CC) $$(CFLAGS) -DIMAGE_BL$(3) -c $$< -o $$@
140
141
142$(PREREQUISITES): $(2)
143	@echo "  DEPS    $$@"
144	@mkdir -p $(1)
145	$$(Q)$$(CC) $$(CFLAGS) -M -MT $(OBJ) -MF $$@ $$<
146
147ifdef IS_ANYTHING_TO_BUILD
148-include $(PREREQUISITES)
149endif
150
151endef
152
153
154# MAKE_S builds an assembly source file and generates the dependency file
155#   $(1) = output directory
156#   $(2) = assembly file (%.S)
157#   $(3) = BL stage (2, 30, 31, 32, 33)
158define MAKE_S
159
160$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
161$(eval PREREQUISITES := $(patsubst %.o,%.d,$(OBJ)))
162
163$(OBJ): $(2)
164	@echo "  AS      $$<"
165	$$(Q)$$(AS) $$(ASFLAGS) -DIMAGE_BL$(3) -c $$< -o $$@
166
167$(PREREQUISITES): $(2)
168	@echo "  DEPS    $$@"
169	@mkdir -p $(1)
170	$$(Q)$$(AS) $$(ASFLAGS) -M -MT $(OBJ) -MF $$@ $$<
171
172ifdef IS_ANYTHING_TO_BUILD
173-include $(PREREQUISITES)
174endif
175
176endef
177
178
179# MAKE_LD generate the linker script using the C preprocessor
180#   $(1) = output linker script
181#   $(2) = input template
182define MAKE_LD
183
184$(eval PREREQUISITES := $(1).d)
185
186$(1): $(2)
187	@echo "  PP      $$<"
188	$$(Q)$$(AS) $$(ASFLAGS) -P -E -D__LINKER__ -o $$@ $$<
189
190$(PREREQUISITES): $(2)
191	@echo "  DEPS    $$@"
192	@mkdir -p $$(dir $$@)
193	$$(Q)$$(AS) $$(ASFLAGS) -M -MT $(1) -MF $$@ $$<
194
195ifdef IS_ANYTHING_TO_BUILD
196-include $(PREREQUISITES)
197endif
198
199endef
200
201
202# MAKE_OBJS builds both C and assembly source files
203#   $(1) = output directory
204#   $(2) = list of source files (both C and assembly)
205#   $(3) = BL stage (2, 30, 31, 32, 33)
206define MAKE_OBJS
207        $(eval C_OBJS := $(filter %.c,$(2)))
208        $(eval REMAIN := $(filter-out %.c,$(2)))
209        $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj),$(3))))
210
211        $(eval S_OBJS := $(filter %.S,$(REMAIN)))
212        $(eval REMAIN := $(filter-out %.S,$(REMAIN)))
213        $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj),$(3))))
214
215        $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN)))
216endef
217
218
219# NOTE: The line continuation '\' is required in the next define otherwise we
220# end up with a line-feed characer at the end of the last c filename.
221# Also bare this issue in mind if extending the list of supported filetypes.
222define SOURCES_TO_OBJS
223        $(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \
224        $(notdir $(patsubst %.S,%.o,$(filter %.S,$(1))))
225endef
226
227
228# MAKE_TOOL_ARGS macro defines the command line arguments for the FIP tool for
229# each BL image. Arguments:
230#   $(1) = BL stage (2, 30, 31, 32, 33)
231#   $(2) = Binary file
232#   $(3) = In FIP (false if empty)
233define MAKE_TOOL_ARGS
234        $(if $(3),$(eval $(call FIP_ADD_PAYLOAD,$(2),--bl$(1),bl$(1))))
235endef
236
237
238# MAKE_BL macro defines the targets and options to build each BL image.
239# Arguments:
240#   $(1) = BL stage (2, 30, 31, 32, 33)
241#   $(2) = In FIP (false if empty)
242define MAKE_BL
243        $(eval BUILD_DIR  := ${BUILD_PLAT}/bl$(1))
244        $(eval SOURCES    := $(BL$(1)_SOURCES) $(BL_COMMON_SOURCES) $(PLAT_BL_COMMON_SOURCES))
245        $(eval OBJS       := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES))))
246        $(eval LINKERFILE := $(call IMG_LINKERFILE,$(1)))
247        $(eval MAPFILE    := $(call IMG_MAPFILE,$(1)))
248        $(eval ELF        := $(call IMG_ELF,$(1)))
249        $(eval DUMP       := $(call IMG_DUMP,$(1)))
250        $(eval BIN        := $(call IMG_BIN,$(1)))
251
252        $(eval $(call MAKE_OBJS,$(BUILD_DIR),$(SOURCES),$(1)))
253        $(eval $(call MAKE_LD,$(LINKERFILE),$(BL$(1)_LINKERFILE)))
254
255$(BUILD_DIR):
256	$$(Q)mkdir -p "$$@"
257
258$(ELF): $(OBJS) $(LINKERFILE)
259	@echo "  LD      $$@"
260	@echo 'const char build_message[] = "Built : "__TIME__", "__DATE__; \
261	       const char version_string[] = "${VERSION_STRING}";' | \
262		$$(CC) $$(CFLAGS) -xc - -o $(BUILD_DIR)/build_message.o
263	$$(Q)$$(LD) -o $$@ $$(LDFLAGS) -Map=$(MAPFILE) --script $(LINKERFILE) \
264					$(BUILD_DIR)/build_message.o $(OBJS)
265
266$(DUMP): $(ELF)
267	@echo "  OD      $$@"
268	$${Q}$${OD} -dx $$< > $$@
269
270$(BIN): $(ELF)
271	@echo "  BIN     $$@"
272	$$(Q)$$(OC) -O binary $$< $$@
273	@echo
274	@echo "Built $$@ successfully"
275	@echo
276
277.PHONY: bl$(1)
278bl$(1): $(BUILD_DIR) $(BIN) $(DUMP)
279
280all: bl$(1)
281
282$(eval $(call MAKE_TOOL_ARGS,$(1),$(BIN),$(2)))
283
284endef
285
286