feat: add is_bazel_6_or_greater utility function (#264)

This commit is contained in:
Greg Magolan 2022-10-26 12:30:08 -07:00 committed by GitHub
parent f242667058
commit 35623e23fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 83 additions and 5 deletions

41
docs/utils.md generated
View File

@ -82,6 +82,47 @@ glob_directories(<a href="#glob_directories-include">include</a>, <a href="#glob
| <a id="glob_directories-kwargs"></a>kwargs | <p align="center"> - </p> | none |
<a id="is_bazel_6_or_greater"></a>
## is_bazel_6_or_greater
<pre>
is_bazel_6_or_greater()
</pre>
Detects if the Bazel version being used is greater than or equal to 6 (including Bazel 6 pre-releases and RCs).
Detecting Bazel 6 or greater is particularly useful in rules as slightly different code paths may be needed to
support bzlmod which was added in Bazel 6.
Unlike the undocumented `native.bazel_version`, which only works in WORKSPACE and repository rules, this function can
be used in rules and BUILD files.
An alternate approach to make the Bazel version available in BUILD files and rules would be to
use the [host_repo](https://github.com/aspect-build/bazel-lib/blob/main/docs/host_repo.md) repository rule
which contains the bazel_version in the exported `host` struct:
WORKSPACE:
```
load("@aspect_bazel_lib//lib:host_repo.bzl", "host_repo")
host_repo(name = "aspect_bazel_lib_host")
```
BUILD.bazel:
```
load("@aspect_bazel_lib_host//:defs.bzl", "host")
print(host.bazel_version)
```
That approach, however, incurs a cost in the user's WORKSPACE.
**RETURNS**
True if the Bazel version being used is greater than or equal to 6 (including pre-releases and RCs)
<a id="is_external_label"></a>
## is_external_label

View File

@ -150,12 +150,48 @@ def _default_timeout(size, timeout):
return timeout
def _is_bazel_6_or_greater():
"""Detects if the Bazel version being used is greater than or equal to 6 (including Bazel 6 pre-releases and RCs).
Detecting Bazel 6 or greater is particularly useful in rules as slightly different code paths may be needed to
support bzlmod which was added in Bazel 6.
Unlike the undocumented `native.bazel_version`, which only works in WORKSPACE and repository rules, this function can
be used in rules and BUILD files.
An alternate approach to make the Bazel version available in BUILD files and rules would be to
use the [host_repo](https://github.com/aspect-build/bazel-lib/blob/main/docs/host_repo.md) repository rule
which contains the bazel_version in the exported `host` struct:
WORKSPACE:
```
load("@aspect_bazel_lib//lib:host_repo.bzl", "host_repo")
host_repo(name = "aspect_bazel_lib_host")
```
BUILD.bazel:
```
load("@aspect_bazel_lib_host//:defs.bzl", "host")
print(host.bazel_version)
```
That approach, however, incurs a cost in the user's WORKSPACE.
Returns:
True if the Bazel version being used is greater than or equal to 6 (including pre-releases and RCs)
"""
# Hacky way to check if the we're using at least Bazel 6. Would be nice if there was a ctx.bazel_version instead.
# native.bazel_version only works in repository rules.
return "apple_binary" not in dir(native)
utils = struct(
is_external_label = _is_external_label,
default_timeout = _default_timeout,
file_exists = _file_exists,
glob_directories = _glob_directories,
is_bazel_6_or_greater = _is_bazel_6_or_greater,
is_external_label = _is_external_label,
path_to_workspace_root = _path_to_workspace_root,
propagate_well_known_tags = _propagate_well_known_tags,
to_label = _to_label,
default_timeout = _default_timeout,
)

View File

@ -2,10 +2,11 @@
load("//lib/private:utils.bzl", "utils")
is_external_label = utils.is_external_label
default_timeout = utils.default_timeout
file_exists = utils.file_exists
glob_directories = utils.glob_directories
is_bazel_6_or_greater = utils.is_bazel_6_or_greater
is_external_label = utils.is_external_label
path_to_workspace_root = utils.path_to_workspace_root
propagate_well_known_tags = utils.propagate_well_known_tags
to_label = utils.to_label
file_exists = utils.file_exists
default_timeout = utils.default_timeout