blob: 406fa3a3e5c7ed28e8b96aed5d254885150fe31d [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 Cross635c3b02016-05-18 15:37:25 -070018 "android/soong/android"
Colin Crossca860ac2016-01-04 14:34:37 -080019 "fmt"
20)
21
Ivan Lozano52767be2019-10-18 14:49:46 -070022func getNdkStlFamily(m LinkableInterface) string {
Colin Crossb60190a2018-09-04 16:28:17 -070023 family, _ := getNdkStlFamilyAndLinkType(m)
24 return family
25}
26
Ivan Lozano52767be2019-10-18 14:49:46 -070027func getNdkStlFamilyAndLinkType(m LinkableInterface) (string, string) {
28 stl := m.SelectedStl()
Dan Albert202fe492017-12-15 13:56:59 -080029 switch stl {
Colin Crossb60190a2018-09-04 16:28:17 -070030 case "ndk_libc++_shared":
31 return "libc++", "shared"
32 case "ndk_libc++_static":
33 return "libc++", "static"
Dan Albert202fe492017-12-15 13:56:59 -080034 case "ndk_system":
Colin Crossb60190a2018-09-04 16:28:17 -070035 return "system", "shared"
Dan Albert202fe492017-12-15 13:56:59 -080036 case "":
Colin Crossb60190a2018-09-04 16:28:17 -070037 return "none", "none"
Dan Albert202fe492017-12-15 13:56:59 -080038 default:
Colin Crossb60190a2018-09-04 16:28:17 -070039 panic(fmt.Errorf("stl: %q is not a valid STL", stl))
Dan Albert202fe492017-12-15 13:56:59 -080040 }
41}
42
Colin Crossca860ac2016-01-04 14:34:37 -080043type StlProperties struct {
Dan Albert749fc782018-01-04 14:21:14 -080044 // Select the STL library to use. Possible values are "libc++",
45 // "libc++_static", "libstdc++", or "none". Leave blank to select the
46 // default.
Colin Cross245ced72017-07-20 16:57:46 -070047 Stl *string `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -080048
49 SelectedStl string `blueprint:"mutated"`
50}
51
Colin Crossa8e07cc2016-04-04 15:07:06 -070052type stl struct {
Colin Crossca860ac2016-01-04 14:34:37 -080053 Properties StlProperties
54}
55
Colin Crossa8e07cc2016-04-04 15:07:06 -070056func (stl *stl) props() []interface{} {
Colin Crossca860ac2016-01-04 14:34:37 -080057 return []interface{}{&stl.Properties}
58}
59
Colin Crossa8e07cc2016-04-04 15:07:06 -070060func (stl *stl) begin(ctx BaseModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -080061 stl.Properties.SelectedStl = func() string {
Colin Cross79248852016-07-12 13:12:33 -070062 s := ""
63 if stl.Properties.Stl != nil {
64 s = *stl.Properties.Stl
65 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070066 if ctx.useSdk() && ctx.Device() {
Colin Cross79248852016-07-12 13:12:33 -070067 switch s {
Sasha Smundakfc22e4e2019-03-24 14:17:56 -070068 case "", "system":
Colin Crossca860ac2016-01-04 14:34:37 -080069 return "ndk_system"
Dan Albert749fc782018-01-04 14:21:14 -080070 case "c++_shared", "c++_static":
Colin Cross79248852016-07-12 13:12:33 -070071 return "ndk_lib" + s
David Benjamin87f9f032017-01-25 14:10:04 -050072 case "libc++":
73 return "ndk_libc++_shared"
74 case "libc++_static":
75 return "ndk_libc++_static"
Colin Cross4a97cb42016-04-21 15:53:42 -070076 case "none":
77 return ""
Colin Crossca860ac2016-01-04 14:34:37 -080078 default:
Colin Cross79248852016-07-12 13:12:33 -070079 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", s)
Colin Crossca860ac2016-01-04 14:34:37 -080080 return ""
81 }
Colin Cross3edeee12017-04-04 12:59:48 -070082 } else if ctx.Windows() {
Colin Cross79248852016-07-12 13:12:33 -070083 switch s {
Pirama Arumuga Nainara403cc72018-08-08 10:28:12 -070084 case "libc++", "libc++_static", "":
85 // Only use static libc++ for Windows.
86 return "libc++_static"
Colin Crossca860ac2016-01-04 14:34:37 -080087 case "none":
88 return ""
89 default:
Colin Cross79248852016-07-12 13:12:33 -070090 ctx.ModuleErrorf("stl: %q is not a supported STL for windows", s)
Colin Crossca860ac2016-01-04 14:34:37 -080091 return ""
92 }
Doug Hornc32c6b02019-01-17 14:44:05 -080093 } else if ctx.Fuchsia() {
94 switch s {
95 case "c++_static":
96 return "libc++_static"
97 case "c++_shared":
98 return "libc++"
99 case "libc++", "libc++_static":
100 return s
101 case "none":
102 return ""
103 case "":
104 if ctx.static() {
105 return "libc++_static"
106 } else {
107 return "libc++"
108 }
109 default:
110 ctx.ModuleErrorf("stl: %q is not a supported STL on Fuchsia", s)
111 return ""
112 }
Colin Crossca860ac2016-01-04 14:34:37 -0800113 } else {
Colin Cross79248852016-07-12 13:12:33 -0700114 switch s {
Dan Willemsen141d5662016-06-15 13:47:51 -0700115 case "libc++", "libc++_static":
Colin Cross79248852016-07-12 13:12:33 -0700116 return s
Colin Crossc511bc52020-04-07 16:50:32 +0000117 case "c++_shared":
118 return "libc++"
119 case "c++_static":
120 return "libc++_static"
Colin Crossca860ac2016-01-04 14:34:37 -0800121 case "none":
122 return ""
Colin Crossc511bc52020-04-07 16:50:32 +0000123 case "", "system":
Colin Crossca860ac2016-01-04 14:34:37 -0800124 if ctx.static() {
125 return "libc++_static"
126 } else {
127 return "libc++"
128 }
129 default:
Colin Cross79248852016-07-12 13:12:33 -0700130 ctx.ModuleErrorf("stl: %q is not a supported STL", s)
Colin Crossca860ac2016-01-04 14:34:37 -0800131 return ""
132 }
133 }
134 }()
135}
136
Dan Albert90b9bbc2018-11-15 11:29:28 -0800137func needsLibAndroidSupport(ctx BaseModuleContext) bool {
Dan Albert1a246272020-07-06 14:49:35 -0700138 version := nativeApiLevelOrPanic(ctx, ctx.sdkVersion())
139 return version.LessThan(android.FirstNonLibAndroidSupportVersion)
Dan Albert90b9bbc2018-11-15 11:29:28 -0800140}
141
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800142func staticUnwinder(ctx android.BaseModuleContext) string {
143 if ctx.Arch().ArchType == android.Arm {
144 return "libunwind_llvm"
145 } else {
146 return "libgcc_stripped"
147 }
148}
149
Colin Crossa8e07cc2016-04-04 15:07:06 -0700150func (stl *stl) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Crossca860ac2016-01-04 14:34:37 -0800151 switch stl.Properties.SelectedStl {
152 case "libstdc++":
Dan Willemsen141d5662016-06-15 13:47:51 -0700153 // Nothing
Colin Crossca860ac2016-01-04 14:34:37 -0800154 case "libc++", "libc++_static":
155 if stl.Properties.SelectedStl == "libc++" {
156 deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl)
157 } else {
158 deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl)
159 }
Dan Albert2da19cb2019-07-24 12:17:40 -0700160 if ctx.Device() && !ctx.useSdk() {
161 // __cxa_demangle is not a part of libc++.so on the device since
162 // it's large and most processes don't need it. Statically link
163 // libc++demangle into every process so that users still have it if
164 // needed, but the linker won't include this unless it is actually
165 // called.
166 // http://b/138245375
167 deps.StaticLibs = append(deps.StaticLibs, "libc++demangle")
168 }
Dan Willemsen2e47b342016-11-17 01:02:25 -0800169 if ctx.toolchain().Bionic() {
Colin Crossca860ac2016-01-04 14:34:37 -0800170 if ctx.staticBinary() {
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800171 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", staticUnwinder(ctx))
172 } else {
173 deps.StaticUnwinderIfLegacy = true
Colin Crossca860ac2016-01-04 14:34:37 -0800174 }
175 }
176 case "":
177 // None or error.
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800178 if ctx.toolchain().Bionic() && ctx.Module().Name() == "libc++" {
179 deps.StaticUnwinderIfLegacy = true
180 }
Colin Crossca860ac2016-01-04 14:34:37 -0800181 case "ndk_system":
182 // TODO: Make a system STL prebuilt for the NDK.
183 // The system STL doesn't have a prebuilt (it uses the system's libstdc++), but it does have
184 // its own includes. The includes are handled in CCBase.Flags().
185 deps.SharedLibs = append([]string{"libstdc++"}, deps.SharedLibs...)
Ryan Prichardb1703652018-03-26 16:30:31 -0700186 case "ndk_libc++_shared", "ndk_libc++_static":
187 if stl.Properties.SelectedStl == "ndk_libc++_shared" {
188 deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl)
189 } else {
190 deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl, "ndk_libc++abi")
191 }
Dan Albert90b9bbc2018-11-15 11:29:28 -0800192 if needsLibAndroidSupport(ctx) {
193 deps.StaticLibs = append(deps.StaticLibs, "ndk_libandroid_support")
194 }
Ryan Prichardb1703652018-03-26 16:30:31 -0700195 if ctx.Arch().ArchType == android.Arm {
196 deps.StaticLibs = append(deps.StaticLibs, "ndk_libunwind")
Peter Collingbournee5ba2862019-12-10 18:37:45 -0800197 } else {
198 deps.StaticLibs = append(deps.StaticLibs, "libgcc_stripped")
Ryan Prichardb1703652018-03-26 16:30:31 -0700199 }
Colin Crossca860ac2016-01-04 14:34:37 -0800200 default:
201 panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl))
202 }
203
204 return deps
205}
206
Colin Crossa8e07cc2016-04-04 15:07:06 -0700207func (stl *stl) flags(ctx ModuleContext, flags Flags) Flags {
Colin Crossca860ac2016-01-04 14:34:37 -0800208 switch stl.Properties.SelectedStl {
209 case "libc++", "libc++_static":
Dan Alberta07b8452018-01-11 13:00:46 -0800210 if ctx.Darwin() {
211 // libc++'s headers are annotated with availability macros that
212 // indicate which version of Mac OS was the first to ship with a
213 // libc++ feature available in its *system's* libc++.dylib. We do
214 // not use the system's library, but rather ship our own. As such,
215 // these availability attributes are meaningless for us but cause
216 // build breaks when we try to use code that would not be available
217 // in the system's dylib.
Colin Cross4af21ed2019-11-04 09:37:55 -0800218 flags.Local.CppFlags = append(flags.Local.CppFlags,
Dan Alberta07b8452018-01-11 13:00:46 -0800219 "-D_LIBCPP_DISABLE_AVAILABILITY")
220 }
221
Dan Willemsen2e47b342016-11-17 01:02:25 -0800222 if !ctx.toolchain().Bionic() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800223 flags.Local.CppFlags = append(flags.Local.CppFlags, "-nostdinc++")
Colin Crossf7a17da2019-10-03 15:48:34 -0700224 flags.extraLibFlags = append(flags.extraLibFlags, "-nostdlib++")
Pirama Arumuga Nainara403cc72018-08-08 10:28:12 -0700225 if ctx.Windows() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800226 flags.Local.CppFlags = append(flags.Local.CppFlags,
Pirama Arumuga Nainara403cc72018-08-08 10:28:12 -0700227 // Disable visiblity annotations since we're using static
228 // libc++.
229 "-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS",
230 "-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS",
231 // Use Win32 threads in libc++.
232 "-D_LIBCPP_HAS_THREAD_API_WIN32")
233 }
Colin Crossca860ac2016-01-04 14:34:37 -0800234 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700235 if ctx.Arch().ArchType == android.Arm {
Colin Cross4af21ed2019-11-04 09:37:55 -0800236 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a")
Colin Crossca860ac2016-01-04 14:34:37 -0800237 }
238 }
239 case "libstdc++":
Dan Willemsen141d5662016-06-15 13:47:51 -0700240 // Nothing
Colin Crossca860ac2016-01-04 14:34:37 -0800241 case "ndk_system":
Colin Cross635c3b02016-05-18 15:37:25 -0700242 ndkSrcRoot := android.PathForSource(ctx, "prebuilts/ndk/current/sources/cxx-stl/system/include")
Colin Cross4af21ed2019-11-04 09:37:55 -0800243 flags.Local.CFlags = append(flags.Local.CFlags, "-isystem "+ndkSrcRoot.String())
Colin Crossca860ac2016-01-04 14:34:37 -0800244 case "ndk_libc++_shared", "ndk_libc++_static":
Christopher Ferrisc3a1e222019-04-10 17:57:50 -0700245 if ctx.Arch().ArchType == android.Arm {
246 // Make sure the _Unwind_XXX symbols are not re-exported.
Colin Cross4af21ed2019-11-04 09:37:55 -0800247 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,libunwind.a")
Christopher Ferrisc3a1e222019-04-10 17:57:50 -0700248 }
Colin Crossca860ac2016-01-04 14:34:37 -0800249 case "":
250 // None or error.
Dan Willemsen2e47b342016-11-17 01:02:25 -0800251 if !ctx.toolchain().Bionic() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800252 flags.Local.CppFlags = append(flags.Local.CppFlags, "-nostdinc++")
Colin Crossf7a17da2019-10-03 15:48:34 -0700253 flags.extraLibFlags = append(flags.extraLibFlags, "-nostdlib++")
Colin Crossca860ac2016-01-04 14:34:37 -0800254 }
255 default:
256 panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl))
257 }
258
259 return flags
260}