feat: detect bzlmod automatically rather than requiring the user sets the flag.
Thanks to @aherrmann for pointing out this is possible: https://github.com/aherrmann/demo-bazel-detect-bzlmod-config-setting
This commit is contained in:
parent
f2bb1d1519
commit
5162ae4d8c
|
@ -155,6 +155,18 @@ That approach, however, incurs a cost in the user's WORKSPACE.
|
|||
True if the Bazel version being used is greater than or equal to 6 (including pre-releases and RCs)
|
||||
|
||||
|
||||
<a id="is_bzlmod_enabled"></a>
|
||||
|
||||
## is_bzlmod_enabled
|
||||
|
||||
<pre>
|
||||
is_bzlmod_enabled()
|
||||
</pre>
|
||||
|
||||
Detect the value of the --enable_bzlmod flag
|
||||
|
||||
|
||||
|
||||
<a id="is_external_label"></a>
|
||||
|
||||
## is_external_label
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
|
||||
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
|
||||
load("//lib/private:stamping.bzl", "stamp_build_setting")
|
||||
load("//lib:utils.bzl", "is_bzlmod_enabled")
|
||||
|
||||
# Ensure that users building their own rules can dep on our bzl_library targets for their stardoc
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
@ -16,11 +17,8 @@ exports_files(
|
|||
# Macro that creates targets enabling the use of `--stamp` in Starlark rules
|
||||
stamp_build_setting(name = "stamp")
|
||||
|
||||
# Add a flag indicating that bzlmod is enabled so that we can use select()
|
||||
# to skip bzlmod-incompatible targets. Note that Bazel does not currently
|
||||
# support declaring a config_setting directly on --enable_bzlmod, so we
|
||||
# must use set this flag instead.
|
||||
|
||||
# Add a config indicating that bzlmod is enabled so that we can use select()
|
||||
# to skip bzlmod-incompatible targets.
|
||||
config_setting(
|
||||
name = "bzlmod",
|
||||
flag_values = {
|
||||
|
@ -30,7 +28,7 @@ config_setting(
|
|||
|
||||
bool_flag(
|
||||
name = "flag_bzlmod",
|
||||
build_setting_default = False,
|
||||
build_setting_default = True if is_bzlmod_enabled() else False,
|
||||
)
|
||||
|
||||
toolchain_type(
|
||||
|
|
|
@ -202,6 +202,10 @@ def _is_bazel_6_or_greater():
|
|||
# native.bazel_version only works in repository rules.
|
||||
return "apple_binary" not in dir(native)
|
||||
|
||||
def is_bzlmod_enabled():
|
||||
"""Detect the value of the --enable_bzlmod flag"""
|
||||
return str(Label("@//:BUILD.bazel")).startswith("@@")
|
||||
|
||||
def _maybe_http_archive(**kwargs):
|
||||
"""Adapts a maybe(http_archive, ...) to look like an http_archive.
|
||||
|
||||
|
@ -245,6 +249,7 @@ utils = struct(
|
|||
file_exists = _file_exists,
|
||||
glob_directories = _glob_directories,
|
||||
is_bazel_6_or_greater = _is_bazel_6_or_greater,
|
||||
is_bzlmod_enabled = is_bzlmod_enabled,
|
||||
is_external_label = _is_external_label,
|
||||
maybe_http_archive = _maybe_http_archive,
|
||||
path_to_workspace_root = _path_to_workspace_root,
|
||||
|
|
|
@ -6,6 +6,7 @@ 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_bzlmod_enabled = utils.is_bzlmod_enabled
|
||||
is_external_label = utils.is_external_label
|
||||
maybe_http_archive = utils.maybe_http_archive
|
||||
path_to_workspace_root = utils.path_to_workspace_root
|
||||
|
|
Loading…
Reference in New Issue