blob: 47c61b08f352990c99c79a5d07815e528d51c398 [file] [log] [blame]
Colin Crossb98c8b02016-07-29 13:44:28 -07001// 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
Colin Cross0af4b842015-04-30 16:36:18 -070016
17import (
Dan Willemsen6ba53672020-04-01 14:32:52 -070018 "fmt"
Dan Willemsencf7c71b2015-12-14 20:02:44 -080019 "os/exec"
Dan Willemsen300151b2017-03-13 12:40:30 -070020 "path/filepath"
Colin Cross0af4b842015-04-30 16:36:18 -070021 "strings"
Dan Willemsen6ba53672020-04-01 14:32:52 -070022 "sync"
Colin Cross0af4b842015-04-30 16:36:18 -070023
Colin Cross635c3b02016-05-18 15:37:25 -070024 "android/soong/android"
Colin Cross0af4b842015-04-30 16:36:18 -070025)
26
27var (
28 darwinCflags = []string{
Colin Cross0af4b842015-04-30 16:36:18 -070029 "-fPIC",
30 "-funwind-tables",
Colin Cross0af4b842015-04-30 16:36:18 -070031
Colin Cross0af4b842015-04-30 16:36:18 -070032 "-isysroot ${macSdkRoot}",
Dan Willemsen85e3d012016-10-26 19:20:58 -070033 "-mmacosx-version-min=${macMinVersion}",
34 "-DMACOSX_DEPLOYMENT_TARGET=${macMinVersion}",
Dan Willemsene97e68a2018-08-28 17:12:56 -070035
36 "-m64",
Colin Cross33bac242021-07-14 17:03:16 -070037
38 "-integrated-as",
39 "-fstack-protector-strong",
Colin Cross0af4b842015-04-30 16:36:18 -070040 }
41
Colin Cross0af4b842015-04-30 16:36:18 -070042 darwinLdflags = []string{
43 "-isysroot ${macSdkRoot}",
44 "-Wl,-syslibroot,${macSdkRoot}",
Dan Willemsen85e3d012016-10-26 19:20:58 -070045 "-mmacosx-version-min=${macMinVersion}",
Colin Cross0af4b842015-04-30 16:36:18 -070046 "-m64",
Yi Kongf8e53702021-07-23 15:34:34 +080047 "-mlinker-version=305",
Colin Cross0af4b842015-04-30 16:36:18 -070048 }
49
Dan Willemsencf7c71b2015-12-14 20:02:44 -080050 darwinSupportedSdkVersions = []string{
Dan Willemsen7368d862021-10-19 21:37:15 -070051 "11",
Dan Willemsene3d50bf2021-12-07 20:19:58 -080052 "12",
Dan Willemsencf7c71b2015-12-14 20:02:44 -080053 }
Dan Willemsen20acc5c2016-05-25 14:47:21 -070054
Josh Gao3923e842016-09-29 14:22:54 -070055 darwinAvailableLibraries = append(
56 addPrefix([]string{
57 "c",
58 "dl",
59 "m",
60 "ncurses",
61 "objc",
62 "pthread",
Josh Gao3923e842016-09-29 14:22:54 -070063 }, "-l"),
Colin Cross124fd9a2016-11-21 17:31:08 -080064 "-framework AppKit",
Josh Gao3923e842016-09-29 14:22:54 -070065 "-framework CoreFoundation",
Colin Cross124fd9a2016-11-21 17:31:08 -080066 "-framework Foundation",
Josh Gao3923e842016-09-29 14:22:54 -070067 "-framework IOKit",
Colin Cross124fd9a2016-11-21 17:31:08 -080068 "-framework Security",
Dan Willemsen97d017e2019-02-20 10:28:56 -080069 "-framework SystemConfiguration",
Josh Gao3923e842016-09-29 14:22:54 -070070 )
Colin Cross0af4b842015-04-30 16:36:18 -070071)
72
73func init() {
Dan Willemsen54daaf02018-03-12 13:24:09 -070074 pctx.VariableFunc("macSdkRoot", func(ctx android.PackageVarContext) string {
Dan Willemsen6ba53672020-04-01 14:32:52 -070075 return getMacTools(ctx).sdkRoot
Dan Willemsencf7c71b2015-12-14 20:02:44 -080076 })
Dan Willemsen876b12f2023-11-08 01:10:51 +000077 pctx.StaticVariable("macMinVersion", "10.14")
Dan Willemsen54daaf02018-03-12 13:24:09 -070078 pctx.VariableFunc("MacArPath", func(ctx android.PackageVarContext) string {
Dan Willemsen6ba53672020-04-01 14:32:52 -070079 return getMacTools(ctx).arPath
Dan Willemsencf7c71b2015-12-14 20:02:44 -080080 })
Colin Cross0af4b842015-04-30 16:36:18 -070081
Dan Willemsen47450072021-10-19 20:24:49 -070082 pctx.VariableFunc("MacLipoPath", func(ctx android.PackageVarContext) string {
83 return getMacTools(ctx).lipoPath
84 })
85
Dan Willemsen54daaf02018-03-12 13:24:09 -070086 pctx.VariableFunc("MacStripPath", func(ctx android.PackageVarContext) string {
Dan Willemsen6ba53672020-04-01 14:32:52 -070087 return getMacTools(ctx).stripPath
Colin Crossb8ecdfe2016-05-03 15:10:29 -070088 })
89
Dan Willemsen54daaf02018-03-12 13:24:09 -070090 pctx.VariableFunc("MacToolPath", func(ctx android.PackageVarContext) string {
Dan Willemsen6ba53672020-04-01 14:32:52 -070091 return getMacTools(ctx).toolPath
Dan Willemsen300151b2017-03-13 12:40:30 -070092 })
93
Colin Cross0523ba22021-07-14 18:45:05 -070094 pctx.StaticVariable("DarwinCflags", strings.Join(darwinCflags, " "))
95 pctx.StaticVariable("DarwinLdflags", strings.Join(darwinLdflags, " "))
96 pctx.StaticVariable("DarwinLldflags", strings.Join(darwinLdflags, " "))
Dan Willemsen282a4b02016-03-09 10:30:22 -080097
Dan Willemsene97e68a2018-08-28 17:12:56 -070098 pctx.StaticVariable("DarwinYasmFlags", "-f macho -m amd64")
Colin Cross0af4b842015-04-30 16:36:18 -070099}
100
Dan Willemsen9fe14102021-07-13 21:52:04 -0700101func MacStripPath(ctx android.PathContext) string {
102 return getMacTools(ctx).stripPath
103}
104
Dan Willemsen6ba53672020-04-01 14:32:52 -0700105type macPlatformTools struct {
106 once sync.Once
107 err error
108
109 sdkRoot string
110 arPath string
Dan Willemsen47450072021-10-19 20:24:49 -0700111 lipoPath string
Dan Willemsen6ba53672020-04-01 14:32:52 -0700112 stripPath string
113 toolPath string
Dan Willemsen66068722017-05-08 21:15:59 +0000114}
115
Dan Willemsen6ba53672020-04-01 14:32:52 -0700116var macTools = &macPlatformTools{}
117
Dan Willemsen9fe14102021-07-13 21:52:04 -0700118func getMacTools(ctx android.PathContext) *macPlatformTools {
Dan Willemsen6ba53672020-04-01 14:32:52 -0700119 macTools.once.Do(func() {
Lukacs T. Berki21d5c7a2021-03-11 08:52:39 +0100120 xcrunTool := "/usr/bin/xcrun"
Dan Willemsen6ba53672020-04-01 14:32:52 -0700121
122 xcrun := func(args ...string) string {
123 if macTools.err != nil {
124 return ""
125 }
126
Dan Willemsen7368d862021-10-19 21:37:15 -0700127 bytes, err := exec.Command(xcrunTool, append([]string{"--sdk", "macosx"}, args...)...).Output()
Dan Willemsen6ba53672020-04-01 14:32:52 -0700128 if err != nil {
129 macTools.err = fmt.Errorf("xcrun %q failed with: %q", args, err)
130 return ""
131 }
132
133 return strings.TrimSpace(string(bytes))
134 }
135
Dan Willemsen7368d862021-10-19 21:37:15 -0700136 sdkVersion := xcrun("--show-sdk-version")
137 sdkVersionSupported := false
138 for _, version := range darwinSupportedSdkVersions {
139 if version == sdkVersion || strings.HasPrefix(sdkVersion, version+".") {
140 sdkVersionSupported = true
Dan Willemsen6ba53672020-04-01 14:32:52 -0700141 }
Dan Willemsen7368d862021-10-19 21:37:15 -0700142 }
143 if !sdkVersionSupported {
144 macTools.err = fmt.Errorf("Unsupported macOS SDK version %q not in %v", sdkVersion, darwinSupportedSdkVersions)
145 return
Dan Willemsencf7c71b2015-12-14 20:02:44 -0800146 }
147
Dan Willemsen7368d862021-10-19 21:37:15 -0700148 macTools.sdkRoot = xcrun("--show-sdk-path")
Dan Willemsencf7c71b2015-12-14 20:02:44 -0800149
Dan Willemsen6ba53672020-04-01 14:32:52 -0700150 macTools.arPath = xcrun("--find", "ar")
Dan Willemsen47450072021-10-19 20:24:49 -0700151 macTools.lipoPath = xcrun("--find", "lipo")
Dan Willemsen6ba53672020-04-01 14:32:52 -0700152 macTools.stripPath = xcrun("--find", "strip")
153 macTools.toolPath = filepath.Dir(xcrun("--find", "ld"))
154 })
155 if macTools.err != nil {
Dan Willemsen9fe14102021-07-13 21:52:04 -0700156 android.ReportPathErrorf(ctx, "%q", macTools.err)
Dan Willemsencf7c71b2015-12-14 20:02:44 -0800157 }
Dan Willemsen6ba53672020-04-01 14:32:52 -0700158 return macTools
Dan Willemsencf7c71b2015-12-14 20:02:44 -0800159}
160
Colin Cross0af4b842015-04-30 16:36:18 -0700161type toolchainDarwin struct {
162 cFlags, ldFlags string
Colin Cross0af4b842015-04-30 16:36:18 -0700163 toolchain64Bit
Colin Crossfc3b0642022-09-01 11:02:15 -0700164 toolchainNoCrt
165 toolchainBase
Colin Cross0af4b842015-04-30 16:36:18 -0700166}
167
Dan Willemsen8528f4e2021-10-19 00:22:06 -0700168type toolchainDarwinX86 struct {
169 toolchainDarwin
170}
171
172type toolchainDarwinArm struct {
173 toolchainDarwin
174}
175
176func (t *toolchainDarwinArm) Name() string {
177 return "arm64"
178}
179
180func (t *toolchainDarwinX86) Name() string {
Colin Cross0af4b842015-04-30 16:36:18 -0700181 return "x86_64"
182}
183
Colin Cross0af4b842015-04-30 16:36:18 -0700184func (t *toolchainDarwin) IncludeFlags() string {
185 return ""
186}
187
Dan Willemsen8528f4e2021-10-19 00:22:06 -0700188func (t *toolchainDarwinArm) ClangTriple() string {
189 return "aarch64-apple-darwin"
190}
191
192func (t *toolchainDarwinX86) ClangTriple() string {
Dan Willemsen01fdd3d2016-03-30 00:01:12 -0700193 return "x86_64-apple-darwin"
Colin Cross0af4b842015-04-30 16:36:18 -0700194}
195
Colin Cross33bac242021-07-14 17:03:16 -0700196func (t *toolchainDarwin) Cflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700197 return "${config.DarwinCflags}"
Colin Cross0af4b842015-04-30 16:36:18 -0700198}
199
Colin Cross33bac242021-07-14 17:03:16 -0700200func (t *toolchainDarwin) Cppflags() string {
Dan Willemsen282a4b02016-03-09 10:30:22 -0800201 return ""
Colin Cross0af4b842015-04-30 16:36:18 -0700202}
203
Colin Cross33bac242021-07-14 17:03:16 -0700204func (t *toolchainDarwin) Ldflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700205 return "${config.DarwinLdflags}"
Colin Cross0af4b842015-04-30 16:36:18 -0700206}
207
Colin Cross33bac242021-07-14 17:03:16 -0700208func (t *toolchainDarwin) Lldflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700209 return "${config.DarwinLldflags}"
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700210}
211
Dan Willemsene97e68a2018-08-28 17:12:56 -0700212func (t *toolchainDarwin) YasmFlags() string {
213 return "${config.DarwinYasmFlags}"
Greg Hartman09302be2017-10-04 17:31:43 -0700214}
215
Dan Willemsen490fd492015-11-24 17:53:15 -0800216func (t *toolchainDarwin) ShlibSuffix() string {
217 return ".dylib"
218}
219
Colin Crossfc3b0642022-09-01 11:02:15 -0700220func (t *toolchainDarwin) ExecutableSuffix() string {
221 return ""
222}
223
Dan Willemsen20acc5c2016-05-25 14:47:21 -0700224func (t *toolchainDarwin) AvailableLibraries() []string {
225 return darwinAvailableLibraries
226}
227
Chih-Hung Hsieh57da8262022-02-15 22:48:04 -0800228func (t *toolchainDarwin) ToolchainCflags() string {
229 return "-B${config.MacToolPath}"
230}
231
232func (t *toolchainDarwin) ToolchainLdflags() string {
233 return "-B${config.MacToolPath}"
Dan Willemsen300151b2017-03-13 12:40:30 -0700234}
235
Dan Willemsen8528f4e2021-10-19 00:22:06 -0700236var toolchainDarwinArmSingleton Toolchain = &toolchainDarwinArm{}
237var toolchainDarwinX86Singleton Toolchain = &toolchainDarwinX86{}
Colin Cross0af4b842015-04-30 16:36:18 -0700238
Dan Willemsen8528f4e2021-10-19 00:22:06 -0700239func darwinArmToolchainFactory(arch android.Arch) Toolchain {
240 return toolchainDarwinArmSingleton
241}
242
243func darwinX86ToolchainFactory(arch android.Arch) Toolchain {
244 return toolchainDarwinX86Singleton
Colin Cross0af4b842015-04-30 16:36:18 -0700245}
246
247func init() {
Dan Willemsen8528f4e2021-10-19 00:22:06 -0700248 registerToolchainFactory(android.Darwin, android.Arm64, darwinArmToolchainFactory)
249 registerToolchainFactory(android.Darwin, android.X86_64, darwinX86ToolchainFactory)
Colin Cross0af4b842015-04-30 16:36:18 -0700250}