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 | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 18 | "os/exec" |
Dan Willemsen | 300151b | 2017-03-13 12:40:30 -0700 | [diff] [blame] | 19 | "path/filepath" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 20 | "strings" |
| 21 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 22 | "android/soong/android" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | var ( |
| 26 | darwinCflags = []string{ |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 27 | "-fdiagnostics-color", |
| 28 | |
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 | 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", |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 51 | darwinClangCflags = append(ClangFilterUnknownCflags(darwinCflags), []string{ |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 52 | "-integrated-as", |
Dan Willemsen | 7a0f848 | 2016-01-12 16:22:40 -0800 | [diff] [blame] | 53 | "-fstack-protector-strong", |
Dan Willemsen | f534a10 | 2016-03-03 17:22:39 -0800 | [diff] [blame] | 54 | }...) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 55 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 56 | darwinClangLdflags = ClangFilterUnknownCflags(darwinLdflags) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 57 | |
Chih-Hung Hsieh | 3101a96 | 2018-04-17 14:16:05 -0700 | [diff] [blame] | 58 | darwinClangLldflags = ClangFilterUnknownLldflags(darwinClangLdflags) |
| 59 | |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 60 | darwinSupportedSdkVersions = []string{ |
Dan Willemsen | 6fb8b8d | 2016-03-09 10:41:21 -0800 | [diff] [blame] | 61 | "10.10", |
| 62 | "10.11", |
Dan Willemsen | 85e3d01 | 2016-10-26 19:20:58 -0700 | [diff] [blame] | 63 | "10.12", |
Nathan Harold | 01abb09 | 2017-09-24 01:51:02 -0700 | [diff] [blame] | 64 | "10.13", |
Dan Willemsen | 5a050c0 | 2018-08-28 16:48:45 -0700 | [diff] [blame] | 65 | "10.14", |
Dan Willemsen | 89dad60 | 2019-10-03 16:44:47 -0700 | [diff] [blame^] | 66 | "10.15", |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 67 | } |
Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 68 | |
Josh Gao | 3923e84 | 2016-09-29 14:22:54 -0700 | [diff] [blame] | 69 | darwinAvailableLibraries = append( |
| 70 | addPrefix([]string{ |
| 71 | "c", |
| 72 | "dl", |
| 73 | "m", |
| 74 | "ncurses", |
| 75 | "objc", |
| 76 | "pthread", |
Josh Gao | 3923e84 | 2016-09-29 14:22:54 -0700 | [diff] [blame] | 77 | }, "-l"), |
Colin Cross | 124fd9a | 2016-11-21 17:31:08 -0800 | [diff] [blame] | 78 | "-framework AppKit", |
Josh Gao | 3923e84 | 2016-09-29 14:22:54 -0700 | [diff] [blame] | 79 | "-framework CoreFoundation", |
Colin Cross | 124fd9a | 2016-11-21 17:31:08 -0800 | [diff] [blame] | 80 | "-framework Foundation", |
Josh Gao | 3923e84 | 2016-09-29 14:22:54 -0700 | [diff] [blame] | 81 | "-framework IOKit", |
Colin Cross | 124fd9a | 2016-11-21 17:31:08 -0800 | [diff] [blame] | 82 | "-framework Security", |
Dan Willemsen | 97d017e | 2019-02-20 10:28:56 -0800 | [diff] [blame] | 83 | "-framework SystemConfiguration", |
Josh Gao | 3923e84 | 2016-09-29 14:22:54 -0700 | [diff] [blame] | 84 | ) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 85 | ) |
| 86 | |
Dan Willemsen | 34fc3b1 | 2015-12-07 12:30:44 -0800 | [diff] [blame] | 87 | const ( |
| 88 | darwinGccVersion = "4.2.1" |
| 89 | ) |
| 90 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 91 | func init() { |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 92 | pctx.VariableFunc("macSdkPath", func(ctx android.PackageVarContext) string { |
| 93 | xcodeselect := ctx.Config().HostSystemTool("xcode-select") |
Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 94 | bytes, err := exec.Command(xcodeselect, "--print-path").Output() |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 95 | if err != nil { |
| 96 | ctx.Errorf("xcode-select failed with: %q", err.Error()) |
| 97 | } |
| 98 | return strings.TrimSpace(string(bytes)) |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 99 | }) |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 100 | pctx.VariableFunc("macSdkRoot", func(ctx android.PackageVarContext) string { |
| 101 | return xcrunSdk(ctx, "--show-sdk-path") |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 102 | }) |
Dan Willemsen | 85e3d01 | 2016-10-26 19:20:58 -0700 | [diff] [blame] | 103 | pctx.StaticVariable("macMinVersion", "10.8") |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 104 | pctx.VariableFunc("MacArPath", func(ctx android.PackageVarContext) string { |
| 105 | return xcrun(ctx, "--find", "ar") |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 106 | }) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 107 | |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 108 | pctx.VariableFunc("MacStripPath", func(ctx android.PackageVarContext) string { |
| 109 | return xcrun(ctx, "--find", "strip") |
Colin Cross | b8ecdfe | 2016-05-03 15:10:29 -0700 | [diff] [blame] | 110 | }) |
| 111 | |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 112 | pctx.VariableFunc("MacToolPath", func(ctx android.PackageVarContext) string { |
| 113 | return filepath.Dir(xcrun(ctx, "--find", "ld")) |
Dan Willemsen | 300151b | 2017-03-13 12:40:30 -0700 | [diff] [blame] | 114 | }) |
| 115 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 116 | pctx.StaticVariable("DarwinGccVersion", darwinGccVersion) |
| 117 | pctx.SourcePathVariable("DarwinGccRoot", |
| 118 | "prebuilts/gcc/${HostPrebuiltTag}/host/i686-apple-darwin-${DarwinGccVersion}") |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 119 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 120 | pctx.StaticVariable("DarwinGccTriple", "i686-apple-darwin11") |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 121 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 122 | pctx.StaticVariable("DarwinClangCflags", strings.Join(darwinClangCflags, " ")) |
| 123 | pctx.StaticVariable("DarwinClangLdflags", strings.Join(darwinClangLdflags, " ")) |
Chih-Hung Hsieh | 3101a96 | 2018-04-17 14:16:05 -0700 | [diff] [blame] | 124 | pctx.StaticVariable("DarwinClangLldflags", strings.Join(darwinClangLldflags, " ")) |
Dan Willemsen | 282a4b0 | 2016-03-09 10:30:22 -0800 | [diff] [blame] | 125 | |
Dan Willemsen | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 126 | pctx.StaticVariable("DarwinYasmFlags", "-f macho -m amd64") |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 129 | func xcrun(ctx android.PackageVarContext, args ...string) string { |
| 130 | xcrun := ctx.Config().HostSystemTool("xcrun") |
Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 131 | bytes, err := exec.Command(xcrun, args...).Output() |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 132 | if err != nil { |
| 133 | ctx.Errorf("xcrun failed with: %q", err.Error()) |
| 134 | } |
| 135 | return strings.TrimSpace(string(bytes)) |
Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 138 | func xcrunSdk(ctx android.PackageVarContext, arg string) string { |
| 139 | xcrun := ctx.Config().HostSystemTool("xcrun") |
| 140 | if selected := ctx.Config().Getenv("MAC_SDK_VERSION"); selected != "" { |
Dan Willemsen | 6fb8b8d | 2016-03-09 10:41:21 -0800 | [diff] [blame] | 141 | if !inList(selected, darwinSupportedSdkVersions) { |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 142 | ctx.Errorf("MAC_SDK_VERSION %s isn't supported: %q", selected, darwinSupportedSdkVersions) |
| 143 | return "" |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 146 | bytes, err := exec.Command(xcrun, "--sdk", "macosx"+selected, arg).Output() |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 147 | if err != nil { |
| 148 | ctx.Errorf("MAC_SDK_VERSION %s is not installed", selected) |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 149 | } |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 150 | return strings.TrimSpace(string(bytes)) |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | for _, sdk := range darwinSupportedSdkVersions { |
Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 154 | bytes, err := exec.Command(xcrun, "--sdk", "macosx"+sdk, arg).Output() |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 155 | if err == nil { |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 156 | return strings.TrimSpace(string(bytes)) |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 157 | } |
| 158 | } |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 159 | ctx.Errorf("Could not find a supported mac sdk: %q", darwinSupportedSdkVersions) |
| 160 | return "" |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 163 | type toolchainDarwin struct { |
| 164 | cFlags, ldFlags string |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 165 | toolchain64Bit |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Dan Willemsen | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 168 | func (t *toolchainDarwin) Name() string { |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 169 | return "x86_64" |
| 170 | } |
| 171 | |
| 172 | func (t *toolchainDarwin) GccRoot() string { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 173 | return "${config.DarwinGccRoot}" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | func (t *toolchainDarwin) GccTriple() string { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 177 | return "${config.DarwinGccTriple}" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | func (t *toolchainDarwin) GccVersion() string { |
Dan Willemsen | 34fc3b1 | 2015-12-07 12:30:44 -0800 | [diff] [blame] | 181 | return darwinGccVersion |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 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 | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 188 | func (t *toolchainDarwin) ClangTriple() string { |
Dan Willemsen | 01fdd3d | 2016-03-30 00:01:12 -0700 | [diff] [blame] | 189 | return "x86_64-apple-darwin" |
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) ClangCflags() string { |
| 193 | return "${config.DarwinClangCflags}" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Dan Willemsen | 282a4b0 | 2016-03-09 10:30:22 -0800 | [diff] [blame] | 196 | func (t *toolchainDarwin) ClangCppflags() string { |
| 197 | return "" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Dan Willemsen | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 200 | func (t *toolchainDarwin) ClangLdflags() string { |
| 201 | return "${config.DarwinClangLdflags}" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Dan Willemsen | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 204 | func (t *toolchainDarwin) ClangLldflags() string { |
| 205 | return "${config.DarwinClangLldflags}" |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Dan Willemsen | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 208 | func (t *toolchainDarwin) YasmFlags() string { |
| 209 | return "${config.DarwinYasmFlags}" |
Greg Hartman | 09302be | 2017-10-04 17:31:43 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 212 | func (t *toolchainDarwin) ShlibSuffix() string { |
| 213 | return ".dylib" |
| 214 | } |
| 215 | |
Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 216 | func (t *toolchainDarwin) AvailableLibraries() []string { |
| 217 | return darwinAvailableLibraries |
| 218 | } |
| 219 | |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 220 | func (t *toolchainDarwin) Bionic() bool { |
| 221 | return false |
| 222 | } |
| 223 | |
Dan Willemsen | 300151b | 2017-03-13 12:40:30 -0700 | [diff] [blame] | 224 | func (t *toolchainDarwin) ToolPath() string { |
| 225 | return "${config.MacToolPath}" |
| 226 | } |
| 227 | |
Dan Willemsen | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 228 | var toolchainDarwinSingleton Toolchain = &toolchainDarwin{} |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 229 | |
Dan Willemsen | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 230 | func darwinToolchainFactory(arch android.Arch) Toolchain { |
| 231 | return toolchainDarwinSingleton |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | func init() { |
Dan Willemsen | e97e68a | 2018-08-28 17:12:56 -0700 | [diff] [blame] | 235 | registerToolchainFactory(android.Darwin, android.X86_64, darwinToolchainFactory) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 236 | } |