File
Blob: build/run_binary_target.bzl
| 1 | # Workaround for bazel not supporting a shared exec and target configuration, even when they are |
| 2 | # identical. https://github.com/bazelbuild/bazel/issues/14848 |
| 3 | # Derived from the tensorflow project (https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/api/generator/api_gen.bzl) |
| 4 | # See https://github.com/tensorflow/tensorflow/issues/60167 for discussion |
| 5 | |
| 6 | def _run_binary_target_impl(ctx): |
| 7 | tool = ctx.attr.tool[DefaultInfo].files_to_run.executable |
| 8 | flags = [ctx.expand_location(a) if "$(location" in a else a for a in ctx.attr.args] |
| 9 | |
| 10 | cmd = " ".join([tool.path] + flags) |
| 11 | ctx.actions.run_shell( |
| 12 | inputs = ctx.files.srcs, |
| 13 | outputs = ctx.outputs.outs, |
| 14 | tools = [tool], |
| 15 | use_default_shell_env = True, |
| 16 | command = cmd, |
| 17 | ) |
| 18 | |
| 19 | run_binary_target = rule( |
| 20 | implementation = _run_binary_target_impl, |
| 21 | attrs = { |
| 22 | "outs": attr.output_list(mandatory = True), |
| 23 | "srcs": attr.label_list(allow_files = True), |
| 24 | "args": attr.string_list(), |
| 25 | # Setting the configuration to "target" to avoid compiling code used in both this |
| 26 | # generator-like target and regular targets twice. For cross-compilation this would need to |
| 27 | # be set to "exec". |
| 28 | # Unfortunately bazel makes it very difficult to set the configuration at build time as |
| 29 | # macros are resolved before select() can be resolved based on the command line. This could |
| 30 | # alternatively be done by defining build targets and the rules used to declare them twice |
| 31 | # (once for exec and for target). |
| 32 | "tool": attr.label( |
| 33 | executable = True, |
| 34 | cfg = "target", |
| 35 | mandatory = True, |
| 36 | ), |
| 37 | }, |
| 38 | ) |