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