Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion build_defs/java.build_defs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ def java_library(name:str, srcs:list=None, src_dir:str=None, resources:list=[],
deps:list=[], modular:bool=False, exported_deps:list=None, visibility:list=None,
test_only:bool&testonly=False, javac_flags:list&javacopts=None, labels:list=[],
toolchain:str=CONFIG.JAVA_TOOLCHAIN):
"""Compiles Java source to a .jar which can be collected by other rules.
"""Compiles Java source to a .jar which can be collected by other rules. If the source makes use of
the Java Native Interface (JNI), and the target is included as a dependency of a C/C++ rule, this
rule instead outputs the JNI headers generated by the Java compiler to allow the source of the
native-code component to be built.

Args:
name (str): Name of the rule
Expand Down Expand Up @@ -87,6 +90,33 @@ def java_library(name:str, srcs:list=None, src_dir:str=None, resources:list=[],

src_dir_label = [] if srcs else ['src_dir:'+src_dir]

def _jni_header_outputs(name, output):
for hdr_path in output:
if hdr_path != "":
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just to guard against the command outputting \n when no headers are generated.

add_out(name, hdr_path)
add_label(name, "cc:inc:" + join_path(package_name(), dirname(hdr_path)))

jni_hdrs = build_rule(
name = name,
tag = "jni_hdrs",
srcs = {
"WORKER": srcs or [src_dir],
},
cmd = " && ".join([
"mkdir -p _jni",
f"{javac_cmd} -h _jni >/dev/null 2>&1",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Silencing the output of javac here as a precaution because it's possible to instruct javac to output to stdout instead of stderr using a command line option (i.e. via javac_flags) - it's an edge case, but one that'll break this target if it's used. The main target will still show compilation errors for the class files.

"find _jni -type f -name '*.h' | sort",
]),
post_build = _jni_header_outputs,
labels = labels,
tools = {
"javac": [javac],
},
test_only = test_only,
visibility = visibility,
deps = deps + (exported_deps or []),
)

return build_rule(
name=name,
srcs={
Expand All @@ -102,6 +132,9 @@ def java_library(name:str, srcs:list=None, src_dir:str=None, resources:list=[],
cmd=cmd,
building_description="Compiling...",
requires=['java'],
provides={
"cc_hdrs": jni_hdrs,
},
labels = labels + ['rule:java_test_library' if test_only else 'rule:java_library'] + src_dir_label,
test_only=test_only,
tools={
Expand Down