-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rule javadoc generates the Javadoc for a set of libraries.
- Loading branch information
Showing
9 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
5.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Additional Java rules for Bazel | ||
|
||
Rules: | ||
|
||
- [javadoc](#javadoc) | ||
|
||
## Usage | ||
|
||
```bazel | ||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") | ||
|
||
http_archive( | ||
name = "extra_rules_java", | ||
sha256 = <sha>, | ||
strip_prefix = "extra_rules_java-{}".format(<tag>), | ||
url = "https://github.com/joca-bt/extra_rules_java/archive/{}.tar.gz".format(<tag>), | ||
) | ||
``` | ||
|
||
## Rules | ||
|
||
### javadoc | ||
|
||
```bazel | ||
javadoc(name, exclude, javacopts, libs, links, packages, title) | ||
``` | ||
|
||
Generates the Javadoc for a set of [libraries](https://bazel.build/reference/be/java#java_library). | ||
|
||
**Arguments** | ||
|
||
| Name | Type | Mandatory | Default | | ||
| --- | --- | --- | --- | | ||
| name | Name | Yes | | | ||
| exclude | List of strings | No | [] | | ||
| javacopts | List of strings | No | [] | | ||
| libs | List of labels | Yes | | | ||
| links | List of strings | No | ["https://docs.oracle.com/en/java/javase/17/docs/api/"] | | ||
| packages | List of strings | Yes | | | ||
| title | String | No | "" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
workspace(name = "extra_rules_java") |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
load("//java/private:javadoc.bzl", _javadoc = "javadoc") | ||
|
||
javadoc = _javadoc |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
visibility("private") | ||
|
||
def to_paths(depset): | ||
return [element.path for element in depset.to_list()] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
visibility("private") | ||
|
||
def extract_jars(jars, dir): | ||
return " && ".join(["unzip -q -o {} -d {}".format(jar, dir) for jar in jars]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
load(":depsets.bzl", "to_paths") | ||
load(":jars.bzl", "extract_jars") | ||
|
||
def _javadoc(ctx, deps, src_dir, javadoc_dir, java_home): | ||
command = [ | ||
"{}/bin/javadoc".format(java_home), | ||
"-quiet", | ||
"-Xdoclint:-missing", | ||
"--class-path {}".format(":".join(deps)), | ||
"--source-path {}".format(src_dir), | ||
"-subpackages {}".format(":".join(ctx.attr.packages)), | ||
"-d {}".format(javadoc_dir), | ||
"-docencoding UTF-8", | ||
"-notimestamp", | ||
] | ||
|
||
if ctx.attr.exclude: | ||
command.append("-exclude {}".format(":".join(ctx.attr.exclude))) | ||
|
||
command.extend(ctx.attr.javacopts) | ||
|
||
for link in ctx.attr.links: | ||
command.append("-link {}".format(link)) | ||
|
||
if ctx.attr.title: | ||
command.append("-doctitle '{}'".format(ctx.attr.title)) | ||
command.append("-windowtitle '{}'".format(ctx.attr.title)) | ||
|
||
return " ".join(command) | ||
|
||
def _javadoc_impl(ctx): | ||
srcs = depset(transitive = [depset(lib[JavaInfo].source_jars) for lib in ctx.attr.libs]) | ||
deps = depset(transitive = [lib[JavaInfo].transitive_compile_time_jars for lib in ctx.attr.libs]) | ||
output = ctx.actions.declare_file("{}.jar".format(ctx.label.name)) | ||
|
||
java_home = ctx.attr._jdk[java_common.JavaRuntimeInfo].java_home | ||
tmp_dir = "{}/_{}/".format(output.dirname, output.basename) | ||
src_dir = "{}/src/".format(tmp_dir) | ||
javadoc_dir = "{}/javadoc/".format(tmp_dir) | ||
|
||
commands = [ | ||
"rm -rf {}".format(tmp_dir), | ||
"mkdir {}".format(tmp_dir), | ||
extract_jars(to_paths(srcs), src_dir), | ||
_javadoc(ctx, to_paths(deps), src_dir, javadoc_dir, java_home), | ||
"{}/bin/jar -c -f {} -C {} .".format(java_home, output.path, javadoc_dir), | ||
] | ||
|
||
ctx.actions.run_shell( | ||
command = " && ".join(commands), | ||
inputs = ctx.files._jdk + srcs.to_list() + deps.to_list(), | ||
outputs = [output], | ||
) | ||
|
||
return [DefaultInfo(files = depset([output]))] | ||
|
||
javadoc = rule( | ||
doc = "Generates the Javadoc for a set of libraries.", | ||
implementation = _javadoc_impl, | ||
attrs = { | ||
"exclude": attr.string_list(), | ||
"javacopts": attr.string_list(), | ||
"libs": attr.label_list( | ||
allow_empty = False, | ||
mandatory = True, | ||
providers = [JavaInfo], | ||
), | ||
"links": attr.string_list( | ||
default = ["https://docs.oracle.com/en/java/javase/17/docs/api/"], | ||
), | ||
"packages": attr.string_list( | ||
allow_empty = False, | ||
mandatory = True, | ||
), | ||
"title": attr.string(), | ||
"_jdk": attr.label( | ||
default = Label("@bazel_tools//tools/jdk:current_java_runtime"), | ||
providers = [java_common.JavaRuntimeInfo], | ||
), | ||
}, | ||
) |