xref: /rk3399_ARM-atf/Makefile (revision 289e0dadbdc4dbe4ac78110f93262dc785d24418)
1#
2# Copyright (c) 2013-2014, 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# Decrease the verbosity of the make script
32# can be made verbose by passing V=1 at the make command line
33ifdef V
34  KBUILD_VERBOSE = ${V}
35else
36  KBUILD_VERBOSE = 0
37endif
38
39# Checkpatch ignores
40CHECK_IGNORE		=	--ignore COMPLEX_MACRO
41
42CHECKPATCH_ARGS		=	--no-tree --no-signoff ${CHECK_IGNORE}
43CHECKCODE_ARGS		=	--no-patch --no-tree --no-signoff ${CHECK_IGNORE}
44
45ifeq "${KBUILD_VERBOSE}" "0"
46	Q=@
47	CHECKCODE_ARGS	+=	--no-summary --terse
48else
49	Q=
50endif
51
52export Q
53
54DEBUG	?= 0
55
56ifneq (${DEBUG}, 0)
57	BUILD_TYPE	:=	debug
58else
59	BUILD_TYPE	:=	release
60endif
61
62BL_COMMON_SOURCES	:=	common/bl_common.c			\
63				lib/aarch64/cache_helpers.S		\
64				lib/aarch64/misc_helpers.S		\
65				lib/aarch64/tlb_helpers.S		\
66				lib/aarch64/xlat_helpers.c		\
67				lib/stdlib/std.c			\
68				lib/io_storage.c			\
69				plat/common/aarch64/platform_helpers.S
70
71ARCH 			?=	aarch64
72
73# By default, build fvp platform
74DEFAULT_PLAT		:=	fvp
75PLAT			?=	${DEFAULT_PLAT}
76# By default, build no SPD component
77SPD			?=	none
78
79BUILD_BASE		:=	./build
80BUILD_PLAT		:=	${BUILD_BASE}/${PLAT}/${BUILD_TYPE}
81
82PLATFORMS		:=	$(shell ls -I common plat/)
83SPDS			:=	$(shell ls -I none services/spd)
84HELP_PLATFORMS		:=	$(shell echo ${PLATFORMS} | sed 's/ /|/g')
85
86# Convenience function for adding build definitions
87# $(eval $(call add_define,FOO)) will have:
88# -DFOO if $(FOO) is empty; -DFOO=$(FOO) otherwise
89define add_define
90DEFINES			+=	-D$(1)$(if $(value $(1)),=$(value $(1)),)
91endef
92
93# Convenience function for verifying option has a boolean value
94# $(eval $(call assert_boolean,FOO)) will assert FOO is 0 or 1
95define assert_boolean
96$(and $(patsubst 0,,$(value $(1))),$(patsubst 1,,$(value $(1))),$(error $(1) must be boolean))
97endef
98
99ifeq (${PLAT},)
100  $(error "Error: Unknown platform. Please use PLAT=<platform name> to specify the platform.")
101endif
102ifeq ($(findstring ${PLAT},${PLATFORMS}),)
103  $(error "Error: Invalid platform. The following platforms are available: ${PLATFORMS}")
104endif
105
106all: msg_start
107
108msg_start:
109	@echo "Building ${PLAT}"
110
111include plat/${PLAT}/platform.mk
112
113ifdef BL1_SOURCES
114NEED_BL1 := yes
115include bl1/bl1.mk
116endif
117
118ifdef BL2_SOURCES
119NEED_BL2 := yes
120include bl2/bl2.mk
121endif
122
123ifdef BL31_SOURCES
124NEED_BL31 := yes
125include bl31/bl31.mk
126endif
127
128# Include SPD Makefile if one has been specified
129ifneq (${SPD},none)
130  # We expect to locate an spd.mk under the specified SPD directory
131  SPD_MAKE		:=	$(shell m="services/spd/${SPD}/${SPD}.mk"; [ -f "$$m" ] && echo "$$m")
132
133  ifeq (${SPD_MAKE},)
134    $(error Error: No services/spd/${SPD}/${SPD}.mk located)
135  endif
136  $(info Including ${SPD_MAKE})
137  include ${SPD_MAKE}
138
139  # If there's BL32 companion for the chosen SPD, and the SPD wants to build the
140  # BL2 from source, we expect that the SPD's Makefile would set NEED_BL32
141  # variable to "yes"
142endif
143
144.PHONY:			all msg_start clean realclean distclean cscope locate-checkpatch checkcodebase checkpatch fiptool fip
145.SUFFIXES:
146
147INCLUDES		+=	-Iinclude/bl1			\
148				-Iinclude/bl2			\
149				-Iinclude/bl31			\
150				-Iinclude/bl31/services		\
151				-Iinclude/bl32			\
152				-Iinclude/bl32/payloads		\
153				-Iinclude/common		\
154				-Iinclude/drivers		\
155				-Iinclude/drivers/arm		\
156				-Iinclude/lib			\
157				-Iinclude/lib/aarch64		\
158				-Iinclude/stdlib		\
159				-Iinclude/stdlib/sys		\
160				-Iplat/${PLAT}			\
161				${PLAT_INCLUDES}		\
162				${SPD_INCLUDES}
163
164ASFLAGS			+= 	-nostdinc -ffreestanding -Wa,--fatal-warnings	\
165				-mgeneral-regs-only -D__ASSEMBLY__ ${INCLUDES}	\
166				-DDEBUG=${DEBUG}
167CFLAGS			:= 	-nostdinc -pedantic -ffreestanding -Wall	\
168				-Werror -mgeneral-regs-only -std=c99 -c -Os	\
169				-DDEBUG=${DEBUG} ${INCLUDES} ${CFLAGS}
170CFLAGS			+=	-ffunction-sections -fdata-sections
171
172LDFLAGS			+=	--fatal-warnings -O1
173LDFLAGS			+=	--gc-sections
174
175
176ifneq (${DEBUG}, 0)
177#CFLAGS			+= 	-g -O0
178CFLAGS			+= 	-g
179# -save-temps -fverbose-asm
180ASFLAGS			+= 	-g -Wa,--gdwarf-2
181else
182CFLAGS			+=	-DNDEBUG=1
183endif
184
185
186CC			:=	${CROSS_COMPILE}gcc
187CPP			:=	${CROSS_COMPILE}cpp
188AS			:=	${CROSS_COMPILE}gcc
189AR			:=	${CROSS_COMPILE}ar
190LD			:=	${CROSS_COMPILE}ld
191OC			:=	${CROSS_COMPILE}objcopy
192OD			:=	${CROSS_COMPILE}objdump
193NM			:=	${CROSS_COMPILE}nm
194PP			:=	${CROSS_COMPILE}gcc -E ${CFLAGS}
195
196BASE_COMMIT		?=	origin/master
197
198# Variables for use with Firmware Image Package
199FIPTOOLPATH		?=	tools/fip_create
200FIPTOOL			?=	${FIPTOOLPATH}/fip_create
201fiptool:		${FIPTOOL}
202fip:			${BUILD_PLAT}/fip.bin
203
204locate-checkpatch:
205ifndef CHECKPATCH
206	$(error "Please set CHECKPATCH to point to the Linux checkpatch.pl file, eg: CHECKPATCH=../linux/script/checkpatch.pl")
207else
208ifeq (,$(wildcard ${CHECKPATCH}))
209	$(error "The file CHECKPATCH points to cannot be found, use eg: CHECKPATCH=../linux/script/checkpatch.pl")
210endif
211endif
212
213clean:
214			@echo "  CLEAN"
215			${Q}rm -rf ${BUILD_PLAT}
216			${Q}${MAKE} --no-print-directory -C ${FIPTOOLPATH} clean
217
218realclean distclean:
219			@echo "  REALCLEAN"
220			${Q}rm -rf ${BUILD_BASE}
221			${Q}rm -f ${CURDIR}/cscope.*
222			${Q}${MAKE} --no-print-directory -C ${FIPTOOLPATH} clean
223
224checkcodebase:		locate-checkpatch
225			@echo "  CHECKING STYLE"
226			@if test -d .git ; then	\
227				git ls-files | while read GIT_FILE ; do ${CHECKPATCH} ${CHECKCODE_ARGS} -f $$GIT_FILE ; done ;	\
228			 else			\
229				 find . -type f -not -iwholename "*.git*" -not -iwholename "*build*" -exec ${CHECKPATCH} ${CHECKCODE_ARGS} -f {} \; ;	\
230			 fi
231
232checkpatch:		locate-checkpatch
233			@echo "  CHECKING STYLE"
234			@git format-patch --stdout ${BASE_COMMIT} | ${CHECKPATCH} ${CHECKPATCH_ARGS} - || true
235
236${FIPTOOL}:
237			${Q}${MAKE} --no-print-directory -C ${FIPTOOLPATH}
238			@echo
239			@echo "Built $@ successfully"
240			@echo
241
242define match_goals
243$(strip $(foreach goal,$(1),$(filter $(goal),$(MAKECMDGOALS))))
244endef
245
246# List of rules that involve building things
247BUILD_TARGETS := all bl1 bl2 bl31 bl32 fip
248
249# Does the list of goals specified on the command line include a build target?
250ifneq ($(call match_goals,${BUILD_TARGETS}),)
251IS_ANYTHING_TO_BUILD := 1
252endif
253
254define MAKE_C
255
256$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
257$(eval PREREQUISITES := $(patsubst %.o,%.d,$(OBJ)))
258
259$(OBJ) : $(2)
260	@echo "  CC      $$<"
261	$$(Q)$$(CC) $$(CFLAGS) -c $$< -o $$@
262
263
264$(PREREQUISITES) : $(2)
265	@echo "  DEPS    $$@"
266	@mkdir -p $(1)
267	$$(Q)$$(CC) $$(CFLAGS) -M -MT $(OBJ) -MF $$@ $$<
268
269ifdef IS_ANYTHING_TO_BUILD
270-include $(PREREQUISITES)
271endif
272
273endef
274
275
276define MAKE_S
277
278$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
279$(eval PREREQUISITES := $(patsubst %.o,%.d,$(OBJ)))
280
281$(OBJ) : $(2)
282	@echo "  AS      $$<"
283	$$(Q)$$(AS) $$(ASFLAGS) -c $$< -o $$@
284
285$(PREREQUISITES) : $(2)
286	@echo "  DEPS    $$@"
287	@mkdir -p $(1)
288	$$(Q)$$(AS) $$(ASFLAGS) -M -MT $(OBJ) -MF $$@ $$<
289
290ifdef IS_ANYTHING_TO_BUILD
291-include $(PREREQUISITES)
292endif
293
294endef
295
296
297define MAKE_LD
298
299$(eval PREREQUISITES := $(1).d)
300
301$(1) : $(2)
302	@echo "  PP      $$<"
303	$$(Q)$$(AS) $$(ASFLAGS) -P -E -o $$@ $$<
304
305$(PREREQUISITES) : $(2)
306	@echo "  DEPS    $$@"
307	@mkdir -p $$(dir $$@)
308	$$(Q)$$(AS) $$(ASFLAGS) -M -MT $(1) -MF $$@ $$<
309
310ifdef IS_ANYTHING_TO_BUILD
311-include $(PREREQUISITES)
312endif
313
314endef
315
316
317define MAKE_OBJS
318	$(eval C_OBJS := $(filter %.c,$(2)))
319	$(eval REMAIN := $(filter-out %.c,$(2)))
320	$(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj))))
321
322	$(eval S_OBJS := $(filter %.S,$(REMAIN)))
323	$(eval REMAIN := $(filter-out %.S,$(REMAIN)))
324	$(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj))))
325
326	$(and $(REMAIN),$(error Unexpected source files present: $(REMAIN)))
327endef
328
329
330# NOTE: The line continuation '\' is required in the next define otherwise we
331# end up with a line-feed characer at the end of the last c filename.
332# Also bare this issue in mind if extending the list of supported filetypes.
333define SOURCES_TO_OBJS
334	$(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \
335	$(notdir $(patsubst %.S,%.o,$(filter %.S,$(1))))
336endef
337
338define MAKE_BL
339	$(eval BUILD_DIR  := ${BUILD_PLAT}/bl$(1))
340	$(eval SOURCES    := $(BL$(1)_SOURCES) $(BL_COMMON_SOURCES) $(PLAT_BL_COMMON_SOURCES))
341	$(eval OBJS       := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES))))
342	$(eval LINKERFILE := $(BUILD_DIR)/bl$(1).ld)
343	$(eval MAPFILE    := $(BUILD_DIR)/bl$(1).map)
344	$(eval ELF        := $(BUILD_DIR)/bl$(1).elf)
345	$(eval DUMP       := $(BUILD_DIR)/bl$(1).dump)
346	$(eval BIN        := $(BUILD_PLAT)/bl$(1).bin)
347
348	$(eval $(call MAKE_OBJS,$(BUILD_DIR),$(SOURCES)))
349	$(eval $(call MAKE_LD,$(LINKERFILE),$(BL$(1)_LINKERFILE)))
350
351$(BUILD_DIR) :
352	$$(Q)mkdir -p "$$@"
353
354$(ELF) : $(OBJS) $(LINKERFILE)
355	@echo "  LD      $$@"
356	@echo 'const char build_message[] = "Built : "__TIME__", "__DATE__;' | \
357		$$(CC) $$(CFLAGS) -xc - -o $(BUILD_DIR)/build_message.o
358	$$(Q)$$(LD) -o $$@ $$(LDFLAGS) -Map=$(MAPFILE) --script $(LINKERFILE) \
359					$(BUILD_DIR)/build_message.o $(OBJS)
360
361$(DUMP) : $(ELF)
362	@echo "  OD      $$@"
363	$${Q}$${OD} -dx $$< > $$@
364
365$(BIN) : $(ELF)
366	@echo "  BIN     $$@"
367	$$(Q)$$(OC) -O binary $$< $$@
368	@echo
369	@echo "Built $$@ successfully"
370	@echo
371
372.PHONY : bl$(1)
373bl$(1) : $(BUILD_DIR) $(BIN) $(DUMP)
374
375all : bl$(1)
376
377$(eval FIP_DEPS += $(if $2,$(BIN),))
378$(eval FIP_ARGS += $(if $2,--bl$(1) $(BIN),))
379
380endef
381
382
383ifeq (${NEED_BL1},yes)
384$(eval $(call MAKE_BL,1))
385endif
386
387ifeq (${NEED_BL2},yes)
388$(eval $(call MAKE_BL,2,in_fip))
389endif
390
391ifeq (${NEED_BL31},yes)
392BL31_SOURCES += ${SPD_SOURCES}
393$(eval $(call MAKE_BL,31,in_fip))
394endif
395
396ifeq (${NEED_BL32},yes)
397$(eval $(call MAKE_BL,32,in_fip))
398endif
399
400${BUILD_PLAT}/fip.bin:	${FIP_DEPS} ${BL33} ${FIPTOOL}
401			$(if ${BL33},,$(error "To build a FIP, please set BL33 to point to the Normal World binary, eg: BL33=../uefi/FVP_AARCH64_EFI.fd"))
402			${Q}${FIPTOOL} --dump \
403				${FIP_ARGS} \
404				--bl33 ${BL33} \
405				$@
406			@echo
407			@echo "Built $@ successfully"
408			@echo
409
410
411cscope:
412	@echo "  CSCOPE"
413	${Q}find ${CURDIR} -name "*.[chsS]" > cscope.files
414	${Q}cscope -b -q -k
415
416help:
417	@echo "usage: ${MAKE} PLAT=<${HELP_PLATFORMS}> <all|bl1|bl2|bl31|distclean|clean|checkcodebase|checkpatch>"
418	@echo ""
419	@echo "PLAT is used to specify which platform you wish to build."
420	@echo "If no platform is specified, PLAT defaults to: ${DEFAULT_PLAT}"
421	@echo ""
422	@echo "Supported Targets:"
423	@echo "  all            Build the BL1, BL2 and BL31 binaries"
424	@echo "  bl1            Build the BL1 binary"
425	@echo "  bl2            Build the BL2 binary"
426	@echo "  bl31           Build the BL31 binary"
427	@echo "  checkcodebase  Check the coding style of the entire source tree"
428	@echo "  checkpatch     Check the coding style on changes in the current"
429	@echo "                 branch against BASE_COMMIT (default origin/master)"
430	@echo "  clean          Clean the build for the selected platform"
431	@echo "  cscope         Generate cscope index"
432	@echo "  distclean      Remove all build artifacts for all platforms"
433	@echo "  fiptool        Build the Firmware Image Package(FIP) creation tool"
434	@echo ""
435	@echo "note: most build targets require PLAT to be set to a specific platform."
436	@echo ""
437	@echo "example: build all targets for the FVP platform:"
438	@echo "  CROSS_COMPILE=aarch64-none-elf- make PLAT=fvp all"
439