blob: 52add5cbb841251ba6affc22005d9177d75e3bf0 [file] [log] [blame]
Colin Cross0af4b842015-04-30 16:36:18 -07001package cc
2
3import (
Dan Willemsencf7c71b2015-12-14 20:02:44 -08004 "fmt"
5 "os/exec"
Colin Cross0af4b842015-04-30 16:36:18 -07006 "strings"
7
8 "android/soong/common"
9)
10
11var (
12 darwinCflags = []string{
13 "-fno-exceptions", // from build/core/combo/select.mk
14 "-Wno-multichar", // from build/core/combo/select.mk
15
Dan Willemsen490fd492015-11-24 17:53:15 -080016 "-fdiagnostics-color",
17
Colin Cross0af4b842015-04-30 16:36:18 -070018 "-fPIC",
19 "-funwind-tables",
Colin Cross0af4b842015-04-30 16:36:18 -070020
21 // Workaround differences in inttypes.h between host and target.
22 //See bug 12708004.
23 "-D__STDC_FORMAT_MACROS",
24 "-D__STDC_CONSTANT_MACROS",
25
26 // HOST_RELEASE_CFLAGS
27 "-O2", // from build/core/combo/select.mk
28 "-g", // from build/core/combo/select.mk
29 "-fno-strict-aliasing", // from build/core/combo/select.mk
30 "-isysroot ${macSdkRoot}",
Dan Willemsencf7c71b2015-12-14 20:02:44 -080031 "-mmacosx-version-min=${macSdkVersion}",
32 "-DMACOSX_DEPLOYMENT_TARGET=${macSdkVersion}",
Colin Cross0af4b842015-04-30 16:36:18 -070033 }
34
Colin Cross0af4b842015-04-30 16:36:18 -070035 darwinLdflags = []string{
36 "-isysroot ${macSdkRoot}",
37 "-Wl,-syslibroot,${macSdkRoot}",
Dan Willemsencf7c71b2015-12-14 20:02:44 -080038 "-mmacosx-version-min=${macSdkVersion}",
Colin Cross0af4b842015-04-30 16:36:18 -070039 }
40
Dan Willemsen282a4b02016-03-09 10:30:22 -080041 darwinSystemCppCppflags = []string{
42 "-isystem ${macToolchainRoot}/usr/include/c++/v1",
43 }
44
45 darwinSystemCppLdflags = []string{
46 "-stdlib=libc++",
47 }
48
Colin Cross0af4b842015-04-30 16:36:18 -070049 // Extended cflags
50 darwinX86Cflags = []string{
51 "-m32",
52 }
53
54 darwinX8664Cflags = []string{
55 "-m64",
56 }
57
58 darwinX86Ldflags = []string{
59 "-m32",
Colin Cross0af4b842015-04-30 16:36:18 -070060 }
61
62 darwinX8664Ldflags = []string{
63 "-m64",
Colin Cross0af4b842015-04-30 16:36:18 -070064 }
65
Dan Willemsenf534a102016-03-03 17:22:39 -080066 darwinClangCflags = append(clangFilterUnknownCflags(darwinCflags), []string{
Colin Cross0af4b842015-04-30 16:36:18 -070067 "-integrated-as",
Dan Willemsen7a0f8482016-01-12 16:22:40 -080068 "-fstack-protector-strong",
Dan Willemsenf534a102016-03-03 17:22:39 -080069 }...)
Colin Cross0af4b842015-04-30 16:36:18 -070070
Dan Willemsen01fdd3d2016-03-30 00:01:12 -070071 darwinX86ClangCflags = append(clangFilterUnknownCflags(darwinX86Cflags), []string{
72 "-msse3",
73 }...)
74
Colin Cross0af4b842015-04-30 16:36:18 -070075 darwinClangLdflags = clangFilterUnknownCflags(darwinLdflags)
76
77 darwinX86ClangLdflags = clangFilterUnknownCflags(darwinX86Ldflags)
78
79 darwinX8664ClangLdflags = clangFilterUnknownCflags(darwinX8664Ldflags)
80
Dan Willemsencf7c71b2015-12-14 20:02:44 -080081 darwinSupportedSdkVersions = []string{
Dan Willemsen6fb8b8d2016-03-09 10:41:21 -080082 "10.8",
83 "10.9",
84 "10.10",
85 "10.11",
Dan Willemsencf7c71b2015-12-14 20:02:44 -080086 }
Colin Cross0af4b842015-04-30 16:36:18 -070087)
88
Dan Willemsen34fc3b12015-12-07 12:30:44 -080089const (
90 darwinGccVersion = "4.2.1"
91)
92
Colin Cross0af4b842015-04-30 16:36:18 -070093func init() {
Dan Willemsencf7c71b2015-12-14 20:02:44 -080094 pctx.VariableFunc("macSdkPath", func(config interface{}) (string, error) {
95 bytes, err := exec.Command("xcode-select", "--print-path").Output()
96 return strings.TrimSpace(string(bytes)), err
97 })
98 pctx.StaticVariable("macToolchainRoot", "${macSdkPath}/Toolchains/XcodeDefault.xctoolchain")
99 pctx.VariableFunc("macSdkRoot", func(config interface{}) (string, error) {
100 return xcrunSdk(config.(common.Config), "--show-sdk-path")
101 })
Dan Willemsen6fb8b8d2016-03-09 10:41:21 -0800102 pctx.StaticVariable("macSdkVersion", darwinSupportedSdkVersions[0])
Dan Willemsencf7c71b2015-12-14 20:02:44 -0800103 pctx.VariableFunc("macArPath", func(config interface{}) (string, error) {
104 bytes, err := exec.Command("xcrun", "--find", "ar").Output()
105 return strings.TrimSpace(string(bytes)), err
106 })
Colin Cross0af4b842015-04-30 16:36:18 -0700107
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700108 pctx.VariableFunc("macStripPath", func(config interface{}) (string, error) {
109 bytes, err := exec.Command("xcrun", "--find", "strip").Output()
110 return strings.TrimSpace(string(bytes)), err
111 })
112
Dan Willemsen34fc3b12015-12-07 12:30:44 -0800113 pctx.StaticVariable("darwinGccVersion", darwinGccVersion)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700114 pctx.SourcePathVariable("darwinGccRoot",
115 "prebuilts/gcc/${HostPrebuiltTag}/host/i686-apple-darwin-${darwinGccVersion}")
Colin Cross0af4b842015-04-30 16:36:18 -0700116
117 pctx.StaticVariable("darwinGccTriple", "i686-apple-darwin11")
118
119 pctx.StaticVariable("darwinCflags", strings.Join(darwinCflags, " "))
120 pctx.StaticVariable("darwinLdflags", strings.Join(darwinLdflags, " "))
Colin Cross0af4b842015-04-30 16:36:18 -0700121
122 pctx.StaticVariable("darwinClangCflags", strings.Join(darwinClangCflags, " "))
123 pctx.StaticVariable("darwinClangLdflags", strings.Join(darwinClangLdflags, " "))
Dan Willemsen282a4b02016-03-09 10:30:22 -0800124
125 pctx.StaticVariable("darwinSystemCppCppflags", strings.Join(darwinSystemCppCppflags, " "))
126 pctx.StaticVariable("darwinSystemCppLdflags", strings.Join(darwinSystemCppLdflags, " "))
Colin Cross0af4b842015-04-30 16:36:18 -0700127
128 // Extended cflags
129 pctx.StaticVariable("darwinX86Cflags", strings.Join(darwinX86Cflags, " "))
130 pctx.StaticVariable("darwinX8664Cflags", strings.Join(darwinX8664Cflags, " "))
131 pctx.StaticVariable("darwinX86Ldflags", strings.Join(darwinX86Ldflags, " "))
132 pctx.StaticVariable("darwinX8664Ldflags", strings.Join(darwinX8664Ldflags, " "))
133
Dan Willemsen01fdd3d2016-03-30 00:01:12 -0700134 pctx.StaticVariable("darwinX86ClangCflags", strings.Join(darwinX86ClangCflags, " "))
Colin Cross0af4b842015-04-30 16:36:18 -0700135 pctx.StaticVariable("darwinX8664ClangCflags",
136 strings.Join(clangFilterUnknownCflags(darwinX8664Cflags), " "))
137 pctx.StaticVariable("darwinX86ClangLdflags", strings.Join(darwinX86ClangLdflags, " "))
138 pctx.StaticVariable("darwinX8664ClangLdflags", strings.Join(darwinX8664ClangLdflags, " "))
139}
140
Dan Willemsencf7c71b2015-12-14 20:02:44 -0800141func xcrunSdk(config common.Config, arg string) (string, error) {
142 if selected := config.Getenv("MAC_SDK_VERSION"); selected != "" {
Dan Willemsen6fb8b8d2016-03-09 10:41:21 -0800143 if !inList(selected, darwinSupportedSdkVersions) {
Dan Willemsencf7c71b2015-12-14 20:02:44 -0800144 return "", fmt.Errorf("MAC_SDK_VERSION %s isn't supported: %q", selected, darwinSupportedSdkVersions)
145 }
146
147 bytes, err := exec.Command("xcrun", "--sdk", "macosx"+selected, arg).Output()
148 if err == nil {
149 return strings.TrimSpace(string(bytes)), err
150 }
151 return "", fmt.Errorf("MAC_SDK_VERSION %s is not installed", selected)
152 }
153
154 for _, sdk := range darwinSupportedSdkVersions {
Dan Willemsen6fb8b8d2016-03-09 10:41:21 -0800155 bytes, err := exec.Command("xcrun", "--sdk", "macosx"+sdk, arg).Output()
Dan Willemsencf7c71b2015-12-14 20:02:44 -0800156 if err == nil {
157 return strings.TrimSpace(string(bytes)), err
158 }
159 }
160 return "", fmt.Errorf("Could not find a supported mac sdk: %q", darwinSupportedSdkVersions)
161}
162
Colin Cross0af4b842015-04-30 16:36:18 -0700163type toolchainDarwin struct {
164 cFlags, ldFlags string
165}
166
167type toolchainDarwinX86 struct {
168 toolchain32Bit
169 toolchainDarwin
170}
171
172type toolchainDarwinX8664 struct {
173 toolchain64Bit
174 toolchainDarwin
175}
176
177func (t *toolchainDarwinX86) Name() string {
178 return "x86"
179}
180
181func (t *toolchainDarwinX8664) Name() string {
182 return "x86_64"
183}
184
185func (t *toolchainDarwin) GccRoot() string {
186 return "${darwinGccRoot}"
187}
188
189func (t *toolchainDarwin) GccTriple() string {
190 return "${darwinGccTriple}"
191}
192
193func (t *toolchainDarwin) GccVersion() string {
Dan Willemsen34fc3b12015-12-07 12:30:44 -0800194 return darwinGccVersion
Colin Cross0af4b842015-04-30 16:36:18 -0700195}
196
197func (t *toolchainDarwin) Cflags() string {
198 return "${darwinCflags} ${darwinX86Cflags}"
199}
200
201func (t *toolchainDarwinX8664) Cflags() string {
202 return "${darwinCflags} ${darwinX8664Cflags}"
203}
204
205func (t *toolchainDarwin) Cppflags() string {
Dan Willemsen282a4b02016-03-09 10:30:22 -0800206 return ""
Colin Cross0af4b842015-04-30 16:36:18 -0700207}
208
209func (t *toolchainDarwinX86) Ldflags() string {
210 return "${darwinLdflags} ${darwinX86Ldflags}"
211}
212
213func (t *toolchainDarwinX8664) Ldflags() string {
214 return "${darwinLdflags} ${darwinX8664Ldflags}"
215}
216
217func (t *toolchainDarwin) IncludeFlags() string {
218 return ""
219}
220
221func (t *toolchainDarwinX86) ClangTriple() string {
Dan Willemsen01fdd3d2016-03-30 00:01:12 -0700222 return "i686-apple-darwin"
Colin Cross0af4b842015-04-30 16:36:18 -0700223}
224
225func (t *toolchainDarwinX86) ClangCflags() string {
226 return "${darwinClangCflags} ${darwinX86ClangCflags}"
227}
228
Colin Cross0af4b842015-04-30 16:36:18 -0700229func (t *toolchainDarwinX8664) ClangTriple() string {
Dan Willemsen01fdd3d2016-03-30 00:01:12 -0700230 return "x86_64-apple-darwin"
Colin Cross0af4b842015-04-30 16:36:18 -0700231}
232
233func (t *toolchainDarwinX8664) ClangCflags() string {
234 return "${darwinClangCflags} ${darwinX8664ClangCflags}"
235}
236
Dan Willemsen282a4b02016-03-09 10:30:22 -0800237func (t *toolchainDarwin) ClangCppflags() string {
238 return ""
Colin Cross0af4b842015-04-30 16:36:18 -0700239}
240
241func (t *toolchainDarwinX86) ClangLdflags() string {
242 return "${darwinClangLdflags} ${darwinX86ClangLdflags}"
243}
244
245func (t *toolchainDarwinX8664) ClangLdflags() string {
246 return "${darwinClangLdflags} ${darwinX8664ClangLdflags}"
247}
248
Dan Willemsen490fd492015-11-24 17:53:15 -0800249func (t *toolchainDarwin) ShlibSuffix() string {
250 return ".dylib"
251}
252
Dan Willemsen282a4b02016-03-09 10:30:22 -0800253func (t *toolchainDarwin) SystemCppCppflags() string {
254 return "${darwinSystemCppCppflags}"
255}
256
257func (t *toolchainDarwin) SystemCppLdflags() string {
258 return "${darwinSystemCppLdflags}"
259}
260
Colin Cross0af4b842015-04-30 16:36:18 -0700261var toolchainDarwinX86Singleton Toolchain = &toolchainDarwinX86{}
262var toolchainDarwinX8664Singleton Toolchain = &toolchainDarwinX8664{}
263
Colin Crossc5c24ad2015-11-20 15:35:00 -0800264func darwinX86ToolchainFactory(arch common.Arch) Toolchain {
Colin Cross0af4b842015-04-30 16:36:18 -0700265 return toolchainDarwinX86Singleton
266}
267
Colin Crossc5c24ad2015-11-20 15:35:00 -0800268func darwinX8664ToolchainFactory(arch common.Arch) Toolchain {
Colin Cross0af4b842015-04-30 16:36:18 -0700269 return toolchainDarwinX8664Singleton
270}
271
272func init() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800273 registerHostToolchainFactory(common.Darwin, common.X86, darwinX86ToolchainFactory)
274 registerHostToolchainFactory(common.Darwin, common.X86_64, darwinX8664ToolchainFactory)
Colin Cross0af4b842015-04-30 16:36:18 -0700275}