This repository was archived by the owner on Dec 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjava_compiler_toolchain.bzl
More file actions
89 lines (79 loc) · 3.08 KB
/
Copy pathjava_compiler_toolchain.bzl
File metadata and controls
89 lines (79 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
'''Implements the `java_compiler_toolchain` rule.
Java compiler toolchain instances are created with `java_compiler_toolchain`
rule instances. A separate `toolchain` rule instance is used to declare a
`java_compiler_toolchain` instance has the type
`@dwtj_rules_java//java/toolchains/java_compiler_toolchain:toolchain_type`.
See [the Bazel Toolchain documentation][1] for more information.
An example might look something like this:
```build
java_compiler_toolchain(
name = "_my_javac",
javac_executable = ":my_javac_executable",
)
toolchain(
name = "my_javac",
exec_compatible_with = [
...
],
target_compatible_with = [
...
],
toolchain = ":_my_javac",
toolchain_type = "@dwtj_rules_java//java/toolchains/java_compiler_toolchain:toolchain_type",
)
```
[1]: https://docs.bazel.build/versions/3.4.0/toolchains.html
'''
JavaCompilerToolchainInfo = provider(
doc = "Specifies the tools, scripts, and configuration needed to compile and JAR Java targets.",
fields = {
"javac_executable": "A `javac` executable file (in the host configuration).",
"jar_executable": "A `jar` executable file (in the host configuration).",
"compile_java_jar_script_template": "A template for a script which is used to compile Java sources to a JAR file.",
"class_path_separator": "The class path separator to use when invoking this `javac` executable."
},
)
def _java_compiler_toolchain_impl(ctx):
java_compiler_toolchain_info = JavaCompilerToolchainInfo(
javac_executable = ctx.file.javac_executable,
jar_executable = ctx.file.jar_executable,
compile_java_jar_script_template = ctx.file._compile_java_jar_script_template,
class_path_separator = ctx.attr.class_path_separator,
)
toolchain_info = platform_common.ToolchainInfo(
java_compiler_toolchain_info = java_compiler_toolchain_info,
)
return [
toolchain_info,
java_compiler_toolchain_info,
]
java_compiler_toolchain = rule(
implementation = _java_compiler_toolchain_impl,
attrs = {
"javac_executable": attr.label(
allow_single_file = True,
mandatory = True,
executable = True,
cfg = "host",
),
"jar_executable": attr.label(
allow_single_file = True,
mandatory = True,
executable = True,
cfg = "host",
),
# NOTE(dwtj): This seems like a somewhat roundabout way to make this
# template available for instantiation in the `compile_java_jar()`
# helper function, but I haven't yet figured out another way to do it
# which resolves the label to a `File`.
# TODO(dwtj): Try the `Label()` constructor.
"_compile_java_jar_script_template": attr.label(
default = "//java:common/actions/TEMPLATE.compile_java_jar.sh",
allow_single_file = True,
),
"class_path_separator": attr.string(
default = ":", # Defaults to the Unix-like class-path separator.
)
},
provides = [JavaCompilerToolchainInfo]
)