Subject: [PATCH bazel-zig-cc 0/4] Upgrade to Bazel 5.1.0 and associated cleanups

From: ~kmicklas <[email-masked]>
Date: Fri, 25 Mar 2022 15:53:45 +0000

Also pins the Go SDK version.

Ken Micklas (4):
  Upgrade to Bazel 5.1.0
  Remove repo fetch-time uname dependency, use new os.arch instead
  Use upstream read_user_netrc from Bazel 5.1
  Pin Go SDK to 1.18 for reproducibility

 .bazelversion      |  2 +-
 WORKSPACE          |  2 +-
 toolchain/defs.bzl | 41 ++++++++---------------------------------
 3 files changed, 10 insertions(+), 35 deletions(-)

-- 
2.34.1

---

From: ~kmicklas <[email-masked]>
Date: Fri, 25 Mar 2022 11:45:39 -0400

From: Ken Micklas <[email-masked]>

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

diff --git a/.bazelversion b/.bazelversion
index af8c8ec..831446c 100644
--- a/.bazelversion
+++ b/.bazelversion
@@ -1 +1 @@
-4.2.2
+5.1.0
-- 
2.34.1

---

From: ~kmicklas <[email-masked]>
Date: Fri, 25 Mar 2022 11:46:11 -0400

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

diff --git a/toolchain/defs.bzl b/toolchain/defs.bzl
index 810db50..840976d 100644
--- a/toolchain/defs.bzl
+++ b/toolchain/defs.bzl
@@ -221,17 +221,14 @@ _ZIG_TOOLS = [
 ]
 
 def _zig_repository_impl(repository_ctx):
-    res = repository_ctx.execute(["uname", "-m"])
-    if res.return_code != 0:
-        fail("failed to run uname -m")
-    arch = res.stdout.strip()
+    arch = repository_ctx.os.arch
+    if arch == "amd64":
+        arch = "x86_64"
 
-    os = "linux"
-    if repository_ctx.os.name.lower().startswith("mac os"):
+    os = repository_ctx.os.name.lower()
+    if os.startswith("mac os"):
         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]
-- 
2.34.1

---

From: ~kmicklas <[email-masked]>
Date: Fri, 25 Mar 2022 11:46:59 -0400

---
 toolchain/defs.bzl | 26 ++------------------------
 1 file changed, 2 insertions(+), 24 deletions(-)

diff --git a/toolchain/defs.bzl b/toolchain/defs.bzl
index 840976d..112a64a 100644
--- a/toolchain/defs.bzl
+++ b/toolchain/defs.bzl
@@ -1,6 +1,6 @@
 load("@bazel_skylib//lib:shell.bzl", "shell")
 load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
-load("@bazel_tools//tools/build_defs/repo:utils.bzl", "read_netrc", "use_netrc")
+load("@bazel_tools//tools/build_defs/repo:utils.bzl", "read_user_netrc", "use_netrc")
 load(":zig_toolchain.bzl", "zig_cc_toolchain_config")
 
 DEFAULT_TOOL_PATHS = {
@@ -240,7 +240,7 @@ def _zig_repository_impl(repository_ctx):
 
     urls = [uf.format(**format_vars) for uf in repository_ctx.attr.url_formats]
     repository_ctx.download_and_extract(
-        auth = use_netrc(_read_user_netrc(repository_ctx), urls, {}),
+        auth = use_netrc(read_user_netrc(repository_ctx), urls, {}),
         url = urls,
         stripPrefix = "zig-{host_platform}-{version}/".format(**format_vars),
         sha256 = zig_sha256,
@@ -396,25 +396,3 @@ def zig_build_macro(absolute_path, zig_include_root):
                     name = "{os}_{gocpu}_platform".format(os = os, gocpu = gocpu),
                     constraint_values = constraint_values,
                 )
-
-# Copied from https://github.com/bazelbuild/bazel/blob/master/tools/build_defs/repo/utils.bzl
-# TODO: Upgrade to Bazel 5.1 and import this instead.
-def _read_user_netrc(ctx):
-    """Read user's default netrc file.
-    Args:
-      ctx: The repository context of the repository rule calling this utility function.
-    Returns:
-      dict mapping a machine names to a dict with the information provided about them.
-    """
-    if ctx.os.name.startswith("windows"):
-        home_dir = ctx.os.environ.get("USERPROFILE", "")
-    else:
-        home_dir = ctx.os.environ.get("HOME", "")
-
-    if not home_dir:
-        return {}
-
-    netrcfile = "{}/.netrc".format(home_dir)
-    if not ctx.path(netrcfile).exists:
-        return {}
-    return read_netrc(ctx, netrcfile)
-- 
2.34.1

---

From: ~kmicklas <[email-masked]>
Date: Fri, 25 Mar 2022 11:47:10 -0400

---
 WORKSPACE | 2 +-

diff --git a/WORKSPACE b/WORKSPACE
index eeb098a..0aff5db 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -28,7 +28,7 @@ load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
 go_rules_dependencies()
 
 # use latest stable.
-go_download_sdk(name = "go_sdk")
+go_download_sdk(name = "go_sdk", version = "1.18")
 
 go_register_toolchains()
 
-- 
2.34.1

---

From: "builds.sr.ht" <[email-masked]>
Date: Fri, 25 Mar 2022 16:07:49 +0000

bazel-zig-cc/patches/.build.yml: FAILED in 14m1s

[Upgrade to Bazel 5.1.0 and associated cleanups][0] from [~kmicklas][1]

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

✗ #722785 FAILED bazel-zig-cc/patches/.build.yml https://builds.sr.ht/~motiejus/job/722785

---

From: Motiejus Jakštys <[email-masked]>
Date: Sat, 26 Mar 2022 16:06:35 +0200

> +go_download_sdk(name = "go_sdk", version = "1.18")

I was on the edge about this myself, since that would require us to
upgrade it with every go version, even patch ones. But I guess you're
right, leaving it like this for reproducibility.

Motiejus

---

From: Motiejus Jakštys <[email-masked]>
Date: Sat, 26 Mar 2022 16:07:17 +0200

>   Pin Go SDK to 1.18 for reproducibility

Thanks, applied all of them (+buildifier). Do you want me to cut v0.5.1?

Motiejus