blob: a7e362c8afda53dc99615e760e8920d6b09de42d [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
Colin Cross4a97cb42016-04-21 15:53:42 -070053 case "none":
54 return ""
Colin Crossca860ac2016-01-04 14:34:37 -080055 default:
Colin Cross79248852016-07-12 13:12:33 -070056 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", s)
Colin Crossca860ac2016-01-04 14:34:37 -080057 return ""
58 }
Colin Crossa1ad8d12016-06-01 17:09:44 -070059 } else if ctx.Os() == android.Windows {
Colin Cross79248852016-07-12 13:12:33 -070060 switch s {
Colin Crossca860ac2016-01-04 14:34:37 -080061 case "libc++", "libc++_static", "libstdc++", "":
62 // libc++ is not supported on mingw
63 return "libstdc++"
64 case "none":
65 return ""
66 default:
Colin Cross79248852016-07-12 13:12:33 -070067 ctx.ModuleErrorf("stl: %q is not a supported STL for windows", s)
Colin Crossca860ac2016-01-04 14:34:37 -080068 return ""
69 }
70 } else {
Colin Cross79248852016-07-12 13:12:33 -070071 switch s {
Dan Willemsen141d5662016-06-15 13:47:51 -070072 case "libc++", "libc++_static":
Colin Cross79248852016-07-12 13:12:33 -070073 return s
Colin Crossca860ac2016-01-04 14:34:37 -080074 case "none":
75 return ""
76 case "":
77 if ctx.static() {
78 return "libc++_static"
79 } else {
80 return "libc++"
81 }
82 default:
Colin Cross79248852016-07-12 13:12:33 -070083 ctx.ModuleErrorf("stl: %q is not a supported STL", s)
Colin Crossca860ac2016-01-04 14:34:37 -080084 return ""
85 }
86 }
87 }()
88}
89
Colin Crossa8e07cc2016-04-04 15:07:06 -070090func (stl *stl) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Crossca860ac2016-01-04 14:34:37 -080091 switch stl.Properties.SelectedStl {
92 case "libstdc++":
Dan Willemsen141d5662016-06-15 13:47:51 -070093 // Nothing
Colin Crossca860ac2016-01-04 14:34:37 -080094 case "libc++", "libc++_static":
95 if stl.Properties.SelectedStl == "libc++" {
96 deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl)
97 } else {
98 deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl)
99 }
100 if ctx.Device() {
Colin Cross635c3b02016-05-18 15:37:25 -0700101 if ctx.Arch().ArchType == android.Arm {
Colin Crossca860ac2016-01-04 14:34:37 -0800102 deps.StaticLibs = append(deps.StaticLibs, "libunwind_llvm")
103 }
104 if ctx.staticBinary() {
Colin Cross3d92b272016-07-14 15:59:32 -0700105 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
Colin Crossca860ac2016-01-04 14:34:37 -0800106 } else {
107 deps.SharedLibs = append(deps.SharedLibs, "libdl")
108 }
109 }
110 case "":
111 // None or error.
112 case "ndk_system":
113 // TODO: Make a system STL prebuilt for the NDK.
114 // The system STL doesn't have a prebuilt (it uses the system's libstdc++), but it does have
115 // its own includes. The includes are handled in CCBase.Flags().
116 deps.SharedLibs = append([]string{"libstdc++"}, deps.SharedLibs...)
117 case "ndk_libc++_shared", "ndk_libstlport_shared":
118 deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl)
119 case "ndk_libc++_static", "ndk_libstlport_static", "ndk_libgnustl_static":
120 deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl)
121 default:
122 panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl))
123 }
124
125 return deps
126}
127
Colin Crossa8e07cc2016-04-04 15:07:06 -0700128func (stl *stl) flags(ctx ModuleContext, flags Flags) Flags {
Colin Crossca860ac2016-01-04 14:34:37 -0800129 switch stl.Properties.SelectedStl {
130 case "libc++", "libc++_static":
131 flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
132 if ctx.Host() {
133 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
134 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
135 flags.LdFlags = append(flags.LdFlags, "-lpthread", "-lm")
136 if ctx.staticBinary() {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700137 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.Os()]...)
Colin Crossca860ac2016-01-04 14:34:37 -0800138 } else {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700139 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.Os()]...)
Colin Crossca860ac2016-01-04 14:34:37 -0800140 }
141 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700142 if ctx.Arch().ArchType == android.Arm {
Colin Crossca860ac2016-01-04 14:34:37 -0800143 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a")
144 }
145 }
146 case "libstdc++":
Dan Willemsen141d5662016-06-15 13:47:51 -0700147 // Nothing
Colin Crossca860ac2016-01-04 14:34:37 -0800148 case "ndk_system":
Colin Cross635c3b02016-05-18 15:37:25 -0700149 ndkSrcRoot := android.PathForSource(ctx, "prebuilts/ndk/current/sources/cxx-stl/system/include")
Colin Crossca860ac2016-01-04 14:34:37 -0800150 flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot.String())
151 case "ndk_libc++_shared", "ndk_libc++_static":
152 // TODO(danalbert): This really shouldn't be here...
153 flags.CppFlags = append(flags.CppFlags, "-std=c++11")
154 case "ndk_libstlport_shared", "ndk_libstlport_static", "ndk_libgnustl_static":
155 // Nothing
156 case "":
157 // None or error.
158 if ctx.Host() {
159 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
160 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
161 if ctx.staticBinary() {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700162 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.Os()]...)
Colin Crossca860ac2016-01-04 14:34:37 -0800163 } else {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700164 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.Os()]...)
Colin Crossca860ac2016-01-04 14:34:37 -0800165 }
166 }
167 default:
168 panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl))
169 }
170
171 return flags
172}
173
Colin Crossa1ad8d12016-06-01 17:09:44 -0700174var hostDynamicGccLibs, hostStaticGccLibs map[android.OsType][]string
Colin Crossca860ac2016-01-04 14:34:37 -0800175
176func init() {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700177 hostDynamicGccLibs = map[android.OsType][]string{
Colin Cross635c3b02016-05-18 15:37:25 -0700178 android.Linux: []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"},
179 android.Darwin: []string{"-lc", "-lSystem"},
180 android.Windows: []string{"-lmsvcr110", "-lmingw32", "-lgcc", "-lmoldname",
Colin Crossca860ac2016-01-04 14:34:37 -0800181 "-lmingwex", "-lmsvcrt", "-ladvapi32", "-lshell32", "-luser32",
182 "-lkernel32", "-lmingw32", "-lgcc", "-lmoldname", "-lmingwex",
183 "-lmsvcrt"},
184 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700185 hostStaticGccLibs = map[android.OsType][]string{
Colin Cross635c3b02016-05-18 15:37:25 -0700186 android.Linux: []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"},
187 android.Darwin: []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"},
188 android.Windows: []string{"NO_STATIC_HOST_BINARIES_ON_WINDOWS"},
Colin Crossca860ac2016-01-04 14:34:37 -0800189 }
190}