Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: detect bzlmod automatically rather than requiring the user sets… #397

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/utils.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions lib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"])
Expand All @@ -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 = {
Expand All @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions lib/private/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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("@@")
alexeagle marked this conversation as resolved.
Show resolved Hide resolved

def _maybe_http_archive(**kwargs):
"""Adapts a maybe(http_archive, ...) to look like an http_archive.

Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions lib/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down