summaryrefslogtreecommitdiff
path: root/Makefile
blob: 6abb10caa15486b93f4ba270c00580452f8e5a90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#
# Copyright (c) 2014-2017, ARM Limited and Contributors. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# Neither the name of ARM nor the names of its contributors may be used
# to endorse or promote products derived from this software without specific
# prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

# TFTF Version
VERSION_MAJOR		:= 0
VERSION_MINOR		:= 1

################################################################################
# Default values for build configurations, and their dependencies
################################################################################

include defaults.mk

PLAT			:= ${DEFAULT_PLAT}

# Assertions enabled for DEBUG builds by default
ENABLE_ASSERTIONS	:= ${DEBUG}

################################################################################

# Do not check the coding style on C library files
CHECK_PATHS		=	$(shell ls -I include -I lib -I tools) \
				$(addprefix include/,$(shell ls -I stdlib include)) \
				$(addprefix lib/,$(shell ls -I stdlib lib))

ifeq (${V},0)
	Q=@
else
	Q=
endif
export Q

ifneq (${DEBUG}, 0)
	BUILD_TYPE	:=	debug
	# Use LOG_LEVEL_INFO by default for debug builds
	LOG_LEVEL       :=      40
else
	BUILD_TYPE	:=	release
	# Use LOG_LEVEL_ERROR by default for release builds
	LOG_LEVEL       :=      20
endif

# Default build string (git branch and commit)
ifeq (${BUILD_STRING},)
        BUILD_STRING    :=      $(shell git log -n 1 --pretty=format:"%h")
endif

VERSION_STRING		:= 	v${VERSION_MAJOR}.${VERSION_MINOR}(${PLAT},${BUILD_TYPE}):${BUILD_STRING}

BUILD_BASE		:=	./build
BUILD_PLAT		:=	${BUILD_BASE}/${PLAT}/${BUILD_TYPE}

PLAT_MAKEFILE		:=	platform.mk
# Generate the platforms list by recursively searching for all directories
# under /plat containing a PLAT_MAKEFILE. Append each platform with a `|`
# char and strip out the final '|'.
PLATFORMS		:=	$(shell find plat/ -name '${PLAT_MAKEFILE}' -print0 |			\
					sed -r 's%[^\x00]*\/([^/]*)\/${PLAT_MAKEFILE}\x00%\1|%g' |	\
					sed -r 's/\|$$//')

# Convenience function for adding build definitions
# $(eval $(call add_define,FOO)) will have:
# -DFOO if $(FOO) is empty; -DFOO=$(FOO) otherwise
define add_define
DEFINES			+=	-D$(1)$(if $(value $(1)),=$(value $(1)),)
endef

# Convenience function for verifying option has a boolean value
# $(eval $(call assert_boolean,FOO)) will assert FOO is 0 or 1
define assert_boolean
$(and $(patsubst 0,,$(value $(1))),$(patsubst 1,,$(value $(1))),$(error $(1) must be boolean))
endef

ifeq (${PLAT},)
  $(error "Error: Unknown platform. Please use PLAT=<platform name> to specify the platform")
endif
PLAT_PATH		:=	$(shell find plat/ -wholename '*/${PLAT}')
PLAT_MAKEFILE_FULL	:=	${PLAT_PATH}/${PLAT_MAKEFILE}
ifeq ($(wildcard ${PLAT_MAKEFILE_FULL}),)
  $(error "Error: Invalid platform. The following platforms are available: ${PLATFORMS}")
endif

ifeq (tests/tests-fwu.xml,$(wildcard ${TESTS_FILE}))
	# Enable test cases and/or helper functions in NS_BL1U and NS_BL2U images
	FWU_BL_TEST := 1
else
	FWU_BL_TEST := 0
endif

all: msg_start

msg_start:
	@echo "Building ${PLAT}"
	@echo "Using the following tests file: ${TESTS_FILE}"

include framework/framework.mk
include tests/tests.mk
include ${PLAT_MAKEFILE_FULL}


.PHONY:			all msg_start clean realclean distclean cscope locate-checkpatch checkcodebase checkpatch
.SUFFIXES:

INCLUDES	:=	-Iinclude					\
			-Iinclude/common				\
			-Iinclude/common/${ARCH}			\
			-Iinclude/drivers				\
			-Iinclude/drivers/arm				\
			-Iinclude/drivers/io				\
			-Iinclude/lib					\
			-Iinclude/lib/${ARCH}				\
			-Iinclude/lib/extensions			\
			-Iinclude/lib/utils				\
			-Iinclude/plat/common				\
			-Iinclude/runtime_services			\
			-Iinclude/runtime_services/secure_el0_payloads	\
			-Iinclude/runtime_services/secure_el1_payloads	\
			-Iinclude/stdlib				\
			-Iinclude/stdlib/sys				\
			${PLAT_INCLUDES}				\
			${FRAMEWORK_INCLUDES}

TFTF_SOURCES	:=	${FRAMEWORK_SOURCES}				\
			${TESTS_SOURCES}				\
			${PLAT_SOURCES}

################################################################################
# Build options checks
################################################################################
$(eval $(call assert_boolean,DEBUG))
$(eval $(call assert_boolean,ENABLE_ASSERTIONS))
$(eval $(call assert_boolean,FIRMWARE_UPDATE))
$(eval $(call assert_boolean,FWU_BL_TEST))
$(eval $(call assert_boolean,NEW_TEST_SESSION))
$(eval $(call assert_boolean,SHELL_COLOR))
$(eval $(call assert_boolean,USE_NVM))

################################################################################
# Add definitions to the cpp preprocessor based on the current build options.
# This is done after including the platform specific makefile to allow the
# platform to overwrite the default options
################################################################################
$(eval $(call add_define,DEBUG))
$(eval $(call add_define,ENABLE_ASSERTIONS))
$(eval $(call add_define,FIRMWARE_UPDATE))
$(eval $(call add_define,FWU_BL_TEST))
$(eval $(call add_define,LOG_LEVEL))
$(eval $(call add_define,NEW_TEST_SESSION))
$(eval $(call add_define,PLAT_${PLAT}))
$(eval $(call add_define,SHELL_COLOR))
$(eval $(call add_define,USE_NVM))

ifeq (${ARCH},aarch32)
        $(eval $(call add_define,AARCH32))
else
        $(eval $(call add_define,AARCH64))
endif

################################################################################

ifeq (${FIRMWARE_UPDATE},1)
  include fwu/ns_bl1u/ns_bl1u.mk
  NS_BL1U_SOURCES	+= ${PLAT_SOURCES}

  include fwu/ns_bl2u/ns_bl2u.mk
  NS_BL2U_SOURCES += ${PLAT_SOURCES}
endif

ifeq (${ARCH}-${PLAT},aarch64-fvp)
  include cactus/cactus.mk
endif

# Check that the file pointed by $TESTS_FILE exists
ifeq (,$(wildcard ${TESTS_FILE}))
  $(error "The file TESTS_FILE points to cannot be found")
endif

ifeq (${DEBUG},1)
TFTF_CFLAGS		+= 	-g
ASFLAGS			+= 	-g -Wa,--gdwarf-2
endif

ASFLAGS_aarch64		=	-mgeneral-regs-only
TFTF_CFLAGS_aarch64	=	-mgeneral-regs-only

ifeq (${ARM_ARCH_MAJOR},7)
ASFLAGS_aarch32		=	-march=armv7-a
TFTF_CFLAGS_aarch32	=	-march=armv7-a
else
ASFLAGS_aarch32		=	-march=armv8-a
TFTF_CFLAGS_aarch32	=	-march=armv8-a
endif

ASFLAGS			+=	-nostdinc -ffreestanding -Wa,--fatal-warnings	\
				-Werror -Wmissing-include-dirs			\
				-D__ASSEMBLY__ $(ASFLAGS_$(ARCH))		\
				-D ARM_ARCH_MAJOR=$(ARM_ARCH_MAJOR)		\
				${DEFINES} ${INCLUDES}
TFTF_CFLAGS		+=	-nostdinc -pedantic -ffreestanding -Wall\
				-Werror -Wno-error=unused-function 		\
				-Wmissing-include-dirs $(TFTF_CFLAGS_$(ARCH))	\
				-std=c99 -c -Os ${DEFINES} ${INCLUDES}
TFTF_CFLAGS		+=	-ffunction-sections -fdata-sections -D ARM_ARCH_MAJOR=$(ARM_ARCH_MAJOR)

# Get the content of CFLAGS user defined value last so they are appended after
# the options defined in the Makefile
TFTF_CFLAGS 		+=	${CFLAGS}

LDFLAGS			+=	--fatal-warnings -O1
LDFLAGS			+=	--gc-sections --build-id=none

# Prepend `-Xlinker` to linker options to enable compiler to pass these
# as options to the linker
LDFLAGS := $(foreach lflag,$(LDFLAGS),-Xlinker $(lflag))
# Do not include startup library sections
LDFLAGS += -nostartfiles

CC			:=	${CROSS_COMPILE}gcc
CPP			:=	${CROSS_COMPILE}cpp
AS			:=	${CROSS_COMPILE}gcc
AR			:=	${CROSS_COMPILE}ar
LD			:=	${CROSS_COMPILE}ld
OC			:=	${CROSS_COMPILE}objcopy
OD			:=	${CROSS_COMPILE}objdump
NM			:=	${CROSS_COMPILE}nm
PP			:=	${CROSS_COMPILE}gcc -E ${TFTF_CFLAGS}

locate-checkpatch:
ifndef CHECKPATCH
	$(error "Please set CHECKPATCH to point to the Linux checkpatch.pl file, eg: CHECKPATCH=../linux/script/checkpatch.pl")
else
ifeq (,$(wildcard ${CHECKPATCH}))
	$(error "The file CHECKPATCH points to cannot be found, use eg: CHECKPATCH=../linux/script/checkpatch.pl")
endif
endif

clean:
			@echo "  CLEAN"
			${Q}rm -rf ${BUILD_PLAT}

realclean distclean:
			@echo "  REALCLEAN"
			${Q}rm -rf ${BUILD_BASE}
			${Q}rm -f ${CURDIR}/cscope.*

checkcodebase:		locate-checkpatch
			@echo "  CHECKING STYLE"
			@if test -d .git ; then	\
				git ls-files \*.c \*.h \*.S | grep -v stdlib | while read GIT_FILE ; do ${CHECKPATCH} -f $$GIT_FILE ; done ;	\
			 else			\
				 find . -type f -not -iwholename "*.git*" -not -iwholename "*build*" -not -iwholename "*stdlib*" -exec ${CHECKPATCH} -f {} \; ;	\
			 fi

checkpatch:		locate-checkpatch
			@echo "  CHECKING STYLE"
			${Q}COMMON_COMMIT=$$(git merge-base HEAD ${BASE_COMMIT});	\
			for commit in `git rev-list $$COMMON_COMMIT..HEAD`; do		\
				printf "\n[*] Checking style of '$$commit'\n\n";	\
				git log --format=email "$$commit~..$$commit"		\
					-- ${CHECK_PATHS} | ${CHECKPATCH} - || true;	\
				git diff --format=email "$$commit~..$$commit"		\
					-- ${CHECK_PATHS} | ${CHECKPATCH} - || true;	\
			done

ifneq (${FIRMWARE_UPDATE},1)
ns_bl1u ns_bl2u:
	@echo "ERROR: Can't build $@ because Firmware Update is not supported \
	on this platform."
	@exit 1
endif

ifneq (${ARCH}-${PLAT},aarch64-fvp)
.PHONY: cactus
cactus:
	@echo "ERROR: $@ is supported only on AArch64 FVP."
	@exit 1
endif

MAKE_DEP = -Wp,-MD,$(DEP) -MT $$@

define MAKE_C

$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))

$(OBJ) : $(2)
	@echo "  CC      $$<"
	$$(Q)$$(CC) $$(TFTF_CFLAGS) -DIMAGE_$(3) $(MAKE_DEP) -c $$< -o $$@

-include $(DEP)
endef


define MAKE_S

$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))

$(OBJ) : $(2)
	@echo "  AS      $$<"
	$$(Q)$$(AS) $$(ASFLAGS) -DIMAGE_$(3) $(MAKE_DEP) -c $$< -o $$@

-include $(DEP)
endef


define MAKE_LD

$(eval DEP := $(1).d)

$(1) : $(2)
	@echo "  PP      $$<"
	$$(Q)$$(AS) $$(ASFLAGS) -P -E $(MAKE_DEP) -o $$@ $$<

-include $(DEP)
endef


define MAKE_OBJS
	$(eval C_OBJS := $(filter %.c,$(2)))
	$(eval REMAIN := $(filter-out %.c,$(2)))
	$(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj),$(3))))

	$(eval S_OBJS := $(filter %.S,$(REMAIN)))
	$(eval REMAIN := $(filter-out %.S,$(REMAIN)))
	$(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj),$(3))))

	$(and $(REMAIN),$(error Unexpected source files present: $(REMAIN)))
endef


# NOTE: The line continuation '\' is required in the next define otherwise we
# end up with a line-feed characer at the end of the last c filename.
# Also bare this issue in mind if extending the list of supported filetypes.
define SOURCES_TO_OBJS
	$(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \
	$(notdir $(patsubst %.S,%.o,$(filter %.S,$(1))))
endef

define uppercase
$(shell echo $(1) | tr '[:lower:]' '[:upper:]')
endef

define MAKE_IMG
	$(eval BUILD_DIR  := ${BUILD_PLAT}/$(1))
	$(eval SOURCES    := $($(call uppercase, $(1))_SOURCES))
	$(eval OBJS       := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES))))
	$(eval LINKERFILE := $(BUILD_DIR)/$(1).ld)
	$(eval MAPFILE    := $(BUILD_DIR)/$(1).map)
	$(eval ELF        := $(BUILD_DIR)/$(1).elf)
	$(eval DUMP       := $(BUILD_DIR)/$(1).dump)
	$(eval BIN        := $(BUILD_PLAT)/$(1).bin)

	$(eval $(call MAKE_OBJS,$(BUILD_DIR),$(SOURCES),$(call uppercase, $(1))))
	$(eval $(call MAKE_LD,$(LINKERFILE),$($(call uppercase, $(1))_LINKERFILE)))

$(BUILD_DIR) :
	$$(Q)mkdir -p "$$@"

$(ELF) : $(OBJS) $(LINKERFILE)
	@echo "  LD      $$@"
	@echo 'const char build_message[] = "Built : "__TIME__", "__DATE__; \
               const char version_string[] = "${VERSION_STRING}";' | \
		$$(CC) $$(TFTF_CFLAGS) -xc - -o $(BUILD_DIR)/build_message.o
	$$(Q)$$(CC) -o $$@ $$(LDFLAGS) -Xlinker -Map=$(MAPFILE) -T $(LINKERFILE) \
		$(BUILD_DIR)/build_message.o $(OBJS)

$(DUMP) : $(ELF)
	@echo "  OD      $$@"
	$${Q}$${OD} -dx $$< > $$@

$(BIN) : $(ELF)
	@echo "  BIN     $$@"
	$$(Q)$$(OC) -O binary $$< $$@
	@echo
	@echo "Built $$@ successfully"
	@echo

.PHONY : $(1)
$(1) : $(BUILD_DIR) $(BIN) $(DUMP)

all : $(1)

endef

$(AUTOGEN_DIR):
	$(Q)mkdir -p "$@"

$(AUTOGEN_DIR)/tests_list.c $(AUTOGEN_DIR)/tests_list.h: $(AUTOGEN_DIR) ${TESTS_FILE} ${PLAT_TESTS_SKIP_LIST}
	@echo "  AUTOGEN $@"
	tools/generate_test_list/generate_test_list.pl $(AUTOGEN_DIR)/tests_list.c $(AUTOGEN_DIR)/tests_list.h  ${TESTS_FILE} $(PLAT_TESTS_SKIP_LIST)

$(eval $(call MAKE_IMG,tftf))

ifeq ($(FIRMWARE_UPDATE), 1)
  $(eval $(call MAKE_IMG,ns_bl1u))
  $(eval $(call MAKE_IMG,ns_bl2u))
endif

ifeq (${ARCH}-${PLAT},aarch64-fvp)
  $(eval $(call MAKE_IMG,cactus))
endif

cscope:
	@echo "  CSCOPE"
	${Q}find ${CURDIR} -name "*.[chsS]" > cscope.files
	${Q}cscope -b -q -k

help:
	@echo "usage: ${MAKE} PLAT=<${PLATFORMS}> <all|tftf|ns_bl1u|ns_bl2u|cactus|distclean|clean|checkcodebase|checkpatch>"
	@echo ""
	@echo "PLAT is used to specify which platform you wish to build."
	@echo "If no platform is specified, PLAT defaults to: ${DEFAULT_PLAT}"
	@echo ""
	@echo "Supported Targets:"
	@echo "  all            Build all supported binaries for this platform"
	@echo "                 (i.e. TFTF and FWU images)"
	@echo "  tftf           Build the TFTF image"
	@echo "  ns_bl1u        Build the NS_BL1U image"
	@echo "  ns_bl2u        Build the NS_BL2U image"
	@echo "  cactus         Build the Cactus image (Test S-EL0 payload)."
	@echo "  checkcodebase  Check the coding style of the entire source tree"
	@echo "  checkpatch     Check the coding style on changes in the current"
	@echo "                 branch against BASE_COMMIT (default origin/master)"
	@echo "  clean          Clean the build for the selected platform"
	@echo "  cscope         Generate cscope index"
	@echo "  distclean      Remove all build artifacts for all platforms"
	@echo ""
	@echo "note: most build targets require PLAT to be set to a specific platform."
	@echo ""
	@echo "example: build all targets for the FVP platform:"
	@echo "  CROSS_COMPILE=aarch64-none-elf- make PLAT=fvp all"