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