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