xref: /aosp_15_r20/trusty/kernel/make/host_tool.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_TOOL_NAME : name of the host 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_LDFLAGS : list of flags for the compiler
30*344aa361SAndroid Build Coastguard Worker# HOST_LIBS : list of host-provided libraries to link against
31*344aa361SAndroid Build Coastguard Worker# HOST_DEPS : list of libraries to build and link against. Recursive
32*344aa361SAndroid Build Coastguard Worker#             dependencies are not supported.
33*344aa361SAndroid Build Coastguard Worker# HOST_SRCDEPS : extra source dependencies
34*344aa361SAndroid Build Coastguard Worker# HOST_STATIC_LINK : statically link the host tool
35*344aa361SAndroid Build Coastguard Worker# HOST_COVERAGE_ENABLED : true/false enable LLVM Source-based code coverage
36*344aa361SAndroid Build Coastguard Worker
37*344aa361SAndroid Build Coastguard Worker# Validate arguments.
38*344aa361SAndroid Build Coastguard Workerifeq ($(HOST_TOOL_NAME), )
39*344aa361SAndroid Build Coastguard Worker$(error HOST_TOOL_NAME must be specified)
40*344aa361SAndroid Build Coastguard Workerendif
41*344aa361SAndroid Build Coastguard Worker
42*344aa361SAndroid Build Coastguard Workerifeq ($(HOST_SRCS), )
43*344aa361SAndroid Build Coastguard Worker$(error HOST_SRCS must be specified)
44*344aa361SAndroid Build Coastguard Workerendif
45*344aa361SAndroid Build Coastguard Worker
46*344aa361SAndroid Build Coastguard WorkerHOST_CC := $(CLANG_BINDIR)/clang
47*344aa361SAndroid Build Coastguard Worker
48*344aa361SAndroid Build Coastguard Workerifeq (false, $(call TOBOOL,$(HOST_STATIC_LINK)))
49*344aa361SAndroid Build Coastguard Worker# ASAN is not compatible with GDB or static linking.
50*344aa361SAndroid Build Coastguard WorkerHOST_SANITIZER_FLAGS := -fsanitize=address -fno-omit-frame-pointer
51*344aa361SAndroid Build Coastguard Workerelse
52*344aa361SAndroid Build Coastguard WorkerHOST_FLAGS += -static
53*344aa361SAndroid Build Coastguard WorkerHOST_LDFLAGS += -static
54*344aa361SAndroid Build Coastguard WorkerHOST_SANITIZER_FLAGS :=
55*344aa361SAndroid Build Coastguard Worker# b/319927400: There is a bug that causes a linker conflict if pthread is
56*344aa361SAndroid Build Coastguard Worker# linked _after_ libc.  Add pthread explicitly to avoid this possibility.
57*344aa361SAndroid Build Coastguard WorkerHOST_LDFLAGS += -lpthread
58*344aa361SAndroid Build Coastguard Workerendif
59*344aa361SAndroid Build Coastguard Worker
60*344aa361SAndroid Build Coastguard Worker# We should use the prebuilt linker rather than the host linker
61*344aa361SAndroid Build Coastguard WorkerHOST_LDFLAGS += -B$(CLANG_BINDIR) -B$(CLANG_HOST_SEARCHDIR) \
62*344aa361SAndroid Build Coastguard Worker	$(foreach dir,$(CLANG_HOST_LDDIRS),-L$(dir)) --sysroot=$(CLANG_HOST_SYSROOT) -fuse-ld=lld
63*344aa361SAndroid Build Coastguard Worker
64*344aa361SAndroid Build Coastguard Worker# When using clang, we need to always use the prebuilt libc++ library
65*344aa361SAndroid Build Coastguard Worker# because we can't be sure what version of libstdc++ the host system
66*344aa361SAndroid Build Coastguard Worker# has, or even if it exists at all.
67*344aa361SAndroid Build Coastguard Workerifneq ($(filter stdc++ c++,$(HOST_LIBS)),)
68*344aa361SAndroid Build Coastguard Worker# Add the prebuilt libraries directory to the tool's rpath,
69*344aa361SAndroid Build Coastguard Worker# so it can use those libraries, e.g., libc++.so
70*344aa361SAndroid Build Coastguard WorkerHOST_LIBCXX_CPPFLAGS := -stdlib=libc++ -isystem$(CLANG_BINDIR)/../include/c++/v1
71*344aa361SAndroid Build Coastguard WorkerHOST_LIBCXX_LDFLAGS := -L$(CLANG_HOST_LIBDIR) -stdlib=libc++ -Wl,-rpath,$(CLANG_HOST_LIBDIR)
72*344aa361SAndroid Build Coastguard Worker# Add relative path inside the SDK package to RPATH
73*344aa361SAndroid Build Coastguard WorkerHOST_SDK_LIBCXX_DIR := $(subst $(CLANG_BINDIR)/..,clang,$(CLANG_HOST_LIBDIR))
74*344aa361SAndroid Build Coastguard WorkerHOST_LIBCXX_LDFLAGS += -Wl,-rpath,'$$ORIGIN/../../../toolchain/$(HOST_SDK_LIBCXX_DIR)'
75*344aa361SAndroid Build Coastguard Workerelse
76*344aa361SAndroid Build Coastguard WorkerHOST_LIBCXX_CPPFLAGS :=
77*344aa361SAndroid Build Coastguard WorkerHOST_LIBCXX_LDFLAGS :=
78*344aa361SAndroid Build Coastguard Workerendif
79*344aa361SAndroid Build Coastguard Worker
80*344aa361SAndroid Build Coastguard WorkerHOST_INCLUDE_DIRS += $(GLOBAL_UAPI_INCLUDES) $(GLOBAL_SHARED_INCLUDES) $(GLOBAL_USER_INCLUDES)
81*344aa361SAndroid Build Coastguard Worker
82*344aa361SAndroid Build Coastguard Worker# Enable LLVM Source-based Code Coverage
83*344aa361SAndroid Build Coastguard Worker# https://clang.llvm.org/docs/SourceBasedCodeCoverage.html
84*344aa361SAndroid Build Coastguard Workerifeq (true,$(call TOBOOL,$(HOST_COVERAGE_ENABLED)))
85*344aa361SAndroid Build Coastguard WorkerHOST_FLAGS += \
86*344aa361SAndroid Build Coastguard Worker	-fprofile-instr-generate=$(HOST_TOOL_NAME).profraw \
87*344aa361SAndroid Build Coastguard Worker	-fcoverage-mapping
88*344aa361SAndroid Build Coastguard Worker
89*344aa361SAndroid Build Coastguard WorkerHOST_LDFLAGS += \
90*344aa361SAndroid Build Coastguard Worker	-fprofile-instr-generate=$(HOST_TOOL_NAME).profraw \
91*344aa361SAndroid Build Coastguard Worker	-fcoverage-mapping
92*344aa361SAndroid Build Coastguard Workerendif
93*344aa361SAndroid Build Coastguard Worker
94*344aa361SAndroid Build Coastguard Worker# Compile tool library dependencies
95*344aa361SAndroid Build Coastguard WorkerHOST_LIB_ARCHIVES :=
96*344aa361SAndroid Build Coastguard Workerinclude $(addsuffix /rules.mk, $(HOST_DEPS))
97*344aa361SAndroid Build Coastguard Worker
98*344aa361SAndroid Build Coastguard Worker# Compile tool sources.
99*344aa361SAndroid Build Coastguard WorkerGENERIC_CC := $(HOST_CC)
100*344aa361SAndroid Build Coastguard WorkerGENERIC_SRCS := $(HOST_SRCS)
101*344aa361SAndroid Build Coastguard WorkerGENERIC_OBJ_DIR := $(BUILDDIR)/host_tools/obj/$(HOST_TOOL_NAME)
102*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))
103*344aa361SAndroid Build Coastguard WorkerGENERIC_CFLAGS := -std=c11 -D_POSIX_C_SOURCE=200809
104*344aa361SAndroid Build Coastguard WorkerGENERIC_CPPFLAGS := -std=c++20 $(HOST_LIBCXX_CPPFLAGS)
105*344aa361SAndroid Build Coastguard WorkerGENERIC_SRCDEPS := $(HOST_SRCDEPS)
106*344aa361SAndroid Build Coastguard WorkerGENERIC_LOG_NAME := $(HOST_TOOL_NAME)
107*344aa361SAndroid Build Coastguard Workerinclude make/generic_compile.mk
108*344aa361SAndroid Build Coastguard Worker
109*344aa361SAndroid Build Coastguard Worker# Link
110*344aa361SAndroid Build Coastguard WorkerHOST_TOOL_BIN := $(BUILDDIR)/host_tools/$(HOST_TOOL_NAME)
111*344aa361SAndroid Build Coastguard Worker$(HOST_TOOL_BIN): CC := $(HOST_CC)
112*344aa361SAndroid Build Coastguard Worker$(HOST_TOOL_BIN): LDFLAGS := -g $(HOST_SANITIZER_FLAGS) $(HOST_LDFLAGS) $(HOST_LIBCXX_LDFLAGS) $(addprefix -l, $(HOST_LIBS))
113*344aa361SAndroid Build Coastguard Worker$(HOST_TOOL_BIN): HOST_TOOL_NAME := $(HOST_TOOL_NAME)
114*344aa361SAndroid Build Coastguard Worker$(HOST_TOOL_BIN): $(GENERIC_OBJS) $(HOST_LIB_ARCHIVES)
115*344aa361SAndroid Build Coastguard Worker	@$(call ECHO,$(HOST_TOOL_NAME),linking,$@)
116*344aa361SAndroid Build Coastguard Worker	@$(MKDIR)
117*344aa361SAndroid Build Coastguard Worker	$(NOECHO)$(CC) $^ $(LDFLAGS) -o $@
118*344aa361SAndroid Build Coastguard Worker	@$(call ECHO_DONE_SILENT,$(HOST_TOOL_NAME),linking,$@)
119*344aa361SAndroid Build Coastguard Worker
120*344aa361SAndroid Build Coastguard WorkerEXTRA_BUILDDEPS += $(HOST_TOOL_BIN)
121*344aa361SAndroid Build Coastguard Worker
122*344aa361SAndroid Build Coastguard Worker# Cleanup inputs
123*344aa361SAndroid Build Coastguard WorkerHOST_TOOL_NAME:=
124*344aa361SAndroid Build Coastguard WorkerHOST_SRCS :=
125*344aa361SAndroid Build Coastguard WorkerHOST_INCLUDE_DIRS :=
126*344aa361SAndroid Build Coastguard WorkerHOST_FLAGS :=
127*344aa361SAndroid Build Coastguard WorkerHOST_LDFLAGS :=
128*344aa361SAndroid Build Coastguard WorkerHOST_LIBS :=
129*344aa361SAndroid Build Coastguard WorkerHOST_DEPS :=
130*344aa361SAndroid Build Coastguard WorkerHOST_SRCDEPS :=
131*344aa361SAndroid Build Coastguard WorkerHOST_STATIC_LINK :=
132*344aa361SAndroid Build Coastguard Worker# Cleanup internal
133*344aa361SAndroid Build Coastguard WorkerHOST_CC :=
134*344aa361SAndroid Build Coastguard WorkerHOST_SANITIZER_FLAGS :=
135*344aa361SAndroid Build Coastguard WorkerHOST_SDK_LIBCXX_DIR :=
136*344aa361SAndroid Build Coastguard WorkerHOST_TOOL_BIN :=
137*344aa361SAndroid Build Coastguard WorkerHOST_OBJ_DIR :=
138*344aa361SAndroid Build Coastguard WorkerGENERIC_OBJS :=
139*344aa361SAndroid Build Coastguard WorkerHOST_LIB_ARCHIVES :=
140