Subject: [PATCH bazel-zig-cc 0/3] Support ARM64 Mac

From: ~kmicklas <[email-masked]>
Date: Thu, 10 Feb 2022 23:32:57 +0000

With this I'm able to build a simple `cc_library` target on an M1.

Ken Micklas (3):
  Fix darwin arm64 case in .envrc for loading bazel
  Interpret arm64 output from uname as aarch64 on mac OS
  Fix and adjust darwin libc header paths depending on CPU

 .envrc             |  2 +-
 toolchain/defs.bzl | 21 +++++++++++++--------
 2 files changed, 14 insertions(+), 9 deletions(-)

-- 
2.32.0

---

From: ~kmicklas <[email-masked]>
Date: Thu, 10 Feb 2022 18:20:56 -0500

From: Ken Micklas <[email-masked]>

---
 .envrc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.envrc b/.envrc
index 802d197..2b1d835 100644
--- a/.envrc
+++ b/.envrc
@@ -17,7 +17,7 @@ case "$(uname | tr A-Z a-z)-$(uname -m)" in
         bzl=$(direnv fetchurl "${_u}linux-arm64" sha256-wd5oYN1PjV4uwnAJe9RtaiEblxoLizhVl4S9BR6pUKE=);;
     darwin-x86_64)
         bzl=$(direnv fetchurl "${_u}darwin-amd64" sha256-5IW7+EUy0CpgsOsjxwJhC1QI3zoZkIek8rXgmVu/LVo=);;
-    darwin-aarch64)
+    darwin-arm64)
         bzl=$(direnv fetchurl "${_u}darwin-arm64" sha256-wi1IYBRm2dOwQ8zXQFHy9CMPm59FCfCXAXyXMDqojRM=);;
     *)
         >&2 echo "unsupported architecture tuple $(uname | tr A-Z a-z)-$(uname -m)"
-- 
2.32.0

---

From: ~kmicklas <[email-masked]>
Date: Thu, 10 Feb 2022 18:22:37 -0500

---
 toolchain/defs.bzl | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/toolchain/defs.bzl b/toolchain/defs.bzl
index 693e2da..2b572d3 100644
--- a/toolchain/defs.bzl
+++ b/toolchain/defs.bzl
@@ -222,12 +222,15 @@ def _zig_repository_impl(repository_ctx):
     res = repository_ctx.execute(["uname", "-m"])
     if res.return_code != 0:
         fail("failed to run uname -m")
-    uname = res.stdout.strip()
+    arch = res.stdout.strip()
 
+    os = "linux"
     if repository_ctx.os.name.lower().startswith("mac os"):
-        host_platform = "macos-{}".format(uname)
-    else:
-        host_platform = "linux-{}".format(uname)
+        os = "macos"
+        if arch == "arm64":
+            # uname -m reports arm64 on an M1 Mac.
+            arch = "aarch64"
+    host_platform = "{}-{}".format(os, arch)
 
     zig_include_root = repository_ctx.attr.host_platform_include_root[host_platform]
     zig_sha256 = repository_ctx.attr.host_platform_sha256[host_platform]
-- 
2.32.0

---

From: ~kmicklas <[email-masked]>
Date: Thu, 10 Feb 2022 18:24:21 -0500

---
 toolchain/defs.bzl | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/toolchain/defs.bzl b/toolchain/defs.bzl
index 2b572d3..8ed93c7 100644
--- a/toolchain/defs.bzl
+++ b/toolchain/defs.bzl
@@ -54,15 +54,17 @@ _GLIBCS = [
 ]
 
 def _target_darwin(gocpu, zigcpu):
+    min_os = "10"
+    if zigcpu == "aarch64":
+        min_os = "11"
     return struct(
         gotarget = "darwin_{}".format(gocpu),
         zigtarget = "{}-macos-gnu".format(zigcpu),
         includes = [
             "libunwind/include",
-            # FIXME: add macos.10, macos.11 and macos.12 targets,
-            # and adjust the includes
-            "libc/include/{}-macos.10-gnu".format(zigcpu),
-            "libc/include/{}-macos-any".format(zigcpu),
+            # TODO: Define a toolchain for each minimum OS version
+            "libc/include/{}-macos.{}-gnu".format(zigcpu, min_os),
+            "libc/include/any-macos.{}-any".format(min_os),
             "libc/include/any-macos-any",
         ],
         linkopts = [],
-- 
2.32.0

---

From: "builds.sr.ht" <[email-masked]>
Date: Thu, 10 Feb 2022 23:45:58 +0000

bazel-zig-cc/patches/.build.yml: SUCCESS in 12m57s

[Support ARM64 Mac][0] from [~kmicklas][1]

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

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

---

From: Motiejus Jakštys <[email-masked]>
Date: Fri, 11 Feb 2022 06:33:56 +0200

>   Fix and adjust darwin libc header paths depending on CPU

Thank you. Applied all 3 patches and tagged v0.4.5.

cc Joe, who had problems with M1 before. These are now fixed.

Motiejus