Skip to content

Commit 235e733

Browse files
committed
feat: generate doc_extract targets
Allows bzl_library targets to produce documentation without needing to load stardoc or add additional targets. Note, it would be better to do this with an aspect that visits the bzl_library graph, but the starlark_doc_extract rule is in Bazel java core, and so we cannot spawn actions using its Java code. It needs a main() entry point. Use a config_setting to make this opt-in, so we don't generate so many targets all the time.
1 parent 454b259 commit 235e733

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

BUILD

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("@rules_license//rules:license.bzl", "license")
2+
load("//rules:common_settings.bzl", "bool_flag")
23
load("//:bzl_library.bzl", "bzl_library")
34

45
package(
@@ -95,3 +96,16 @@ filegroup(
9596
"//toolchains/unittest:distribution",
9697
] + glob(["*.bzl"]),
9798
)
99+
100+
bool_flag(
101+
name = "extract_docs",
102+
build_setting_default = False,
103+
visibility = ["//visibility:public"],
104+
)
105+
106+
config_setting(
107+
name = "extract_docs_flag",
108+
flag_values = {
109+
":extract_docs": "true",
110+
},
111+
)

bzl_library.bzl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ load(
2323

2424
StarlarkLibraryInfo = _StarlarkLibraryInfo
2525

26-
def bzl_library(name, **kwargs):
26+
def bzl_library(name, srcs = [], deps = [], **kwargs):
2727
"""Wrapper for bzl_library.
2828
2929
Args:
@@ -38,9 +38,22 @@ def bzl_library(name, **kwargs):
3838
_ = kwargs.pop("target_compatible_with", None)
3939
_bzl_library(
4040
name = name,
41+
srcs = srcs,
42+
deps = deps,
4143
compatible_with = [],
4244
exec_compatible_with = [],
4345
features = [],
4446
target_compatible_with = [],
4547
**kwargs
4648
)
49+
enable_doc_extract = select({
50+
Label("//:extract_docs_flag"): hasattr(native, "starlark_doc_extract"),
51+
"//conditions:default": False,
52+
})
53+
if enable_doc_extract:
54+
for i, src in enumerate(srcs):
55+
native.starlark_doc_extract(
56+
name = "{}.doc_extract{}".format(name, i if i > 0 else ""),
57+
src = src,
58+
deps = deps,
59+
)

0 commit comments

Comments
 (0)