Skip to content

Commit

Permalink
feat: extra language inputs for codegen
Browse files Browse the repository at this point in the history
When using language plugins like Cap'n'proto for Java from Bazel,
there needs to be a way to add language extras as inputs to the
codegen phase.

This is only needed when generating capnp code in multiple
languages. Even if the generator is only producing a C++ in a
given invocation, the imports for other languages will fail to be
found due to Bazel's strict sandboxing. This new attribute fixes
that condition.

Fixes and closes capnproto#1903.

Relates-To: elide-dev/bigtent#6
Relates-To: capnproto#1903
Signed-off-by: Sam Gammon <[email protected]>
  • Loading branch information
sgammon committed Jan 16, 2024
1 parent e628e82 commit fb473d1
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion c++/src/capnp/cc_capnp_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,18 @@ def _capnp_gen_impl(ctx):

args.add_all([s for s in ctx.files.srcs])

# process language extras, starting with files we should include
extra_inputs = ctx.files.language_extras or []

# then compute input flags, taking the parent of the first file in each extra as the include path
if len(ctx.attr.language_extras) > 0:
for extra in ctx.attr.language_extras:
files = extra.files.to_list()
if len(files) > 0:
args.add_all(["-I", files[0].dirname.removesuffix("/capnp")])

ctx.actions.run(
inputs = inputs + ctx.files._capnpc_cxx + ctx.files._capnpc_capnp + ctx.files._capnp_system,
inputs = inputs + ctx.files._capnpc_cxx + ctx.files._capnpc_capnp + ctx.files._capnp_system + extra_inputs,
outputs = ctx.outputs.outs,
executable = ctx.executable._capnpc,
arguments = [args],
Expand All @@ -72,6 +82,7 @@ _capnp_gen = rule(
"data": attr.label_list(allow_files = True),
"outs": attr.output_list(),
"src_prefix": attr.string(),
"language_extras": attr.label_list(default = []),
"_capnpc": attr.label(executable = True, allow_single_file = True, cfg = "exec", default = "@capnp-cpp//src/capnp:capnp_tool"),
"_capnpc_cxx": attr.label(executable = True, allow_single_file = True, cfg = "exec", default = "@capnp-cpp//src/capnp:capnpc-c++"),
"_capnpc_capnp": attr.label(executable = True, allow_single_file = True, cfg = "exec", default = "@capnp-cpp//src/capnp:capnpc-capnp"),
Expand All @@ -87,6 +98,7 @@ def cc_capnp_library(
data = [],
deps = [],
src_prefix = "",
language_extras = [],
visibility = None,
target_compatible_with = None,
**kwargs):
Expand All @@ -99,6 +111,7 @@ def cc_capnp_library(
be compiled
deps: other cc_capnp_library rules to depend on
src_prefix: src_prefix for capnp compiler to the source root
language_extras: Extra inputs to add to the capnp compiler invocation
visibility: rule visibility
target_compatible_with: target compatibility
**kwargs: rest of the arguments to cc_library rule
Expand All @@ -112,6 +125,7 @@ def cc_capnp_library(
srcs = srcs,
deps = [s + "_gen" for s in deps],
data = data,
language_extras = language_extras,
outs = hdrs + srcs_cpp,
src_prefix = src_prefix,
visibility = visibility,
Expand Down

0 comments on commit fb473d1

Please sign in to comment.