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...) |
Dan Albert | 749fc78 | 2018-01-04 14:21:14 -0800 | [diff] [blame] | 132 | case "ndk_libc++_shared": |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 133 | deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl) |
Dan Albert | 749fc78 | 2018-01-04 14:21:14 -0800 | [diff] [blame] | 134 | case "ndk_libc++_static": |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 135 | deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl) |
| 136 | default: |
| 137 | panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl)) |
| 138 | } |
| 139 | |
| 140 | return deps |
| 141 | } |
| 142 | |
Colin Cross | a8e07cc | 2016-04-04 15:07:06 -0700 | [diff] [blame] | 143 | func (stl *stl) flags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 144 | switch stl.Properties.SelectedStl { |
| 145 | case "libc++", "libc++_static": |
| 146 | flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX") |
Dan Albert | a07b845 | 2018-01-11 13:00:46 -0800 | [diff] [blame^] | 147 | |
| 148 | if ctx.Darwin() { |
| 149 | // libc++'s headers are annotated with availability macros that |
| 150 | // indicate which version of Mac OS was the first to ship with a |
| 151 | // libc++ feature available in its *system's* libc++.dylib. We do |
| 152 | // not use the system's library, but rather ship our own. As such, |
| 153 | // these availability attributes are meaningless for us but cause |
| 154 | // build breaks when we try to use code that would not be available |
| 155 | // in the system's dylib. |
| 156 | flags.CppFlags = append(flags.CppFlags, |
| 157 | "-D_LIBCPP_DISABLE_AVAILABILITY") |
| 158 | } |
| 159 | |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 160 | if !ctx.toolchain().Bionic() { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 161 | flags.CppFlags = append(flags.CppFlags, "-nostdinc++") |
| 162 | flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs") |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 163 | if ctx.staticBinary() { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 164 | flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.Os()]...) |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 165 | } else { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 166 | flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.Os()]...) |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 167 | } |
| 168 | } else { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 169 | if ctx.Arch().ArchType == android.Arm { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 170 | flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a") |
| 171 | } |
| 172 | } |
| 173 | case "libstdc++": |
Dan Willemsen | 141d566 | 2016-06-15 13:47:51 -0700 | [diff] [blame] | 174 | // Nothing |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 175 | case "ndk_system": |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 176 | ndkSrcRoot := android.PathForSource(ctx, "prebuilts/ndk/current/sources/cxx-stl/system/include") |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 177 | flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot.String()) |
| 178 | case "ndk_libc++_shared", "ndk_libc++_static": |
| 179 | // TODO(danalbert): This really shouldn't be here... |
| 180 | flags.CppFlags = append(flags.CppFlags, "-std=c++11") |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 181 | case "": |
| 182 | // None or error. |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 183 | if !ctx.toolchain().Bionic() { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 184 | flags.CppFlags = append(flags.CppFlags, "-nostdinc++") |
| 185 | flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs") |
| 186 | if ctx.staticBinary() { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 187 | flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.Os()]...) |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 188 | } else { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 189 | flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.Os()]...) |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | default: |
| 193 | panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl)) |
| 194 | } |
| 195 | |
| 196 | return flags |
| 197 | } |
| 198 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 199 | var hostDynamicGccLibs, hostStaticGccLibs map[android.OsType][]string |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 200 | |
| 201 | func init() { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 202 | hostDynamicGccLibs = map[android.OsType][]string{ |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 203 | android.Linux: []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"}, |
| 204 | android.Darwin: []string{"-lc", "-lSystem"}, |
| 205 | android.Windows: []string{"-lmsvcr110", "-lmingw32", "-lgcc", "-lmoldname", |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 206 | "-lmingwex", "-lmsvcrt", "-ladvapi32", "-lshell32", "-luser32", |
| 207 | "-lkernel32", "-lmingw32", "-lgcc", "-lmoldname", "-lmingwex", |
| 208 | "-lmsvcrt"}, |
| 209 | } |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 210 | hostStaticGccLibs = map[android.OsType][]string{ |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 211 | android.Linux: []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"}, |
| 212 | android.Darwin: []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"}, |
| 213 | android.Windows: []string{"NO_STATIC_HOST_BINARIES_ON_WINDOWS"}, |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 214 | } |
| 215 | } |