From 5ed3751df534d2be18891ce7a8e2def29789eda3 Mon Sep 17 00:00:00 2001 From: Matthew Clarkson Date: Wed, 22 Apr 2026 09:51:55 +0100 Subject: [PATCH 1/3] feat: add `repo_utils.arch()` Provides the host CPU architecture. Previously available within `repo_utils.platform()`, now an explicit function. Review-by: Matthew Clarkson # gatekeeper --- lib/private/repo_utils.bzl | 62 +++++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/lib/private/repo_utils.bzl b/lib/private/repo_utils.bzl index bd4e2338d..7dc2a1e4b 100644 --- a/lib/private/repo_utils.bzl +++ b/lib/private/repo_utils.bzl @@ -58,10 +58,10 @@ def _get_env_var(rctx, name, default): def _get_home_directory(rctx): return _get_env_var(rctx, "HOME", None) -def _platform(rctx): - """Returns a normalized name of the host os and CPU architecture. +def _arch(rctx): + """Returns a normalized name of the host CPU architecture. - Alias archictures names are normalized: + Alias architectures names are normalized: x86_64 => amd64 aarch64 => arm64 @@ -69,28 +69,24 @@ def _platform(rctx): The result can be used to generate repository names for host toolchain repositories for toolchains that use these normalized names. - Common os & architecture pairs that are returned are, + Common archtecture names are: - - darwin_amd64 - - darwin_arm64 - - linux_amd64 - - linux_arm64 - - linux_s390x - - linux_ppc64le - - windows_amd64 + - amd64 + - arm64 + - s390x + - ppc64le Args: rctx: rctx Returns: - The normalized `_` string of the host os and CPU architecture. + The normalized host CPU architecture string. """ - os = _os(rctx) # NB: in bazel 5.1.1 rctx.os.arch was added which https://github.com/bazelbuild/bazel/commit/32d1606dac2fea730abe174c41870b7ee70ae041. # Once we drop support for anything older than Bazel 5.1.1 than we can simplify # this function. - if os == "windows": + if _os(rctx) == "windows": proc_arch = (_get_env_var(rctx, "PROCESSOR_ARCHITECTURE", "") or _get_env_var(rctx, "PROCESSOR_ARCHITEW6432", "")) if proc_arch == "ARM64": @@ -99,12 +95,43 @@ def _platform(rctx): arch = "amd64" else: arch = rctx.execute(["uname", "-m"]).stdout.strip() - arch_map = { + + aliases = { "x86_64": "amd64", "aarch64": "arm64", } - if arch in arch_map.keys(): - arch = arch_map[arch] + + return aliases.get(arch, arch) + +def _platform(rctx): + """Returns a normalized name of the host os and CPU architecture. + + Alias architectures names are normalized: + + x86_64 => amd64 + aarch64 => arm64 + + The result can be used to generate repository names for host toolchain + repositories for toolchains that use these normalized names. + + Common os & architecture pairs that are returned are, + + - darwin_amd64 + - darwin_arm64 + - linux_amd64 + - linux_arm64 + - linux_s390x + - linux_ppc64le + - windows_amd64 + + Args: + rctx: rctx + + Returns: + The normalized `_` string of the host os and CPU architecture. + """ + os = _os(rctx) + arch = _arch(rctx) return "%s_%s" % (os, arch) repo_utils = struct( @@ -114,5 +141,6 @@ repo_utils = struct( get_env_var = _get_env_var, get_home_directory = _get_home_directory, os = _os, + arch = _arch, platform = _platform, ) From 9b400942a401e5a12773b56c3aeef65beea23e12 Mon Sep 17 00:00:00 2001 From: Matthew Clarkson Date: Wed, 22 Apr 2026 09:53:20 +0100 Subject: [PATCH 2/3] fix(repo_utils.arch): handle failing `uname` execution Review-by: Matthew Clarkson # gatekeeper --- lib/private/repo_utils.bzl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/private/repo_utils.bzl b/lib/private/repo_utils.bzl index 7dc2a1e4b..4722ce5ac 100644 --- a/lib/private/repo_utils.bzl +++ b/lib/private/repo_utils.bzl @@ -94,7 +94,10 @@ def _arch(rctx): else: arch = "amd64" else: - arch = rctx.execute(["uname", "-m"]).stdout.strip() + result = rctx.execute(["uname", "-m"]) + if result.return_code != 0: + fail("uname execution failed: %s" % result.stderr.strip()) + arch = result.stdout.strip() aliases = { "x86_64": "amd64", From 60f67d0eb065889d9ebe50f697afc6fa7f2061f5 Mon Sep 17 00:00:00 2001 From: Matthew Clarkson Date: Wed, 22 Apr 2026 10:01:04 +0100 Subject: [PATCH 3/3] feat(repo_utils.arch): use `rctx.os.arch` where available Available in Bazel 5.1.1+, it exposes the JVM `os.arch` string as lower case. When available this allows avoiding execution of `uname`. There _could_ be a possibility of `rctx.os.arch` not aligning with `uname -m` in all cases. For the common CPU architectures, it does. If there are cases where it diverges, new aliases can be added to align. Review-by: Matthew Clarkson # gatekeeper --- lib/private/repo_utils.bzl | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/private/repo_utils.bzl b/lib/private/repo_utils.bzl index 4722ce5ac..8f91ffbbf 100644 --- a/lib/private/repo_utils.bzl +++ b/lib/private/repo_utils.bzl @@ -82,11 +82,9 @@ def _arch(rctx): Returns: The normalized host CPU architecture string. """ - - # NB: in bazel 5.1.1 rctx.os.arch was added which https://github.com/bazelbuild/bazel/commit/32d1606dac2fea730abe174c41870b7ee70ae041. - # Once we drop support for anything older than Bazel 5.1.1 than we can simplify - # this function. - if _os(rctx) == "windows": + if hasattr(rctx.os, "arch"): + arch = rctx.os.arch + elif _os(rctx) == "windows": proc_arch = (_get_env_var(rctx, "PROCESSOR_ARCHITECTURE", "") or _get_env_var(rctx, "PROCESSOR_ARCHITEW6432", "")) if proc_arch == "ARM64":