Skip to content
Closed
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 rules/java_rules.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 @@ -84,6 +87,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 != "":
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",
"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,
)

return build_rule(
name=name,
srcs={
Expand All @@ -99,6 +129,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