| 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 |  | 
| Dan Albert | 202fe49 | 2017-12-15 13:56:59 -0800 | [diff] [blame] | 22 | func getNdkStlFamily(ctx android.ModuleContext, m *Module) string { | 
|  | 23 | stl := m.stl.Properties.SelectedStl | 
|  | 24 | switch stl { | 
|  | 25 | case "ndk_libc++_shared", "ndk_libc++_static": | 
|  | 26 | return "libc++" | 
| Dan Albert | 202fe49 | 2017-12-15 13:56:59 -0800 | [diff] [blame] | 27 | case "ndk_system": | 
|  | 28 | return "system" | 
|  | 29 | case "": | 
|  | 30 | return "none" | 
|  | 31 | default: | 
|  | 32 | ctx.ModuleErrorf("stl: %q is not a valid STL", stl) | 
|  | 33 | return "" | 
|  | 34 | } | 
|  | 35 | } | 
|  | 36 |  | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 37 | type StlProperties struct { | 
| Dan Albert | 749fc78 | 2018-01-04 14:21:14 -0800 | [diff] [blame] | 38 | // Select the STL library to use.  Possible values are "libc++", | 
|  | 39 | // "libc++_static", "libstdc++", or "none". Leave blank to select the | 
|  | 40 | // default. | 
| Colin Cross | 245ced7 | 2017-07-20 16:57:46 -0700 | [diff] [blame] | 41 | Stl *string `android:"arch_variant"` | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 42 |  | 
|  | 43 | SelectedStl string `blueprint:"mutated"` | 
|  | 44 | } | 
|  | 45 |  | 
| Colin Cross | a8e07cc | 2016-04-04 15:07:06 -0700 | [diff] [blame] | 46 | type stl struct { | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 47 | Properties StlProperties | 
|  | 48 | } | 
|  | 49 |  | 
| Colin Cross | a8e07cc | 2016-04-04 15:07:06 -0700 | [diff] [blame] | 50 | func (stl *stl) props() []interface{} { | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 51 | return []interface{}{&stl.Properties} | 
|  | 52 | } | 
|  | 53 |  | 
| Colin Cross | a8e07cc | 2016-04-04 15:07:06 -0700 | [diff] [blame] | 54 | func (stl *stl) begin(ctx BaseModuleContext) { | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 55 | stl.Properties.SelectedStl = func() string { | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 56 | s := "" | 
|  | 57 | if stl.Properties.Stl != nil { | 
|  | 58 | s = *stl.Properties.Stl | 
|  | 59 | } | 
| Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 60 | if ctx.useSdk() && ctx.Device() { | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 61 | switch s { | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 62 | case "": | 
|  | 63 | return "ndk_system" | 
| Dan Albert | 749fc78 | 2018-01-04 14:21:14 -0800 | [diff] [blame] | 64 | case "c++_shared", "c++_static": | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 65 | return "ndk_lib" + s | 
| David Benjamin | 87f9f03 | 2017-01-25 14:10:04 -0500 | [diff] [blame] | 66 | case "libc++": | 
|  | 67 | return "ndk_libc++_shared" | 
|  | 68 | case "libc++_static": | 
|  | 69 | return "ndk_libc++_static" | 
| Colin Cross | 4a97cb4 | 2016-04-21 15:53:42 -0700 | [diff] [blame] | 70 | case "none": | 
|  | 71 | return "" | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 72 | default: | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 73 | 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] | 74 | return "" | 
|  | 75 | } | 
| Colin Cross | 3edeee1 | 2017-04-04 12:59:48 -0700 | [diff] [blame] | 76 | } else if ctx.Windows() { | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 77 | switch s { | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 78 | case "libc++", "libc++_static", "libstdc++", "": | 
|  | 79 | // libc++ is not supported on mingw | 
|  | 80 | return "libstdc++" | 
|  | 81 | case "none": | 
|  | 82 | return "" | 
|  | 83 | default: | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 84 | ctx.ModuleErrorf("stl: %q is not a supported STL for windows", s) | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 85 | return "" | 
|  | 86 | } | 
|  | 87 | } else { | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 88 | switch s { | 
| Dan Willemsen | 141d566 | 2016-06-15 13:47:51 -0700 | [diff] [blame] | 89 | case "libc++", "libc++_static": | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 90 | return s | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 91 | case "none": | 
|  | 92 | return "" | 
|  | 93 | case "": | 
|  | 94 | if ctx.static() { | 
|  | 95 | return "libc++_static" | 
|  | 96 | } else { | 
|  | 97 | return "libc++" | 
|  | 98 | } | 
|  | 99 | default: | 
| Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 100 | ctx.ModuleErrorf("stl: %q is not a supported STL", s) | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 101 | return "" | 
|  | 102 | } | 
|  | 103 | } | 
|  | 104 | }() | 
|  | 105 | } | 
|  | 106 |  | 
| Colin Cross | a8e07cc | 2016-04-04 15:07:06 -0700 | [diff] [blame] | 107 | func (stl *stl) deps(ctx BaseModuleContext, deps Deps) Deps { | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 108 | switch stl.Properties.SelectedStl { | 
|  | 109 | case "libstdc++": | 
| Dan Willemsen | 141d566 | 2016-06-15 13:47:51 -0700 | [diff] [blame] | 110 | // Nothing | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 111 | case "libc++", "libc++_static": | 
|  | 112 | if stl.Properties.SelectedStl == "libc++" { | 
|  | 113 | deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl) | 
|  | 114 | } else { | 
|  | 115 | deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl) | 
|  | 116 | } | 
| Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 117 | if ctx.toolchain().Bionic() { | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 118 | if ctx.Arch().ArchType == android.Arm { | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 119 | deps.StaticLibs = append(deps.StaticLibs, "libunwind_llvm") | 
|  | 120 | } | 
|  | 121 | if ctx.staticBinary() { | 
| Colin Cross | 3d92b27 | 2016-07-14 15:59:32 -0700 | [diff] [blame] | 122 | deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl") | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 123 | } | 
|  | 124 | } | 
|  | 125 | case "": | 
|  | 126 | // None or error. | 
|  | 127 | case "ndk_system": | 
|  | 128 | // TODO: Make a system STL prebuilt for the NDK. | 
|  | 129 | // The system STL doesn't have a prebuilt (it uses the system's libstdc++), but it does have | 
|  | 130 | // its own includes. The includes are handled in CCBase.Flags(). | 
|  | 131 | deps.SharedLibs = append([]string{"libstdc++"}, deps.SharedLibs...) | 
| Ryan Prichard | b170365 | 2018-03-26 16:30:31 -0700 | [diff] [blame] | 132 | case "ndk_libc++_shared", "ndk_libc++_static": | 
|  | 133 | if stl.Properties.SelectedStl == "ndk_libc++_shared" { | 
|  | 134 | deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl) | 
|  | 135 | } else { | 
|  | 136 | deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl, "ndk_libc++abi") | 
|  | 137 | } | 
|  | 138 | deps.StaticLibs = append(deps.StaticLibs, "ndk_libandroid_support") | 
|  | 139 | if ctx.Arch().ArchType == android.Arm { | 
|  | 140 | deps.StaticLibs = append(deps.StaticLibs, "ndk_libunwind") | 
|  | 141 | } | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 142 | default: | 
|  | 143 | panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl)) | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | return deps | 
|  | 147 | } | 
|  | 148 |  | 
| Colin Cross | a8e07cc | 2016-04-04 15:07:06 -0700 | [diff] [blame] | 149 | func (stl *stl) flags(ctx ModuleContext, flags Flags) Flags { | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 150 | switch stl.Properties.SelectedStl { | 
|  | 151 | case "libc++", "libc++_static": | 
|  | 152 | flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX") | 
| Dan Albert | a07b845 | 2018-01-11 13:00:46 -0800 | [diff] [blame] | 153 |  | 
|  | 154 | if ctx.Darwin() { | 
|  | 155 | // libc++'s headers are annotated with availability macros that | 
|  | 156 | // indicate which version of Mac OS was the first to ship with a | 
|  | 157 | // libc++ feature available in its *system's* libc++.dylib. We do | 
|  | 158 | // not use the system's library, but rather ship our own. As such, | 
|  | 159 | // these availability attributes are meaningless for us but cause | 
|  | 160 | // build breaks when we try to use code that would not be available | 
|  | 161 | // in the system's dylib. | 
|  | 162 | flags.CppFlags = append(flags.CppFlags, | 
|  | 163 | "-D_LIBCPP_DISABLE_AVAILABILITY") | 
|  | 164 | } | 
|  | 165 |  | 
| Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 166 | if !ctx.toolchain().Bionic() { | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 167 | flags.CppFlags = append(flags.CppFlags, "-nostdinc++") | 
|  | 168 | flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs") | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 169 | if ctx.staticBinary() { | 
| Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 170 | flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.Os()]...) | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 171 | } else { | 
| Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 172 | flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.Os()]...) | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 173 | } | 
|  | 174 | } else { | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 175 | if ctx.Arch().ArchType == android.Arm { | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 176 | flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a") | 
|  | 177 | } | 
|  | 178 | } | 
|  | 179 | case "libstdc++": | 
| Dan Willemsen | 141d566 | 2016-06-15 13:47:51 -0700 | [diff] [blame] | 180 | // Nothing | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 181 | case "ndk_system": | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 182 | ndkSrcRoot := android.PathForSource(ctx, "prebuilts/ndk/current/sources/cxx-stl/system/include") | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 183 | flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot.String()) | 
|  | 184 | case "ndk_libc++_shared", "ndk_libc++_static": | 
| Elliott Hughes | 5789ca9 | 2018-02-16 17:15:19 -0800 | [diff] [blame] | 185 | // Nothing. | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 186 | case "": | 
|  | 187 | // None or error. | 
| Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 188 | if !ctx.toolchain().Bionic() { | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 189 | flags.CppFlags = append(flags.CppFlags, "-nostdinc++") | 
|  | 190 | flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs") | 
|  | 191 | if ctx.staticBinary() { | 
| Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 192 | flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.Os()]...) | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 193 | } else { | 
| Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 194 | flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.Os()]...) | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 195 | } | 
|  | 196 | } | 
|  | 197 | default: | 
|  | 198 | panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl)) | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | return flags | 
|  | 202 | } | 
|  | 203 |  | 
| Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 204 | var hostDynamicGccLibs, hostStaticGccLibs map[android.OsType][]string | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 205 |  | 
|  | 206 | func init() { | 
| Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 207 | hostDynamicGccLibs = map[android.OsType][]string{ | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 208 | android.Linux:  []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"}, | 
|  | 209 | android.Darwin: []string{"-lc", "-lSystem"}, | 
|  | 210 | android.Windows: []string{"-lmsvcr110", "-lmingw32", "-lgcc", "-lmoldname", | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 211 | "-lmingwex", "-lmsvcrt", "-ladvapi32", "-lshell32", "-luser32", | 
|  | 212 | "-lkernel32", "-lmingw32", "-lgcc", "-lmoldname", "-lmingwex", | 
|  | 213 | "-lmsvcrt"}, | 
|  | 214 | } | 
| Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 215 | hostStaticGccLibs = map[android.OsType][]string{ | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 216 | android.Linux:   []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"}, | 
|  | 217 | android.Darwin:  []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"}, | 
|  | 218 | android.Windows: []string{"NO_STATIC_HOST_BINARIES_ON_WINDOWS"}, | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 219 | } | 
|  | 220 | } |