1# Copyright (C) 2021 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15# Helpers for stl property resolution. 16# These mappings taken from build/soong/cc/stl.go 17 18load("//build/bazel/product_variables:constants.bzl", "constants") 19 20_libcpp_stl_names = { 21 "libc++": True, 22 "libc++_static": True, 23 "": True, 24 "system": True, 25} 26 27# https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/stl.go;l=157;drc=55d98d2ba142d6c35894b1092397e2b5a70bc2e8 28_common_static_deps = select({ 29 constants.ArchVariantToConstraints["android"]: ["//external/libcxxabi:libc++demangle"], 30 "//conditions:default": [], 31}) 32 33# https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/stl.go;l=162;drc=cb0ac95bde896fa2aa59193a37ceb580758c322c 34# this should vary based on vndk version 35# skip libm and libc because then we would have duplicates due to system_shared_library 36_libunwind = "//prebuilts/clang/host/linux-x86:libunwind" 37 38_static_binary_deps = select({ 39 constants.ArchVariantToConstraints["android"]: [_libunwind], 40 constants.ArchVariantToConstraints["linux_bionic"]: [_libunwind], 41 "//conditions:default": [], 42}) 43 44def _stl_name_resolver(stl_name, is_shared): 45 if stl_name == "none": 46 return stl_name 47 48 if stl_name not in _libcpp_stl_names: 49 fail("Unhandled stl %s" % stl_name) 50 51 if stl_name in ("", "system"): 52 if is_shared: 53 stl_name = "libc++" 54 else: 55 stl_name = "libc++_static" 56 return stl_name 57 58def stl_info_from_attr(stl_name, is_shared, is_binary = False): 59 deps = _stl_deps(stl_name, is_shared, is_binary) 60 flags = _stl_flags(stl_name, is_shared) 61 return struct( 62 static_deps = deps.static, 63 shared_deps = deps.shared, 64 cppflags = flags.cppflags, 65 linkopts = flags.linkopts, 66 ) 67 68def _stl_deps(stl_name, is_shared, is_binary = False): 69 stl_name = _stl_name_resolver(stl_name, is_shared) 70 if stl_name == "none": 71 return struct(static = [], shared = []) 72 73 shared, static = [], [] 74 if stl_name == "libc++": 75 static, shared = _shared_stl_deps() 76 elif stl_name == "libc++_static": 77 static = _static_stl_deps() 78 if is_binary: 79 static += _static_binary_deps 80 return struct( 81 static = static, 82 shared = shared, 83 ) 84 85def _static_stl_deps(): 86 # TODO(b/201079053): Handle useSdk, windows, preferably with selects. 87 return ["//external/libcxx:libc++_static"] + _common_static_deps 88 89def _shared_stl_deps(): 90 return (_common_static_deps, ["//external/libcxx:libc++"]) 91 92def _stl_flags(stl_name, is_shared): 93 """returns flags that control STL inclusion 94 95 Should be kept up to date with 96 https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/stl.go;l=197;drc=8722ca5486fa62c07520e09db54b1b330b48da17 97 98 Args: 99 stl_name: string, name of STL library to use 100 is_shared: bool, if true, the STL should be linked dynamically 101 Returns: 102 struct containing flags for CC compilation 103 """ 104 stl_name = _stl_name_resolver(stl_name, is_shared) 105 106 cppflags_darwin = [] 107 cppflags_windows_not_bionic = [] 108 cppflags_not_bionic = [] 109 linkopts_not_bionic = [] 110 if stl_name in ("libc++", "libc++_static"): 111 cppflags_not_bionic.append("-nostdinc++") 112 linkopts_not_bionic.append("-nostdlib++") 113 114 # libc++'s headers are annotated with availability macros that 115 # indicate which version of Mac OS was the first to ship with a 116 # libc++ feature available in its *system's* libc++.dylib. We do 117 # not use the system's library, but rather ship our own. As such, 118 # these availability attributes are meaningless for us but cause 119 # build breaks when we try to use code that would not be available 120 # in the system's dylib. 121 cppflags_darwin.append("-D_LIBCPP_DISABLE_AVAILABILITY") 122 123 # Disable visiblity annotations since we're using static libc++. 124 cppflags_windows_not_bionic.append("-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS") 125 cppflags_windows_not_bionic.append("-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS") 126 127 # Use Win32 threads in libc++. 128 cppflags_windows_not_bionic.append("-D_LIBCPP_HAS_THREAD_API_WIN32") 129 elif stl_name in ("none"): 130 cppflags_not_bionic.append("-nostdinc++") 131 linkopts_not_bionic.append("-nostdlib++") 132 else: 133 #TODO(b/201079053) handle ndk STL flags 134 pass 135 136 return struct( 137 cppflags = select({ 138 "//build/bazel/platforms/os:bionic": [], 139 "//conditions:default": cppflags_not_bionic, 140 }) + select({ 141 "//build/bazel/platforms/os:darwin": cppflags_darwin, 142 "//build/bazel/platforms/os:windows": ( 143 cppflags_windows_not_bionic 144 ), 145 "//conditions:default": [], 146 }), 147 linkopts = select({ 148 "//build/bazel/platforms/os:bionic": [], 149 "//conditions:default": linkopts_not_bionic, 150 }), 151 ) 152