1# Copyright 2018 The Hafnium Authors.
2#
3# Use of this source code is governed by a BSD-style
4# license that can be found in the LICENSE file or at
5# https://opensource.org/licenses/BSD-3-Clause.
6
7# Select the project to build.
8PROJECT ?= reference
9
10TOOLCHAIN_LIB := $(shell clang --print-resource-dir)
11
12ENABLE_ASSERTIONS ?= 1
13
14GN_ARGS := project="$(PROJECT)"
15GN_ARGS += toolchain_lib="$(TOOLCHAIN_LIB)"
16ifeq ($(filter $(ENABLE_ASSERTIONS), 1 0),)
17         $(error invalid value for ENABLE_ASSERTIONS, should be 1 or 0)
18endif
19GN_ARGS += enable_assertions="$(ENABLE_ASSERTIONS)"
20
21# If HAFNIUM_HERMETIC_BUILD is "true" (not default), invoke `make` inside
22# a container. The 'run_in_container.sh' script will set the variable value to
23# 'inside' to avoid recursion.
24ifeq ($(HAFNIUM_HERMETIC_BUILD),true)
25
26# TODO: This is not ideal as (a) we invoke the container once per command-line
27# target, and (b) we cannot pass `make` arguments to the script. We could
28# consider creating a bash alias for `make` to invoke the script directly.
29
30# Need to define at least one non-default target.
31all:
32	@$(CURDIR)/build/run_in_container.sh make PROJECT=$(PROJECT) \
33		ENABLE_ASSERTIONS=$(ENABLE_ASSERTIONS) $@
34
35# Catch-all target.
36.DEFAULT:
37	@$(CURDIR)/build/run_in_container.sh make PROJECT=$(PROJECT) \
38		ENABLE_ASSERTIONS=$(ENABLE_ASSERTIONS) $@
39
40else  # HAFNIUM_HERMETIC_BUILD
41
42# Set path to prebuilts used in the build.
43UNAME_S := $(shell uname -s | tr '[:upper:]' '[:lower:]')
44UNAME_M := $(shell uname -m)
45
46ifeq ($(UNAME_M),x86_64)
47UNAME_M := x64
48endif
49
50PREBUILTS := $(CURDIR)/prebuilts/$(UNAME_S)-$(UNAME_M)
51GN ?= $(PREBUILTS)/gn/gn
52NINJA ?= $(PREBUILTS)/ninja/ninja
53
54CHECKPATCH := $(CURDIR)/third_party/linux/scripts/checkpatch.pl \
55	--ignore BRACES,SPDX_LICENSE_TAG,VOLATILE,SPLIT_STRING,AVOID_EXTERNS,USE_SPINLOCK_T,NEW_TYPEDEFS,INITIALISED_STATIC,FILE_PATH_CHANGES,EMBEDDED_FUNCTION_NAME,SINGLE_STATEMENT_DO_WHILE_MACRO,MACRO_WITH_FLOW_CONTROL,PREFER_PACKED,PREFER_ALIGNED,INDENTED_LABEL,SPACING --quiet
56
57# Specifies the grep pattern for ignoring specific files in checkpatch.
58# C++ headers, *.hh, are automatically excluded.
59# Separate the different items in the list with a grep or (\|).
60# debug_el1.c : uses XMACROS, which checkpatch doesn't understand.
61# perfmon.c : uses XMACROS, which checkpatch doesn't understand.
62# feature_id.c : uses XMACROS, which checkpatch doesn't understand.
63CHECKPATCH_IGNORE := "src/arch/aarch64/hypervisor/debug_el1.c\|src/arch/aarch64/hypervisor/perfmon.c\|src/arch/aarch64/hypervisor/feature_id.c"
64
65OUT ?= out/$(PROJECT)
66OUT_DIR = out/$(PROJECT)
67
68.PHONY: all
69all: $(OUT_DIR)/build.ninja
70	@$(NINJA) -C $(OUT_DIR)
71
72$(OUT_DIR)/build.ninja:
73	@$(GN) --export-compile-commands gen --args='$(GN_ARGS)' $(OUT_DIR)
74
75.PHONY: clean
76clean:
77	@$(NINJA) -C $(OUT_DIR) -t clean
78
79.PHONY: clobber
80clobber:
81	rm -rf $(OUT)
82
83# see .clang-format.
84.PHONY: format
85format:
86	@echo "Formatting..."
87	@find src/ -name \*.c -o -name \*.cc -o -name \*.h | xargs -r clang-format -style file -i
88	@find inc/ -name \*.c -o -name \*.cc -o -name \*.h | xargs -r clang-format -style file -i
89	@find test/ -name \*.c -o -name \*.cc -o -name \*.h | xargs -r clang-format -style file -i
90	@find project/ -name \*.c -o -name \*.cc -o -name \*.h | xargs -r clang-format -style file -i
91	@find vmlib/ -name \*.c -o -name \*.cc -o -name \*.h | xargs -r clang-format -style file -i
92	@find . \( -name \*.gn -o -name \*.gni \) | xargs -n1 $(GN) format
93
94.PHONY: checkpatch
95checkpatch:
96	@find src/ -name \*.c -o -name \*.h | grep -v $(CHECKPATCH_IGNORE) | xargs $(CHECKPATCH) -f
97	@find inc/ -name \*.c -o -name \*.h | grep -v $(CHECKPATCH_IGNORE) | xargs $(CHECKPATCH) -f
98	# TODO: enable for test/
99	@find project/ -name \*.c -o -name \*.h | grep -v $(CHECKPATCH_IGNORE) | xargs $(CHECKPATCH) -f
100
101# see .clang-tidy.
102.PHONY: tidy
103tidy: $(OUT_DIR)/build.ninja
104	@$(NINJA) -C $(OUT_DIR)
105	@echo "Tidying..."
106	# TODO: enable readability-magic-numbers once there are fewer violations.
107	# TODO: enable for c++ tests as it currently gives spurious errors.
108	@find src/ \( -name \*.c \) | xargs clang-tidy -p $(OUT_DIR) -fix
109	@find test/ \( -name \*.c \) | xargs clang-tidy -p $(OUT_DIR) -fix
110
111.PHONY: check
112check: $(OUT_DIR)/build.ninja
113	@$(NINJA) -C $(OUT_DIR)
114	@echo "Checking..."
115	# TODO: enable for c++ tests as it currently gives spurious errors.
116	@find src/ \( -name \*.c \) | xargs clang-check -p $(OUT_DIR) -analyze -fix-what-you-can
117	@find test/ \( -name \*.c \) | xargs clang-check -p $(OUT_DIR) -analyze -fix-what-you-can
118
119.PHONY: license
120license:
121	@find build/ -name \*.S -o -name \*.c -o -name \*.cc -o -name \*.h -o -name \*.dts -o -name \*.ld | xargs -n1 python build/license.py --style c
122	@find inc/ -name \*.S -o -name \*.c -o -name \*.cc -o -name \*.h -o -name \*.dts | xargs -n1 python build/license.py --style c
123	@find src/ -name \*.S -o -name \*.c -o -name \*.cc -o -name \*.h -o -name \*.dts | xargs -n1 python build/license.py --style c
124	@find test/ -name \*.S -o -name \*.c -o -name \*.cc -o -name \*.h -o -name \*.dts | xargs -n1 python build/license.py --style c
125	@find vmlib/ -name \*.S -o -name \*.c -o -name \*.cc -o -name \*.h -o -name \*.dts | xargs -n1 python build/license.py --style c
126	@find build/ -name \*.py -o -name \*.sh -o -name \*.inc -o -name Dockerfile* | xargs -n1 python build/license.py --style hash
127	@find kokoro/ -name \*.sh -o -name \*.cfg | xargs -n1 python build/license.py --style hash
128	@find test/ -name \*.py| xargs -n1 python build/license.py --style hash
129	@find . \( -path ./driver/linux -o -path ./third_party \) -prune -o \( -name \*.gn -o -name \*.gni \) -print | xargs -n1 python build/license.py --style hash
130
131.PHONY: update-prebuilts
132update-prebuilts: prebuilts/linux-aarch64/linux/vmlinuz
133
134prebuilts/linux-aarch64/linux/vmlinuz: $(OUT_DIR)/build.ninja
135	@$(NINJA) -C $(OUT_DIR) "third_party/linux"
136	cp out/reference/obj/third_party/linux/linux.bin $@
137
138endif  # HAFNIUM_HERMETIC_BUILD
139