| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 1 | // 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 |  | 
|  | 15 | package cc | 
|  | 16 |  | 
|  | 17 | import ( | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 18 | "android/soong/android" | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 19 | "fmt" | 
|  | 20 | ) | 
|  | 21 |  | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 22 | func getNdkStlFamily(m LinkableInterface) string { | 
| Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 23 | family, _ := getNdkStlFamilyAndLinkType(m) | 
|  | 24 | return family | 
|  | 25 | } | 
|  | 26 |  | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 27 | func getNdkStlFamilyAndLinkType(m LinkableInterface) (string, string) { | 
|  | 28 | stl := m.SelectedStl() | 
| Dan Albert | 202fe49 | 2017-12-15 13:56:59 -0800 | [diff] [blame] | 29 | switch stl { | 
| Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 30 | case "ndk_libc++_shared": | 
|  | 31 | return "libc++", "shared" | 
|  | 32 | case "ndk_libc++_static": | 
|  | 33 | return "libc++", "static" | 
| Dan Albert | 202fe49 | 2017-12-15 13:56:59 -0800 | [diff] [blame] | 34 | case "ndk_system": | 
| Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 35 | return "system", "shared" | 
| Dan Albert | 202fe49 | 2017-12-15 13:56:59 -0800 | [diff] [blame] | 36 | case "": | 
| Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 37 | return "none", "none" | 
| Dan Albert | 202fe49 | 2017-12-15 13:56:59 -0800 | [diff] [blame] | 38 | default: | 
| Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 39 | panic(fmt.Errorf("stl: %q is not a valid STL", stl)) | 
| Dan Albert | 202fe49 | 2017-12-15 13:56:59 -0800 | [diff] [blame] | 40 | } | 
|  | 41 | } | 
|  | 42 |  | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 43 | type StlProperties struct { | 
| Dan Albert | 749fc78 | 2018-01-04 14:21:14 -0800 | [diff] [blame] | 44 | // Select the STL library to use.  Possible values are "libc++", | 
|  | 45 | // "libc++_static", "libstdc++", or "none". Leave blank to select the | 
|  | 46 | // default. | 
| Colin Cross | 245ced7 | 2017-07-20 16:57:46 -0700 | [diff] [blame] | 47 | Stl *string `android:"arch_variant"` | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 48 |  | 
|  | 49 | SelectedStl string `blueprint:"mutated"` | 
|  | 50 | } | 
|  | 51 |  | 
| Colin Cross | a8e07cc | 2016-04-04 15:07:06 -0700 | [diff] [blame] | 52 | type stl struct { | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 53 | Properties StlProperties | 
|  | 54 | } | 
|  | 55 |  | 
| Colin Cross | a8e07cc | 2016-04-04 15:07:06 -0700 | [diff] [blame] | 56 | func (stl *stl) props() []interface{} { | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 57 | return []interface{}{&stl.Properties} | 
|  | 58 | } | 
|  | 59 |  | 
| Colin Cross | a8e07cc | 2016-04-04 15:07:06 -0700 | [diff] [blame] | 60 | func (stl *stl) begin(ctx BaseModuleContext) { | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 61 | stl.Properties.SelectedStl = func() string { | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 62 | s := "" | 
|  | 63 | if stl.Properties.Stl != nil { | 
|  | 64 | s = *stl.Properties.Stl | 
|  | 65 | } | 
| Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 66 | if ctx.useSdk() && ctx.Device() { | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 67 | switch s { | 
| Sasha Smundak | fc22e4e | 2019-03-24 14:17:56 -0700 | [diff] [blame] | 68 | case "", "system": | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 69 | return "ndk_system" | 
| Dan Albert | 749fc78 | 2018-01-04 14:21:14 -0800 | [diff] [blame] | 70 | case "c++_shared", "c++_static": | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 71 | return "ndk_lib" + s | 
| David Benjamin | 87f9f03 | 2017-01-25 14:10:04 -0500 | [diff] [blame] | 72 | case "libc++": | 
|  | 73 | return "ndk_libc++_shared" | 
|  | 74 | case "libc++_static": | 
|  | 75 | return "ndk_libc++_static" | 
| Colin Cross | 4a97cb4 | 2016-04-21 15:53:42 -0700 | [diff] [blame] | 76 | case "none": | 
|  | 77 | return "" | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 78 | default: | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 79 | ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", s) | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 80 | return "" | 
|  | 81 | } | 
| Colin Cross | 3edeee1 | 2017-04-04 12:59:48 -0700 | [diff] [blame] | 82 | } else if ctx.Windows() { | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 83 | switch s { | 
| Pirama Arumuga Nainar | a403cc7 | 2018-08-08 10:28:12 -0700 | [diff] [blame] | 84 | case "libc++", "libc++_static", "": | 
|  | 85 | // Only use static libc++ for Windows. | 
|  | 86 | return "libc++_static" | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 87 | case "none": | 
|  | 88 | return "" | 
|  | 89 | default: | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 90 | ctx.ModuleErrorf("stl: %q is not a supported STL for windows", s) | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 91 | return "" | 
|  | 92 | } | 
| Doug Horn | c32c6b0 | 2019-01-17 14:44:05 -0800 | [diff] [blame] | 93 | } 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 Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 113 | } else { | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 114 | switch s { | 
| Dan Willemsen | 141d566 | 2016-06-15 13:47:51 -0700 | [diff] [blame] | 115 | case "libc++", "libc++_static": | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 116 | return s | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 117 | case "c++_shared": | 
|  | 118 | return "libc++" | 
|  | 119 | case "c++_static": | 
|  | 120 | return "libc++_static" | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 121 | case "none": | 
|  | 122 | return "" | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 123 | case "", "system": | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 124 | if ctx.static() { | 
|  | 125 | return "libc++_static" | 
|  | 126 | } else { | 
|  | 127 | return "libc++" | 
|  | 128 | } | 
|  | 129 | default: | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 130 | ctx.ModuleErrorf("stl: %q is not a supported STL", s) | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 131 | return "" | 
|  | 132 | } | 
|  | 133 | } | 
|  | 134 | }() | 
|  | 135 | } | 
|  | 136 |  | 
| Dan Albert | 90b9bbc | 2018-11-15 11:29:28 -0800 | [diff] [blame] | 137 | func needsLibAndroidSupport(ctx BaseModuleContext) bool { | 
| Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 138 | version := nativeApiLevelOrPanic(ctx, ctx.sdkVersion()) | 
|  | 139 | return version.LessThan(android.FirstNonLibAndroidSupportVersion) | 
| Dan Albert | 90b9bbc | 2018-11-15 11:29:28 -0800 | [diff] [blame] | 140 | } | 
|  | 141 |  | 
| Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 142 | func staticUnwinder(ctx android.BaseModuleContext) string { | 
| Ryan Prichard | b35a85e | 2021-01-13 19:18:53 -0800 | [diff] [blame] | 143 | return "libunwind" | 
| Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 144 | } | 
|  | 145 |  | 
| Colin Cross | a8e07cc | 2016-04-04 15:07:06 -0700 | [diff] [blame] | 146 | func (stl *stl) deps(ctx BaseModuleContext, deps Deps) Deps { | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 147 | switch stl.Properties.SelectedStl { | 
|  | 148 | case "libstdc++": | 
| Dan Willemsen | 141d566 | 2016-06-15 13:47:51 -0700 | [diff] [blame] | 149 | // Nothing | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 150 | case "libc++", "libc++_static": | 
|  | 151 | if stl.Properties.SelectedStl == "libc++" { | 
|  | 152 | deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl) | 
|  | 153 | } else { | 
|  | 154 | deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl) | 
|  | 155 | } | 
| Dan Albert | 2da19cb | 2019-07-24 12:17:40 -0700 | [diff] [blame] | 156 | if ctx.Device() && !ctx.useSdk() { | 
|  | 157 | // __cxa_demangle is not a part of libc++.so on the device since | 
|  | 158 | // it's large and most processes don't need it. Statically link | 
|  | 159 | // libc++demangle into every process so that users still have it if | 
|  | 160 | // needed, but the linker won't include this unless it is actually | 
|  | 161 | // called. | 
|  | 162 | // http://b/138245375 | 
|  | 163 | deps.StaticLibs = append(deps.StaticLibs, "libc++demangle") | 
|  | 164 | } | 
| Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 165 | if ctx.toolchain().Bionic() { | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 166 | if ctx.staticBinary() { | 
| Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 167 | deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", staticUnwinder(ctx)) | 
|  | 168 | } else { | 
|  | 169 | deps.StaticUnwinderIfLegacy = true | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 170 | } | 
|  | 171 | } | 
|  | 172 | case "": | 
|  | 173 | // None or error. | 
| Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 174 | if ctx.toolchain().Bionic() && ctx.Module().Name() == "libc++" { | 
|  | 175 | deps.StaticUnwinderIfLegacy = true | 
|  | 176 | } | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 177 | case "ndk_system": | 
|  | 178 | // TODO: Make a system STL prebuilt for the NDK. | 
|  | 179 | // The system STL doesn't have a prebuilt (it uses the system's libstdc++), but it does have | 
|  | 180 | // its own includes. The includes are handled in CCBase.Flags(). | 
|  | 181 | deps.SharedLibs = append([]string{"libstdc++"}, deps.SharedLibs...) | 
| Ryan Prichard | b170365 | 2018-03-26 16:30:31 -0700 | [diff] [blame] | 182 | case "ndk_libc++_shared", "ndk_libc++_static": | 
|  | 183 | if stl.Properties.SelectedStl == "ndk_libc++_shared" { | 
|  | 184 | deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl) | 
|  | 185 | } else { | 
|  | 186 | deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl, "ndk_libc++abi") | 
|  | 187 | } | 
| Dan Albert | 90b9bbc | 2018-11-15 11:29:28 -0800 | [diff] [blame] | 188 | if needsLibAndroidSupport(ctx) { | 
|  | 189 | deps.StaticLibs = append(deps.StaticLibs, "ndk_libandroid_support") | 
|  | 190 | } | 
| Ryan Prichard | b35a85e | 2021-01-13 19:18:53 -0800 | [diff] [blame] | 191 | // TODO: Switch the NDK over to the LLVM unwinder for non-arm32 architectures. | 
| Ryan Prichard | b170365 | 2018-03-26 16:30:31 -0700 | [diff] [blame] | 192 | if ctx.Arch().ArchType == android.Arm { | 
|  | 193 | deps.StaticLibs = append(deps.StaticLibs, "ndk_libunwind") | 
| Peter Collingbourne | e5ba286 | 2019-12-10 18:37:45 -0800 | [diff] [blame] | 194 | } else { | 
|  | 195 | deps.StaticLibs = append(deps.StaticLibs, "libgcc_stripped") | 
| Ryan Prichard | b170365 | 2018-03-26 16:30:31 -0700 | [diff] [blame] | 196 | } | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 197 | default: | 
|  | 198 | panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl)) | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | return deps | 
|  | 202 | } | 
|  | 203 |  | 
| Colin Cross | a8e07cc | 2016-04-04 15:07:06 -0700 | [diff] [blame] | 204 | func (stl *stl) flags(ctx ModuleContext, flags Flags) Flags { | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 205 | switch stl.Properties.SelectedStl { | 
|  | 206 | case "libc++", "libc++_static": | 
| Dan Albert | a07b845 | 2018-01-11 13:00:46 -0800 | [diff] [blame] | 207 | if ctx.Darwin() { | 
|  | 208 | // libc++'s headers are annotated with availability macros that | 
|  | 209 | // indicate which version of Mac OS was the first to ship with a | 
|  | 210 | // libc++ feature available in its *system's* libc++.dylib. We do | 
|  | 211 | // not use the system's library, but rather ship our own. As such, | 
|  | 212 | // these availability attributes are meaningless for us but cause | 
|  | 213 | // build breaks when we try to use code that would not be available | 
|  | 214 | // in the system's dylib. | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 215 | flags.Local.CppFlags = append(flags.Local.CppFlags, | 
| Dan Albert | a07b845 | 2018-01-11 13:00:46 -0800 | [diff] [blame] | 216 | "-D_LIBCPP_DISABLE_AVAILABILITY") | 
|  | 217 | } | 
|  | 218 |  | 
| Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 219 | if !ctx.toolchain().Bionic() { | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 220 | flags.Local.CppFlags = append(flags.Local.CppFlags, "-nostdinc++") | 
| Colin Cross | f7a17da | 2019-10-03 15:48:34 -0700 | [diff] [blame] | 221 | flags.extraLibFlags = append(flags.extraLibFlags, "-nostdlib++") | 
| Pirama Arumuga Nainar | a403cc7 | 2018-08-08 10:28:12 -0700 | [diff] [blame] | 222 | if ctx.Windows() { | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 223 | flags.Local.CppFlags = append(flags.Local.CppFlags, | 
| Pirama Arumuga Nainar | a403cc7 | 2018-08-08 10:28:12 -0700 | [diff] [blame] | 224 | // Disable visiblity annotations since we're using static | 
|  | 225 | // libc++. | 
|  | 226 | "-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS", | 
|  | 227 | "-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS", | 
|  | 228 | // Use Win32 threads in libc++. | 
|  | 229 | "-D_LIBCPP_HAS_THREAD_API_WIN32") | 
|  | 230 | } | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 231 | } | 
|  | 232 | case "libstdc++": | 
| Dan Willemsen | 141d566 | 2016-06-15 13:47:51 -0700 | [diff] [blame] | 233 | // Nothing | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 234 | case "ndk_system": | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 235 | ndkSrcRoot := android.PathForSource(ctx, "prebuilts/ndk/current/sources/cxx-stl/system/include") | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 236 | flags.Local.CFlags = append(flags.Local.CFlags, "-isystem "+ndkSrcRoot.String()) | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 237 | case "ndk_libc++_shared", "ndk_libc++_static": | 
| Christopher Ferris | c3a1e22 | 2019-04-10 17:57:50 -0700 | [diff] [blame] | 238 | if ctx.Arch().ArchType == android.Arm { | 
|  | 239 | // Make sure the _Unwind_XXX symbols are not re-exported. | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 240 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,libunwind.a") | 
| Christopher Ferris | c3a1e22 | 2019-04-10 17:57:50 -0700 | [diff] [blame] | 241 | } | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 242 | case "": | 
|  | 243 | // None or error. | 
| Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 244 | if !ctx.toolchain().Bionic() { | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 245 | flags.Local.CppFlags = append(flags.Local.CppFlags, "-nostdinc++") | 
| Colin Cross | f7a17da | 2019-10-03 15:48:34 -0700 | [diff] [blame] | 246 | flags.extraLibFlags = append(flags.extraLibFlags, "-nostdlib++") | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 247 | } | 
|  | 248 | default: | 
|  | 249 | panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl)) | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | return flags | 
|  | 253 | } |