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 | |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 23 | func getNdkStlFamily(m *Module) string { |
| 24 | family, _ := getNdkStlFamilyAndLinkType(m) |
| 25 | return family |
| 26 | } |
| 27 | |
| 28 | func getNdkStlFamilyAndLinkType(m *Module) (string, string) { |
Dan Albert | 202fe49 | 2017-12-15 13:56:59 -0800 | [diff] [blame] | 29 | stl := m.stl.Properties.SelectedStl |
| 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 { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 69 | case "": |
| 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 | } |
| 94 | } else { |
Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 95 | switch s { |
Dan Willemsen | 141d566 | 2016-06-15 13:47:51 -0700 | [diff] [blame] | 96 | case "libc++", "libc++_static": |
Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 97 | return s |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 98 | case "none": |
| 99 | return "" |
| 100 | case "": |
| 101 | if ctx.static() { |
| 102 | return "libc++_static" |
| 103 | } else { |
| 104 | return "libc++" |
| 105 | } |
| 106 | default: |
Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 107 | ctx.ModuleErrorf("stl: %q is not a supported STL", s) |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 108 | return "" |
| 109 | } |
| 110 | } |
| 111 | }() |
| 112 | } |
| 113 | |
Dan Albert | 90b9bbc | 2018-11-15 11:29:28 -0800 | [diff] [blame] | 114 | func needsLibAndroidSupport(ctx BaseModuleContext) bool { |
| 115 | versionStr, err := normalizeNdkApiLevel(ctx, ctx.sdkVersion(), ctx.Arch()) |
| 116 | if err != nil { |
| 117 | ctx.PropertyErrorf("sdk_version", err.Error()) |
| 118 | } |
| 119 | |
| 120 | if versionStr == "current" { |
| 121 | return false |
| 122 | } |
| 123 | |
| 124 | version, err := strconv.Atoi(versionStr) |
| 125 | if err != nil { |
| 126 | panic(fmt.Sprintf( |
| 127 | "invalid API level returned from normalizeNdkApiLevel: %q", |
| 128 | versionStr)) |
| 129 | } |
| 130 | |
| 131 | return version < 21 |
| 132 | } |
| 133 | |
Colin Cross | a8e07cc | 2016-04-04 15:07:06 -0700 | [diff] [blame] | 134 | func (stl *stl) deps(ctx BaseModuleContext, deps Deps) Deps { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 135 | switch stl.Properties.SelectedStl { |
| 136 | case "libstdc++": |
Dan Willemsen | 141d566 | 2016-06-15 13:47:51 -0700 | [diff] [blame] | 137 | // Nothing |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 138 | case "libc++", "libc++_static": |
| 139 | if stl.Properties.SelectedStl == "libc++" { |
| 140 | deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl) |
| 141 | } else { |
| 142 | deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl) |
| 143 | } |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 144 | if ctx.toolchain().Bionic() { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 145 | if ctx.Arch().ArchType == android.Arm { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 146 | deps.StaticLibs = append(deps.StaticLibs, "libunwind_llvm") |
| 147 | } |
| 148 | if ctx.staticBinary() { |
Colin Cross | 3d92b27 | 2016-07-14 15:59:32 -0700 | [diff] [blame] | 149 | deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl") |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | case "": |
| 153 | // None or error. |
| 154 | case "ndk_system": |
| 155 | // TODO: Make a system STL prebuilt for the NDK. |
| 156 | // The system STL doesn't have a prebuilt (it uses the system's libstdc++), but it does have |
| 157 | // its own includes. The includes are handled in CCBase.Flags(). |
| 158 | deps.SharedLibs = append([]string{"libstdc++"}, deps.SharedLibs...) |
Ryan Prichard | b170365 | 2018-03-26 16:30:31 -0700 | [diff] [blame] | 159 | case "ndk_libc++_shared", "ndk_libc++_static": |
| 160 | if stl.Properties.SelectedStl == "ndk_libc++_shared" { |
| 161 | deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl) |
| 162 | } else { |
| 163 | deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl, "ndk_libc++abi") |
| 164 | } |
Dan Albert | 90b9bbc | 2018-11-15 11:29:28 -0800 | [diff] [blame] | 165 | if needsLibAndroidSupport(ctx) { |
| 166 | deps.StaticLibs = append(deps.StaticLibs, "ndk_libandroid_support") |
| 167 | } |
Ryan Prichard | b170365 | 2018-03-26 16:30:31 -0700 | [diff] [blame] | 168 | if ctx.Arch().ArchType == android.Arm { |
| 169 | deps.StaticLibs = append(deps.StaticLibs, "ndk_libunwind") |
| 170 | } |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 171 | default: |
| 172 | panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl)) |
| 173 | } |
| 174 | |
| 175 | return deps |
| 176 | } |
| 177 | |
Colin Cross | a8e07cc | 2016-04-04 15:07:06 -0700 | [diff] [blame] | 178 | func (stl *stl) flags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 179 | switch stl.Properties.SelectedStl { |
| 180 | case "libc++", "libc++_static": |
| 181 | flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX") |
Dan Albert | a07b845 | 2018-01-11 13:00:46 -0800 | [diff] [blame] | 182 | |
| 183 | if ctx.Darwin() { |
| 184 | // libc++'s headers are annotated with availability macros that |
| 185 | // indicate which version of Mac OS was the first to ship with a |
| 186 | // libc++ feature available in its *system's* libc++.dylib. We do |
| 187 | // not use the system's library, but rather ship our own. As such, |
| 188 | // these availability attributes are meaningless for us but cause |
| 189 | // build breaks when we try to use code that would not be available |
| 190 | // in the system's dylib. |
| 191 | flags.CppFlags = append(flags.CppFlags, |
| 192 | "-D_LIBCPP_DISABLE_AVAILABILITY") |
| 193 | } |
| 194 | |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 195 | if !ctx.toolchain().Bionic() { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 196 | flags.CppFlags = append(flags.CppFlags, "-nostdinc++") |
| 197 | flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs") |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 198 | if ctx.staticBinary() { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 199 | flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.Os()]...) |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 200 | } else { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 201 | flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.Os()]...) |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 202 | } |
Pirama Arumuga Nainar | a403cc7 | 2018-08-08 10:28:12 -0700 | [diff] [blame] | 203 | if ctx.Windows() { |
| 204 | // Use SjLj exceptions for 32-bit. libgcc_eh implements SjLj |
| 205 | // exception model for 32-bit. |
| 206 | if ctx.Arch().ArchType == android.X86 { |
| 207 | flags.CppFlags = append(flags.CppFlags, "-fsjlj-exceptions") |
| 208 | } |
| 209 | flags.CppFlags = append(flags.CppFlags, |
| 210 | // Disable visiblity annotations since we're using static |
| 211 | // libc++. |
| 212 | "-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS", |
| 213 | "-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS", |
| 214 | // Use Win32 threads in libc++. |
| 215 | "-D_LIBCPP_HAS_THREAD_API_WIN32") |
| 216 | } |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 217 | } else { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 218 | if ctx.Arch().ArchType == android.Arm { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 219 | flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a") |
| 220 | } |
| 221 | } |
| 222 | case "libstdc++": |
Dan Willemsen | 141d566 | 2016-06-15 13:47:51 -0700 | [diff] [blame] | 223 | // Nothing |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 224 | case "ndk_system": |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 225 | ndkSrcRoot := android.PathForSource(ctx, "prebuilts/ndk/current/sources/cxx-stl/system/include") |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 226 | flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot.String()) |
| 227 | case "ndk_libc++_shared", "ndk_libc++_static": |
Elliott Hughes | 5789ca9 | 2018-02-16 17:15:19 -0800 | [diff] [blame] | 228 | // Nothing. |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 229 | case "": |
| 230 | // None or error. |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 231 | if !ctx.toolchain().Bionic() { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 232 | flags.CppFlags = append(flags.CppFlags, "-nostdinc++") |
| 233 | flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs") |
| 234 | if ctx.staticBinary() { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 235 | flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.Os()]...) |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 236 | } else { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 237 | flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.Os()]...) |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | default: |
| 241 | panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl)) |
| 242 | } |
| 243 | |
| 244 | return flags |
| 245 | } |
| 246 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 247 | var hostDynamicGccLibs, hostStaticGccLibs map[android.OsType][]string |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 248 | |
| 249 | func init() { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 250 | hostDynamicGccLibs = map[android.OsType][]string{ |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 251 | android.Linux: []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"}, |
| 252 | android.Darwin: []string{"-lc", "-lSystem"}, |
Pirama Arumuga Nainar | a403cc7 | 2018-08-08 10:28:12 -0700 | [diff] [blame] | 253 | android.Windows: []string{"-Wl,--start-group", "-lmingw32", "-lgcc", "-lgcc_eh", |
Pirama Arumuga Nainar | 087bba7 | 2018-11-29 17:19:25 -0800 | [diff] [blame] | 254 | "-lmoldname", "-lmingwex", "-lmsvcrt", "-lucrt", "-lpthread", |
Pirama Arumuga Nainar | a403cc7 | 2018-08-08 10:28:12 -0700 | [diff] [blame] | 255 | "-ladvapi32", "-lshell32", "-luser32", "-lkernel32", "-lpsapi", |
| 256 | "-Wl,--end-group"}, |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 257 | } |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 258 | hostStaticGccLibs = map[android.OsType][]string{ |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 259 | android.Linux: []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"}, |
| 260 | android.Darwin: []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"}, |
| 261 | android.Windows: []string{"NO_STATIC_HOST_BINARIES_ON_WINDOWS"}, |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 262 | } |
| 263 | } |