xref: /aosp_15_r20/trusty/kernel/make/host_test.mk (revision 344aa361028b423587d4ef3fa52a23d194628137)
1*344aa361SAndroid Build Coastguard Worker#
2*344aa361SAndroid Build Coastguard Worker# Copyright (c) 2017, Google, Inc. All rights reserved
3*344aa361SAndroid Build Coastguard Worker#
4*344aa361SAndroid Build Coastguard Worker# Permission is hereby granted, free of charge, to any person obtaining
5*344aa361SAndroid Build Coastguard Worker# a copy of this software and associated documentation files
6*344aa361SAndroid Build Coastguard Worker# (the "Software"), to deal in the Software without restriction,
7*344aa361SAndroid Build Coastguard Worker# including without limitation the rights to use, copy, modify, merge,
8*344aa361SAndroid Build Coastguard Worker# publish, distribute, sublicense, and/or sell copies of the Software,
9*344aa361SAndroid Build Coastguard Worker# and to permit persons to whom the Software is furnished to do so,
10*344aa361SAndroid Build Coastguard Worker# subject to the following conditions:
11*344aa361SAndroid Build Coastguard Worker#
12*344aa361SAndroid Build Coastguard Worker# The above copyright notice and this permission notice shall be
13*344aa361SAndroid Build Coastguard Worker# included in all copies or substantial portions of the Software.
14*344aa361SAndroid Build Coastguard Worker#
15*344aa361SAndroid Build Coastguard Worker# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*344aa361SAndroid Build Coastguard Worker# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*344aa361SAndroid Build Coastguard Worker# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18*344aa361SAndroid Build Coastguard Worker# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19*344aa361SAndroid Build Coastguard Worker# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20*344aa361SAndroid Build Coastguard Worker# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21*344aa361SAndroid Build Coastguard Worker# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22*344aa361SAndroid Build Coastguard Worker#
23*344aa361SAndroid Build Coastguard Worker
24*344aa361SAndroid Build Coastguard Worker# args:
25*344aa361SAndroid Build Coastguard Worker# HOST_TEST : name of the test binary (required)
26*344aa361SAndroid Build Coastguard Worker# HOST_SRCS : list of source files (required)
27*344aa361SAndroid Build Coastguard Worker# HOST_INCLUDE_DIRS : list of include directories
28*344aa361SAndroid Build Coastguard Worker# HOST_FLAGS : list of flags for the compiler
29*344aa361SAndroid Build Coastguard Worker# HOST_LIBS : list of host-provided libraries to link against
30*344aa361SAndroid Build Coastguard Worker# HOST_DEPS : list of libraries to build and link against. Recursive
31*344aa361SAndroid Build Coastguard Worker#             dependencies are not supported.
32*344aa361SAndroid Build Coastguard Worker# HOST_COVERAGE_ENABLED : true/false enable LLVM Source-based code coverage
33*344aa361SAndroid Build Coastguard Worker
34*344aa361SAndroid Build Coastguard Worker
35*344aa361SAndroid Build Coastguard Worker# Validate arguments.
36*344aa361SAndroid Build Coastguard Workerifeq ($(HOST_TEST), )
37*344aa361SAndroid Build Coastguard Worker$(error HOST_TEST must be specified)
38*344aa361SAndroid Build Coastguard Workerendif
39*344aa361SAndroid Build Coastguard Worker
40*344aa361SAndroid Build Coastguard Workerifeq ($(HOST_SRCS), )
41*344aa361SAndroid Build Coastguard Worker$(error HOST_SRCS must be specified)
42*344aa361SAndroid Build Coastguard Workerendif
43*344aa361SAndroid Build Coastguard Worker
44*344aa361SAndroid Build Coastguard Worker# Select same builddir when included form user-space or kernel
45*344aa361SAndroid Build Coastguard Workerifeq ($(strip $(TRUSTY_TOP_LEVEL_BUILDDIR)),)
46*344aa361SAndroid Build Coastguard WorkerHOST_TEST_BUILDDIR := $(BUILDDIR)
47*344aa361SAndroid Build Coastguard Workerelse
48*344aa361SAndroid Build Coastguard WorkerHOST_TEST_BUILDDIR := $(TRUSTY_TOP_LEVEL_BUILDDIR)
49*344aa361SAndroid Build Coastguard Workerendif
50*344aa361SAndroid Build Coastguard Worker
51*344aa361SAndroid Build Coastguard Worker# We should use the prebuilt linker rather than the host linker
52*344aa361SAndroid Build Coastguard WorkerHOST_LDFLAGS := -B$(CLANG_BINDIR) -B$(CLANG_HOST_SEARCHDIR) \
53*344aa361SAndroid Build Coastguard Worker	$(foreach dir,$(CLANG_HOST_LDDIRS),-L$(dir)) --sysroot=$(CLANG_HOST_SYSROOT) -fuse-ld=lld
54*344aa361SAndroid Build Coastguard Worker
55*344aa361SAndroid Build Coastguard WorkerHOST_CC := $(CLANG_BINDIR)/clang
56*344aa361SAndroid Build Coastguard WorkerHOST_SANITIZER_FLAGS := -fsanitize=address -fno-omit-frame-pointer
57*344aa361SAndroid Build Coastguard WorkerHOST_RUN_ENV := ASAN_OPTIONS=symbolize=1 ASAN_SYMBOLIZER_PATH=$(CLANG_BINDIR)/llvm-symbolizer
58*344aa361SAndroid Build Coastguard WorkerHOST_LIBCXX_CPPFLAGS := -stdlib=libc++ -isystem$(CLANG_BINDIR)/../include/c++/v1
59*344aa361SAndroid Build Coastguard WorkerHOST_LIBCXX_LDFLAGS := -L$(CLANG_HOST_LIBDIR) -stdlib=libc++ -Wl,-rpath,$(CLANG_HOST_LIBDIR)
60*344aa361SAndroid Build Coastguard Worker# ASAN is not compatible with GDB.
61*344aa361SAndroid Build Coastguard WorkerHOST_DEBUGGER :=
62*344aa361SAndroid Build Coastguard Worker
63*344aa361SAndroid Build Coastguard WorkerHOST_INCLUDE_DIRS += $(GLOBAL_UAPI_INCLUDES) $(GLOBAL_SHARED_INCLUDES) $(GLOBAL_USER_INCLUDES)
64*344aa361SAndroid Build Coastguard Worker
65*344aa361SAndroid Build Coastguard Worker# Enable LLVM Source-based Code Coverage
66*344aa361SAndroid Build Coastguard Worker# https://clang.llvm.org/docs/SourceBasedCodeCoverage.html
67*344aa361SAndroid Build Coastguard Workerifeq (true,$(call TOBOOL,$(HOST_COVERAGE_ENABLED)))
68*344aa361SAndroid Build Coastguard WorkerHOST_FLAGS += \
69*344aa361SAndroid Build Coastguard Worker	-fprofile-instr-generate=$(HOST_TEST).profraw \
70*344aa361SAndroid Build Coastguard Worker	-fcoverage-mapping \
71*344aa361SAndroid Build Coastguard Worker	-mllvm -enable-value-profiling=false
72*344aa361SAndroid Build Coastguard Worker
73*344aa361SAndroid Build Coastguard WorkerHOST_LDFLAGS += \
74*344aa361SAndroid Build Coastguard Worker	-fprofile-instr-generate=$(HOST_TEST).profraw \
75*344aa361SAndroid Build Coastguard Worker	-fcoverage-mapping \
76*344aa361SAndroid Build Coastguard Worker	-mllvm -enable-value-profiling=false
77*344aa361SAndroid Build Coastguard Workerendif
78*344aa361SAndroid Build Coastguard Worker
79*344aa361SAndroid Build Coastguard Worker# Compile test library dependencies
80*344aa361SAndroid Build Coastguard WorkerHOST_LIB_ARCHIVES :=
81*344aa361SAndroid Build Coastguard Worker$(foreach t,$(HOST_DEPS),\
82*344aa361SAndroid Build Coastguard Worker	$(eval include $(addsuffix /rules.mk,$(t))))
83*344aa361SAndroid Build Coastguard Worker
84*344aa361SAndroid Build Coastguard Worker# Compile test sources.
85*344aa361SAndroid Build Coastguard WorkerGENERIC_CC := $(HOST_CC)
86*344aa361SAndroid Build Coastguard WorkerGENERIC_SRCS := $(HOST_SRCS)
87*344aa361SAndroid Build Coastguard WorkerGENERIC_OBJ_DIR := $(HOST_TEST_BUILDDIR)/host_tests/obj/$(HOST_TEST)
88*344aa361SAndroid Build Coastguard WorkerGENERIC_FLAGS := -O1 -g -Wall -Wextra -Wno-unused-parameter -Werror -Wno-missing-field-initializers $(HOST_SANITIZER_FLAGS) $(HOST_FLAGS) $(addprefix -I, $(HOST_INCLUDE_DIRS))
89*344aa361SAndroid Build Coastguard WorkerGENERIC_CFLAGS := -std=c11 -D_POSIX_C_SOURCE=200809
90*344aa361SAndroid Build Coastguard WorkerGENERIC_CPPFLAGS := -std=c++20 -Wno-c99-designator $(HOST_LIBCXX_CPPFLAGS)
91*344aa361SAndroid Build Coastguard WorkerGENERIC_LOG_NAME := $(HOST_TEST)
92*344aa361SAndroid Build Coastguard Workerinclude make/generic_compile.mk
93*344aa361SAndroid Build Coastguard Worker
94*344aa361SAndroid Build Coastguard Worker# Link
95*344aa361SAndroid Build Coastguard WorkerHOST_TEST_BIN := $(HOST_TEST_BUILDDIR)/host_tests/$(HOST_TEST)
96*344aa361SAndroid Build Coastguard Worker$(HOST_TEST_BIN): CC := $(HOST_CC)
97*344aa361SAndroid Build Coastguard Worker$(HOST_TEST_BIN): LDFLAGS := -g $(HOST_SANITIZER_FLAGS) $(HOST_LDFLAGS) $(HOST_LIBCXX_LDFLAGS) $(addprefix -l, $(HOST_LIBS))
98*344aa361SAndroid Build Coastguard Worker$(HOST_TEST_BIN): HOST_TEST := $(HOST_TEST)
99*344aa361SAndroid Build Coastguard Worker$(HOST_TEST_BIN): $(GENERIC_OBJS) $(HOST_LIB_ARCHIVES)
100*344aa361SAndroid Build Coastguard Worker	@$(call ECHO,$(HOST_TEST),linking,$@)
101*344aa361SAndroid Build Coastguard Worker	@$(MKDIR)
102*344aa361SAndroid Build Coastguard Worker	$(NOECHO)$(CC) $^ $(LDFLAGS) -o $@
103*344aa361SAndroid Build Coastguard Worker	@$(call ECHO_DONE_SILENT,$(HOST_TEST),linking,$@)
104*344aa361SAndroid Build Coastguard Worker
105*344aa361SAndroid Build Coastguard Worker# Build host test by default
106*344aa361SAndroid Build Coastguard Workerall:: $(HOST_TEST_BIN)
107*344aa361SAndroid Build Coastguard Worker
108*344aa361SAndroid Build Coastguard Worker# Aliases
109*344aa361SAndroid Build Coastguard Workerhost_tests: $(HOST_TEST_BIN)
110*344aa361SAndroid Build Coastguard Worker
111*344aa361SAndroid Build Coastguard Workerrun_$(HOST_TEST): RUN_ENV := $(HOST_RUN_ENV)
112*344aa361SAndroid Build Coastguard Workerrun_$(HOST_TEST): DEBUGGER := $(HOST_DEBUGGER)
113*344aa361SAndroid Build Coastguard Workerrun_$(HOST_TEST): $(HOST_TEST_BIN) .PHONY
114*344aa361SAndroid Build Coastguard Worker	@echo running $<
115*344aa361SAndroid Build Coastguard Worker	$(NOECHO)$(RUN_ENV) $(DEBUGGER) $<
116*344aa361SAndroid Build Coastguard Worker
117*344aa361SAndroid Build Coastguard Workerrun_host_tests: run_$(HOST_TEST) .PHONY
118*344aa361SAndroid Build Coastguard Worker
119*344aa361SAndroid Build Coastguard Worker# Cleanup inputs
120*344aa361SAndroid Build Coastguard WorkerHOST_TEST :=
121*344aa361SAndroid Build Coastguard WorkerHOST_TEST_BUILDDIR :=
122*344aa361SAndroid Build Coastguard WorkerHOST_SRCS :=
123*344aa361SAndroid Build Coastguard WorkerHOST_INCLUDE_DIRS :=
124*344aa361SAndroid Build Coastguard WorkerHOST_FLAGS :=
125*344aa361SAndroid Build Coastguard WorkerHOST_LIBS :=
126*344aa361SAndroid Build Coastguard WorkerHOST_DEPS :=
127*344aa361SAndroid Build Coastguard WorkerHOST_COVERAGE_ENABLED :=
128*344aa361SAndroid Build Coastguard Worker# Cleanup internal
129*344aa361SAndroid Build Coastguard WorkerHOST_CC :=
130*344aa361SAndroid Build Coastguard WorkerHOST_SANITIZER_FLAGS :=
131*344aa361SAndroid Build Coastguard WorkerHOST_RUN_ENV :=
132*344aa361SAndroid Build Coastguard WorkerHOST_LIBCXX_CPPFLAGS :=
133*344aa361SAndroid Build Coastguard WorkerHOST_LDFLAGS :=
134*344aa361SAndroid Build Coastguard WorkerHOST_LIBCXX_LDFLAGS :=
135*344aa361SAndroid Build Coastguard WorkerHOST_DEBUGGER :=
136*344aa361SAndroid Build Coastguard WorkerHOST_TEST_BIN :=
137*344aa361SAndroid Build Coastguard WorkerHOST_OBJ_DIR :=
138*344aa361SAndroid Build Coastguard WorkerGENERIC_OBJS :=
139*344aa361SAndroid Build Coastguard WorkerHOST_LIB_ARCHIVES :=
140