diff --git a/toolchain/defs.bzl b/toolchain/defs.bzl index f185737..bc0babe 100644 --- a/toolchain/defs.bzl +++ b/toolchain/defs.bzl @@ -1,5 +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(":zig_toolchain.bzl", "zig_cc_toolchain_config") DEFAULT_TOOL_PATHS = { @@ -240,10 +241,10 @@ def _zig_repository_impl(repository_ctx): "host_platform": host_platform, } - url_formats = repository_ctx.attr.url_formats - + urls = [uf.format(**format_vars) for uf in repository_ctx.attr.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, ) @@ -398,3 +399,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)