blob: 0f2a8782483e1a4e05f0db340e41907c5bb7134d [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
Ivan Lozano52767be2019-10-18 14:49:46 -070028func getNdkStlFamilyAndLinkType(m LinkableInterface) (string, string) {
29 stl := m.SelectedStl()
Dan Albert202fe492017-12-15 13:56:59 -080030 switch stl {
Colin Crossb60190a2018-09-04 16:28:17 -070031 case "ndk_libc++_shared":
32 return "libc++", "shared"
33 case "ndk_libc++_static":
34 return "libc++", "static"
Dan Albert202fe492017-12-15 13:56:59 -080035 case "ndk_system":
Colin Crossb60190a2018-09-04 16:28:17 -070036 return "system", "shared"
Dan Albert202fe492017-12-15 13:56:59 -080037 case "":
Colin Crossb60190a2018-09-04 16:28:17 -070038 return "none", "none"
Dan Albert202fe492017-12-15 13:56:59 -080039 default:
Colin Crossb60190a2018-09-04 16:28:17 -070040 panic(fmt.Errorf("stl: %q is not a valid STL", stl))
Dan Albert202fe492017-12-15 13:56:59 -080041 }
42}
43
Colin Crossca860ac2016-01-04 14:34:37 -080044type StlProperties struct {
Dan Albert749fc782018-01-04 14:21:14 -080045 // Select the STL library to use. Possible values are "libc++",
46 // "libc++_static", "libstdc++", or "none". Leave blank to select the
47 // default.
Colin Cross245ced72017-07-20 16:57:46 -070048 Stl *string `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -080049
50 SelectedStl string `blueprint:"mutated"`
51}
52
Colin Crossa8e07cc2016-04-04 15:07:06 -070053type stl struct {
Colin Crossca860ac2016-01-04 14:34:37 -080054 Properties StlProperties
55}
56
Colin Crossa8e07cc2016-04-04 15:07:06 -070057func (stl *stl) props() []interface{} {
Colin Crossca860ac2016-01-04 14:34:37 -080058 return []interface{}{&stl.Properties}
59}
60
Colin Crossa8e07cc2016-04-04 15:07:06 -070061func (stl *stl) begin(ctx BaseModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -080062 stl.Properties.SelectedStl = func() string {
Colin Cross79248852016-07-12 13:12:33 -070063 s := ""
64 if stl.Properties.Stl != nil {
65 s = *stl.Properties.Stl
Colin Crossbe763f72021-04-26 17:07:17 -070066 } else if ctx.header() {
67 s = "none"
Colin Cross79248852016-07-12 13:12:33 -070068 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070069 if ctx.useSdk() && ctx.Device() {
Colin Cross79248852016-07-12 13:12:33 -070070 switch s {
Sasha Smundakfc22e4e2019-03-24 14:17:56 -070071 case "", "system":
Colin Crossca860ac2016-01-04 14:34:37 -080072 return "ndk_system"
Dan Albert749fc782018-01-04 14:21:14 -080073 case "c++_shared", "c++_static":
Colin Cross79248852016-07-12 13:12:33 -070074 return "ndk_lib" + s
David Benjamin87f9f032017-01-25 14:10:04 -050075 case "libc++":
76 return "ndk_libc++_shared"
77 case "libc++_static":
78 return "ndk_libc++_static"
Colin Cross4a97cb42016-04-21 15:53:42 -070079 case "none":
80 return ""
Colin Crossca860ac2016-01-04 14:34:37 -080081 default:
Colin Cross79248852016-07-12 13:12:33 -070082 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", s)
Colin Crossca860ac2016-01-04 14:34:37 -080083 return ""
84 }
Colin Cross3edeee12017-04-04 12:59:48 -070085 } else if ctx.Windows() {
Colin Cross79248852016-07-12 13:12:33 -070086 switch s {
Pirama Arumuga Nainara403cc72018-08-08 10:28:12 -070087 case "libc++", "libc++_static", "":
88 // Only use static libc++ for Windows.
89 return "libc++_static"
Colin Crossca860ac2016-01-04 14:34:37 -080090 case "none":
91 return ""
92 default:
Colin Cross79248852016-07-12 13:12:33 -070093 ctx.ModuleErrorf("stl: %q is not a supported STL for windows", s)
Colin Crossca860ac2016-01-04 14:34:37 -080094 return ""
95 }
96 } else {
Colin Cross79248852016-07-12 13:12:33 -070097 switch s {
Dan Willemsen141d5662016-06-15 13:47:51 -070098 case "libc++", "libc++_static":
Colin Cross79248852016-07-12 13:12:33 -070099 return s
Colin Crossc511bc52020-04-07 16:50:32 +0000100 case "c++_shared":
101 return "libc++"
102 case "c++_static":
103 return "libc++_static"
Colin Crossca860ac2016-01-04 14:34:37 -0800104 case "none":
105 return ""
Colin Crossc511bc52020-04-07 16:50:32 +0000106 case "", "system":
Colin Crossca860ac2016-01-04 14:34:37 -0800107 if ctx.static() {
108 return "libc++_static"
109 } else {
110 return "libc++"
111 }
112 default:
Colin Cross79248852016-07-12 13:12:33 -0700113 ctx.ModuleErrorf("stl: %q is not a supported STL", s)
Colin Crossca860ac2016-01-04 14:34:37 -0800114 return ""
115 }
116 }
117 }()
118}
119
Dan Albert90b9bbc2018-11-15 11:29:28 -0800120func needsLibAndroidSupport(ctx BaseModuleContext) bool {
Dan Albert1a246272020-07-06 14:49:35 -0700121 version := nativeApiLevelOrPanic(ctx, ctx.sdkVersion())
122 return version.LessThan(android.FirstNonLibAndroidSupportVersion)
Dan Albert90b9bbc2018-11-15 11:29:28 -0800123}
124
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800125func staticUnwinder(ctx android.BaseModuleContext) string {
Inseob Kimd4c9f552021-04-08 19:28:28 +0900126 vndkVersion := ctx.Module().(*Module).VndkVersion()
127
128 // Modules using R vndk use different unwinder
129 if vndkVersion == "30" {
130 if ctx.Arch().ArchType == android.Arm {
131 return "libunwind_llvm"
132 } else {
133 return "libgcc_stripped"
134 }
135 }
136
Ryan Prichardb35a85e2021-01-13 19:18:53 -0800137 return "libunwind"
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800138}
139
Colin Crossa8e07cc2016-04-04 15:07:06 -0700140func (stl *stl) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Crossca860ac2016-01-04 14:34:37 -0800141 switch stl.Properties.SelectedStl {
142 case "libstdc++":
Dan Willemsen141d5662016-06-15 13:47:51 -0700143 // Nothing
Colin Crossca860ac2016-01-04 14:34:37 -0800144 case "libc++", "libc++_static":
145 if stl.Properties.SelectedStl == "libc++" {
146 deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl)
147 } else {
148 deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl)
149 }
Dan Albert2da19cb2019-07-24 12:17:40 -0700150 if ctx.Device() && !ctx.useSdk() {
151 // __cxa_demangle is not a part of libc++.so on the device since
152 // it's large and most processes don't need it. Statically link
153 // libc++demangle into every process so that users still have it if
154 // needed, but the linker won't include this unless it is actually
155 // called.
156 // http://b/138245375
157 deps.StaticLibs = append(deps.StaticLibs, "libc++demangle")
158 }
Dan Willemsen2e47b342016-11-17 01:02:25 -0800159 if ctx.toolchain().Bionic() {
Colin Crossca860ac2016-01-04 14:34:37 -0800160 if ctx.staticBinary() {
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800161 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", staticUnwinder(ctx))
162 } else {
163 deps.StaticUnwinderIfLegacy = true
Colin Crossca860ac2016-01-04 14:34:37 -0800164 }
165 }
166 case "":
167 // None or error.
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800168 if ctx.toolchain().Bionic() && ctx.Module().Name() == "libc++" {
169 deps.StaticUnwinderIfLegacy = true
170 }
Colin Crossca860ac2016-01-04 14:34:37 -0800171 case "ndk_system":
172 // TODO: Make a system STL prebuilt for the NDK.
173 // The system STL doesn't have a prebuilt (it uses the system's libstdc++), but it does have
174 // its own includes. The includes are handled in CCBase.Flags().
175 deps.SharedLibs = append([]string{"libstdc++"}, deps.SharedLibs...)
Ryan Prichardb1703652018-03-26 16:30:31 -0700176 case "ndk_libc++_shared", "ndk_libc++_static":
177 if stl.Properties.SelectedStl == "ndk_libc++_shared" {
178 deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl)
179 } else {
180 deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl, "ndk_libc++abi")
181 }
Dan Albert90b9bbc2018-11-15 11:29:28 -0800182 if needsLibAndroidSupport(ctx) {
Colin Crossc113e3c2021-06-14 16:15:15 -0700183 // Use LateStaticLibs for ndk_libandroid_support so that its include directories
184 // come after ndk_libc++_static or ndk_libc++_shared.
185 deps.LateStaticLibs = append(deps.LateStaticLibs, "ndk_libandroid_support")
Dan Albert90b9bbc2018-11-15 11:29:28 -0800186 }
Ryan Prichard4ccd4902021-03-31 15:29:11 -0700187 deps.StaticLibs = append(deps.StaticLibs, "ndk_libunwind")
Colin Crossca860ac2016-01-04 14:34:37 -0800188 default:
189 panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl))
190 }
191
192 return deps
193}
194
Colin Crossa8e07cc2016-04-04 15:07:06 -0700195func (stl *stl) flags(ctx ModuleContext, flags Flags) Flags {
Colin Crossca860ac2016-01-04 14:34:37 -0800196 switch stl.Properties.SelectedStl {
197 case "libc++", "libc++_static":
Dan Alberta07b8452018-01-11 13:00:46 -0800198 if ctx.Darwin() {
199 // libc++'s headers are annotated with availability macros that
200 // indicate which version of Mac OS was the first to ship with a
201 // libc++ feature available in its *system's* libc++.dylib. We do
202 // not use the system's library, but rather ship our own. As such,
203 // these availability attributes are meaningless for us but cause
204 // build breaks when we try to use code that would not be available
205 // in the system's dylib.
Colin Cross4af21ed2019-11-04 09:37:55 -0800206 flags.Local.CppFlags = append(flags.Local.CppFlags,
Dan Alberta07b8452018-01-11 13:00:46 -0800207 "-D_LIBCPP_DISABLE_AVAILABILITY")
208 }
209
Dan Willemsen2e47b342016-11-17 01:02:25 -0800210 if !ctx.toolchain().Bionic() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800211 flags.Local.CppFlags = append(flags.Local.CppFlags, "-nostdinc++")
Colin Crossf7a17da2019-10-03 15:48:34 -0700212 flags.extraLibFlags = append(flags.extraLibFlags, "-nostdlib++")
Pirama Arumuga Nainara403cc72018-08-08 10:28:12 -0700213 if ctx.Windows() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800214 flags.Local.CppFlags = append(flags.Local.CppFlags,
Pirama Arumuga Nainara403cc72018-08-08 10:28:12 -0700215 // Disable visiblity annotations since we're using static
216 // libc++.
217 "-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS",
218 "-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS",
219 // Use Win32 threads in libc++.
220 "-D_LIBCPP_HAS_THREAD_API_WIN32")
221 }
Colin Crossca860ac2016-01-04 14:34:37 -0800222 }
223 case "libstdc++":
Dan Willemsen141d5662016-06-15 13:47:51 -0700224 // Nothing
Colin Crossca860ac2016-01-04 14:34:37 -0800225 case "ndk_system":
Colin Cross635c3b02016-05-18 15:37:25 -0700226 ndkSrcRoot := android.PathForSource(ctx, "prebuilts/ndk/current/sources/cxx-stl/system/include")
Colin Cross4af21ed2019-11-04 09:37:55 -0800227 flags.Local.CFlags = append(flags.Local.CFlags, "-isystem "+ndkSrcRoot.String())
Colin Crossca860ac2016-01-04 14:34:37 -0800228 case "ndk_libc++_shared", "ndk_libc++_static":
Christopher Ferrisc3a1e222019-04-10 17:57:50 -0700229 if ctx.Arch().ArchType == android.Arm {
230 // Make sure the _Unwind_XXX symbols are not re-exported.
Colin Cross4af21ed2019-11-04 09:37:55 -0800231 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,libunwind.a")
Christopher Ferrisc3a1e222019-04-10 17:57:50 -0700232 }
Colin Crossca860ac2016-01-04 14:34:37 -0800233 case "":
234 // None or error.
Dan Willemsen2e47b342016-11-17 01:02:25 -0800235 if !ctx.toolchain().Bionic() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800236 flags.Local.CppFlags = append(flags.Local.CppFlags, "-nostdinc++")
Colin Crossf7a17da2019-10-03 15:48:34 -0700237 flags.extraLibFlags = append(flags.extraLibFlags, "-nostdlib++")
Colin Crossca860ac2016-01-04 14:34:37 -0800238 }
239 default:
240 panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl))
241 }
242
243 return flags
244}