| 1 |
""" |
| 2 |
The run_cxxbridge_cmd rule is a variant of the run_binary rule defined in |
| 3 |
`@bazel_skylib//rules:run_binary.bzl` that specifically runs `@cxxbridge_cmd//:cxxbridge` and |
| 4 |
provides a `crate_features` attribute that gets expanded into `--cfg` arguments to cxxbuild. |
| 5 |
|
| 6 |
Defining this as a rule (rather than a macro that thinly wraps `run_binary`) allows `crate_features` |
| 7 |
to be evaluated as a "select" [1], which is not possible for macro parameters [2]. |
| 8 |
|
| 9 |
[1] https://bazel.build/docs/configurable-attributes#select-and-dependencies |
| 10 |
[2] https://bazel.build/docs/configurable-attributes#faq-select-macro |
| 11 |
""" |
| 12 |
|
| 13 |
def _run_cxxbridge_cmd_impl(ctx): |
| 14 |
tool_as_list = [ctx.attr._cxxbridge] |
| 15 |
args = [ |
| 16 |
# https://bazel.build/rules/lib/builtins/ctx#expand_location |
| 17 |
ctx.expand_location(a, tool_as_list) |
| 18 |
for a in ctx.attr.args |
| 19 |
] |
| 20 |
for f in ctx.attr.crate_features: |
| 21 |
args.append("--cfg") |
| 22 |
args.append("feature=\"%s\"" % f) |
| 23 |
|
| 24 |
# https://bazel.build/rules/lib/builtins/actions.html#run |
| 25 |
ctx.actions.run( |
| 26 |
outputs = ctx.outputs.outs, |
| 27 |
inputs = ctx.files.srcs, |
| 28 |
tools = [ctx.executable._cxxbridge], |
| 29 |
executable = ctx.executable._cxxbridge, |
| 30 |
arguments = args, |
| 31 |
mnemonic = "RunCxxbridgeCmd", |
| 32 |
) |
| 33 |
|
| 34 |
return DefaultInfo( |
| 35 |
files = depset(ctx.outputs.outs), |
| 36 |
runfiles = ctx.runfiles(ctx.outputs.outs), |
| 37 |
) |
| 38 |
|
| 39 |
run_cxxbridge_cmd = rule( |
| 40 |
implementation = _run_cxxbridge_cmd_impl, |
| 41 |
attrs = { |
| 42 |
"srcs": attr.label_list( |
| 43 |
doc = "Source dependencies for this rule", |
| 44 |
allow_files = True, |
| 45 |
mandatory = True, |
| 46 |
), |
| 47 |
"outs": attr.output_list( |
| 48 |
doc = "C++ output files generated by cxxbridge_cmd.", |
| 49 |
mandatory = True, |
| 50 |
), |
| 51 |
"args": attr.string_list( |
| 52 |
doc = "Arguments to `cxxbridge_cmd`.", |
| 53 |
mandatory = True, |
| 54 |
), |
| 55 |
"crate_features": attr.string_list( |
| 56 |
doc = "Optional list of cargo features that CXX bridge definitions may depend on.", |
| 57 |
), |
| 58 |
"_cxxbridge": attr.label( |
| 59 |
default = Label("@cargo_bindeps//:cxxbridge-cmd__cxxbridge"), |
| 60 |
allow_single_file = True, |
| 61 |
executable = True, |
| 62 |
cfg = "exec", |
| 63 |
), |
| 64 |
}, |
| 65 |
) |