Skip to content

Commit 42edf3f

Browse files
authored
fix: sync common Bazelisk constants and functions to bazelisks.bzl (#464)
1 parent 61c4f08 commit 42edf3f

File tree

4 files changed

+79
-48
lines changed

4 files changed

+79
-48
lines changed

bazel_integration_test/bzlmod/bazel_binaries.bzl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ load(
77
_bazelisk_binary_repo_rule = "bazelisk_binary",
88
_local_bazel_binary_repo_rule = "local_bazel_binary",
99
)
10+
load("//bazel_integration_test/private:bazelisks.bzl", "bazelisks")
1011
load("//bazel_integration_test/private:no_deps_utils.bzl", "no_deps_utils")
1112

1213
# MARK: - bazel_binaries_helper Repository Rule
@@ -141,9 +142,6 @@ def _declare_local_bazel_binary(local):
141142
)
142143
return vi
143144

144-
# TODO(GH184): Make this configurable.
145-
_BAZELISK_VERSION = "1.25.0"
146-
147145
def _bazel_binaries_impl(module_ctx):
148146
dep_names = []
149147
dev_dep_names = []
@@ -160,7 +158,8 @@ def _bazel_binaries_impl(module_ctx):
160158
bazelisk_repo_name = "bazel_binaries_bazelisk"
161159
_bazelisk_binary_repo_rule(
162160
name = bazelisk_repo_name,
163-
version = _BAZELISK_VERSION,
161+
# TODO(GH184): Make this configurable.
162+
version = bazelisks.DEFAULT_VERSION,
164163
)
165164
_add_dep_name(bazelisk_repo_name, is_dev_dependency = ext_is_dev_dep)
166165

bazel_integration_test/private/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ bzl_library(
4949
name = "bazel_binaries",
5050
srcs = ["bazel_binaries.bzl"],
5151
deps = [
52+
":bazelisks",
5253
":no_deps_utils",
5354
"@bazel_skylib//lib:paths",
5455
"@cgrindel_bazel_starlib//bzllib:defs",
@@ -67,3 +68,8 @@ bzl_library(
6768
":bazel_integration_test",
6869
],
6970
)
71+
72+
bzl_library(
73+
name = "bazelisks",
74+
srcs = ["bazelisks.bzl"],
75+
)

bazel_integration_test/private/bazel_binaries.bzl

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,18 @@
22

33
load("@bazel_skylib//lib:paths.bzl", "paths")
44
load("@cgrindel_bazel_starlib//bzllib:defs.bzl", "lists")
5+
load(":bazelisks.bzl", "bazelisks")
56
load(":no_deps_utils.bzl", "no_deps_utils")
67

78
# Lovingly inspired by https://github.com/bazelbuild/bazel-integration-testing/blob/master/tools/repositories.bzl.
89

910
# MARK: - Helpers
1011

11-
_BAZELISK_URL_TEMPLATE = "https://github.com/bazelbuild/bazelisk/releases/download/v{version}/{filename}"
12-
13-
def _is_linux(os_name):
14-
return os_name.startswith("linux")
15-
16-
def _is_macos(os_name):
17-
return os_name.startswith("mac os")
18-
19-
def _is_windows(os_name):
20-
return os_name.startswith("windows")
21-
22-
def _is_x86_64(arch_name):
23-
return arch_name.startswith("amd64") or arch_name.startswith("x86_64")
24-
25-
def _is_arm(arch_name):
26-
return arch_name.startswith("aarch64") or arch_name.startswith("arm")
27-
2812
def _download_bazelisk_binary(repository_ctx, version):
29-
os_name = repository_ctx.os.name.lower()
30-
arch_name = repository_ctx.os.arch.lower()
31-
32-
if _is_linux(os_name) and _is_x86_64(arch_name):
33-
suffix = "linux-amd64"
34-
elif _is_linux(os_name) and _is_arm(arch_name):
35-
suffix = "linux-arm64"
36-
elif _is_macos(os_name) and _is_x86_64(arch_name):
37-
suffix = "darwin-amd64"
38-
elif _is_macos(os_name) and _is_arm(arch_name):
39-
suffix = "darwin-arm64"
40-
elif _is_windows(os_name) and _is_x86_64(arch_name):
41-
suffix = "windows-amd64.exe"
42-
elif _is_windows(os_name) and _is_arm(arch_name):
43-
suffix = "windows-arm64.exe"
44-
else:
45-
fail("Unrecognized os and arch. os: {}, arch: {}".format(
46-
os_name,
47-
arch_name,
48-
))
49-
50-
filename = "bazelisk-%s" % suffix
51-
url = _BAZELISK_URL_TEMPLATE.format(
13+
url = bazelisks.url(
5214
version = version,
53-
filename = filename,
15+
os = repository_ctx.os.name,
16+
arch = repository_ctx.os.arch,
5417
)
5518
repository_ctx.download(
5619
url = url,
@@ -62,10 +25,10 @@ def get_version_from_file(repository_ctx):
6225
"""Read the Bazel version string from the version file.
6326
6427
Args:
65-
repository_ctx: Repository rule context object.
28+
repository_ctx: Repository rule context object.
6629
6730
Returns:
68-
The first non-empty line of the file with surrounding white space stripped.
31+
The first non-empty line of the file with surrounding white space stripped.
6932
"""
7033
version_file = repository_ctx.attr.version_file
7134
if repository_ctx.attr.version_file == None:
@@ -276,7 +239,7 @@ that load dependencies via the `WORKSPACE`.\
276239

277240
def bazel_binaries(
278241
versions,
279-
bazelisk_version = "1.18.0",
242+
bazelisk_version = bazelisks.DEFAULT_VERSION,
280243
current = None,
281244
name = "bazel_binaries"):
282245
"""Download the specified bazel binaries.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""Module containing Bazelisk constants and utility functions."""
2+
3+
_DEFAULT_VERSION = "1.26.0"
4+
5+
_BAZELISK_URL_TEMPLATE = "https://github.com/bazelbuild/bazelisk/releases/download/v{version}/{filename}"
6+
7+
def _is_linux(os_name):
8+
return os_name.startswith("linux")
9+
10+
def _is_macos(os_name):
11+
return os_name.startswith("mac os")
12+
13+
def _is_windows(os_name):
14+
return os_name.startswith("windows")
15+
16+
def _is_x86_64(arch_name):
17+
return arch_name.startswith("amd64") or arch_name.startswith("x86_64")
18+
19+
def _is_arm(arch_name):
20+
return arch_name.startswith("aarch64") or arch_name.startswith("arm")
21+
22+
def _url(os, arch, version = _DEFAULT_VERSION):
23+
"""Provide the Bazelisk URL for a specific version, OS, arch.
24+
25+
Args:
26+
os: The OS name as a `string`.
27+
arch: The arch as a `string`.
28+
version: Optional. The version as a `string`. Uses the default version
29+
if not specified.
30+
31+
Returns:
32+
A URl as a `string`.
33+
"""
34+
os_name = os.lower()
35+
arch_name = arch.lower()
36+
if _is_linux(os_name) and _is_x86_64(arch_name):
37+
suffix = "linux-amd64"
38+
elif _is_linux(os_name) and _is_arm(arch_name):
39+
suffix = "linux-arm64"
40+
elif _is_macos(os_name) and _is_x86_64(arch_name):
41+
suffix = "darwin-amd64"
42+
elif _is_macos(os_name) and _is_arm(arch_name):
43+
suffix = "darwin-arm64"
44+
elif _is_windows(os_name) and _is_x86_64(arch_name):
45+
suffix = "windows-amd64.exe"
46+
elif _is_windows(os_name) and _is_arm(arch_name):
47+
suffix = "windows-arm64.exe"
48+
else:
49+
fail("Unrecognized os and arch. os: {}, arch: {}".format(
50+
os_name,
51+
arch_name,
52+
))
53+
54+
filename = "bazelisk-%s" % suffix
55+
return _BAZELISK_URL_TEMPLATE.format(
56+
version = version,
57+
filename = filename,
58+
)
59+
60+
bazelisks = struct(
61+
DEFAULT_VERSION = _DEFAULT_VERSION,
62+
url = _url,
63+
)

0 commit comments

Comments
 (0)