Subject: [PATCH bazel-zig-cc 0/3] Download improvments

From: ~kmicklas <[email-masked]>
Date: Thu, 17 Mar 2022 20:04:29 +0000

This adds support for netrc and multiple download mirrors.

Ken Micklas (3):
  Pull default SHA256s into constant and don't update
  Support multiple URL formats
  Support netrc for Zig SDK download

 toolchain/defs.bzl | 66 +++++++++++++++++++++++++++++++++++-----------
 1 file changed, 50 insertions(+), 16 deletions(-)

-- 
2.34.1

---

From: ~kmicklas <[email-masked]>
Date: Thu, 17 Mar 2022 15:11:17 -0400

From: Ken Micklas <[email-masked]>

Starlark's default argument semantics are pretty weird and likely not
what you would expect:
https://github.com/bazelbuild/starlark/blob/master/spec.md#functions

Additionally, this is just easier to read.
---
 toolchain/defs.bzl | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/toolchain/defs.bzl b/toolchain/defs.bzl
index 8ed93c7..f942c5b 100644
--- a/toolchain/defs.bzl
+++ b/toolchain/defs.bzl
@@ -158,29 +158,28 @@ _URL_FORMAT_JAKSTYS = "https://dl.jakstys.lt/zig/zig-{host_platform}-{version}.t
 
 _VERSION = "0.10.0-dev.513+029844210"
 
+_HOST_PLATFORM_SHA256 = {
+    "linux-aarch64": "fc0b929948d57d1b15aaac33db0cd136a1f79830926c9285fd3f5b6445f1ef95",
+    "linux-x86_64": "c6f323b02b0bcf7b18143983bf2525640dd541f39af661e963823c9a61a09b28",
+    "macos-aarch64": "0ccd8dad1264474ca824d45c3021b7e549b67792fa1a66cb9d4d7040775319bb",
+    "macos-x86_64": "3c939bebe8648c277ee66d1c600e68576e382d0309dc29eb4c32aba96b83f6f0",
+}
+
 def register_toolchains(
         register = [],
         version = _VERSION,
         url_format = _URL_FORMAT_JAKSTYS,
-        host_platform_sha256 = {}):
+        host_platform_sha256 = _HOST_PLATFORM_SHA256):
     """
         Download zig toolchain and register some.
         @param register registers the given toolchains to the system using
         native.register_toolchains(). See README for possible choices.
     """
-    sha256s = {
-        "linux-aarch64": "fc0b929948d57d1b15aaac33db0cd136a1f79830926c9285fd3f5b6445f1ef95",
-        "linux-x86_64": "c6f323b02b0bcf7b18143983bf2525640dd541f39af661e963823c9a61a09b28",
-        "macos-aarch64": "0ccd8dad1264474ca824d45c3021b7e549b67792fa1a66cb9d4d7040775319bb",
-        "macos-x86_64": "3c939bebe8648c277ee66d1c600e68576e382d0309dc29eb4c32aba96b83f6f0",
-    }
-    sha256s.update(host_platform_sha256)
-
     zig_repository(
         name = "zig_sdk",
         version = version,
         url_format = url_format,
-        host_platform_sha256 = sha256s,
+        host_platform_sha256 = host_platform_sha256,
         host_platform_include_root = {
             "linux-aarch64": "lib/",
             "linux-x86_64": "lib/",
-- 
2.34.1

---

From: ~kmicklas <[email-masked]>
Date: Thu, 17 Mar 2022 15:39:41 -0400

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

diff --git a/toolchain/defs.bzl b/toolchain/defs.bzl
index f942c5b..f883cc2 100644
--- a/toolchain/defs.bzl
+++ b/toolchain/defs.bzl
@@ -168,7 +168,8 @@ _HOST_PLATFORM_SHA256 = {
 def register_toolchains(
         register = [],
         version = _VERSION,
-        url_format = _URL_FORMAT_JAKSTYS,
+        url_format = [_URL_FORMAT_JAKSTYS],
+        url_formats = [],
         host_platform_sha256 = _HOST_PLATFORM_SHA256):
     """
         Download zig toolchain and register some.
@@ -178,7 +179,8 @@ def register_toolchains(
     zig_repository(
         name = "zig_sdk",
         version = version,
-        url_format = url_format,
+        url_format = url_format if type(url_format) == type("") else None,
+        url_formats = url_format if type(url_format) == type([""]) else None,
         host_platform_sha256 = host_platform_sha256,
         host_platform_include_root = {
             "linux-aarch64": "lib/",
@@ -239,10 +241,17 @@ def _zig_repository_impl(repository_ctx):
         "version": repository_ctx.attr.version,
         "host_platform": host_platform,
     }
-    zig_url = repository_ctx.attr.url_format.format(**format_vars)
+
+    url_format = repository_ctx.attr.url_format
+    url_formats = repository_ctx.attr.url_formats
+
+    if len(url_formats) == 0:
+        url_formats = [url_format]
+    elif url_format != "":
+        fail("Only one of url_format and url_formats should be specified.")
 
     repository_ctx.download_and_extract(
-        url = zig_url,
+        url = [uf.format(**format_vars) for uf in url_formats],
         stripPrefix = "zig-{host_platform}-{version}/".format(**format_vars),
         sha256 = zig_sha256,
     )
@@ -279,7 +288,8 @@ zig_repository = repository_rule(
     attrs = {
         "version": attr.string(),
         "host_platform_sha256": attr.string_dict(),
-        "url_format": attr.string(),
+        "url_format": attr.string(doc = "Deprecated in favor of url_formats"),
+        "url_formats": attr.string_list(),
         "host_platform_include_root": attr.string_dict(),
     },
     implementation = _zig_repository_impl,
-- 
2.34.1

---

From: ~kmicklas <[email-masked]>
Date: Thu, 17 Mar 2022 15:47:45 -0400

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

diff --git a/toolchain/defs.bzl b/toolchain/defs.bzl
index f883cc2..0ea7a26 100644
--- a/toolchain/defs.bzl
+++ b/toolchain/defs.bzl
@@ -1,5 +1,5 @@
 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(":zig_toolchain.bzl", "zig_cc_toolchain_config")
 
 DEFAULT_TOOL_PATHS = {
@@ -250,8 +250,11 @@ def _zig_repository_impl(repository_ctx):
     elif url_format != "":
         fail("Only one of url_format and url_formats should be specified.")
 
+    urls = [uf.format(**format_vars) for uf in url_formats]
+
     repository_ctx.download_and_extract(
-        url = [uf.format(**format_vars) for uf in url_formats],
+        auth = use_netrc(read_user_netrc(repository_ctx), urls, {}),
+        url = urls,
         stripPrefix = "zig-{host_platform}-{version}/".format(**format_vars),
         sha256 = zig_sha256,
     )
@@ -407,3 +410,25 @@ 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: "builds.sr.ht" <[email-masked]>
Date: Thu, 17 Mar 2022 20:17:53 +0000

bazel-zig-cc/patches/.build.yml: SUCCESS in 13m21s

[Download improvments][0] from [~kmicklas][1]

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

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