Skip to content

Commit

Permalink
Create a symbolic macro if supported (#138)
Browse files Browse the repository at this point in the history
With Bazel 8 and if the wrapped kind is not a legacy macro, create a
symbolic macro instead of a legacy macro.

Also update stardoc to the latest version to get `load` statements.
  • Loading branch information
fmeum authored Dec 6, 2024
1 parent 65b3c02 commit 6e6d4f4
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 10 deletions.
6 changes: 6 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")

# For stardoc to reference the files
exports_files(
["with_cfg.bzl"],
visibility = ["//docs:__pkg__"],
)

bzl_library(
name = "with_cfg",
srcs = ["with_cfg.bzl"],
Expand Down
9 changes: 2 additions & 7 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module(
compatibility_level = 1,
)

bazel_dep(name = "bazel_features", version = "1.22.0")
bazel_dep(name = "bazel_skylib", version = "1.4.1")
bazel_dep(name = "platforms", version = "0.0.6")

Expand All @@ -12,15 +13,9 @@ bazel_dep(name = "bazel_skylib_gazelle_plugin", version = "1.4.1", dev_dependenc
bazel_dep(name = "buildifier_prebuilt", version = "6.4.0", dev_dependency = True)
bazel_dep(name = "gazelle", version = "0.33.0", dev_dependency = True, repo_name = "bazel_gazelle")
bazel_dep(name = "rules_testing", version = "0.4.0", dev_dependency = True)
bazel_dep(name = "stardoc", dev_dependency = True)
git_override(
module_name = "stardoc",
commit = "3baa5d1761970c6285d2ac9c3adccfaac42f54c5",
remote = "https://github.com/bazelbuild/stardoc.git",
)

bazel_dep(name = "rules_java", version = "7.6.1", dev_dependency = True)
bazel_dep(name = "rules_proto", version = "6.0.0", dev_dependency = True)
bazel_dep(name = "stardoc", version = "0.7.2", dev_dependency = True)
bazel_dep(name = "toolchains_protoc", version = "0.3.0", dev_dependency = True)

protoc = use_extension(
Expand Down
2 changes: 1 addition & 1 deletion docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load("@rules_proto//proto:proto_lang_toolchain.bzl", "proto_lang_toolchain")

stardoc_with_diff_test(
name = "defs",
bzl_library_target = "//with_cfg:defs",
bzl_library_target = "//:with_cfg",
)

update_docs(name = "update")
Expand Down
4 changes: 4 additions & 0 deletions docs/defs.md

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

2 changes: 1 addition & 1 deletion with_cfg/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

# For stardoc to reference the files
# Legacy export, no longer used by this module.
exports_files(["defs.bzl"])

bzl_library(
Expand Down
1 change: 1 addition & 0 deletions with_cfg/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,6 @@ bzl_library(
":select",
":setting",
":utils",
"@bazel_features//:features",
],
)
1 change: 1 addition & 0 deletions with_cfg/private/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ RuleInfo = provider(fields = [
"kind",
"native",
"providers",
"supports_inheritance",
"test",
])

Expand Down
5 changes: 5 additions & 0 deletions with_cfg/private/with_cfg.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def with_cfg(
implicit_targets = implicit_targets,
providers = DEFAULT_PROVIDERS + extra_providers,
native = _is_native(kind),
supports_inheritance = _supports_inheritance(kind),
)
return make_builder(rule_info)

Expand Down Expand Up @@ -155,5 +156,9 @@ def is_test(rule_name):
def _is_native(kind):
return str(kind).startswith("<built-in rule ")

def _supports_inheritance(kind):
# Legacy macros don't support inheritance.
return not str(kind).startswith("<function ")

def get_implicit_targets(rule_name):
return IMPLICIT_TARGETS.get(rule_name, [])
9 changes: 8 additions & 1 deletion with_cfg/private/wrapper.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@bazel_features//:features.bzl", "bazel_features")
load(":args.bzl", "rewrite_args")
load(":rewrite.bzl", "make_label_rewriter", "rewrite_locations_in_attr")
load(":select.bzl", "map_attr")
Expand All @@ -15,7 +16,7 @@ def make_wrapper(
values,
original_settings_label,
attrs_to_reset):
return lambda *, name, **kwargs: _wrapper(
wrapper_impl = lambda *, name, **kwargs: _wrapper(
name = name,
kwargs = kwargs,
rule_info = rule_info,
Expand All @@ -25,6 +26,12 @@ def make_wrapper(
original_settings_label = original_settings_label,
attrs_to_reset = attrs_to_reset,
)
if bazel_features.globals.macro and rule_info.supports_inheritance:
return bazel_features.globals.macro(
implementation = wrapper_impl,
inherit_attrs = rule_info.kind,
)
return wrapper_impl

# Attributes common to all rules.
# These attributes are applied to all targets generated by the wrapper.
Expand Down

0 comments on commit 6e6d4f4

Please sign in to comment.