Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 1 | package cc |
| 2 | |
| 3 | import ( |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 4 | "fmt" |
| 5 | "os/exec" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 6 | "strings" |
| 7 | |
| 8 | "android/soong/common" |
| 9 | ) |
| 10 | |
| 11 | var ( |
| 12 | darwinCflags = []string{ |
| 13 | "-fno-exceptions", // from build/core/combo/select.mk |
| 14 | "-Wno-multichar", // from build/core/combo/select.mk |
| 15 | |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 16 | "-fdiagnostics-color", |
| 17 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 18 | "-fPIC", |
| 19 | "-funwind-tables", |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 20 | |
| 21 | // Workaround differences in inttypes.h between host and target. |
| 22 | //See bug 12708004. |
| 23 | "-D__STDC_FORMAT_MACROS", |
| 24 | "-D__STDC_CONSTANT_MACROS", |
| 25 | |
| 26 | // HOST_RELEASE_CFLAGS |
| 27 | "-O2", // from build/core/combo/select.mk |
| 28 | "-g", // from build/core/combo/select.mk |
| 29 | "-fno-strict-aliasing", // from build/core/combo/select.mk |
| 30 | "-isysroot ${macSdkRoot}", |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 31 | "-mmacosx-version-min=${macSdkVersion}", |
| 32 | "-DMACOSX_DEPLOYMENT_TARGET=${macSdkVersion}", |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 35 | darwinLdflags = []string{ |
| 36 | "-isysroot ${macSdkRoot}", |
| 37 | "-Wl,-syslibroot,${macSdkRoot}", |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 38 | "-mmacosx-version-min=${macSdkVersion}", |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Dan Willemsen | 282a4b0 | 2016-03-09 10:30:22 -0800 | [diff] [blame] | 41 | darwinSystemCppCppflags = []string{ |
| 42 | "-isystem ${macToolchainRoot}/usr/include/c++/v1", |
| 43 | } |
| 44 | |
| 45 | darwinSystemCppLdflags = []string{ |
| 46 | "-stdlib=libc++", |
| 47 | } |
| 48 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 49 | // Extended cflags |
| 50 | darwinX86Cflags = []string{ |
| 51 | "-m32", |
| 52 | } |
| 53 | |
| 54 | darwinX8664Cflags = []string{ |
| 55 | "-m64", |
| 56 | } |
| 57 | |
| 58 | darwinX86Ldflags = []string{ |
| 59 | "-m32", |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | darwinX8664Ldflags = []string{ |
| 63 | "-m64", |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Dan Willemsen | f534a10 | 2016-03-03 17:22:39 -0800 | [diff] [blame] | 66 | darwinClangCflags = append(clangFilterUnknownCflags(darwinCflags), []string{ |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 67 | "-integrated-as", |
Dan Willemsen | 7a0f848 | 2016-01-12 16:22:40 -0800 | [diff] [blame] | 68 | "-fstack-protector-strong", |
Dan Willemsen | f534a10 | 2016-03-03 17:22:39 -0800 | [diff] [blame] | 69 | }...) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 70 | |
Dan Willemsen | 01fdd3d | 2016-03-30 00:01:12 -0700 | [diff] [blame] | 71 | darwinX86ClangCflags = append(clangFilterUnknownCflags(darwinX86Cflags), []string{ |
| 72 | "-msse3", |
| 73 | }...) |
| 74 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 75 | darwinClangLdflags = clangFilterUnknownCflags(darwinLdflags) |
| 76 | |
| 77 | darwinX86ClangLdflags = clangFilterUnknownCflags(darwinX86Ldflags) |
| 78 | |
| 79 | darwinX8664ClangLdflags = clangFilterUnknownCflags(darwinX8664Ldflags) |
| 80 | |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 81 | darwinSupportedSdkVersions = []string{ |
Dan Willemsen | 6fb8b8d | 2016-03-09 10:41:21 -0800 | [diff] [blame] | 82 | "10.8", |
| 83 | "10.9", |
| 84 | "10.10", |
| 85 | "10.11", |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 86 | } |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 87 | ) |
| 88 | |
Dan Willemsen | 34fc3b1 | 2015-12-07 12:30:44 -0800 | [diff] [blame] | 89 | const ( |
| 90 | darwinGccVersion = "4.2.1" |
| 91 | ) |
| 92 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 93 | func init() { |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 94 | pctx.VariableFunc("macSdkPath", func(config interface{}) (string, error) { |
| 95 | bytes, err := exec.Command("xcode-select", "--print-path").Output() |
| 96 | return strings.TrimSpace(string(bytes)), err |
| 97 | }) |
| 98 | pctx.StaticVariable("macToolchainRoot", "${macSdkPath}/Toolchains/XcodeDefault.xctoolchain") |
| 99 | pctx.VariableFunc("macSdkRoot", func(config interface{}) (string, error) { |
| 100 | return xcrunSdk(config.(common.Config), "--show-sdk-path") |
| 101 | }) |
Dan Willemsen | 6fb8b8d | 2016-03-09 10:41:21 -0800 | [diff] [blame] | 102 | pctx.StaticVariable("macSdkVersion", darwinSupportedSdkVersions[0]) |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 103 | pctx.VariableFunc("macArPath", func(config interface{}) (string, error) { |
| 104 | bytes, err := exec.Command("xcrun", "--find", "ar").Output() |
| 105 | return strings.TrimSpace(string(bytes)), err |
| 106 | }) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 107 | |
Colin Cross | b8ecdfe | 2016-05-03 15:10:29 -0700 | [diff] [blame^] | 108 | pctx.VariableFunc("macStripPath", func(config interface{}) (string, error) { |
| 109 | bytes, err := exec.Command("xcrun", "--find", "strip").Output() |
| 110 | return strings.TrimSpace(string(bytes)), err |
| 111 | }) |
| 112 | |
Dan Willemsen | 34fc3b1 | 2015-12-07 12:30:44 -0800 | [diff] [blame] | 113 | pctx.StaticVariable("darwinGccVersion", darwinGccVersion) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 114 | pctx.SourcePathVariable("darwinGccRoot", |
| 115 | "prebuilts/gcc/${HostPrebuiltTag}/host/i686-apple-darwin-${darwinGccVersion}") |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 116 | |
| 117 | pctx.StaticVariable("darwinGccTriple", "i686-apple-darwin11") |
| 118 | |
| 119 | pctx.StaticVariable("darwinCflags", strings.Join(darwinCflags, " ")) |
| 120 | pctx.StaticVariable("darwinLdflags", strings.Join(darwinLdflags, " ")) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 121 | |
| 122 | pctx.StaticVariable("darwinClangCflags", strings.Join(darwinClangCflags, " ")) |
| 123 | pctx.StaticVariable("darwinClangLdflags", strings.Join(darwinClangLdflags, " ")) |
Dan Willemsen | 282a4b0 | 2016-03-09 10:30:22 -0800 | [diff] [blame] | 124 | |
| 125 | pctx.StaticVariable("darwinSystemCppCppflags", strings.Join(darwinSystemCppCppflags, " ")) |
| 126 | pctx.StaticVariable("darwinSystemCppLdflags", strings.Join(darwinSystemCppLdflags, " ")) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 127 | |
| 128 | // Extended cflags |
| 129 | pctx.StaticVariable("darwinX86Cflags", strings.Join(darwinX86Cflags, " ")) |
| 130 | pctx.StaticVariable("darwinX8664Cflags", strings.Join(darwinX8664Cflags, " ")) |
| 131 | pctx.StaticVariable("darwinX86Ldflags", strings.Join(darwinX86Ldflags, " ")) |
| 132 | pctx.StaticVariable("darwinX8664Ldflags", strings.Join(darwinX8664Ldflags, " ")) |
| 133 | |
Dan Willemsen | 01fdd3d | 2016-03-30 00:01:12 -0700 | [diff] [blame] | 134 | pctx.StaticVariable("darwinX86ClangCflags", strings.Join(darwinX86ClangCflags, " ")) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 135 | pctx.StaticVariable("darwinX8664ClangCflags", |
| 136 | strings.Join(clangFilterUnknownCflags(darwinX8664Cflags), " ")) |
| 137 | pctx.StaticVariable("darwinX86ClangLdflags", strings.Join(darwinX86ClangLdflags, " ")) |
| 138 | pctx.StaticVariable("darwinX8664ClangLdflags", strings.Join(darwinX8664ClangLdflags, " ")) |
| 139 | } |
| 140 | |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 141 | func xcrunSdk(config common.Config, arg string) (string, error) { |
| 142 | if selected := config.Getenv("MAC_SDK_VERSION"); selected != "" { |
Dan Willemsen | 6fb8b8d | 2016-03-09 10:41:21 -0800 | [diff] [blame] | 143 | if !inList(selected, darwinSupportedSdkVersions) { |
Dan Willemsen | cf7c71b | 2015-12-14 20:02:44 -0800 | [diff] [blame] | 144 | return "", fmt.Errorf("MAC_SDK_VERSION %s isn't supported: %q", selected, darwinSupportedSdkVersions) |
| 145 | } |
| 146 | |
| 147 | bytes, err := exec.Command("xcrun", "--sdk", "macosx"+selected, arg).Output() |
| 148 | if err == nil { |
| 149 | return strings.TrimSpace(string(bytes)), err |
| 150 | } |
| 151 | return "", fmt.Errorf("MAC_SDK_VERSION %s is not installed", selected) |
| 152 | } |
| 153 | |
| 154 | for _, sdk := range darwinSupportedSdkVersions { |
Dan Willemsen | 6fb8b8d | 2016-03-09 10:41:21 -0800 | [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 { |
| 157 | return strings.TrimSpace(string(bytes)), err |
| 158 | } |
| 159 | } |
| 160 | return "", fmt.Errorf("Could not find a supported mac sdk: %q", darwinSupportedSdkVersions) |
| 161 | } |
| 162 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 163 | type toolchainDarwin struct { |
| 164 | cFlags, ldFlags string |
| 165 | } |
| 166 | |
| 167 | type toolchainDarwinX86 struct { |
| 168 | toolchain32Bit |
| 169 | toolchainDarwin |
| 170 | } |
| 171 | |
| 172 | type toolchainDarwinX8664 struct { |
| 173 | toolchain64Bit |
| 174 | toolchainDarwin |
| 175 | } |
| 176 | |
| 177 | func (t *toolchainDarwinX86) Name() string { |
| 178 | return "x86" |
| 179 | } |
| 180 | |
| 181 | func (t *toolchainDarwinX8664) Name() string { |
| 182 | return "x86_64" |
| 183 | } |
| 184 | |
| 185 | func (t *toolchainDarwin) GccRoot() string { |
| 186 | return "${darwinGccRoot}" |
| 187 | } |
| 188 | |
| 189 | func (t *toolchainDarwin) GccTriple() string { |
| 190 | return "${darwinGccTriple}" |
| 191 | } |
| 192 | |
| 193 | func (t *toolchainDarwin) GccVersion() string { |
Dan Willemsen | 34fc3b1 | 2015-12-07 12:30:44 -0800 | [diff] [blame] | 194 | return darwinGccVersion |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | func (t *toolchainDarwin) Cflags() string { |
| 198 | return "${darwinCflags} ${darwinX86Cflags}" |
| 199 | } |
| 200 | |
| 201 | func (t *toolchainDarwinX8664) Cflags() string { |
| 202 | return "${darwinCflags} ${darwinX8664Cflags}" |
| 203 | } |
| 204 | |
| 205 | func (t *toolchainDarwin) Cppflags() string { |
Dan Willemsen | 282a4b0 | 2016-03-09 10:30:22 -0800 | [diff] [blame] | 206 | return "" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | func (t *toolchainDarwinX86) Ldflags() string { |
| 210 | return "${darwinLdflags} ${darwinX86Ldflags}" |
| 211 | } |
| 212 | |
| 213 | func (t *toolchainDarwinX8664) Ldflags() string { |
| 214 | return "${darwinLdflags} ${darwinX8664Ldflags}" |
| 215 | } |
| 216 | |
| 217 | func (t *toolchainDarwin) IncludeFlags() string { |
| 218 | return "" |
| 219 | } |
| 220 | |
| 221 | func (t *toolchainDarwinX86) ClangTriple() string { |
Dan Willemsen | 01fdd3d | 2016-03-30 00:01:12 -0700 | [diff] [blame] | 222 | return "i686-apple-darwin" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | func (t *toolchainDarwinX86) ClangCflags() string { |
| 226 | return "${darwinClangCflags} ${darwinX86ClangCflags}" |
| 227 | } |
| 228 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 229 | func (t *toolchainDarwinX8664) ClangTriple() string { |
Dan Willemsen | 01fdd3d | 2016-03-30 00:01:12 -0700 | [diff] [blame] | 230 | return "x86_64-apple-darwin" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | func (t *toolchainDarwinX8664) ClangCflags() string { |
| 234 | return "${darwinClangCflags} ${darwinX8664ClangCflags}" |
| 235 | } |
| 236 | |
Dan Willemsen | 282a4b0 | 2016-03-09 10:30:22 -0800 | [diff] [blame] | 237 | func (t *toolchainDarwin) ClangCppflags() string { |
| 238 | return "" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | func (t *toolchainDarwinX86) ClangLdflags() string { |
| 242 | return "${darwinClangLdflags} ${darwinX86ClangLdflags}" |
| 243 | } |
| 244 | |
| 245 | func (t *toolchainDarwinX8664) ClangLdflags() string { |
| 246 | return "${darwinClangLdflags} ${darwinX8664ClangLdflags}" |
| 247 | } |
| 248 | |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 249 | func (t *toolchainDarwin) ShlibSuffix() string { |
| 250 | return ".dylib" |
| 251 | } |
| 252 | |
Dan Willemsen | 282a4b0 | 2016-03-09 10:30:22 -0800 | [diff] [blame] | 253 | func (t *toolchainDarwin) SystemCppCppflags() string { |
| 254 | return "${darwinSystemCppCppflags}" |
| 255 | } |
| 256 | |
| 257 | func (t *toolchainDarwin) SystemCppLdflags() string { |
| 258 | return "${darwinSystemCppLdflags}" |
| 259 | } |
| 260 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 261 | var toolchainDarwinX86Singleton Toolchain = &toolchainDarwinX86{} |
| 262 | var toolchainDarwinX8664Singleton Toolchain = &toolchainDarwinX8664{} |
| 263 | |
Colin Cross | c5c24ad | 2015-11-20 15:35:00 -0800 | [diff] [blame] | 264 | func darwinX86ToolchainFactory(arch common.Arch) Toolchain { |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 265 | return toolchainDarwinX86Singleton |
| 266 | } |
| 267 | |
Colin Cross | c5c24ad | 2015-11-20 15:35:00 -0800 | [diff] [blame] | 268 | func darwinX8664ToolchainFactory(arch common.Arch) Toolchain { |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 269 | return toolchainDarwinX8664Singleton |
| 270 | } |
| 271 | |
| 272 | func init() { |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 273 | registerHostToolchainFactory(common.Darwin, common.X86, darwinX86ToolchainFactory) |
| 274 | registerHostToolchainFactory(common.Darwin, common.X86_64, darwinX8664ToolchainFactory) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 275 | } |