blob: 280beed0ad7faa833729e30941e5ed7e8a08e564 [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
26 Stl string
27
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 {
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 Cross4a97cb42016-04-21 15:53:42 -070049 case "none":
50 return ""
Colin Crossca860ac2016-01-04 14:34:37 -080051 default:
52 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", stl.Properties.Stl)
53 return ""
54 }
Colin Crossa1ad8d12016-06-01 17:09:44 -070055 } else if ctx.Os() == android.Windows {
Colin Crossca860ac2016-01-04 14:34:37 -080056 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 Crossa1ad8d12016-06-01 17:09:44 -070063 ctx.ModuleErrorf("stl: %q is not a supported STL for windows", stl.Properties.Stl)
Colin Crossca860ac2016-01-04 14:34:37 -080064 return ""
65 }
66 } else {
67 switch stl.Properties.Stl {
Dan Willemsen141d5662016-06-15 13:47:51 -070068 case "libc++", "libc++_static":
Colin Crossca860ac2016-01-04 14:34:37 -080069 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 Crossa8e07cc2016-04-04 15:07:06 -070086func (stl *stl) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Crossca860ac2016-01-04 14:34:37 -080087 switch stl.Properties.SelectedStl {
88 case "libstdc++":
Dan Willemsen141d5662016-06-15 13:47:51 -070089 // Nothing
Colin Crossca860ac2016-01-04 14:34:37 -080090 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 Cross635c3b02016-05-18 15:37:25 -070097 if ctx.Arch().ArchType == android.Arm {
Colin Crossca860ac2016-01-04 14:34:37 -080098 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 Crossa8e07cc2016-04-04 15:07:06 -0700124func (stl *stl) flags(ctx ModuleContext, flags Flags) Flags {
Colin Crossca860ac2016-01-04 14:34:37 -0800125 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 Crossa1ad8d12016-06-01 17:09:44 -0700133 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.Os()]...)
Colin Crossca860ac2016-01-04 14:34:37 -0800134 } else {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700135 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.Os()]...)
Colin Crossca860ac2016-01-04 14:34:37 -0800136 }
137 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700138 if ctx.Arch().ArchType == android.Arm {
Colin Crossca860ac2016-01-04 14:34:37 -0800139 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a")
140 }
141 }
142 case "libstdc++":
Dan Willemsen141d5662016-06-15 13:47:51 -0700143 // Nothing
Colin Crossca860ac2016-01-04 14:34:37 -0800144 case "ndk_system":
Colin Cross635c3b02016-05-18 15:37:25 -0700145 ndkSrcRoot := android.PathForSource(ctx, "prebuilts/ndk/current/sources/cxx-stl/system/include")
Colin Crossca860ac2016-01-04 14:34:37 -0800146 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 Crossa1ad8d12016-06-01 17:09:44 -0700158 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.Os()]...)
Colin Crossca860ac2016-01-04 14:34:37 -0800159 } else {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700160 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.Os()]...)
Colin Crossca860ac2016-01-04 14:34:37 -0800161 }
162 }
163 default:
164 panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl))
165 }
166
167 return flags
168}
169
Colin Crossa1ad8d12016-06-01 17:09:44 -0700170var hostDynamicGccLibs, hostStaticGccLibs map[android.OsType][]string
Colin Crossca860ac2016-01-04 14:34:37 -0800171
172func init() {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700173 hostDynamicGccLibs = map[android.OsType][]string{
Colin Cross635c3b02016-05-18 15:37:25 -0700174 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 Crossca860ac2016-01-04 14:34:37 -0800177 "-lmingwex", "-lmsvcrt", "-ladvapi32", "-lshell32", "-luser32",
178 "-lkernel32", "-lmingw32", "-lgcc", "-lmoldname", "-lmingwex",
179 "-lmsvcrt"},
180 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700181 hostStaticGccLibs = map[android.OsType][]string{
Colin Cross635c3b02016-05-18 15:37:25 -0700182 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 Crossca860ac2016-01-04 14:34:37 -0800185 }
186}