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 | |
| 32 | // Workaround differences in inttypes.h between host and target. |
| 33 | //See bug 12708004. |
| 34 | "-D__STDC_FORMAT_MACROS", |
| 35 | "-D__STDC_CONSTANT_MACROS", |
| 36 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 37 | "-isysroot ${macSdkRoot}", |
Dan Willemsen | 85e3d01 | 2016-10-26 19:20:58 -0700 | [diff] [blame] | 38 | "-mmacosx-version-min=${macMinVersion}", |
| 39 | "-DMACOSX_DEPLOYMENT_TARGET=${macMinVersion}", |
Dan Willemsen | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 40 | |
| 41 | "-m64", |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 42 | |
| 43 | "-integrated-as", |
| 44 | "-fstack-protector-strong", |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 47 | darwinLdflags = []string{ |
| 48 | "-isysroot ${macSdkRoot}", |
| 49 | "-Wl,-syslibroot,${macSdkRoot}", |
Dan Willemsen | 85e3d01 | 2016-10-26 19:20:58 -0700 | [diff] [blame] | 50 | "-mmacosx-version-min=${macMinVersion}", |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 51 | "-m64", |
Yi Kong | f8e5370 | 2021-07-23 15:34:34 +0800 | [diff] [blame] | 52 | "-mlinker-version=305", |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 55 | darwinSupportedSdkVersions = []string{ |
Dan Willemsen | 7368d86 | 2021-10-19 21:37:15 -0700 | [diff] [blame] | 56 | "11", |
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 | |
Dan Willemsen | 34fc3b1 | 2015-12-07 12:30:44 -0800 | [diff] [blame] | 77 | const ( |
| 78 | darwinGccVersion = "4.2.1" |
| 79 | ) |
| 80 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 81 | func init() { |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 82 | pctx.VariableFunc("macSdkRoot", func(ctx android.PackageVarContext) string { |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 83 | return getMacTools(ctx).sdkRoot |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 84 | }) |
Dan Willemsen | 3dc4326 | 2021-07-29 12:16:14 -0700 | [diff] [blame] | 85 | pctx.StaticVariable("macMinVersion", "10.13") |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 86 | pctx.VariableFunc("MacArPath", func(ctx android.PackageVarContext) string { |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 87 | return getMacTools(ctx).arPath |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 88 | }) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 89 | |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 90 | pctx.VariableFunc("MacStripPath", func(ctx android.PackageVarContext) string { |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 91 | return getMacTools(ctx).stripPath |
Colin Cross | b8ecdfe | 2016-05-03 15:10:29 -0700 | [diff] [blame] | 92 | }) |
| 93 | |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 94 | pctx.VariableFunc("MacToolPath", func(ctx android.PackageVarContext) string { |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 95 | return getMacTools(ctx).toolPath |
Dan Willemsen | 300151b | 2017-03-13 12:40:30 -0700 | [diff] [blame] | 96 | }) |
| 97 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 98 | pctx.StaticVariable("DarwinGccVersion", darwinGccVersion) |
| 99 | pctx.SourcePathVariable("DarwinGccRoot", |
| 100 | "prebuilts/gcc/${HostPrebuiltTag}/host/i686-apple-darwin-${DarwinGccVersion}") |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 101 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 102 | pctx.StaticVariable("DarwinGccTriple", "i686-apple-darwin11") |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 103 | |
Colin Cross | 0523ba2 | 2021-07-14 18:45:05 -0700 | [diff] [blame] | 104 | pctx.StaticVariable("DarwinCflags", strings.Join(darwinCflags, " ")) |
| 105 | pctx.StaticVariable("DarwinLdflags", strings.Join(darwinLdflags, " ")) |
| 106 | pctx.StaticVariable("DarwinLldflags", strings.Join(darwinLdflags, " ")) |
Dan Willemsen | 282a4b0 | 2016-03-09 10:30:22 -0800 | [diff] [blame] | 107 | |
Dan Willemsen | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 108 | pctx.StaticVariable("DarwinYasmFlags", "-f macho -m amd64") |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 111 | func MacStripPath(ctx android.PathContext) string { |
| 112 | return getMacTools(ctx).stripPath |
| 113 | } |
| 114 | |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 115 | type macPlatformTools struct { |
| 116 | once sync.Once |
| 117 | err error |
| 118 | |
| 119 | sdkRoot string |
| 120 | arPath string |
| 121 | stripPath string |
| 122 | toolPath string |
Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 125 | var macTools = &macPlatformTools{} |
| 126 | |
Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 127 | func getMacTools(ctx android.PathContext) *macPlatformTools { |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 128 | macTools.once.Do(func() { |
Lukacs T. Berki | 21d5c7a | 2021-03-11 08:52:39 +0100 | [diff] [blame] | 129 | xcrunTool := "/usr/bin/xcrun" |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 130 | |
| 131 | xcrun := func(args ...string) string { |
| 132 | if macTools.err != nil { |
| 133 | return "" |
| 134 | } |
| 135 | |
Dan Willemsen | 7368d86 | 2021-10-19 21:37:15 -0700 | [diff] [blame] | 136 | bytes, err := exec.Command(xcrunTool, append([]string{"--sdk", "macosx"}, args...)...).Output() |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 137 | if err != nil { |
| 138 | macTools.err = fmt.Errorf("xcrun %q failed with: %q", args, err) |
| 139 | return "" |
| 140 | } |
| 141 | |
| 142 | return strings.TrimSpace(string(bytes)) |
| 143 | } |
| 144 | |
Dan Willemsen | 7368d86 | 2021-10-19 21:37:15 -0700 | [diff] [blame] | 145 | sdkVersion := xcrun("--show-sdk-version") |
| 146 | sdkVersionSupported := false |
| 147 | for _, version := range darwinSupportedSdkVersions { |
| 148 | if version == sdkVersion || strings.HasPrefix(sdkVersion, version+".") { |
| 149 | sdkVersionSupported = true |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 150 | } |
Dan Willemsen | 7368d86 | 2021-10-19 21:37:15 -0700 | [diff] [blame] | 151 | } |
| 152 | if !sdkVersionSupported { |
| 153 | macTools.err = fmt.Errorf("Unsupported macOS SDK version %q not in %v", sdkVersion, darwinSupportedSdkVersions) |
| 154 | return |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Dan Willemsen | 7368d86 | 2021-10-19 21:37:15 -0700 | [diff] [blame] | 157 | macTools.sdkRoot = xcrun("--show-sdk-path") |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 158 | |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 159 | macTools.arPath = xcrun("--find", "ar") |
| 160 | macTools.stripPath = xcrun("--find", "strip") |
| 161 | macTools.toolPath = filepath.Dir(xcrun("--find", "ld")) |
| 162 | }) |
| 163 | if macTools.err != nil { |
Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 164 | android.ReportPathErrorf(ctx, "%q", macTools.err) |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 165 | } |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 166 | return macTools |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 169 | type toolchainDarwin struct { |
| 170 | cFlags, ldFlags string |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 171 | toolchain64Bit |
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 | |
Dan Willemsen | 8528f4e | 2021-10-19 00:22:06 -0700 | [diff] [blame] | 190 | func (t *toolchainDarwinArm) GccRoot() string { |
| 191 | panic("unimplemented") |
| 192 | } |
| 193 | |
| 194 | func (t *toolchainDarwinArm) GccTriple() string { |
| 195 | panic("unimplemented") |
| 196 | } |
| 197 | |
| 198 | func (t *toolchainDarwinArm) GccVersion() string { |
| 199 | panic("unimplemented") |
| 200 | } |
| 201 | |
| 202 | func (t *toolchainDarwinX86) GccRoot() string { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 203 | return "${config.DarwinGccRoot}" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Dan Willemsen | 8528f4e | 2021-10-19 00:22:06 -0700 | [diff] [blame] | 206 | func (t *toolchainDarwinX86) GccTriple() string { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 207 | return "${config.DarwinGccTriple}" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Dan Willemsen | 8528f4e | 2021-10-19 00:22:06 -0700 | [diff] [blame] | 210 | func (t *toolchainDarwinX86) GccVersion() string { |
Dan Willemsen | 34fc3b1 | 2015-12-07 12:30:44 -0800 | [diff] [blame] | 211 | return darwinGccVersion |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 214 | func (t *toolchainDarwin) IncludeFlags() string { |
| 215 | return "" |
| 216 | } |
| 217 | |
Dan Willemsen | 8528f4e | 2021-10-19 00:22:06 -0700 | [diff] [blame] | 218 | func (t *toolchainDarwinArm) ClangTriple() string { |
| 219 | return "aarch64-apple-darwin" |
| 220 | } |
| 221 | |
| 222 | func (t *toolchainDarwinX86) ClangTriple() string { |
Dan Willemsen | 01fdd3d | 2016-03-30 00:01:12 -0700 | [diff] [blame] | 223 | return "x86_64-apple-darwin" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 226 | func (t *toolchainDarwin) Cflags() string { |
Colin Cross | 0523ba2 | 2021-07-14 18:45:05 -0700 | [diff] [blame] | 227 | return "${config.DarwinCflags}" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 230 | func (t *toolchainDarwin) Cppflags() string { |
Dan Willemsen | 282a4b0 | 2016-03-09 10:30:22 -0800 | [diff] [blame] | 231 | return "" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 234 | func (t *toolchainDarwin) Ldflags() string { |
Colin Cross | 0523ba2 | 2021-07-14 18:45:05 -0700 | [diff] [blame] | 235 | return "${config.DarwinLdflags}" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 238 | func (t *toolchainDarwin) Lldflags() string { |
Colin Cross | 0523ba2 | 2021-07-14 18:45:05 -0700 | [diff] [blame] | 239 | return "${config.DarwinLldflags}" |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Dan Willemsen | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 242 | func (t *toolchainDarwin) YasmFlags() string { |
| 243 | return "${config.DarwinYasmFlags}" |
Greg Hartman | 09302be | 2017-10-04 17:31:43 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 246 | func (t *toolchainDarwin) ShlibSuffix() string { |
| 247 | return ".dylib" |
| 248 | } |
| 249 | |
Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 250 | func (t *toolchainDarwin) AvailableLibraries() []string { |
| 251 | return darwinAvailableLibraries |
| 252 | } |
| 253 | |
Dan Willemsen | 300151b | 2017-03-13 12:40:30 -0700 | [diff] [blame] | 254 | func (t *toolchainDarwin) ToolPath() string { |
| 255 | return "${config.MacToolPath}" |
| 256 | } |
| 257 | |
Dan Willemsen | 8528f4e | 2021-10-19 00:22:06 -0700 | [diff] [blame] | 258 | var toolchainDarwinArmSingleton Toolchain = &toolchainDarwinArm{} |
| 259 | var toolchainDarwinX86Singleton Toolchain = &toolchainDarwinX86{} |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 260 | |
Dan Willemsen | 8528f4e | 2021-10-19 00:22:06 -0700 | [diff] [blame] | 261 | func darwinArmToolchainFactory(arch android.Arch) Toolchain { |
| 262 | return toolchainDarwinArmSingleton |
| 263 | } |
| 264 | |
| 265 | func darwinX86ToolchainFactory(arch android.Arch) Toolchain { |
| 266 | return toolchainDarwinX86Singleton |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | func init() { |
Dan Willemsen | 8528f4e | 2021-10-19 00:22:06 -0700 | [diff] [blame] | 270 | registerToolchainFactory(android.Darwin, android.Arm64, darwinArmToolchainFactory) |
| 271 | registerToolchainFactory(android.Darwin, android.X86_64, darwinX86ToolchainFactory) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 272 | } |