| 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 | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 18 | "fmt" | 
| Colin Cross | cb0ac95 | 2021-07-20 13:17:15 -0700 | [diff] [blame] | 19 |  | 
|  | 20 | "android/soong/android" | 
| 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 |  | 
| Liz Kammer | 7128d38 | 2022-05-12 11:42:33 -0400 | [diff] [blame] | 28 | func 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 Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 38 | func getNdkStlFamilyAndLinkType(m LinkableInterface) (string, string) { | 
|  | 39 | stl := m.SelectedStl() | 
| Dan Albert | 202fe49 | 2017-12-15 13:56:59 -0800 | [diff] [blame] | 40 | switch stl { | 
| Colin Cross | 4c608f5 | 2023-01-17 12:06:45 -0800 | [diff] [blame] | 41 | case "ndk_libc++_shared", "libc++": | 
| Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 42 | return "libc++", "shared" | 
| Colin Cross | 4c608f5 | 2023-01-17 12:06:45 -0800 | [diff] [blame] | 43 | case "ndk_libc++_static", "libc++_static": | 
| Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 44 | return "libc++", "static" | 
| Dan Albert | 202fe49 | 2017-12-15 13:56:59 -0800 | [diff] [blame] | 45 | case "ndk_system": | 
| Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 46 | return "system", "shared" | 
| Dan Albert | 202fe49 | 2017-12-15 13:56:59 -0800 | [diff] [blame] | 47 | case "": | 
| Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 48 | return "none", "none" | 
| Dan Albert | 202fe49 | 2017-12-15 13:56:59 -0800 | [diff] [blame] | 49 | default: | 
| Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 50 | panic(fmt.Errorf("stl: %q is not a valid STL", stl)) | 
| Dan Albert | 202fe49 | 2017-12-15 13:56:59 -0800 | [diff] [blame] | 51 | } | 
|  | 52 | } | 
|  | 53 |  | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 54 | type StlProperties struct { | 
| Dan Albert | 749fc78 | 2018-01-04 14:21:14 -0800 | [diff] [blame] | 55 | // Select the STL library to use.  Possible values are "libc++", | 
|  | 56 | // "libc++_static", "libstdc++", or "none". Leave blank to select the | 
|  | 57 | // default. | 
| Colin Cross | 245ced7 | 2017-07-20 16:57:46 -0700 | [diff] [blame] | 58 | Stl *string `android:"arch_variant"` | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 59 |  | 
|  | 60 | SelectedStl string `blueprint:"mutated"` | 
|  | 61 | } | 
|  | 62 |  | 
| Colin Cross | a8e07cc | 2016-04-04 15:07:06 -0700 | [diff] [blame] | 63 | type stl struct { | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 64 | Properties StlProperties | 
|  | 65 | } | 
|  | 66 |  | 
| Colin Cross | a8e07cc | 2016-04-04 15:07:06 -0700 | [diff] [blame] | 67 | func (stl *stl) props() []interface{} { | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 68 | return []interface{}{&stl.Properties} | 
|  | 69 | } | 
|  | 70 |  | 
| Colin Cross | a8e07cc | 2016-04-04 15:07:06 -0700 | [diff] [blame] | 71 | func (stl *stl) begin(ctx BaseModuleContext) { | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 72 | stl.Properties.SelectedStl = func() string { | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 73 | s := "" | 
|  | 74 | if stl.Properties.Stl != nil { | 
|  | 75 | s = *stl.Properties.Stl | 
| Colin Cross | be763f7 | 2021-04-26 17:07:17 -0700 | [diff] [blame] | 76 | } else if ctx.header() { | 
|  | 77 | s = "none" | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 78 | } | 
| Liz Kammer | 7128d38 | 2022-05-12 11:42:33 -0400 | [diff] [blame] | 79 | if s == "none" { | 
|  | 80 | return "" | 
|  | 81 | } | 
|  | 82 | s = deduplicateStlInput(s) | 
| Prashanth Swaminathan | 08e634d | 2023-07-26 09:42:41 -0700 | [diff] [blame] | 83 | if ctx.useSdk() && ctx.Device() { | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 84 | switch s { | 
| Sasha Smundak | fc22e4e | 2019-03-24 14:17:56 -0700 | [diff] [blame] | 85 | case "", "system": | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 86 | return "ndk_system" | 
| David Benjamin | 87f9f03 | 2017-01-25 14:10:04 -0500 | [diff] [blame] | 87 | case "libc++": | 
|  | 88 | return "ndk_libc++_shared" | 
|  | 89 | case "libc++_static": | 
|  | 90 | return "ndk_libc++_static" | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 91 | default: | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 92 | 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] | 93 | return "" | 
|  | 94 | } | 
| Colin Cross | 3edeee1 | 2017-04-04 12:59:48 -0700 | [diff] [blame] | 95 | } else if ctx.Windows() { | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 96 | switch s { | 
| Pirama Arumuga Nainar | a403cc7 | 2018-08-08 10:28:12 -0700 | [diff] [blame] | 97 | case "libc++", "libc++_static", "": | 
|  | 98 | // Only use static libc++ for Windows. | 
|  | 99 | return "libc++_static" | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 100 | default: | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 101 | ctx.ModuleErrorf("stl: %q is not a supported STL for windows", s) | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 102 | return "" | 
|  | 103 | } | 
|  | 104 | } else { | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 105 | switch s { | 
| Dan Willemsen | 141d566 | 2016-06-15 13:47:51 -0700 | [diff] [blame] | 106 | case "libc++", "libc++_static": | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 107 | return s | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 108 | case "", "system": | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 109 | if ctx.static() { | 
|  | 110 | return "libc++_static" | 
|  | 111 | } else { | 
|  | 112 | return "libc++" | 
|  | 113 | } | 
|  | 114 | default: | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 115 | ctx.ModuleErrorf("stl: %q is not a supported STL", s) | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 116 | return "" | 
|  | 117 | } | 
|  | 118 | } | 
|  | 119 | }() | 
|  | 120 | } | 
|  | 121 |  | 
| Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 122 | func staticUnwinder(ctx android.BaseModuleContext) string { | 
| Inseob Kim | d4c9f55 | 2021-04-08 19:28:28 +0900 | [diff] [blame] | 123 | 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 Prichard | b35a85e | 2021-01-13 19:18:53 -0800 | [diff] [blame] | 134 | return "libunwind" | 
| Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 135 | } | 
|  | 136 |  | 
| Colin Cross | a8e07cc | 2016-04-04 15:07:06 -0700 | [diff] [blame] | 137 | func (stl *stl) deps(ctx BaseModuleContext, deps Deps) Deps { | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 138 | switch stl.Properties.SelectedStl { | 
|  | 139 | case "libstdc++": | 
| Dan Willemsen | 141d566 | 2016-06-15 13:47:51 -0700 | [diff] [blame] | 140 | // Nothing | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 141 | case "libc++", "libc++_static": | 
|  | 142 | if stl.Properties.SelectedStl == "libc++" { | 
|  | 143 | deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl) | 
|  | 144 | } else { | 
|  | 145 | deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl) | 
|  | 146 | } | 
| Dan Albert | 2da19cb | 2019-07-24 12:17:40 -0700 | [diff] [blame] | 147 | if ctx.Device() && !ctx.useSdk() { | 
|  | 148 | // __cxa_demangle is not a part of libc++.so on the device since | 
|  | 149 | // it's large and most processes don't need it. Statically link | 
|  | 150 | // libc++demangle into every process so that users still have it if | 
|  | 151 | // needed, but the linker won't include this unless it is actually | 
|  | 152 | // called. | 
|  | 153 | // http://b/138245375 | 
|  | 154 | deps.StaticLibs = append(deps.StaticLibs, "libc++demangle") | 
|  | 155 | } | 
| Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 156 | if ctx.toolchain().Bionic() { | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 157 | if ctx.staticBinary() { | 
| Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 158 | deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", staticUnwinder(ctx)) | 
|  | 159 | } else { | 
|  | 160 | deps.StaticUnwinderIfLegacy = true | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 161 | } | 
|  | 162 | } | 
|  | 163 | case "": | 
|  | 164 | // None or error. | 
| Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 165 | if ctx.toolchain().Bionic() && ctx.Module().Name() == "libc++" { | 
|  | 166 | deps.StaticUnwinderIfLegacy = true | 
|  | 167 | } | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 168 | case "ndk_system": | 
|  | 169 | // TODO: Make a system STL prebuilt for the NDK. | 
|  | 170 | // The system STL doesn't have a prebuilt (it uses the system's libstdc++), but it does have | 
|  | 171 | // its own includes. The includes are handled in CCBase.Flags(). | 
|  | 172 | deps.SharedLibs = append([]string{"libstdc++"}, deps.SharedLibs...) | 
| Spandan Das | 72b8fcb | 2023-09-05 23:02:27 +0000 | [diff] [blame] | 173 | deps.HeaderLibs = append([]string{"ndk_system"}, deps.HeaderLibs...) | 
| Ryan Prichard | b170365 | 2018-03-26 16:30:31 -0700 | [diff] [blame] | 174 | case "ndk_libc++_shared", "ndk_libc++_static": | 
|  | 175 | if stl.Properties.SelectedStl == "ndk_libc++_shared" { | 
|  | 176 | deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl) | 
|  | 177 | } else { | 
|  | 178 | deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl, "ndk_libc++abi") | 
|  | 179 | } | 
| Ryan Prichard | 4ccd490 | 2021-03-31 15:29:11 -0700 | [diff] [blame] | 180 | deps.StaticLibs = append(deps.StaticLibs, "ndk_libunwind") | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 181 | default: | 
|  | 182 | panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl)) | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | return deps | 
|  | 186 | } | 
|  | 187 |  | 
| Colin Cross | a8e07cc | 2016-04-04 15:07:06 -0700 | [diff] [blame] | 188 | func (stl *stl) flags(ctx ModuleContext, flags Flags) Flags { | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 189 | switch stl.Properties.SelectedStl { | 
|  | 190 | case "libc++", "libc++_static": | 
| Dan Albert | a07b845 | 2018-01-11 13:00:46 -0800 | [diff] [blame] | 191 | if ctx.Darwin() { | 
|  | 192 | // libc++'s headers are annotated with availability macros that | 
|  | 193 | // indicate which version of Mac OS was the first to ship with a | 
|  | 194 | // libc++ feature available in its *system's* libc++.dylib. We do | 
|  | 195 | // not use the system's library, but rather ship our own. As such, | 
|  | 196 | // these availability attributes are meaningless for us but cause | 
|  | 197 | // build breaks when we try to use code that would not be available | 
|  | 198 | // in the system's dylib. | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 199 | flags.Local.CppFlags = append(flags.Local.CppFlags, | 
| Dan Albert | a07b845 | 2018-01-11 13:00:46 -0800 | [diff] [blame] | 200 | "-D_LIBCPP_DISABLE_AVAILABILITY") | 
|  | 201 | } | 
|  | 202 |  | 
| Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 203 | if !ctx.toolchain().Bionic() { | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 204 | flags.Local.CppFlags = append(flags.Local.CppFlags, "-nostdinc++") | 
| Colin Cross | f7a17da | 2019-10-03 15:48:34 -0700 | [diff] [blame] | 205 | flags.extraLibFlags = append(flags.extraLibFlags, "-nostdlib++") | 
| Pirama Arumuga Nainar | a403cc7 | 2018-08-08 10:28:12 -0700 | [diff] [blame] | 206 | if ctx.Windows() { | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 207 | flags.Local.CppFlags = append(flags.Local.CppFlags, | 
| Pirama Arumuga Nainar | a403cc7 | 2018-08-08 10:28:12 -0700 | [diff] [blame] | 208 | // Disable visiblity annotations since we're using static | 
|  | 209 | // libc++. | 
|  | 210 | "-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS", | 
|  | 211 | "-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS", | 
|  | 212 | // Use Win32 threads in libc++. | 
|  | 213 | "-D_LIBCPP_HAS_THREAD_API_WIN32") | 
|  | 214 | } | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 215 | } | 
|  | 216 | case "libstdc++": | 
| Dan Willemsen | 141d566 | 2016-06-15 13:47:51 -0700 | [diff] [blame] | 217 | // Nothing | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 218 | case "ndk_system": | 
| Spandan Das | 72b8fcb | 2023-09-05 23:02:27 +0000 | [diff] [blame] | 219 | // Nothing: The exports of ndk_system will be added automatically to the local cflags | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 220 | case "ndk_libc++_shared", "ndk_libc++_static": | 
| Christopher Ferris | c3a1e22 | 2019-04-10 17:57:50 -0700 | [diff] [blame] | 221 | if ctx.Arch().ArchType == android.Arm { | 
|  | 222 | // Make sure the _Unwind_XXX symbols are not re-exported. | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 223 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,libunwind.a") | 
| Christopher Ferris | c3a1e22 | 2019-04-10 17:57:50 -0700 | [diff] [blame] | 224 | } | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 225 | case "": | 
|  | 226 | // None or error. | 
| Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 227 | if !ctx.toolchain().Bionic() { | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 228 | flags.Local.CppFlags = append(flags.Local.CppFlags, "-nostdinc++") | 
| Colin Cross | f7a17da | 2019-10-03 15:48:34 -0700 | [diff] [blame] | 229 | flags.extraLibFlags = append(flags.extraLibFlags, "-nostdlib++") | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 230 | } | 
|  | 231 | default: | 
|  | 232 | panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl)) | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | return flags | 
|  | 236 | } |