blob: 8f92dcb402793056b9868cfddf473a245a439534 [file] [log] [blame]
Colin Crossca860ac2016-01-04 14:34:37 -08001// Copyright 2016 Google Inc. All rights reserved.
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
15package cc
16
17import (
Colin Crossca860ac2016-01-04 14:34:37 -080018 "fmt"
Colin Crosscb0ac952021-07-20 13:17:15 -070019
20 "android/soong/android"
Colin Crossca860ac2016-01-04 14:34:37 -080021)
22
Ivan Lozano52767be2019-10-18 14:49:46 -070023func getNdkStlFamily(m LinkableInterface) string {
Colin Crossb60190a2018-09-04 16:28:17 -070024 family, _ := getNdkStlFamilyAndLinkType(m)
25 return family
26}
27
Liz Kammer7128d382022-05-12 11:42:33 -040028func deduplicateStlInput(stl string) string {
29 switch stl {
30 case "c++_shared":
31 return "libc++"
32 case "c++_static":
33 return "libc++_static"
34 }
35 return stl
36}
37
Ivan Lozano52767be2019-10-18 14:49:46 -070038func getNdkStlFamilyAndLinkType(m LinkableInterface) (string, string) {
39 stl := m.SelectedStl()
Dan Albert202fe492017-12-15 13:56:59 -080040 switch stl {
Colin Cross4c608f52023-01-17 12:06:45 -080041 case "ndk_libc++_shared", "libc++":
Colin Crossb60190a2018-09-04 16:28:17 -070042 return "libc++", "shared"
Colin Cross4c608f52023-01-17 12:06:45 -080043 case "ndk_libc++_static", "libc++_static":
Colin Crossb60190a2018-09-04 16:28:17 -070044 return "libc++", "static"
Dan Albert202fe492017-12-15 13:56:59 -080045 case "ndk_system":
Colin Crossb60190a2018-09-04 16:28:17 -070046 return "system", "shared"
Dan Albert202fe492017-12-15 13:56:59 -080047 case "":
Colin Crossb60190a2018-09-04 16:28:17 -070048 return "none", "none"
Dan Albert202fe492017-12-15 13:56:59 -080049 default:
Colin Crossb60190a2018-09-04 16:28:17 -070050 panic(fmt.Errorf("stl: %q is not a valid STL", stl))
Dan Albert202fe492017-12-15 13:56:59 -080051 }
52}
53
Colin Crossca860ac2016-01-04 14:34:37 -080054type StlProperties struct {
Dan Albert749fc782018-01-04 14:21:14 -080055 // Select the STL library to use. Possible values are "libc++",
56 // "libc++_static", "libstdc++", or "none". Leave blank to select the
57 // default.
Colin Cross245ced72017-07-20 16:57:46 -070058 Stl *string `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -080059
60 SelectedStl string `blueprint:"mutated"`
61}
62
Colin Crossa8e07cc2016-04-04 15:07:06 -070063type stl struct {
Colin Crossca860ac2016-01-04 14:34:37 -080064 Properties StlProperties
65}
66
Colin Crossa8e07cc2016-04-04 15:07:06 -070067func (stl *stl) props() []interface{} {
Colin Crossca860ac2016-01-04 14:34:37 -080068 return []interface{}{&stl.Properties}
69}
70
Colin Crossa8e07cc2016-04-04 15:07:06 -070071func (stl *stl) begin(ctx BaseModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -080072 stl.Properties.SelectedStl = func() string {
Colin Cross79248852016-07-12 13:12:33 -070073 s := ""
74 if stl.Properties.Stl != nil {
75 s = *stl.Properties.Stl
Colin Crossbe763f72021-04-26 17:07:17 -070076 } else if ctx.header() {
77 s = "none"
Colin Cross79248852016-07-12 13:12:33 -070078 }
Liz Kammer7128d382022-05-12 11:42:33 -040079 if s == "none" {
80 return ""
81 }
82 s = deduplicateStlInput(s)
Prashanth Swaminathan08e634d2023-07-26 09:42:41 -070083 if ctx.useSdk() && ctx.Device() {
Colin Cross79248852016-07-12 13:12:33 -070084 switch s {
Sasha Smundakfc22e4e2019-03-24 14:17:56 -070085 case "", "system":
Colin Crossca860ac2016-01-04 14:34:37 -080086 return "ndk_system"
David Benjamin87f9f032017-01-25 14:10:04 -050087 case "libc++":
88 return "ndk_libc++_shared"
89 case "libc++_static":
90 return "ndk_libc++_static"
Colin Crossca860ac2016-01-04 14:34:37 -080091 default:
Colin Cross79248852016-07-12 13:12:33 -070092 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", s)
Colin Crossca860ac2016-01-04 14:34:37 -080093 return ""
94 }
Colin Cross3edeee12017-04-04 12:59:48 -070095 } else if ctx.Windows() {
Colin Cross79248852016-07-12 13:12:33 -070096 switch s {
Pirama Arumuga Nainara403cc72018-08-08 10:28:12 -070097 case "libc++", "libc++_static", "":
98 // Only use static libc++ for Windows.
99 return "libc++_static"
Colin Crossca860ac2016-01-04 14:34:37 -0800100 default:
Colin Cross79248852016-07-12 13:12:33 -0700101 ctx.ModuleErrorf("stl: %q is not a supported STL for windows", s)
Colin Crossca860ac2016-01-04 14:34:37 -0800102 return ""
103 }
104 } else {
Colin Cross79248852016-07-12 13:12:33 -0700105 switch s {
Dan Willemsen141d5662016-06-15 13:47:51 -0700106 case "libc++", "libc++_static":
Colin Cross79248852016-07-12 13:12:33 -0700107 return s
Colin Crossc511bc52020-04-07 16:50:32 +0000108 case "", "system":
Colin Crossca860ac2016-01-04 14:34:37 -0800109 if ctx.static() {
110 return "libc++_static"
111 } else {
112 return "libc++"
113 }
114 default:
Colin Cross79248852016-07-12 13:12:33 -0700115 ctx.ModuleErrorf("stl: %q is not a supported STL", s)
Colin Crossca860ac2016-01-04 14:34:37 -0800116 return ""
117 }
118 }
119 }()
120}
121
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800122func staticUnwinder(ctx android.BaseModuleContext) string {
Inseob Kimd4c9f552021-04-08 19:28:28 +0900123 vndkVersion := ctx.Module().(*Module).VndkVersion()
124
125 // Modules using R vndk use different unwinder
126 if vndkVersion == "30" {
127 if ctx.Arch().ArchType == android.Arm {
128 return "libunwind_llvm"
129 } else {
130 return "libgcc_stripped"
131 }
132 }
133
Ryan Prichardb35a85e2021-01-13 19:18:53 -0800134 return "libunwind"
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800135}
136
Sam Delmericoa8762ad2022-10-14 12:25:13 -0400137// Should be kept up to date with
138// https://cs.android.com/android/platform/superproject/+/master:build/bazel/rules/cc/stl.bzl;l=46;drc=21771b671ae08565033768a6d3d151c54f887fa2
Colin Crossa8e07cc2016-04-04 15:07:06 -0700139func (stl *stl) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Crossca860ac2016-01-04 14:34:37 -0800140 switch stl.Properties.SelectedStl {
141 case "libstdc++":
Dan Willemsen141d5662016-06-15 13:47:51 -0700142 // Nothing
Colin Crossca860ac2016-01-04 14:34:37 -0800143 case "libc++", "libc++_static":
144 if stl.Properties.SelectedStl == "libc++" {
145 deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl)
146 } else {
147 deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl)
148 }
Dan Albert2da19cb2019-07-24 12:17:40 -0700149 if ctx.Device() && !ctx.useSdk() {
150 // __cxa_demangle is not a part of libc++.so on the device since
151 // it's large and most processes don't need it. Statically link
152 // libc++demangle into every process so that users still have it if
153 // needed, but the linker won't include this unless it is actually
154 // called.
155 // http://b/138245375
156 deps.StaticLibs = append(deps.StaticLibs, "libc++demangle")
157 }
Dan Willemsen2e47b342016-11-17 01:02:25 -0800158 if ctx.toolchain().Bionic() {
Colin Crossca860ac2016-01-04 14:34:37 -0800159 if ctx.staticBinary() {
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800160 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", staticUnwinder(ctx))
161 } else {
162 deps.StaticUnwinderIfLegacy = true
Colin Crossca860ac2016-01-04 14:34:37 -0800163 }
164 }
165 case "":
166 // None or error.
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800167 if ctx.toolchain().Bionic() && ctx.Module().Name() == "libc++" {
168 deps.StaticUnwinderIfLegacy = true
169 }
Colin Crossca860ac2016-01-04 14:34:37 -0800170 case "ndk_system":
171 // TODO: Make a system STL prebuilt for the NDK.
172 // The system STL doesn't have a prebuilt (it uses the system's libstdc++), but it does have
173 // its own includes. The includes are handled in CCBase.Flags().
174 deps.SharedLibs = append([]string{"libstdc++"}, deps.SharedLibs...)
Ryan Prichardb1703652018-03-26 16:30:31 -0700175 case "ndk_libc++_shared", "ndk_libc++_static":
176 if stl.Properties.SelectedStl == "ndk_libc++_shared" {
177 deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl)
178 } else {
179 deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl, "ndk_libc++abi")
180 }
Ryan Prichard4ccd4902021-03-31 15:29:11 -0700181 deps.StaticLibs = append(deps.StaticLibs, "ndk_libunwind")
Colin Crossca860ac2016-01-04 14:34:37 -0800182 default:
183 panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl))
184 }
185
186 return deps
187}
188
Sam Delmericoa8762ad2022-10-14 12:25:13 -0400189// Should be kept up to date with
190// https://cs.android.com/android/platform/superproject/+/master:build/bazel/rules/cc/stl.bzl;l=94;drc=5bc8e39d2637927dc57dd0850210d43d348a1341
Colin Crossa8e07cc2016-04-04 15:07:06 -0700191func (stl *stl) flags(ctx ModuleContext, flags Flags) Flags {
Colin Crossca860ac2016-01-04 14:34:37 -0800192 switch stl.Properties.SelectedStl {
193 case "libc++", "libc++_static":
Dan Alberta07b8452018-01-11 13:00:46 -0800194 if ctx.Darwin() {
195 // libc++'s headers are annotated with availability macros that
196 // indicate which version of Mac OS was the first to ship with a
197 // libc++ feature available in its *system's* libc++.dylib. We do
198 // not use the system's library, but rather ship our own. As such,
199 // these availability attributes are meaningless for us but cause
200 // build breaks when we try to use code that would not be available
201 // in the system's dylib.
Colin Cross4af21ed2019-11-04 09:37:55 -0800202 flags.Local.CppFlags = append(flags.Local.CppFlags,
Dan Alberta07b8452018-01-11 13:00:46 -0800203 "-D_LIBCPP_DISABLE_AVAILABILITY")
204 }
205
Dan Willemsen2e47b342016-11-17 01:02:25 -0800206 if !ctx.toolchain().Bionic() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800207 flags.Local.CppFlags = append(flags.Local.CppFlags, "-nostdinc++")
Colin Crossf7a17da2019-10-03 15:48:34 -0700208 flags.extraLibFlags = append(flags.extraLibFlags, "-nostdlib++")
Pirama Arumuga Nainara403cc72018-08-08 10:28:12 -0700209 if ctx.Windows() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800210 flags.Local.CppFlags = append(flags.Local.CppFlags,
Pirama Arumuga Nainara403cc72018-08-08 10:28:12 -0700211 // Disable visiblity annotations since we're using static
212 // libc++.
213 "-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS",
214 "-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS",
215 // Use Win32 threads in libc++.
216 "-D_LIBCPP_HAS_THREAD_API_WIN32")
217 }
Colin Crossca860ac2016-01-04 14:34:37 -0800218 }
219 case "libstdc++":
Dan Willemsen141d5662016-06-15 13:47:51 -0700220 // Nothing
Colin Crossca860ac2016-01-04 14:34:37 -0800221 case "ndk_system":
Colin Cross635c3b02016-05-18 15:37:25 -0700222 ndkSrcRoot := android.PathForSource(ctx, "prebuilts/ndk/current/sources/cxx-stl/system/include")
Colin Cross4af21ed2019-11-04 09:37:55 -0800223 flags.Local.CFlags = append(flags.Local.CFlags, "-isystem "+ndkSrcRoot.String())
Colin Crossca860ac2016-01-04 14:34:37 -0800224 case "ndk_libc++_shared", "ndk_libc++_static":
Christopher Ferrisc3a1e222019-04-10 17:57:50 -0700225 if ctx.Arch().ArchType == android.Arm {
226 // Make sure the _Unwind_XXX symbols are not re-exported.
Colin Cross4af21ed2019-11-04 09:37:55 -0800227 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,libunwind.a")
Christopher Ferrisc3a1e222019-04-10 17:57:50 -0700228 }
Colin Crossca860ac2016-01-04 14:34:37 -0800229 case "":
230 // None or error.
Dan Willemsen2e47b342016-11-17 01:02:25 -0800231 if !ctx.toolchain().Bionic() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800232 flags.Local.CppFlags = append(flags.Local.CppFlags, "-nostdinc++")
Colin Crossf7a17da2019-10-03 15:48:34 -0700233 flags.extraLibFlags = append(flags.extraLibFlags, "-nostdlib++")
Colin Crossca860ac2016-01-04 14:34:37 -0800234 }
235 default:
236 panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl))
237 }
238
239 return flags
240}