Skip to content

Commit

Permalink
Allow stardoc to be a java_binary or jar
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Apr 28, 2023
1 parent dcd2d8b commit bf6632e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
4 changes: 2 additions & 2 deletions docs/stardoc_rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ Generates documentation for exported starlark rule definitions in a target starl
| <a id="stardoc-format"></a>format | The format of the output file. Valid values: 'markdown' or 'proto'. | <code>"markdown"</code> |
| <a id="stardoc-symbol_names"></a>symbol_names | A list of symbol names to generate documentation for. These should correspond to the names of rule definitions in the input file. If this list is empty, then documentation for all exported rule definitions will be generated. | <code>[]</code> |
| <a id="stardoc-semantic_flags"></a>semantic_flags | A list of canonical flags to affect Starlark semantics for the Starlark interpreter during documentation generation. This should only be used to maintain compatibility with non-default semantic flags required to use the given Starlark symbols.<br><br>For example, if <code>//foo:bar.bzl</code> does not build except when a user would specify <code>--incompatible_foo_semantic=false</code>, then this attribute should contain "--incompatible_foo_semantic=false". | <code>[]</code> |
| <a id="stardoc-stardoc"></a>stardoc | The location of the stardoc tool. | <code>Label("//stardoc:prebuilt_stardoc_binary")</code> |
| <a id="stardoc-renderer"></a>renderer | The location of the renderer tool. | <code>Label("//stardoc:renderer")</code> |
| <a id="stardoc-stardoc"></a>stardoc | The location of the stardoc tool (a <code>java_binary</code> target or a <code>.jar</code> file). | <code>Label("//stardoc:stardoc")</code> |
| <a id="stardoc-renderer"></a>renderer | The location of the renderer tool (a <code>java_binary</code> target or a <code>.jar</code> file). | <code>Label("//stardoc:renderer")</code> |
| <a id="stardoc-aspect_template"></a>aspect_template | The input file template for generating documentation of aspects | <code>Label("//stardoc:templates/markdown_tables/aspect.vm")</code> |
| <a id="stardoc-func_template"></a>func_template | The input file template for generating documentation of functions. | <code>Label("//stardoc:templates/markdown_tables/func.vm")</code> |
| <a id="stardoc-header_template"></a>header_template | The input file template for the header of the output documentation. | <code>Label("//stardoc:templates/markdown_tables/header.vm")</code> |
Expand Down
2 changes: 1 addition & 1 deletion stardoc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ java_binary(
java_import(
name = "prebuilt_stardoc_binary",
jars = ["stardoc_binary.jar"],
visibility = ["//visibility:public"],
visibility = ["//visibility:private"],
)

java_binary(
Expand Down
29 changes: 25 additions & 4 deletions stardoc/stardoc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def stardoc(
format = "markdown",
symbol_names = [],
semantic_flags = [],
stardoc = Label("//stardoc:prebuilt_stardoc_binary"),
stardoc = Label("//stardoc"),
renderer = Label("//stardoc:renderer"),
aspect_template = Label("//stardoc:templates/markdown_tables/aspect.vm"),
func_template = Label("//stardoc:templates/markdown_tables/func.vm"),
Expand All @@ -52,8 +52,8 @@ def stardoc(
For example, if `//foo:bar.bzl` does not build except when a user would specify
`--incompatible_foo_semantic=false`, then this attribute should contain
"--incompatible_foo_semantic=false".
stardoc: The location of the stardoc tool.
renderer: The location of the renderer tool.
stardoc: The location of the stardoc tool (a `java_binary` target or a `.jar` file).
renderer: The location of the renderer tool (a `java_binary` target or a `.jar` file).
aspect_template: The input file template for generating documentation of aspects
header_template: The input file template for the header of the output documentation.
func_template: The input file template for generating documentation of functions.
Expand All @@ -68,7 +68,9 @@ def stardoc(
java_binary(
name = stardoc_with_runfiles_name,
main_class = "com.google.devtools.build.skydoc.SkydocMain",
runtime_deps = [stardoc],
# java_binary targets cannot be added as deps of a java_binary, so we may need to get the
# corresponding _deploy.jar target.
runtime_deps = [_get_deploy_jar_label_if_not_jar(stardoc)],
data = [input] + deps,
tags = ["manual"],
visibility = ["//visibility:private"],
Expand All @@ -91,3 +93,22 @@ def stardoc(
rule_template = rule_template,
**kwargs
)

def _get_deploy_jar_label_if_not_jar(label):
"""Returns the label of the _deploy.jar target corresponding to the given java_binary label."""

# label could be a string or a Label.
label_str = str(label)

# Convert "@my_stardoc" to "@my_stardoc//:my_stardoc".
if not "//" in label_str:
if not label_str.startswith("@"):
fail("Invalid label: %s" % label_str)
label_str = label_str + "//:" + label_str[1]
if not ":" in label_str:
label_str = label_str + ":" + label_str.split("/")[-1]

if label_str.endswith(".jar"):
return label_str
else:
return label_str + "_deploy.jar"

0 comments on commit bf6632e

Please sign in to comment.