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 | |
| 22 | type StlProperties struct { |
| 23 | // select the STL library to use. Possible values are "libc++", "libc++_static", |
| 24 | // "stlport", "stlport_static", "ndk", "libstdc++", or "none". Leave blank to select the |
| 25 | // default |
Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 26 | Stl *string |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 27 | |
| 28 | SelectedStl string `blueprint:"mutated"` |
| 29 | } |
| 30 | |
Colin Cross | a8e07cc | 2016-04-04 15:07:06 -0700 | [diff] [blame] | 31 | type stl struct { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 32 | Properties StlProperties |
| 33 | } |
| 34 | |
Colin Cross | a8e07cc | 2016-04-04 15:07:06 -0700 | [diff] [blame] | 35 | func (stl *stl) props() []interface{} { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 36 | return []interface{}{&stl.Properties} |
| 37 | } |
| 38 | |
Colin Cross | a8e07cc | 2016-04-04 15:07:06 -0700 | [diff] [blame] | 39 | func (stl *stl) begin(ctx BaseModuleContext) { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 40 | stl.Properties.SelectedStl = func() string { |
Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 41 | s := "" |
| 42 | if stl.Properties.Stl != nil { |
| 43 | s = *stl.Properties.Stl |
| 44 | } |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 45 | if ctx.sdk() && ctx.Device() { |
Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 46 | switch s { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 47 | case "": |
| 48 | return "ndk_system" |
| 49 | case "c++_shared", "c++_static", |
| 50 | "stlport_shared", "stlport_static", |
| 51 | "gnustl_static": |
Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 52 | return "ndk_lib" + s |
David Benjamin | 87f9f03 | 2017-01-25 14:10:04 -0500 | [diff] [blame] | 53 | case "libc++": |
| 54 | return "ndk_libc++_shared" |
| 55 | case "libc++_static": |
| 56 | return "ndk_libc++_static" |
Colin Cross | 4a97cb4 | 2016-04-21 15:53:42 -0700 | [diff] [blame] | 57 | case "none": |
| 58 | return "" |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 59 | default: |
Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 60 | 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] | 61 | return "" |
| 62 | } |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 63 | } else if ctx.Os() == android.Windows { |
Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 64 | switch s { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 65 | case "libc++", "libc++_static", "libstdc++", "": |
| 66 | // libc++ is not supported on mingw |
| 67 | return "libstdc++" |
| 68 | case "none": |
| 69 | return "" |
| 70 | default: |
Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 71 | ctx.ModuleErrorf("stl: %q is not a supported STL for windows", s) |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 72 | return "" |
| 73 | } |
| 74 | } else { |
Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 75 | switch s { |
Dan Willemsen | 141d566 | 2016-06-15 13:47:51 -0700 | [diff] [blame] | 76 | case "libc++", "libc++_static": |
Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 77 | return s |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 78 | case "none": |
| 79 | return "" |
| 80 | case "": |
| 81 | if ctx.static() { |
| 82 | return "libc++_static" |
| 83 | } else { |
| 84 | return "libc++" |
| 85 | } |
| 86 | default: |
Colin Cross | 7924885 | 2016-07-12 13:12:33 -0700 | [diff] [blame] | 87 | ctx.ModuleErrorf("stl: %q is not a supported STL", s) |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 88 | return "" |
| 89 | } |
| 90 | } |
| 91 | }() |
| 92 | } |
| 93 | |
Colin Cross | a8e07cc | 2016-04-04 15:07:06 -0700 | [diff] [blame] | 94 | func (stl *stl) deps(ctx BaseModuleContext, deps Deps) Deps { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 95 | switch stl.Properties.SelectedStl { |
| 96 | case "libstdc++": |
Dan Willemsen | 141d566 | 2016-06-15 13:47:51 -0700 | [diff] [blame] | 97 | // Nothing |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 98 | case "libc++", "libc++_static": |
| 99 | if stl.Properties.SelectedStl == "libc++" { |
| 100 | deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl) |
| 101 | } else { |
| 102 | deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl) |
| 103 | } |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 104 | if ctx.toolchain().Bionic() { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 105 | if ctx.Arch().ArchType == android.Arm { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 106 | deps.StaticLibs = append(deps.StaticLibs, "libunwind_llvm") |
| 107 | } |
| 108 | if ctx.staticBinary() { |
Colin Cross | 3d92b27 | 2016-07-14 15:59:32 -0700 | [diff] [blame] | 109 | deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl") |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 110 | } else { |
| 111 | deps.SharedLibs = append(deps.SharedLibs, "libdl") |
| 112 | } |
| 113 | } |
| 114 | case "": |
| 115 | // None or error. |
| 116 | case "ndk_system": |
| 117 | // TODO: Make a system STL prebuilt for the NDK. |
| 118 | // The system STL doesn't have a prebuilt (it uses the system's libstdc++), but it does have |
| 119 | // its own includes. The includes are handled in CCBase.Flags(). |
| 120 | deps.SharedLibs = append([]string{"libstdc++"}, deps.SharedLibs...) |
Dan Albert | 3895d5b | 2016-09-14 16:50:48 -0700 | [diff] [blame] | 121 | case "ndk_libc++_shared": |
| 122 | deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl, |
| 123 | "libdl") |
| 124 | case "ndk_libc++_static": |
| 125 | deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl) |
| 126 | deps.SharedLibs = append(deps.SharedLibs, "libdl") |
| 127 | case "ndk_libstlport_shared": |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 128 | deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl) |
Dan Albert | 3895d5b | 2016-09-14 16:50:48 -0700 | [diff] [blame] | 129 | case "ndk_libstlport_static", "ndk_libgnustl_static": |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 130 | deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl) |
| 131 | default: |
| 132 | panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl)) |
| 133 | } |
| 134 | |
| 135 | return deps |
| 136 | } |
| 137 | |
Colin Cross | a8e07cc | 2016-04-04 15:07:06 -0700 | [diff] [blame] | 138 | func (stl *stl) flags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 139 | switch stl.Properties.SelectedStl { |
| 140 | case "libc++", "libc++_static": |
| 141 | flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX") |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 142 | if !ctx.toolchain().Bionic() { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 143 | flags.CppFlags = append(flags.CppFlags, "-nostdinc++") |
| 144 | flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs") |
| 145 | flags.LdFlags = append(flags.LdFlags, "-lpthread", "-lm") |
| 146 | if ctx.staticBinary() { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 147 | flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.Os()]...) |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 148 | } else { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 149 | flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.Os()]...) |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 150 | } |
| 151 | } else { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 152 | if ctx.Arch().ArchType == android.Arm { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 153 | flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a") |
| 154 | } |
| 155 | } |
| 156 | case "libstdc++": |
Dan Willemsen | 141d566 | 2016-06-15 13:47:51 -0700 | [diff] [blame] | 157 | // Nothing |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 158 | case "ndk_system": |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 159 | ndkSrcRoot := android.PathForSource(ctx, "prebuilts/ndk/current/sources/cxx-stl/system/include") |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 160 | flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot.String()) |
| 161 | case "ndk_libc++_shared", "ndk_libc++_static": |
| 162 | // TODO(danalbert): This really shouldn't be here... |
| 163 | flags.CppFlags = append(flags.CppFlags, "-std=c++11") |
| 164 | case "ndk_libstlport_shared", "ndk_libstlport_static", "ndk_libgnustl_static": |
| 165 | // Nothing |
| 166 | case "": |
| 167 | // None or error. |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 168 | if !ctx.toolchain().Bionic() { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 169 | flags.CppFlags = append(flags.CppFlags, "-nostdinc++") |
| 170 | flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs") |
| 171 | if ctx.staticBinary() { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 172 | flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.Os()]...) |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 173 | } else { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 174 | flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.Os()]...) |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | default: |
| 178 | panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl)) |
| 179 | } |
| 180 | |
| 181 | return flags |
| 182 | } |
| 183 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 184 | var hostDynamicGccLibs, hostStaticGccLibs map[android.OsType][]string |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 185 | |
| 186 | func init() { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 187 | hostDynamicGccLibs = map[android.OsType][]string{ |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 188 | android.Linux: []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"}, |
| 189 | android.Darwin: []string{"-lc", "-lSystem"}, |
| 190 | android.Windows: []string{"-lmsvcr110", "-lmingw32", "-lgcc", "-lmoldname", |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 191 | "-lmingwex", "-lmsvcrt", "-ladvapi32", "-lshell32", "-luser32", |
| 192 | "-lkernel32", "-lmingw32", "-lgcc", "-lmoldname", "-lmingwex", |
| 193 | "-lmsvcrt"}, |
| 194 | } |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 195 | hostStaticGccLibs = map[android.OsType][]string{ |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 196 | android.Linux: []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"}, |
| 197 | android.Darwin: []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"}, |
| 198 | android.Windows: []string{"NO_STATIC_HOST_BINARIES_ON_WINDOWS"}, |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 199 | } |
| 200 | } |