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