blob: 43776f766b0ce50a31f1b73c0b1bb67c19f88781 [file] [log] [blame]
Colin Crossca860ac2016-01-04 14:34:37 -08001// 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
15package cc
16
17import (
Colin Cross635c3b02016-05-18 15:37:25 -070018 "android/soong/android"
Colin Crossca860ac2016-01-04 14:34:37 -080019 "fmt"
20)
21
22type 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 Cross79248852016-07-12 13:12:33 -070026 Stl *string
Colin Crossca860ac2016-01-04 14:34:37 -080027
28 SelectedStl string `blueprint:"mutated"`
29}
30
Colin Crossa8e07cc2016-04-04 15:07:06 -070031type stl struct {
Colin Crossca860ac2016-01-04 14:34:37 -080032 Properties StlProperties
33}
34
Colin Crossa8e07cc2016-04-04 15:07:06 -070035func (stl *stl) props() []interface{} {
Colin Crossca860ac2016-01-04 14:34:37 -080036 return []interface{}{&stl.Properties}
37}
38
Colin Crossa8e07cc2016-04-04 15:07:06 -070039func (stl *stl) begin(ctx BaseModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -080040 stl.Properties.SelectedStl = func() string {
Colin Cross79248852016-07-12 13:12:33 -070041 s := ""
42 if stl.Properties.Stl != nil {
43 s = *stl.Properties.Stl
44 }
Colin Crossca860ac2016-01-04 14:34:37 -080045 if ctx.sdk() && ctx.Device() {
Colin Cross79248852016-07-12 13:12:33 -070046 switch s {
Colin Crossca860ac2016-01-04 14:34:37 -080047 case "":
48 return "ndk_system"
49 case "c++_shared", "c++_static",
50 "stlport_shared", "stlport_static",
51 "gnustl_static":
Colin Cross79248852016-07-12 13:12:33 -070052 return "ndk_lib" + s
David Benjamin87f9f032017-01-25 14:10:04 -050053 case "libc++":
54 return "ndk_libc++_shared"
55 case "libc++_static":
56 return "ndk_libc++_static"
Colin Cross4a97cb42016-04-21 15:53:42 -070057 case "none":
58 return ""
Colin Crossca860ac2016-01-04 14:34:37 -080059 default:
Colin Cross79248852016-07-12 13:12:33 -070060 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", s)
Colin Crossca860ac2016-01-04 14:34:37 -080061 return ""
62 }
Colin Crossa1ad8d12016-06-01 17:09:44 -070063 } else if ctx.Os() == android.Windows {
Colin Cross79248852016-07-12 13:12:33 -070064 switch s {
Colin Crossca860ac2016-01-04 14:34:37 -080065 case "libc++", "libc++_static", "libstdc++", "":
66 // libc++ is not supported on mingw
67 return "libstdc++"
68 case "none":
69 return ""
70 default:
Colin Cross79248852016-07-12 13:12:33 -070071 ctx.ModuleErrorf("stl: %q is not a supported STL for windows", s)
Colin Crossca860ac2016-01-04 14:34:37 -080072 return ""
73 }
74 } else {
Colin Cross79248852016-07-12 13:12:33 -070075 switch s {
Dan Willemsen141d5662016-06-15 13:47:51 -070076 case "libc++", "libc++_static":
Colin Cross79248852016-07-12 13:12:33 -070077 return s
Colin Crossca860ac2016-01-04 14:34:37 -080078 case "none":
79 return ""
80 case "":
81 if ctx.static() {
82 return "libc++_static"
83 } else {
84 return "libc++"
85 }
86 default:
Colin Cross79248852016-07-12 13:12:33 -070087 ctx.ModuleErrorf("stl: %q is not a supported STL", s)
Colin Crossca860ac2016-01-04 14:34:37 -080088 return ""
89 }
90 }
91 }()
92}
93
Colin Crossa8e07cc2016-04-04 15:07:06 -070094func (stl *stl) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Crossca860ac2016-01-04 14:34:37 -080095 switch stl.Properties.SelectedStl {
96 case "libstdc++":
Dan Willemsen141d5662016-06-15 13:47:51 -070097 // Nothing
Colin Crossca860ac2016-01-04 14:34:37 -080098 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 Willemsen2e47b342016-11-17 01:02:25 -0800104 if ctx.toolchain().Bionic() {
Colin Cross635c3b02016-05-18 15:37:25 -0700105 if ctx.Arch().ArchType == android.Arm {
Colin Crossca860ac2016-01-04 14:34:37 -0800106 deps.StaticLibs = append(deps.StaticLibs, "libunwind_llvm")
107 }
108 if ctx.staticBinary() {
Colin Cross3d92b272016-07-14 15:59:32 -0700109 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
Colin Crossca860ac2016-01-04 14:34:37 -0800110 } 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 Albert3895d5b2016-09-14 16:50:48 -0700121 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 Crossca860ac2016-01-04 14:34:37 -0800128 deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl)
Dan Albert3895d5b2016-09-14 16:50:48 -0700129 case "ndk_libstlport_static", "ndk_libgnustl_static":
Colin Crossca860ac2016-01-04 14:34:37 -0800130 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 Crossa8e07cc2016-04-04 15:07:06 -0700138func (stl *stl) flags(ctx ModuleContext, flags Flags) Flags {
Colin Crossca860ac2016-01-04 14:34:37 -0800139 switch stl.Properties.SelectedStl {
140 case "libc++", "libc++_static":
141 flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
Dan Willemsen2e47b342016-11-17 01:02:25 -0800142 if !ctx.toolchain().Bionic() {
Colin Crossca860ac2016-01-04 14:34:37 -0800143 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 Crossa1ad8d12016-06-01 17:09:44 -0700147 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.Os()]...)
Colin Crossca860ac2016-01-04 14:34:37 -0800148 } else {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700149 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.Os()]...)
Colin Crossca860ac2016-01-04 14:34:37 -0800150 }
151 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700152 if ctx.Arch().ArchType == android.Arm {
Colin Crossca860ac2016-01-04 14:34:37 -0800153 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a")
154 }
155 }
156 case "libstdc++":
Dan Willemsen141d5662016-06-15 13:47:51 -0700157 // Nothing
Colin Crossca860ac2016-01-04 14:34:37 -0800158 case "ndk_system":
Colin Cross635c3b02016-05-18 15:37:25 -0700159 ndkSrcRoot := android.PathForSource(ctx, "prebuilts/ndk/current/sources/cxx-stl/system/include")
Colin Crossca860ac2016-01-04 14:34:37 -0800160 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 Willemsen2e47b342016-11-17 01:02:25 -0800168 if !ctx.toolchain().Bionic() {
Colin Crossca860ac2016-01-04 14:34:37 -0800169 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
170 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
171 if ctx.staticBinary() {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700172 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.Os()]...)
Colin Crossca860ac2016-01-04 14:34:37 -0800173 } else {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700174 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.Os()]...)
Colin Crossca860ac2016-01-04 14:34:37 -0800175 }
176 }
177 default:
178 panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl))
179 }
180
181 return flags
182}
183
Colin Crossa1ad8d12016-06-01 17:09:44 -0700184var hostDynamicGccLibs, hostStaticGccLibs map[android.OsType][]string
Colin Crossca860ac2016-01-04 14:34:37 -0800185
186func init() {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700187 hostDynamicGccLibs = map[android.OsType][]string{
Colin Cross635c3b02016-05-18 15:37:25 -0700188 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 Crossca860ac2016-01-04 14:34:37 -0800191 "-lmingwex", "-lmsvcrt", "-ladvapi32", "-lshell32", "-luser32",
192 "-lkernel32", "-lmingw32", "-lgcc", "-lmoldname", "-lmingwex",
193 "-lmsvcrt"},
194 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700195 hostStaticGccLibs = map[android.OsType][]string{
Colin Cross635c3b02016-05-18 15:37:25 -0700196 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 Crossca860ac2016-01-04 14:34:37 -0800199 }
200}