-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathversions.bzl
136 lines (120 loc) · 5.11 KB
/
versions.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# check_bazel_version_compatible below cannot be called from anywhere
# because native.bazel_version's availability is restricted:
# https://github.com/bazelbuild/bazel/issues/8305
# An alternative hacky way is as follows:
# https://github.com/protocolbuffers/upb/blob/master/bazel/repository_defs.bzl
#
# There are utility functions for parsing versions numbers here:
# load("@bazel_skylib//lib:versions.bzl", "versions")
# But we don't want to use them, as skylib is not yet loaded when code
# in this file executes (and there's no way to execute it later; see first
# paragraph above).
# It's important to keep this list short enough (not more then 4 items)
# because every bazel version tested requires a lot of space on CI
# See https://github.com/tweag/rules_haskell/pull/1781#issuecomment-1187640454
SUPPORTED_BAZEL_VERSIONS = [
"6.0.0",
"6.3.2",
"6.5.0",
"7.1.0",
]
SUPPORTED_NIXPKGS_BAZEL_PACKAGES = [
"bazel_6",
"bazel_7",
]
def _parse_version_chunk(version_chunk):
"""
Args:
version_chunk: a chunk of a semantic version, possibly with trailing
content at the end; such as "29", "2", or "3 foobar".
We handle trailing content because
native.bazel_version can: try printing actual_version
in check_version below (got for example
"2.0.0- (@non-git)").
Returns:
The chunk string with trailing content removed, such as "29", "2", or "3"
"""
for i in range(len(version_chunk)):
c = version_chunk[i]
if not c.isdigit():
return version_chunk[:i]
return version_chunk
def _parse_bazel_version(bazel_version):
"""
Args:
bazel_version: a string that starts with a semantic version
like "2.0" or "0.29.1" or "0.29 foobar"
Returns:
The version as a int list such as [2, 0], [0, 29, 1], or [0, 29]
"""
return [int(_parse_version_chunk(x)) for x in bazel_version.split(".")]
def is_at_least(threshold, version):
"""Check that a version is higher or equals to a threshold.
Args:
threshold: the minimum version string
version: the version string to be compared to the threshold
Returns:
True if version >= threshold.
"""
# Vendored from https://github.com/bazelbuild/bazel-skylib/blob/e30197f3799eb038fbed424e365573f493d52fa5/lib/versions.bzl
# Needed for check_bazel_version below.
return _parse_bazel_version(version) >= _parse_bazel_version(threshold)
def is_at_most(threshold, version):
"""Check that a version is lower or equals to a threshold.
Args:
threshold: the maximum version string
version: the version string to be compared to the threshold
Returns:
True if version <= threshold.
"""
# Vendored from https://github.com/bazelbuild/bazel-skylib/blob/e30197f3799eb038fbed424e365573f493d52fa5/lib/versions.bzl
# Needed for check_bazel_version below.
return _parse_bazel_version(version) <= _parse_bazel_version(threshold)
def check_bazel_version(minimum_bazel_version, maximum_bazel_version = None, bazel_version = None):
"""Check that the version of Bazel is valid within the specified range.
Args:
minimum_bazel_version: minimum version of Bazel expected
maximum_bazel_version: maximum version of Bazel expected
bazel_version: the version of Bazel to check. Used for testing, defaults to native.bazel_version
Returns:
(bool, string):
bool: `True`, if the version meets the criteria, otherwise `False`.
string: An appropriate message if the version doesn't match.
"""
# Vendored from https://github.com/bazelbuild/bazel-skylib/blob/e30197f3799eb038fbed424e365573f493d52fa5/lib/versions.bzl#L82
# The upstream version `fail`s if the version doesn't match.
# This version instead returns false.
if not bazel_version:
if "bazel_version" not in dir(native):
return (False, "Current Bazel version is lower than 0.2.1; expected at least {}".format(
minimum_bazel_version,
))
elif not native.bazel_version:
# Using a non-release version, assume it is good.
return (True, "")
else:
bazel_version = native.bazel_version
if not is_at_least(
threshold = minimum_bazel_version,
version = bazel_version,
):
return (False, "Current Bazel version is {}; expected at least {}".format(
bazel_version,
minimum_bazel_version,
))
if maximum_bazel_version:
if not is_at_most(
threshold = maximum_bazel_version,
version = bazel_version,
):
return (False, "Current Bazel version is {}; expected at most {}".format(
bazel_version,
maximum_bazel_version,
))
return (True, "")
def check_bazel_version_compatible(actual_version):
min_bazel = SUPPORTED_BAZEL_VERSIONS[0]
max_bazel = SUPPORTED_BAZEL_VERSIONS[-1]
(compatible, msg) = check_bazel_version(min_bazel, max_bazel, actual_version)
if not compatible:
print("WARNING:", msg)