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{ |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 29 | "-fdiagnostics-color", |
| 30 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 31 | "-fPIC", |
| 32 | "-funwind-tables", |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 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 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 39 | "-isysroot ${macSdkRoot}", |
Dan Willemsen | 85e3d01 | 2016-10-26 19:20:58 -0700 | [diff] [blame] | 40 | "-mmacosx-version-min=${macMinVersion}", |
| 41 | "-DMACOSX_DEPLOYMENT_TARGET=${macMinVersion}", |
Dan Willemsen | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 42 | |
| 43 | "-m64", |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 46 | darwinLdflags = []string{ |
| 47 | "-isysroot ${macSdkRoot}", |
| 48 | "-Wl,-syslibroot,${macSdkRoot}", |
Dan Willemsen | 85e3d01 | 2016-10-26 19:20:58 -0700 | [diff] [blame] | 49 | "-mmacosx-version-min=${macMinVersion}", |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 50 | "-m64", |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 53 | darwinClangCflags = append(ClangFilterUnknownCflags(darwinCflags), []string{ |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 54 | "-integrated-as", |
Dan Willemsen | 7a0f848 | 2016-01-12 16:22:40 -0800 | [diff] [blame] | 55 | "-fstack-protector-strong", |
Dan Willemsen | f534a10 | 2016-03-03 17:22:39 -0800 | [diff] [blame] | 56 | }...) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 57 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 58 | darwinClangLdflags = ClangFilterUnknownCflags(darwinLdflags) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 59 | |
Chih-Hung Hsieh | 3101a96 | 2018-04-17 14:16:05 -0700 | [diff] [blame] | 60 | darwinClangLldflags = ClangFilterUnknownLldflags(darwinClangLdflags) |
| 61 | |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 62 | darwinSupportedSdkVersions = []string{ |
Dan Willemsen | 6fb8b8d | 2016-03-09 10:41:21 -0800 | [diff] [blame] | 63 | "10.10", |
| 64 | "10.11", |
Dan Willemsen | 85e3d01 | 2016-10-26 19:20:58 -0700 | [diff] [blame] | 65 | "10.12", |
Nathan Harold | 01abb09 | 2017-09-24 01:51:02 -0700 | [diff] [blame] | 66 | "10.13", |
Dan Willemsen | 5a050c0 | 2018-08-28 16:48:45 -0700 | [diff] [blame] | 67 | "10.14", |
Dan Willemsen | 89dad60 | 2019-10-03 16:44:47 -0700 | [diff] [blame] | 68 | "10.15", |
Dan Willemsen | 695dc69 | 2020-07-14 16:53:43 -0700 | [diff] [blame] | 69 | "11.0", |
Dan Willemsen | 5191b00 | 2021-01-21 14:56:39 -0800 | [diff] [blame] | 70 | "11.1", |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 71 | } |
Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 72 | |
Josh Gao | 3923e84 | 2016-09-29 14:22:54 -0700 | [diff] [blame] | 73 | darwinAvailableLibraries = append( |
| 74 | addPrefix([]string{ |
| 75 | "c", |
| 76 | "dl", |
| 77 | "m", |
| 78 | "ncurses", |
| 79 | "objc", |
| 80 | "pthread", |
Josh Gao | 3923e84 | 2016-09-29 14:22:54 -0700 | [diff] [blame] | 81 | }, "-l"), |
Colin Cross | 124fd9a | 2016-11-21 17:31:08 -0800 | [diff] [blame] | 82 | "-framework AppKit", |
Josh Gao | 3923e84 | 2016-09-29 14:22:54 -0700 | [diff] [blame] | 83 | "-framework CoreFoundation", |
Colin Cross | 124fd9a | 2016-11-21 17:31:08 -0800 | [diff] [blame] | 84 | "-framework Foundation", |
Josh Gao | 3923e84 | 2016-09-29 14:22:54 -0700 | [diff] [blame] | 85 | "-framework IOKit", |
Colin Cross | 124fd9a | 2016-11-21 17:31:08 -0800 | [diff] [blame] | 86 | "-framework Security", |
Dan Willemsen | 97d017e | 2019-02-20 10:28:56 -0800 | [diff] [blame] | 87 | "-framework SystemConfiguration", |
Josh Gao | 3923e84 | 2016-09-29 14:22:54 -0700 | [diff] [blame] | 88 | ) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 89 | ) |
| 90 | |
Dan Willemsen | 34fc3b1 | 2015-12-07 12:30:44 -0800 | [diff] [blame] | 91 | const ( |
| 92 | darwinGccVersion = "4.2.1" |
| 93 | ) |
| 94 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 95 | func init() { |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 96 | pctx.VariableFunc("macSdkRoot", func(ctx android.PackageVarContext) string { |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 97 | return getMacTools(ctx).sdkRoot |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 98 | }) |
Elliott Hughes | 7622713 | 2020-02-13 16:23:12 -0800 | [diff] [blame] | 99 | pctx.StaticVariable("macMinVersion", "10.10") |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 100 | pctx.VariableFunc("MacArPath", func(ctx android.PackageVarContext) string { |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 101 | return getMacTools(ctx).arPath |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 102 | }) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 103 | |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 104 | pctx.VariableFunc("MacStripPath", func(ctx android.PackageVarContext) string { |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 105 | return getMacTools(ctx).stripPath |
Colin Cross | b8ecdfe | 2016-05-03 15:10:29 -0700 | [diff] [blame] | 106 | }) |
| 107 | |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 108 | pctx.VariableFunc("MacToolPath", func(ctx android.PackageVarContext) string { |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 109 | return getMacTools(ctx).toolPath |
Dan Willemsen | 300151b | 2017-03-13 12:40:30 -0700 | [diff] [blame] | 110 | }) |
| 111 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 112 | pctx.StaticVariable("DarwinGccVersion", darwinGccVersion) |
| 113 | pctx.SourcePathVariable("DarwinGccRoot", |
| 114 | "prebuilts/gcc/${HostPrebuiltTag}/host/i686-apple-darwin-${DarwinGccVersion}") |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 115 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 116 | pctx.StaticVariable("DarwinGccTriple", "i686-apple-darwin11") |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 117 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 118 | pctx.StaticVariable("DarwinClangCflags", strings.Join(darwinClangCflags, " ")) |
| 119 | pctx.StaticVariable("DarwinClangLdflags", strings.Join(darwinClangLdflags, " ")) |
Chih-Hung Hsieh | 3101a96 | 2018-04-17 14:16:05 -0700 | [diff] [blame] | 120 | pctx.StaticVariable("DarwinClangLldflags", strings.Join(darwinClangLldflags, " ")) |
Dan Willemsen | 282a4b0 | 2016-03-09 10:30:22 -0800 | [diff] [blame] | 121 | |
Dan Willemsen | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 122 | pctx.StaticVariable("DarwinYasmFlags", "-f macho -m amd64") |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 125 | type macPlatformTools struct { |
| 126 | once sync.Once |
| 127 | err error |
| 128 | |
| 129 | sdkRoot string |
| 130 | arPath string |
| 131 | stripPath string |
| 132 | toolPath string |
Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 135 | var macTools = &macPlatformTools{} |
| 136 | |
| 137 | func getMacTools(ctx android.PackageVarContext) *macPlatformTools { |
| 138 | macTools.once.Do(func() { |
Lukacs T. Berki | 21d5c7a | 2021-03-11 08:52:39 +0100 | [diff] [blame^] | 139 | xcrunTool := "/usr/bin/xcrun" |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 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) |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 172 | return "" |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 175 | macTools.sdkRoot = xcrunSdk("--show-sdk-path") |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 176 | |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 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) |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 183 | } |
Dan Willemsen | 6ba5367 | 2020-04-01 14:32:52 -0700 | [diff] [blame] | 184 | return macTools |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 187 | type toolchainDarwin struct { |
| 188 | cFlags, ldFlags string |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 189 | toolchain64Bit |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Dan Willemsen | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 192 | func (t *toolchainDarwin) Name() string { |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 193 | return "x86_64" |
| 194 | } |
| 195 | |
| 196 | func (t *toolchainDarwin) GccRoot() string { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 197 | return "${config.DarwinGccRoot}" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | func (t *toolchainDarwin) GccTriple() string { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 201 | return "${config.DarwinGccTriple}" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | func (t *toolchainDarwin) GccVersion() string { |
Dan Willemsen | 34fc3b1 | 2015-12-07 12:30:44 -0800 | [diff] [blame] | 205 | return darwinGccVersion |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 208 | func (t *toolchainDarwin) IncludeFlags() string { |
| 209 | return "" |
| 210 | } |
| 211 | |
Dan Willemsen | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 212 | func (t *toolchainDarwin) ClangTriple() string { |
Dan Willemsen | 01fdd3d | 2016-03-30 00:01:12 -0700 | [diff] [blame] | 213 | return "x86_64-apple-darwin" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Dan Willemsen | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 216 | func (t *toolchainDarwin) ClangCflags() string { |
| 217 | return "${config.DarwinClangCflags}" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Dan Willemsen | 282a4b0 | 2016-03-09 10:30:22 -0800 | [diff] [blame] | 220 | func (t *toolchainDarwin) ClangCppflags() string { |
| 221 | return "" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Dan Willemsen | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 224 | func (t *toolchainDarwin) ClangLdflags() string { |
| 225 | return "${config.DarwinClangLdflags}" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Dan Willemsen | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 228 | func (t *toolchainDarwin) ClangLldflags() string { |
| 229 | return "${config.DarwinClangLldflags}" |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Dan Willemsen | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 232 | func (t *toolchainDarwin) YasmFlags() string { |
| 233 | return "${config.DarwinYasmFlags}" |
Greg Hartman | 09302be | 2017-10-04 17:31:43 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 236 | func (t *toolchainDarwin) ShlibSuffix() string { |
| 237 | return ".dylib" |
| 238 | } |
| 239 | |
Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 240 | func (t *toolchainDarwin) AvailableLibraries() []string { |
| 241 | return darwinAvailableLibraries |
| 242 | } |
| 243 | |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 244 | func (t *toolchainDarwin) Bionic() bool { |
| 245 | return false |
| 246 | } |
| 247 | |
Dan Willemsen | 300151b | 2017-03-13 12:40:30 -0700 | [diff] [blame] | 248 | func (t *toolchainDarwin) ToolPath() string { |
| 249 | return "${config.MacToolPath}" |
| 250 | } |
| 251 | |
Dan Willemsen | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 252 | var toolchainDarwinSingleton Toolchain = &toolchainDarwin{} |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 253 | |
Dan Willemsen | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 254 | func darwinToolchainFactory(arch android.Arch) Toolchain { |
| 255 | return toolchainDarwinSingleton |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | func init() { |
Dan Willemsen | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 259 | registerToolchainFactory(android.Darwin, android.X86_64, darwinToolchainFactory) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 260 | } |