From 8528f4ec5efc9497f80683f6fb23ee690ed181d0 Mon Sep 17 00:00:00 2001 From: Dan Willemsen Date: Tue, 19 Oct 2021 00:22:06 -0700 Subject: [PATCH] Add Darwin+Arm64 toolchain support This just sets up the toolchain and allows Darwin+Arm64 to be specified as a HostCross target. These variants will not be exported to Make, or be installed on a Soong-only build. A future CL will add support for universal binaries using these variants. This config is a bit stranger than the regular 64/32 multilib, as it's two primary 64-bit configs. And on a Darwin/X86 machine, the Arm64 versions are HostCross (doesn't work on the current machines), while a Darwin/Arm64 machine, either version works (if Rosetta is installed). Bug: 203607969 Change-Id: Iacaed77d267773672da027cd74917e33fb1c1e94 --- android/androidmk.go | 5 ++ android/arch.go | 7 ++- bazel/configurability.go | 2 + cc/config/Android.bp | 2 +- .../{x86_darwin_host.go => darwin_host.go} | 52 +++++++++++++++---- rust/config/Android.bp | 2 +- .../{x86_darwin_host.go => darwin_host.go} | 43 +++++++++++++-- 7 files changed, 98 insertions(+), 15 deletions(-) rename cc/config/{x86_darwin_host.go => darwin_host.go} (81%) rename rust/config/{x86_darwin_host.go => darwin_host.go} (67%) diff --git a/android/androidmk.go b/android/androidmk.go index 38451b7da..b6b04a6bc 100644 --- a/android/androidmk.go +++ b/android/androidmk.go @@ -921,6 +921,11 @@ func shouldSkipAndroidMkProcessing(module *ModuleBase) bool { return true } + // Only expose the primary Darwin target, as Make does not understand Darwin+Arm64 + if module.Os() == Darwin && module.Target().HostCross { + return true + } + return !module.Enabled() || module.commonProperties.HideFromMake || // Make does not understand LinuxBionic diff --git a/android/arch.go b/android/arch.go index 5e3e9204a..3bf54b711 100644 --- a/android/arch.go +++ b/android/arch.go @@ -308,7 +308,7 @@ var ( // LinuxMusl is the OS for the Linux kernel plus the musl runtime. LinuxMusl = newOsType("linux_musl", Host, false, X86, X86_64) // Darwin is the OS for MacOS/Darwin host machines. - Darwin = newOsType("darwin", Host, false, X86_64) + Darwin = newOsType("darwin", Host, false, Arm64, X86_64) // LinuxBionic is the OS for the Linux kernel plus the Bionic libc runtime, but without the // rest of Android. LinuxBionic = newOsType("linux_bionic", Host, false, Arm64, X86_64) @@ -696,6 +696,11 @@ func archMutator(bpctx blueprint.BottomUpMutatorContext) { for i, m := range modules { addTargetProperties(m, targets[i], multiTargets, i == 0) m.base().setArchProperties(mctx) + + // Install support doesn't understand Darwin+Arm64 + if os == Darwin && targets[i].HostCross { + m.base().commonProperties.SkipInstall = true + } } } diff --git a/bazel/configurability.go b/bazel/configurability.go index e9641e7ff..f05c8e55c 100644 --- a/bazel/configurability.go +++ b/bazel/configurability.go @@ -39,6 +39,7 @@ const ( osArchAndroidArm64 = "android_arm64" osArchAndroidX86 = "android_x86" osArchAndroidX86_64 = "android_x86_64" + osArchDarwinArm64 = "darwin_arm64" osArchDarwinX86_64 = "darwin_x86_64" osArchLinuxX86 = "linux_glibc_x86" osArchLinuxX86_64 = "linux_glibc_x86_64" @@ -96,6 +97,7 @@ var ( osArchAndroidArm64: "//build/bazel/platforms/os_arch:android_arm64", osArchAndroidX86: "//build/bazel/platforms/os_arch:android_x86", osArchAndroidX86_64: "//build/bazel/platforms/os_arch:android_x86_64", + osArchDarwinArm64: "//build/bazel/platforms/os_arch:darwin_arm64", osArchDarwinX86_64: "//build/bazel/platforms/os_arch:darwin_x86_64", osArchLinuxX86: "//build/bazel/platforms/os_arch:linux_glibc_x86", osArchLinuxX86_64: "//build/bazel/platforms/os_arch:linux_glibc_x86_64", diff --git a/cc/config/Android.bp b/cc/config/Android.bp index 3e8ee48ca..7b7ee2849 100644 --- a/cc/config/Android.bp +++ b/cc/config/Android.bp @@ -24,7 +24,7 @@ bootstrap_go_package { "x86_device.go", "x86_64_device.go", - "x86_darwin_host.go", + "darwin_host.go", "x86_linux_host.go", "x86_linux_bionic_host.go", "x86_windows_host.go", diff --git a/cc/config/x86_darwin_host.go b/cc/config/darwin_host.go similarity index 81% rename from cc/config/x86_darwin_host.go rename to cc/config/darwin_host.go index ad82b9476..7eb4f8379 100644 --- a/cc/config/x86_darwin_host.go +++ b/cc/config/darwin_host.go @@ -174,19 +174,43 @@ type toolchainDarwin struct { toolchain64Bit } -func (t *toolchainDarwin) Name() string { +type toolchainDarwinX86 struct { + toolchainDarwin +} + +type toolchainDarwinArm struct { + toolchainDarwin +} + +func (t *toolchainDarwinArm) Name() string { + return "arm64" +} + +func (t *toolchainDarwinX86) Name() string { return "x86_64" } -func (t *toolchainDarwin) GccRoot() string { +func (t *toolchainDarwinArm) GccRoot() string { + panic("unimplemented") +} + +func (t *toolchainDarwinArm) GccTriple() string { + panic("unimplemented") +} + +func (t *toolchainDarwinArm) GccVersion() string { + panic("unimplemented") +} + +func (t *toolchainDarwinX86) GccRoot() string { return "${config.DarwinGccRoot}" } -func (t *toolchainDarwin) GccTriple() string { +func (t *toolchainDarwinX86) GccTriple() string { return "${config.DarwinGccTriple}" } -func (t *toolchainDarwin) GccVersion() string { +func (t *toolchainDarwinX86) GccVersion() string { return darwinGccVersion } @@ -194,7 +218,11 @@ func (t *toolchainDarwin) IncludeFlags() string { return "" } -func (t *toolchainDarwin) ClangTriple() string { +func (t *toolchainDarwinArm) ClangTriple() string { + return "aarch64-apple-darwin" +} + +func (t *toolchainDarwinX86) ClangTriple() string { return "x86_64-apple-darwin" } @@ -230,12 +258,18 @@ func (t *toolchainDarwin) ToolPath() string { return "${config.MacToolPath}" } -var toolchainDarwinSingleton Toolchain = &toolchainDarwin{} +var toolchainDarwinArmSingleton Toolchain = &toolchainDarwinArm{} +var toolchainDarwinX86Singleton Toolchain = &toolchainDarwinX86{} -func darwinToolchainFactory(arch android.Arch) Toolchain { - return toolchainDarwinSingleton +func darwinArmToolchainFactory(arch android.Arch) Toolchain { + return toolchainDarwinArmSingleton +} + +func darwinX86ToolchainFactory(arch android.Arch) Toolchain { + return toolchainDarwinX86Singleton } func init() { - registerToolchainFactory(android.Darwin, android.X86_64, darwinToolchainFactory) + registerToolchainFactory(android.Darwin, android.Arm64, darwinArmToolchainFactory) + registerToolchainFactory(android.Darwin, android.X86_64, darwinX86ToolchainFactory) } diff --git a/rust/config/Android.bp b/rust/config/Android.bp index 5b121c3aa..7757c79fc 100644 --- a/rust/config/Android.bp +++ b/rust/config/Android.bp @@ -16,7 +16,7 @@ bootstrap_go_package { "lints.go", "toolchain.go", "allowed_list.go", - "x86_darwin_host.go", + "darwin_host.go", "x86_linux_bionic_host.go", "x86_linux_host.go", "x86_device.go", diff --git a/rust/config/x86_darwin_host.go b/rust/config/darwin_host.go similarity index 67% rename from rust/config/x86_darwin_host.go rename to rust/config/darwin_host.go index 8ff0dd4be..03bea8274 100644 --- a/rust/config/x86_darwin_host.go +++ b/rust/config/darwin_host.go @@ -25,41 +25,64 @@ var ( DarwinRustLinkFlags = []string{ "-B${cc_config.MacToolPath}", } + darwinArm64Rustflags = []string{} + darwinArm64Linkflags = []string{} darwinX8664Rustflags = []string{} darwinX8664Linkflags = []string{} ) func init() { + registerToolchainFactory(android.Darwin, android.Arm64, darwinArm64ToolchainFactory) registerToolchainFactory(android.Darwin, android.X86_64, darwinX8664ToolchainFactory) + pctx.StaticVariable("DarwinToolchainRustFlags", strings.Join(DarwinRustFlags, " ")) pctx.StaticVariable("DarwinToolchainLinkFlags", strings.Join(DarwinRustLinkFlags, " ")) + + pctx.StaticVariable("DarwinToolchainArm64RustFlags", strings.Join(darwinArm64Rustflags, " ")) + pctx.StaticVariable("DarwinToolchainArm64LinkFlags", strings.Join(darwinArm64Linkflags, " ")) pctx.StaticVariable("DarwinToolchainX8664RustFlags", strings.Join(darwinX8664Rustflags, " ")) pctx.StaticVariable("DarwinToolchainX8664LinkFlags", strings.Join(darwinX8664Linkflags, " ")) } type toolchainDarwin struct { + toolchain64Bit toolchainRustFlags string toolchainLinkFlags string } -type toolchainDarwinX8664 struct { - toolchain64Bit +type toolchainDarwinArm64 struct { toolchainDarwin } +type toolchainDarwinX8664 struct { + toolchainDarwin +} + +func (toolchainDarwinArm64) Supported() bool { + return true +} + func (toolchainDarwinX8664) Supported() bool { return true } -func (toolchainDarwinX8664) Bionic() bool { +func (toolchainDarwin) Bionic() bool { return false } +func (t *toolchainDarwinArm64) Name() string { + return "arm64" +} + func (t *toolchainDarwinX8664) Name() string { return "x86_64" } +func (t *toolchainDarwinArm64) RustTriple() string { + return "aarch64-apple-darwin" +} + func (t *toolchainDarwinX8664) RustTriple() string { return "x86_64-apple-darwin" } @@ -76,6 +99,15 @@ func (t *toolchainDarwin) ProcMacroSuffix() string { return ".dylib" } +func (t *toolchainDarwinArm64) ToolchainLinkFlags() string { + // Prepend the lld flags from cc_config so we stay in sync with cc + return "${cc_config.DarwinLldflags} ${config.DarwinToolchainLinkFlags} ${config.DarwinToolchainArm64LinkFlags}" +} + +func (t *toolchainDarwinArm64) ToolchainRustFlags() string { + return "${config.DarwinToolchainRustFlags} ${config.DarwinToolchainArm64RustFlags}" +} + func (t *toolchainDarwinX8664) ToolchainLinkFlags() string { // Prepend the lld flags from cc_config so we stay in sync with cc return "${cc_config.DarwinLldflags} ${config.DarwinToolchainLinkFlags} ${config.DarwinToolchainX8664LinkFlags}" @@ -85,8 +117,13 @@ func (t *toolchainDarwinX8664) ToolchainRustFlags() string { return "${config.DarwinToolchainRustFlags} ${config.DarwinToolchainX8664RustFlags}" } +func darwinArm64ToolchainFactory(arch android.Arch) Toolchain { + return toolchainDarwinArm64Singleton +} + func darwinX8664ToolchainFactory(arch android.Arch) Toolchain { return toolchainDarwinX8664Singleton } +var toolchainDarwinArm64Singleton Toolchain = &toolchainDarwinArm64{} var toolchainDarwinX8664Singleton Toolchain = &toolchainDarwinX8664{}