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