diff --git a/docs/utils.md b/docs/utils.md index 061cef16c..b871681ee 100644 --- a/docs/utils.md +++ b/docs/utils.md @@ -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) + + +## is_bzlmod_enabled + +
+is_bzlmod_enabled()
+
+ +Detect the value of the --enable_bzlmod flag + + + ## is_external_label diff --git a/lib/BUILD.bazel b/lib/BUILD.bazel index e41719a5a..54194fa0f 100644 --- a/lib/BUILD.bazel +++ b/lib/BUILD.bazel @@ -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( diff --git a/lib/private/utils.bzl b/lib/private/utils.bzl index e7c162f43..c6ed8c0b2 100644 --- a/lib/private/utils.bzl +++ b/lib/private/utils.bzl @@ -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, diff --git a/lib/utils.bzl b/lib/utils.bzl index 5459ea358..5c9134bf9 100644 --- a/lib/utils.bzl +++ b/lib/utils.bzl @@ -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