xref: /aosp_15_r20/build/bazel/rules/cc/stl.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1*7594170eSAndroid Build Coastguard Worker# Copyright (C) 2021 The Android Open Source Project
2*7594170eSAndroid Build Coastguard Worker#
3*7594170eSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*7594170eSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*7594170eSAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*7594170eSAndroid Build Coastguard Worker#
7*7594170eSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
8*7594170eSAndroid Build Coastguard Worker#
9*7594170eSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*7594170eSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*7594170eSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*7594170eSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*7594170eSAndroid Build Coastguard Worker# limitations under the License.
14*7594170eSAndroid Build Coastguard Worker
15*7594170eSAndroid Build Coastguard Worker# Helpers for stl property resolution.
16*7594170eSAndroid Build Coastguard Worker# These mappings taken from build/soong/cc/stl.go
17*7594170eSAndroid Build Coastguard Worker
18*7594170eSAndroid Build Coastguard Workerload("//build/bazel/platforms/arch/variants:constants.bzl", "arch_variant_to_constraints")
19*7594170eSAndroid Build Coastguard Worker
20*7594170eSAndroid Build Coastguard Worker_libcpp_stl_names = {
21*7594170eSAndroid Build Coastguard Worker    "libc++": True,
22*7594170eSAndroid Build Coastguard Worker    "libc++_static": True,
23*7594170eSAndroid Build Coastguard Worker    "": True,
24*7594170eSAndroid Build Coastguard Worker    "system": True,
25*7594170eSAndroid Build Coastguard Worker}
26*7594170eSAndroid Build Coastguard Worker
27*7594170eSAndroid Build Coastguard Worker# https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/stl.go;l=162;drc=cb0ac95bde896fa2aa59193a37ceb580758c322c
28*7594170eSAndroid Build Coastguard Worker# this should vary based on vndk version
29*7594170eSAndroid Build Coastguard Worker# skip libm and libc because then we would have duplicates due to system_shared_library
30*7594170eSAndroid Build Coastguard Worker_libunwind = "//prebuilts/clang/host/linux-x86:libunwind"
31*7594170eSAndroid Build Coastguard Worker
32*7594170eSAndroid Build Coastguard Worker_ndk_system = "//prebuilts/ndk:ndk_system"
33*7594170eSAndroid Build Coastguard Worker
34*7594170eSAndroid Build Coastguard Worker_static_binary_deps = select({
35*7594170eSAndroid Build Coastguard Worker    arch_variant_to_constraints["android"]: [_libunwind],
36*7594170eSAndroid Build Coastguard Worker    arch_variant_to_constraints["linux_bionic"]: [_libunwind],
37*7594170eSAndroid Build Coastguard Worker    "//build/bazel/rules/apex:unbundled_app": [],
38*7594170eSAndroid Build Coastguard Worker    "//conditions:default": [],
39*7594170eSAndroid Build Coastguard Worker})
40*7594170eSAndroid Build Coastguard Worker
41*7594170eSAndroid Build Coastguard Workerdef _stl_name_resolver(stl_name, is_shared):
42*7594170eSAndroid Build Coastguard Worker    if stl_name == "none":
43*7594170eSAndroid Build Coastguard Worker        return stl_name
44*7594170eSAndroid Build Coastguard Worker
45*7594170eSAndroid Build Coastguard Worker    if stl_name not in _libcpp_stl_names:
46*7594170eSAndroid Build Coastguard Worker        fail("Unhandled stl %s" % stl_name)
47*7594170eSAndroid Build Coastguard Worker
48*7594170eSAndroid Build Coastguard Worker    if stl_name in ("", "system"):
49*7594170eSAndroid Build Coastguard Worker        if is_shared:
50*7594170eSAndroid Build Coastguard Worker            stl_name = "libc++"
51*7594170eSAndroid Build Coastguard Worker        else:
52*7594170eSAndroid Build Coastguard Worker            stl_name = "libc++_static"
53*7594170eSAndroid Build Coastguard Worker    return stl_name
54*7594170eSAndroid Build Coastguard Worker
55*7594170eSAndroid Build Coastguard Workerdef stl_info_from_attr(stl_name, is_shared, is_binary = False):
56*7594170eSAndroid Build Coastguard Worker    deps = _stl_deps(stl_name, is_shared, is_binary)
57*7594170eSAndroid Build Coastguard Worker    flags = _stl_flags(stl_name, is_shared)
58*7594170eSAndroid Build Coastguard Worker    return struct(
59*7594170eSAndroid Build Coastguard Worker        static_deps = deps.static,
60*7594170eSAndroid Build Coastguard Worker        shared_deps = deps.shared,
61*7594170eSAndroid Build Coastguard Worker        deps = deps.deps,
62*7594170eSAndroid Build Coastguard Worker        cppflags = flags.cppflags,
63*7594170eSAndroid Build Coastguard Worker        linkopts = flags.linkopts,
64*7594170eSAndroid Build Coastguard Worker    )
65*7594170eSAndroid Build Coastguard Worker
66*7594170eSAndroid Build Coastguard Workerdef _stl_deps(stl_name, is_shared, is_binary = False):
67*7594170eSAndroid Build Coastguard Worker    if stl_name == "none":
68*7594170eSAndroid Build Coastguard Worker        return struct(static = [], shared = [], deps = [])
69*7594170eSAndroid Build Coastguard Worker
70*7594170eSAndroid Build Coastguard Worker    shared, static, deps = [], [], []
71*7594170eSAndroid Build Coastguard Worker    if stl_name in ("", "system"):
72*7594170eSAndroid Build Coastguard Worker        if is_shared:
73*7594170eSAndroid Build Coastguard Worker            shared = select({
74*7594170eSAndroid Build Coastguard Worker                # non-sdk variant, effective STL is libc++
75*7594170eSAndroid Build Coastguard Worker                arch_variant_to_constraints["android"]: [
76*7594170eSAndroid Build Coastguard Worker                    "//external/libcxx:libc++",
77*7594170eSAndroid Build Coastguard Worker                ],
78*7594170eSAndroid Build Coastguard Worker                # sdk variant, effective STL is ndk_system
79*7594170eSAndroid Build Coastguard Worker                "//build/bazel/rules/apex:unbundled_app": [
80*7594170eSAndroid Build Coastguard Worker                    "//bionic/libc:libstdc++",
81*7594170eSAndroid Build Coastguard Worker                ],
82*7594170eSAndroid Build Coastguard Worker                "//conditions:default": ["//external/libcxx:libc++"],  # libc++ is used by host variants
83*7594170eSAndroid Build Coastguard Worker            })
84*7594170eSAndroid Build Coastguard Worker            static = select({
85*7594170eSAndroid Build Coastguard Worker                arch_variant_to_constraints["android"]: [
86*7594170eSAndroid Build Coastguard Worker                    "//external/libcxxabi:libc++demangle",
87*7594170eSAndroid Build Coastguard Worker                ],
88*7594170eSAndroid Build Coastguard Worker                # sdk variant does not have an entry since ndk_system's deps have been added in `shared`
89*7594170eSAndroid Build Coastguard Worker                "//build/bazel/rules/apex:unbundled_app": [],
90*7594170eSAndroid Build Coastguard Worker                "//conditions:default": [],
91*7594170eSAndroid Build Coastguard Worker            })
92*7594170eSAndroid Build Coastguard Worker            deps = select({
93*7594170eSAndroid Build Coastguard Worker                # sdk variant, effective STL is ndk_system
94*7594170eSAndroid Build Coastguard Worker                "//build/bazel/rules/apex:unbundled_app": [
95*7594170eSAndroid Build Coastguard Worker                    _ndk_system,
96*7594170eSAndroid Build Coastguard Worker                ],
97*7594170eSAndroid Build Coastguard Worker                "//conditions:default": [],
98*7594170eSAndroid Build Coastguard Worker            })
99*7594170eSAndroid Build Coastguard Worker
100*7594170eSAndroid Build Coastguard Worker        else:
101*7594170eSAndroid Build Coastguard Worker            shared = select({
102*7594170eSAndroid Build Coastguard Worker                # sdk variant, effective STL is ndk_system
103*7594170eSAndroid Build Coastguard Worker                "//build/bazel/rules/apex:unbundled_app": [
104*7594170eSAndroid Build Coastguard Worker                    "//bionic/libc:libstdc++",
105*7594170eSAndroid Build Coastguard Worker                ],
106*7594170eSAndroid Build Coastguard Worker                "//conditions:default": [],
107*7594170eSAndroid Build Coastguard Worker            })
108*7594170eSAndroid Build Coastguard Worker            static = select({
109*7594170eSAndroid Build Coastguard Worker                # non-sdk variant, effective STL is libc++_static
110*7594170eSAndroid Build Coastguard Worker                arch_variant_to_constraints["android"]: [
111*7594170eSAndroid Build Coastguard Worker                    "//external/libcxx:libc++_static",
112*7594170eSAndroid Build Coastguard Worker                    "//external/libcxxabi:libc++demangle",
113*7594170eSAndroid Build Coastguard Worker                ],
114*7594170eSAndroid Build Coastguard Worker                # sdk variant does not have an entry since ndk_system's deps have been added in `shared`
115*7594170eSAndroid Build Coastguard Worker                "//build/bazel/rules/apex:unbundled_app": [],
116*7594170eSAndroid Build Coastguard Worker                "//conditions:default": ["//external/libcxx:libc++_static"],  # libc++_static is used by host variants
117*7594170eSAndroid Build Coastguard Worker            })
118*7594170eSAndroid Build Coastguard Worker            deps = select({
119*7594170eSAndroid Build Coastguard Worker                # sdk variant, effective STL is ndk_system
120*7594170eSAndroid Build Coastguard Worker                "//build/bazel/rules/apex:unbundled_app": [
121*7594170eSAndroid Build Coastguard Worker                    _ndk_system,
122*7594170eSAndroid Build Coastguard Worker                ],
123*7594170eSAndroid Build Coastguard Worker                "//conditions:default": [],
124*7594170eSAndroid Build Coastguard Worker            })
125*7594170eSAndroid Build Coastguard Worker    elif stl_name == "libc++":
126*7594170eSAndroid Build Coastguard Worker        shared = select({
127*7594170eSAndroid Build Coastguard Worker            arch_variant_to_constraints["android"]: [
128*7594170eSAndroid Build Coastguard Worker                "//external/libcxx:libc++",
129*7594170eSAndroid Build Coastguard Worker            ],
130*7594170eSAndroid Build Coastguard Worker            # sdk variant, effective STL is ndk_libc++_shared
131*7594170eSAndroid Build Coastguard Worker            "//build/bazel/rules/apex:unbundled_app": [
132*7594170eSAndroid Build Coastguard Worker                "//prebuilts/ndk:ndk_libc++_shared",
133*7594170eSAndroid Build Coastguard Worker            ],
134*7594170eSAndroid Build Coastguard Worker            "//conditions:default": ["//external/libcxx:libc++"],  # libc++ is used by host variants
135*7594170eSAndroid Build Coastguard Worker        })
136*7594170eSAndroid Build Coastguard Worker        static = select({
137*7594170eSAndroid Build Coastguard Worker            arch_variant_to_constraints["android"]: [
138*7594170eSAndroid Build Coastguard Worker                "//external/libcxxabi:libc++demangle",
139*7594170eSAndroid Build Coastguard Worker            ],
140*7594170eSAndroid Build Coastguard Worker            "//build/bazel/rules/apex:unbundled_app": [
141*7594170eSAndroid Build Coastguard Worker                "//prebuilts/ndk:ndk_libunwind",
142*7594170eSAndroid Build Coastguard Worker            ],
143*7594170eSAndroid Build Coastguard Worker            "//conditions:default": [],
144*7594170eSAndroid Build Coastguard Worker        })
145*7594170eSAndroid Build Coastguard Worker
146*7594170eSAndroid Build Coastguard Worker    elif stl_name == "libc++_static":
147*7594170eSAndroid Build Coastguard Worker        static = select({
148*7594170eSAndroid Build Coastguard Worker            arch_variant_to_constraints["android"]: [
149*7594170eSAndroid Build Coastguard Worker                "//external/libcxx:libc++_static",
150*7594170eSAndroid Build Coastguard Worker                "//external/libcxxabi:libc++demangle",
151*7594170eSAndroid Build Coastguard Worker            ],
152*7594170eSAndroid Build Coastguard Worker            # sdk variant, effective STL is ndk_libc++_static
153*7594170eSAndroid Build Coastguard Worker            "//build/bazel/rules/apex:unbundled_app": [
154*7594170eSAndroid Build Coastguard Worker                "//prebuilts/ndk:ndk_libc++_static",
155*7594170eSAndroid Build Coastguard Worker                "//prebuilts/ndk:ndk_libc++abi",
156*7594170eSAndroid Build Coastguard Worker                "//prebuilts/ndk:ndk_libunwind",
157*7594170eSAndroid Build Coastguard Worker            ],
158*7594170eSAndroid Build Coastguard Worker            "//conditions:default": ["//external/libcxx:libc++_static"],  # libc++_static is used by host variants
159*7594170eSAndroid Build Coastguard Worker        })
160*7594170eSAndroid Build Coastguard Worker    if is_binary:
161*7594170eSAndroid Build Coastguard Worker        static += _static_binary_deps
162*7594170eSAndroid Build Coastguard Worker    return struct(
163*7594170eSAndroid Build Coastguard Worker        static = static,
164*7594170eSAndroid Build Coastguard Worker        shared = shared,
165*7594170eSAndroid Build Coastguard Worker        deps = deps,
166*7594170eSAndroid Build Coastguard Worker    )
167*7594170eSAndroid Build Coastguard Worker
168*7594170eSAndroid Build Coastguard Workerdef _stl_flags(stl_name, is_shared):
169*7594170eSAndroid Build Coastguard Worker    """returns flags that control STL inclusion
170*7594170eSAndroid Build Coastguard Worker
171*7594170eSAndroid Build Coastguard Worker    Should be kept up to date with
172*7594170eSAndroid Build Coastguard Worker    https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/stl.go;l=197;drc=8722ca5486fa62c07520e09db54b1b330b48da17
173*7594170eSAndroid Build Coastguard Worker
174*7594170eSAndroid Build Coastguard Worker    Args:
175*7594170eSAndroid Build Coastguard Worker        stl_name: string, name of STL library to use
176*7594170eSAndroid Build Coastguard Worker        is_shared: bool, if true, the STL should be linked dynamically
177*7594170eSAndroid Build Coastguard Worker    Returns:
178*7594170eSAndroid Build Coastguard Worker        struct containing flags for CC compilation
179*7594170eSAndroid Build Coastguard Worker    """
180*7594170eSAndroid Build Coastguard Worker    stl_name = _stl_name_resolver(stl_name, is_shared)
181*7594170eSAndroid Build Coastguard Worker
182*7594170eSAndroid Build Coastguard Worker    cppflags_darwin = []
183*7594170eSAndroid Build Coastguard Worker    cppflags_windows_not_bionic = []
184*7594170eSAndroid Build Coastguard Worker    cppflags_not_bionic = []
185*7594170eSAndroid Build Coastguard Worker    linkopts_not_bionic = []
186*7594170eSAndroid Build Coastguard Worker    if stl_name in ("libc++", "libc++_static"):
187*7594170eSAndroid Build Coastguard Worker        cppflags_not_bionic.append("-nostdinc++")
188*7594170eSAndroid Build Coastguard Worker        linkopts_not_bionic.append("-nostdlib++")
189*7594170eSAndroid Build Coastguard Worker
190*7594170eSAndroid Build Coastguard Worker        # libc++'s headers are annotated with availability macros that
191*7594170eSAndroid Build Coastguard Worker        # indicate which version of Mac OS was the first to ship with a
192*7594170eSAndroid Build Coastguard Worker        # libc++ feature available in its *system's* libc++.dylib. We do
193*7594170eSAndroid Build Coastguard Worker        # not use the system's library, but rather ship our own. As such,
194*7594170eSAndroid Build Coastguard Worker        # these availability attributes are meaningless for us but cause
195*7594170eSAndroid Build Coastguard Worker        # build breaks when we try to use code that would not be available
196*7594170eSAndroid Build Coastguard Worker        # in the system's dylib.
197*7594170eSAndroid Build Coastguard Worker        cppflags_darwin.append("-D_LIBCPP_DISABLE_AVAILABILITY")
198*7594170eSAndroid Build Coastguard Worker
199*7594170eSAndroid Build Coastguard Worker        # Disable visiblity annotations since we're using static libc++.
200*7594170eSAndroid Build Coastguard Worker        cppflags_windows_not_bionic.append("-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS")
201*7594170eSAndroid Build Coastguard Worker        cppflags_windows_not_bionic.append("-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS")
202*7594170eSAndroid Build Coastguard Worker
203*7594170eSAndroid Build Coastguard Worker        # Use Win32 threads in libc++.
204*7594170eSAndroid Build Coastguard Worker        cppflags_windows_not_bionic.append("-D_LIBCPP_HAS_THREAD_API_WIN32")
205*7594170eSAndroid Build Coastguard Worker    elif stl_name in ("none"):
206*7594170eSAndroid Build Coastguard Worker        cppflags_not_bionic.append("-nostdinc++")
207*7594170eSAndroid Build Coastguard Worker        linkopts_not_bionic.append("-nostdlib++")
208*7594170eSAndroid Build Coastguard Worker    else:
209*7594170eSAndroid Build Coastguard Worker        #TODO(b/201079053) handle ndk STL flags
210*7594170eSAndroid Build Coastguard Worker        pass
211*7594170eSAndroid Build Coastguard Worker
212*7594170eSAndroid Build Coastguard Worker    return struct(
213*7594170eSAndroid Build Coastguard Worker        cppflags = select({
214*7594170eSAndroid Build Coastguard Worker            "//build/bazel_common_rules/platforms/os:bionic": [],
215*7594170eSAndroid Build Coastguard Worker            "//build/bazel/rules/apex:unbundled_app": [],
216*7594170eSAndroid Build Coastguard Worker            "//conditions:default": cppflags_not_bionic,
217*7594170eSAndroid Build Coastguard Worker        }) + select({
218*7594170eSAndroid Build Coastguard Worker            "//build/bazel_common_rules/platforms/os:darwin": cppflags_darwin,
219*7594170eSAndroid Build Coastguard Worker            "//build/bazel_common_rules/platforms/os:windows": (
220*7594170eSAndroid Build Coastguard Worker                cppflags_windows_not_bionic
221*7594170eSAndroid Build Coastguard Worker            ),
222*7594170eSAndroid Build Coastguard Worker            "//conditions:default": [],
223*7594170eSAndroid Build Coastguard Worker        }),
224*7594170eSAndroid Build Coastguard Worker        linkopts = select({
225*7594170eSAndroid Build Coastguard Worker            "//build/bazel_common_rules/platforms/os:bionic": [],
226*7594170eSAndroid Build Coastguard Worker            "//build/bazel/rules/apex:unbundled_app": [],
227*7594170eSAndroid Build Coastguard Worker            "//build/bazel/rules/apex:unbundled_app.arm": ["-Wl,--exclude-libs,libunwind.a"],
228*7594170eSAndroid Build Coastguard Worker            "//conditions:default": linkopts_not_bionic,
229*7594170eSAndroid Build Coastguard Worker        }),
230*7594170eSAndroid Build Coastguard Worker    )
231