Created
May 19, 2025 21:38
-
-
Save fzakaria/e9be7c4020ccac192b5054efd77d1bff to your computer and use it in GitHub Desktop.
Creating Java libraries for flatbuffers with flatc
This file contains hidden or 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
""" | |
FlatBuffer Java rule for Bazel | |
""" | |
FlatbufferInfo = provider( | |
doc = "A provider for FlatBuffer schema files.", | |
fields = { | |
"fbs_files": "The list of fbs files that were used as inputs", | |
}, | |
) | |
def _flatbuffers_java_impl(ctx): | |
output_dir = ctx.actions.declare_directory(ctx.label.name + "_java") | |
input_file_paths = [f.path for f in ctx.files.srcs] | |
dependency_fbs_files = [] | |
dependency_fbs__args = [] | |
for d in ctx.attr.deps: | |
if d[FlatbufferInfo].fbs_files: | |
for f in d[FlatbufferInfo].fbs_files.to_list(): | |
dependency_fbs_files.append(f) | |
dependency_fbs__args.extend(["-I", f.path]) | |
ctx.actions.run( | |
inputs = ctx.files.srcs + dependency_fbs_files + [ctx.executable._flatc], | |
outputs = [output_dir], | |
executable = ctx.executable._flatc, | |
arguments = [ | |
"--java", | |
"-o", | |
output_dir.path, | |
] + dependency_fbs__args + input_file_paths, | |
mnemonic = "FlatcJavaGen", | |
progress_message = "Generating Java from FlatBuffers: {}".format(ctx.label.name), | |
) | |
java_toolchain = ctx.toolchains["@bazel_tools//tools/jdk:toolchain_type"].java | |
srcjar = ctx.actions.declare_file(ctx.attr.name + ".srcjar") | |
java_common.pack_sources(ctx.actions, sources = [output_dir], java_toolchain = java_toolchain, output_source_jar = srcjar) | |
out_jar = ctx.actions.declare_file("lib{}.jar".format(ctx.attr.name)) | |
java_info = java_common.compile( | |
ctx, | |
java_toolchain = java_toolchain, | |
source_jars = [srcjar], | |
output = out_jar, | |
deps = [ d[JavaInfo] for d in ctx.attr.deps] + [ ctx.attr._stdlib[JavaInfo] ], | |
) | |
return [ | |
DefaultInfo(files = depset(direct = [out_jar])), | |
java_info, | |
FlatbufferInfo( | |
fbs_files = depset(ctx.files.srcs, transitive = [ d[FlatbufferInfo].fbs_files for d in ctx.attr.deps]), | |
), | |
] | |
flatbuffers_java = rule( | |
implementation = _flatbuffers_java_impl, | |
attrs = { | |
"srcs": attr.label_list(allow_files = [".fbs"]), | |
"deps": attr.label_list( | |
doc = "List of other flatbuffers_java or flatbuffers_java_library targets that this target depends on.", | |
providers = [FlatbufferInfo, JavaInfo], | |
), | |
"_flatc": attr.label( | |
allow_single_file = True, | |
executable = True, | |
cfg = "exec", | |
default = Label("@flatbuffers//:flatc"), | |
doc = "The flatc executable that will be used to compile the flatbuffers.", | |
), | |
"_stdlib" : attr.label( | |
default = "@maven//:com_google_flatbuffers_flatbuffers_java", | |
doc = "The flatbuffers java stdlib that is used to compile the flatbuffers and provide runtime support.", | |
), | |
}, | |
# This is needed to bring in the java_common library from Bazel | |
# https://bazel.build/extending/rules#configuration_fragments | |
fragments = ["java"], | |
output_to_genfiles = True, | |
toolchains = ["@bazel_tools//tools/jdk:toolchain_type"], | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment