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
15package config
16
17import (
18	"fmt"
19	"os/exec"
20	"path/filepath"
21	"strings"
22	"sync"
23
24	"android/soong/android"
25)
26
27var (
28	darwinCflags = []string{
29		"-fdiagnostics-color",
30
31		"-fPIC",
32		"-funwind-tables",
33
34		// Workaround differences in inttypes.h between host and target.
35		//See bug 12708004.
36		"-D__STDC_FORMAT_MACROS",
37		"-D__STDC_CONSTANT_MACROS",
38
39		"-isysroot ${macSdkRoot}",
40		"-mmacosx-version-min=${macMinVersion}",
41		"-DMACOSX_DEPLOYMENT_TARGET=${macMinVersion}",
42
43		"-m64",
44	}
45
46	darwinLdflags = []string{
47		"-isysroot ${macSdkRoot}",
48		"-Wl,-syslibroot,${macSdkRoot}",
49		"-mmacosx-version-min=${macMinVersion}",
50		"-m64",
51	}
52
53	darwinClangCflags = append(ClangFilterUnknownCflags(darwinCflags), []string{
54		"-integrated-as",
55		"-fstack-protector-strong",
56	}...)
57
58	darwinClangLdflags = ClangFilterUnknownCflags(darwinLdflags)
59
60	darwinClangLldflags = ClangFilterUnknownLldflags(darwinClangLdflags)
61
62	darwinSupportedSdkVersions = []string{
63		"10.10",
64		"10.11",
65		"10.12",
66		"10.13",
67		"10.14",
68		"10.15",
69		"11.0",
70		"11.1",
71	}
72
73	darwinAvailableLibraries = append(
74		addPrefix([]string{
75			"c",
76			"dl",
77			"m",
78			"ncurses",
79			"objc",
80			"pthread",
81		}, "-l"),
82		"-framework AppKit",
83		"-framework CoreFoundation",
84		"-framework Foundation",
85		"-framework IOKit",
86		"-framework Security",
87		"-framework SystemConfiguration",
88	)
89)
90
91const (
92	darwinGccVersion = "4.2.1"
93)
94
95func init() {
96	pctx.VariableFunc("macSdkRoot", func(ctx android.PackageVarContext) string {
97		return getMacTools(ctx).sdkRoot
98	})
99	pctx.StaticVariable("macMinVersion", "10.10")
100	pctx.VariableFunc("MacArPath", func(ctx android.PackageVarContext) string {
101		return getMacTools(ctx).arPath
102	})
103
104	pctx.VariableFunc("MacStripPath", func(ctx android.PackageVarContext) string {
105		return getMacTools(ctx).stripPath
106	})
107
108	pctx.VariableFunc("MacToolPath", func(ctx android.PackageVarContext) string {
109		return getMacTools(ctx).toolPath
110	})
111
112	pctx.StaticVariable("DarwinGccVersion", darwinGccVersion)
113	pctx.SourcePathVariable("DarwinGccRoot",
114		"prebuilts/gcc/${HostPrebuiltTag}/host/i686-apple-darwin-${DarwinGccVersion}")
115
116	pctx.StaticVariable("DarwinGccTriple", "i686-apple-darwin11")
117
118	pctx.StaticVariable("DarwinClangCflags", strings.Join(darwinClangCflags, " "))
119	pctx.StaticVariable("DarwinClangLdflags", strings.Join(darwinClangLdflags, " "))
120	pctx.StaticVariable("DarwinClangLldflags", strings.Join(darwinClangLldflags, " "))
121
122	pctx.StaticVariable("DarwinYasmFlags", "-f macho -m amd64")
123}
124
125type macPlatformTools struct {
126	once sync.Once
127	err  error
128
129	sdkRoot   string
130	arPath    string
131	stripPath string
132	toolPath  string
133}
134
135var macTools = &macPlatformTools{}
136
137func getMacTools(ctx android.PackageVarContext) *macPlatformTools {
138	macTools.once.Do(func() {
139		xcrunTool := "/usr/bin/xcrun"
140
141		xcrun := func(args ...string) string {
142			if macTools.err != nil {
143				return ""
144			}
145
146			bytes, err := exec.Command(xcrunTool, args...).Output()
147			if err != nil {
148				macTools.err = fmt.Errorf("xcrun %q failed with: %q", args, err)
149				return ""
150			}
151
152			return strings.TrimSpace(string(bytes))
153		}
154
155		xcrunSdk := func(arg string) string {
156			if selected := ctx.Config().Getenv("MAC_SDK_VERSION"); selected != "" {
157				if !inList(selected, darwinSupportedSdkVersions) {
158					macTools.err = fmt.Errorf("MAC_SDK_VERSION %s isn't supported: %q", selected, darwinSupportedSdkVersions)
159					return ""
160				}
161
162				return xcrun("--sdk", "macosx"+selected, arg)
163			}
164
165			for _, sdk := range darwinSupportedSdkVersions {
166				bytes, err := exec.Command(xcrunTool, "--sdk", "macosx"+sdk, arg).Output()
167				if err == nil {
168					return strings.TrimSpace(string(bytes))
169				}
170			}
171			macTools.err = fmt.Errorf("Could not find a supported mac sdk: %q", darwinSupportedSdkVersions)
172			return ""
173		}
174
175		macTools.sdkRoot = xcrunSdk("--show-sdk-path")
176
177		macTools.arPath = xcrun("--find", "ar")
178		macTools.stripPath = xcrun("--find", "strip")
179		macTools.toolPath = filepath.Dir(xcrun("--find", "ld"))
180	})
181	if macTools.err != nil {
182		ctx.Errorf("%q", macTools.err)
183	}
184	return macTools
185}
186
187type toolchainDarwin struct {
188	cFlags, ldFlags string
189	toolchain64Bit
190}
191
192func (t *toolchainDarwin) Name() string {
193	return "x86_64"
194}
195
196func (t *toolchainDarwin) GccRoot() string {
197	return "${config.DarwinGccRoot}"
198}
199
200func (t *toolchainDarwin) GccTriple() string {
201	return "${config.DarwinGccTriple}"
202}
203
204func (t *toolchainDarwin) GccVersion() string {
205	return darwinGccVersion
206}
207
208func (t *toolchainDarwin) IncludeFlags() string {
209	return ""
210}
211
212func (t *toolchainDarwin) ClangTriple() string {
213	return "x86_64-apple-darwin"
214}
215
216func (t *toolchainDarwin) ClangCflags() string {
217	return "${config.DarwinClangCflags}"
218}
219
220func (t *toolchainDarwin) ClangCppflags() string {
221	return ""
222}
223
224func (t *toolchainDarwin) ClangLdflags() string {
225	return "${config.DarwinClangLdflags}"
226}
227
228func (t *toolchainDarwin) ClangLldflags() string {
229	return "${config.DarwinClangLldflags}"
230}
231
232func (t *toolchainDarwin) YasmFlags() string {
233	return "${config.DarwinYasmFlags}"
234}
235
236func (t *toolchainDarwin) ShlibSuffix() string {
237	return ".dylib"
238}
239
240func (t *toolchainDarwin) AvailableLibraries() []string {
241	return darwinAvailableLibraries
242}
243
244func (t *toolchainDarwin) Bionic() bool {
245	return false
246}
247
248func (t *toolchainDarwin) ToolPath() string {
249	return "${config.MacToolPath}"
250}
251
252var toolchainDarwinSingleton Toolchain = &toolchainDarwin{}
253
254func darwinToolchainFactory(arch android.Arch) Toolchain {
255	return toolchainDarwinSingleton
256}
257
258func init() {
259	registerToolchainFactory(android.Darwin, android.X86_64, darwinToolchainFactory)
260}
261