Subject: [PATCH bazel-zig-cc 0/2] Use -mcpu=apple_m1 when targeting macos + aarch64

From: ~jvolkman <[email-masked]>
Date: Sat, 17 Dec 2022 19:28:05 +0000

I ran into a build failure using bazel-zig-cc to build BoringSSL -
Google's OpenSSL fork that ships with Bazel rules. BoringSSL assumes
some features present on an M1 mac and fails if they're not present [1].

When using -target aarch64-macos.11-none, zig sets the cpu model to
"generic", as far as I can tell, which lacks a bunch of features -
including SHA2 and AES that BoringSSL is expecting. I think we can
assume that macos + aarch64 means at least an M1 ship, so instead we can
use the -mcpu=apple_m1 flag which adds these features and more [3].

I also renamed _target_darwin to _target_macos to align with the Bazel
os name. I think darwin technically lies under iOS and Apple's other
mobile OSs as well.

1:
https://github.com/google/boringssl/blob/97dd962a20b1d19c9e569cf8756fbcfc48ff7c73/crypto/cpu_aarch64_apple.c#L56
2:
https://github.com/ziglang/zig/blob/90477e5c10c9c263a3c0038e1ae7b814d2c5397e/lib/std/target/aarch64.zig#L2031
3:
https://github.com/ziglang/zig/blob/90477e5c10c9c263a3c0038e1ae7b814d2c5397e/lib/std/target/aarch64.zig#L1507

Jeremy Volkman (2):
  Rename _target_darwin to _target_macos
  Use -mcpu=apple_m1 when targeting macos + aarch64

 toolchain/private/defs.bzl | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

-- 
2.34.5

---

From: ~jvolkman <[email-masked]>
Date: Sat, 17 Dec 2022 10:19:28 -0800

From: Jeremy Volkman <[email-masked]>

This aligns with bazel's @platforms//os:macos
---
 toolchain/private/defs.bzl | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/toolchain/private/defs.bzl b/toolchain/private/defs.bzl
index 96830ca..8387ce6 100644
--- a/toolchain/private/defs.bzl
+++ b/toolchain/private/defs.bzl
@@ -39,14 +39,14 @@ def zig_tool_path(os):
 def target_structs():
     ret = []
     for zigcpu, gocpu in (("x86_64", "amd64"), ("aarch64", "arm64")):
-        ret.append(_target_darwin(gocpu, zigcpu))
+        ret.append(_target_macos(gocpu, zigcpu))
         ret.append(_target_windows(gocpu, zigcpu))
         ret.append(_target_linux_musl(gocpu, zigcpu))
         for glibc in _GLIBCS:
             ret.append(_target_linux_gnu(gocpu, zigcpu, glibc))
     return ret
 
-def _target_darwin(gocpu, zigcpu):
+def _target_macos(gocpu, zigcpu):
     min_os = "11"
     return struct(
         gotarget = "darwin_{}".format(gocpu),
-- 
2.34.5

---

From: ~jvolkman <[email-masked]>
Date: Sat, 17 Dec 2022 10:26:04 -0800

---
 toolchain/private/defs.bzl | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/toolchain/private/defs.bzl b/toolchain/private/defs.bzl
index 8387ce6..8571621 100644
--- a/toolchain/private/defs.bzl
+++ b/toolchain/private/defs.bzl
@@ -48,6 +48,11 @@ def target_structs():
 
 def _target_macos(gocpu, zigcpu):
     min_os = "11"
+    copts = []
+
+    if zigcpu == "aarch64":
+        copts = ["-mcpu=apple_m1"]
+
     return struct(
         gotarget = "darwin_{}".format(gocpu),
         zigtarget = "{}-macos-none".format(zigcpu),
@@ -59,7 +64,7 @@ def _target_macos(gocpu, zigcpu):
             "libc/include/any-macos-any",
         ] + _INCLUDE_TAIL,
         dynamic_library_linkopts = ["-Wl,-undefined=dynamic_lookup"],
-        copts = [],
+        copts = copts,
         libc = "darwin",
         bazel_target_cpu = "darwin",
         constraint_values = [
-- 
2.34.5

---

From: "builds.sr.ht" <[email-masked]>
Date: Sat, 17 Dec 2022 19:46:20 +0000

bazel-zig-cc/patches/.build.yml: SUCCESS in 18m11s

[Use -mcpu=apple_m1 when targeting macos + aarch64][0] from [~jvolkman][1]

[0]: https://lists.sr.ht/~motiejus/bazel-zig-cc/patches/37601
[1]: mailto:[email-masked]

✓ #906945 SUCCESS bazel-zig-cc/patches/.build.yml https://builds.sr.ht/~motiejus/job/906945