Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 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 | |
| 15 | package config |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 16 | |
| 17 | import ( |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 18 | "fmt" |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 19 | "os/exec" |
Dan Willemsen | 300151b | 2017-03-13 12:40:30 -0700 | [diff] [blame] | 20 | "path/filepath" |
Yu Liu | 2c03276 | 2024-07-24 19:27:51 +0000 | [diff] [blame] | 21 | "runtime" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 22 | "strings" |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 23 | "sync" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 24 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 25 | "android/soong/android" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | var ( |
| 29 | darwinCflags = []string{ |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 30 | "-fPIC", |
| 31 | "-funwind-tables", |
Elliott Hughes | 67612c3 | 2024-08-20 15:07:30 +0000 | [diff] [blame] | 32 | "-fno-omit-frame-pointer", |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 33 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 34 | "-isysroot ${macSdkRoot}", |
Dan Willemsen | 85e3d01 | 2016-10-26 19:20:58 -0700 | [diff] [blame] | 35 | "-mmacosx-version-min=${macMinVersion}", |
| 36 | "-DMACOSX_DEPLOYMENT_TARGET=${macMinVersion}", |
Dan Willemsen | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 37 | |
| 38 | "-m64", |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 39 | |
| 40 | "-integrated-as", |
| 41 | "-fstack-protector-strong", |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 44 | darwinLdflags = []string{ |
| 45 | "-isysroot ${macSdkRoot}", |
| 46 | "-Wl,-syslibroot,${macSdkRoot}", |
Dan Willemsen | 85e3d01 | 2016-10-26 19:20:58 -0700 | [diff] [blame] | 47 | "-mmacosx-version-min=${macMinVersion}", |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 48 | "-m64", |
Yi Kong | f8e5370 | 2021-07-23 15:34:34 +0800 | [diff] [blame] | 49 | "-mlinker-version=305", |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 52 | darwinSupportedSdkVersions = []string{ |
Dan Willemsen | 7368d86 | 2021-10-19 21:37:15 -0700 | [diff] [blame] | 53 | "11", |
Dan Willemsen | e3d50bf | 2021-12-07 20:19:58 -0800 | [diff] [blame] | 54 | "12", |
Fabien Sanglard | 7166669 | 2024-05-30 09:29:14 +0200 | [diff] [blame] | 55 | "13", |
| 56 | "14", |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 57 | } |
Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 58 | |
Josh Gao | 3923e84 | 2016-09-29 14:22:54 -0700 | [diff] [blame] | 59 | darwinAvailableLibraries = append( |
| 60 | addPrefix([]string{ |
| 61 | "c", |
| 62 | "dl", |
| 63 | "m", |
| 64 | "ncurses", |
| 65 | "objc", |
| 66 | "pthread", |
Josh Gao | 3923e84 | 2016-09-29 14:22:54 -0700 | [diff] [blame] | 67 | }, "-l"), |
Colin Cross | 124fd9a | 2016-11-21 17:31:08 -0800 | [diff] [blame] | 68 | "-framework AppKit", |
Josh Gao | 3923e84 | 2016-09-29 14:22:54 -0700 | [diff] [blame] | 69 | "-framework CoreFoundation", |
Colin Cross | 124fd9a | 2016-11-21 17:31:08 -0800 | [diff] [blame] | 70 | "-framework Foundation", |
Josh Gao | 3923e84 | 2016-09-29 14:22:54 -0700 | [diff] [blame] | 71 | "-framework IOKit", |
Colin Cross | 124fd9a | 2016-11-21 17:31:08 -0800 | [diff] [blame] | 72 | "-framework Security", |
Dan Willemsen | 97d017e | 2019-02-20 10:28:56 -0800 | [diff] [blame] | 73 | "-framework SystemConfiguration", |
Josh Gao | 3923e84 | 2016-09-29 14:22:54 -0700 | [diff] [blame] | 74 | ) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 75 | ) |
| 76 | |
| 77 | func init() { |
Yu Liu | 2c03276 | 2024-07-24 19:27:51 +0000 | [diff] [blame] | 78 | if runtime.GOOS == "darwin" { |
| 79 | pctx.VariableFunc("macSdkRoot", func(ctx android.PackageVarContext) string { |
| 80 | return getMacTools(ctx).sdkRoot |
| 81 | }) |
| 82 | pctx.StaticVariable("macMinVersion", "10.14") |
| 83 | pctx.VariableFunc("MacArPath", func(ctx android.PackageVarContext) string { |
| 84 | return getMacTools(ctx).arPath |
| 85 | }) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 86 | |
Yu Liu | 2c03276 | 2024-07-24 19:27:51 +0000 | [diff] [blame] | 87 | pctx.VariableFunc("MacLipoPath", func(ctx android.PackageVarContext) string { |
| 88 | return getMacTools(ctx).lipoPath |
| 89 | }) |
Dan Willemsen | 4745007 | 2021-10-19 20:24:49 -0700 | [diff] [blame] | 90 | |
Yu Liu | 2c03276 | 2024-07-24 19:27:51 +0000 | [diff] [blame] | 91 | pctx.VariableFunc("MacStripPath", func(ctx android.PackageVarContext) string { |
| 92 | return getMacTools(ctx).stripPath |
| 93 | }) |
Colin Cross | b8ecdfe | 2016-05-03 15:10:29 -0700 | [diff] [blame] | 94 | |
Yu Liu | 2c03276 | 2024-07-24 19:27:51 +0000 | [diff] [blame] | 95 | pctx.VariableFunc("MacToolPath", func(ctx android.PackageVarContext) string { |
| 96 | return getMacTools(ctx).toolPath |
| 97 | }) |
Dan Willemsen | 300151b | 2017-03-13 12:40:30 -0700 | [diff] [blame] | 98 | |
Yu Liu | 2c03276 | 2024-07-24 19:27:51 +0000 | [diff] [blame] | 99 | pctx.StaticVariable("DarwinCflags", strings.Join(darwinCflags, " ")) |
| 100 | pctx.StaticVariable("DarwinLdflags", strings.Join(darwinLdflags, " ")) |
| 101 | pctx.StaticVariable("DarwinLldflags", strings.Join(darwinLdflags, " ")) |
Dan Willemsen | 282a4b0 | 2016-03-09 10:30:22 -0800 | [diff] [blame] | 102 | |
Yu Liu | 2c03276 | 2024-07-24 19:27:51 +0000 | [diff] [blame] | 103 | pctx.StaticVariable("DarwinYasmFlags", "-f macho -m amd64") |
| 104 | } |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 107 | func MacStripPath(ctx android.PathContext) string { |
| 108 | return getMacTools(ctx).stripPath |
| 109 | } |
| 110 | |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 111 | type macPlatformTools struct { |
| 112 | once sync.Once |
| 113 | err error |
| 114 | |
| 115 | sdkRoot string |
| 116 | arPath string |
Dan Willemsen | 4745007 | 2021-10-19 20:24:49 -0700 | [diff] [blame] | 117 | lipoPath string |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 118 | stripPath string |
| 119 | toolPath string |
Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 122 | var macTools = &macPlatformTools{} |
| 123 | |
Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 124 | func getMacTools(ctx android.PathContext) *macPlatformTools { |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 125 | macTools.once.Do(func() { |
Lukacs T. Berki | 21d5c7a | 2021-03-11 08:52:39 +0100 | [diff] [blame] | 126 | xcrunTool := "/usr/bin/xcrun" |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 127 | |
| 128 | xcrun := func(args ...string) string { |
| 129 | if macTools.err != nil { |
| 130 | return "" |
| 131 | } |
| 132 | |
Dan Willemsen | 7368d86 | 2021-10-19 21:37:15 -0700 | [diff] [blame] | 133 | bytes, err := exec.Command(xcrunTool, append([]string{"--sdk", "macosx"}, args...)...).Output() |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 134 | if err != nil { |
| 135 | macTools.err = fmt.Errorf("xcrun %q failed with: %q", args, err) |
| 136 | return "" |
| 137 | } |
| 138 | |
| 139 | return strings.TrimSpace(string(bytes)) |
| 140 | } |
| 141 | |
Dan Willemsen | 7368d86 | 2021-10-19 21:37:15 -0700 | [diff] [blame] | 142 | sdkVersion := xcrun("--show-sdk-version") |
| 143 | sdkVersionSupported := false |
| 144 | for _, version := range darwinSupportedSdkVersions { |
| 145 | if version == sdkVersion || strings.HasPrefix(sdkVersion, version+".") { |
| 146 | sdkVersionSupported = true |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 147 | } |
Dan Willemsen | 7368d86 | 2021-10-19 21:37:15 -0700 | [diff] [blame] | 148 | } |
| 149 | if !sdkVersionSupported { |
| 150 | macTools.err = fmt.Errorf("Unsupported macOS SDK version %q not in %v", sdkVersion, darwinSupportedSdkVersions) |
| 151 | return |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Dan Willemsen | 7368d86 | 2021-10-19 21:37:15 -0700 | [diff] [blame] | 154 | macTools.sdkRoot = xcrun("--show-sdk-path") |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 155 | |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 156 | macTools.arPath = xcrun("--find", "ar") |
Dan Willemsen | 4745007 | 2021-10-19 20:24:49 -0700 | [diff] [blame] | 157 | macTools.lipoPath = xcrun("--find", "lipo") |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 158 | macTools.stripPath = xcrun("--find", "strip") |
| 159 | macTools.toolPath = filepath.Dir(xcrun("--find", "ld")) |
| 160 | }) |
| 161 | if macTools.err != nil { |
Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 162 | android.ReportPathErrorf(ctx, "%q", macTools.err) |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 163 | } |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 164 | return macTools |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 167 | type toolchainDarwin struct { |
| 168 | cFlags, ldFlags string |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 169 | toolchain64Bit |
Colin Cross | fc3b064 | 2022-09-01 11:02:15 -0700 | [diff] [blame] | 170 | toolchainNoCrt |
| 171 | toolchainBase |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Dan Willemsen | 8528f4e | 2021-10-19 00:22:06 -0700 | [diff] [blame] | 174 | type toolchainDarwinX86 struct { |
| 175 | toolchainDarwin |
| 176 | } |
| 177 | |
| 178 | type toolchainDarwinArm struct { |
| 179 | toolchainDarwin |
| 180 | } |
| 181 | |
| 182 | func (t *toolchainDarwinArm) Name() string { |
| 183 | return "arm64" |
| 184 | } |
| 185 | |
| 186 | func (t *toolchainDarwinX86) Name() string { |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 187 | return "x86_64" |
| 188 | } |
| 189 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 190 | func (t *toolchainDarwin) IncludeFlags() string { |
| 191 | return "" |
| 192 | } |
| 193 | |
Dan Willemsen | 8528f4e | 2021-10-19 00:22:06 -0700 | [diff] [blame] | 194 | func (t *toolchainDarwinArm) ClangTriple() string { |
| 195 | return "aarch64-apple-darwin" |
| 196 | } |
| 197 | |
| 198 | func (t *toolchainDarwinX86) ClangTriple() string { |
Dan Willemsen | 01fdd3d | 2016-03-30 00:01:12 -0700 | [diff] [blame] | 199 | return "x86_64-apple-darwin" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 202 | func (t *toolchainDarwin) Cflags() string { |
Colin Cross | 0523ba2 | 2021-07-14 18:45:05 -0700 | [diff] [blame] | 203 | return "${config.DarwinCflags}" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 206 | func (t *toolchainDarwin) Cppflags() string { |
Dan Willemsen | 282a4b0 | 2016-03-09 10:30:22 -0800 | [diff] [blame] | 207 | return "" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 210 | func (t *toolchainDarwin) Ldflags() string { |
Colin Cross | 0523ba2 | 2021-07-14 18:45:05 -0700 | [diff] [blame] | 211 | return "${config.DarwinLdflags}" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 214 | func (t *toolchainDarwin) Lldflags() string { |
Colin Cross | 0523ba2 | 2021-07-14 18:45:05 -0700 | [diff] [blame] | 215 | return "${config.DarwinLldflags}" |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Dan Willemsen | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 218 | func (t *toolchainDarwin) YasmFlags() string { |
| 219 | return "${config.DarwinYasmFlags}" |
Greg Hartman | 09302be | 2017-10-04 17:31:43 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 222 | func (t *toolchainDarwin) ShlibSuffix() string { |
| 223 | return ".dylib" |
| 224 | } |
| 225 | |
Colin Cross | fc3b064 | 2022-09-01 11:02:15 -0700 | [diff] [blame] | 226 | func (t *toolchainDarwin) ExecutableSuffix() string { |
| 227 | return "" |
| 228 | } |
| 229 | |
Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 230 | func (t *toolchainDarwin) AvailableLibraries() []string { |
| 231 | return darwinAvailableLibraries |
| 232 | } |
| 233 | |
Chih-Hung Hsieh | 57da826 | 2022-02-15 22:48:04 -0800 | [diff] [blame] | 234 | func (t *toolchainDarwin) ToolchainCflags() string { |
| 235 | return "-B${config.MacToolPath}" |
| 236 | } |
| 237 | |
| 238 | func (t *toolchainDarwin) ToolchainLdflags() string { |
| 239 | return "-B${config.MacToolPath}" |
Dan Willemsen | 300151b | 2017-03-13 12:40:30 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Dan Willemsen | 8528f4e | 2021-10-19 00:22:06 -0700 | [diff] [blame] | 242 | var toolchainDarwinArmSingleton Toolchain = &toolchainDarwinArm{} |
| 243 | var toolchainDarwinX86Singleton Toolchain = &toolchainDarwinX86{} |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 244 | |
Dan Willemsen | 8528f4e | 2021-10-19 00:22:06 -0700 | [diff] [blame] | 245 | func darwinArmToolchainFactory(arch android.Arch) Toolchain { |
| 246 | return toolchainDarwinArmSingleton |
| 247 | } |
| 248 | |
| 249 | func darwinX86ToolchainFactory(arch android.Arch) Toolchain { |
| 250 | return toolchainDarwinX86Singleton |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | func init() { |
Dan Willemsen | 8528f4e | 2021-10-19 00:22:06 -0700 | [diff] [blame] | 254 | registerToolchainFactory(android.Darwin, android.Arm64, darwinArmToolchainFactory) |
| 255 | registerToolchainFactory(android.Darwin, android.X86_64, darwinX86ToolchainFactory) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 256 | } |