|
4 | 4 |
|
5 | 5 | """Supports running a tool from the install filegroup.""" |
6 | 6 |
|
7 | | -load("@rules_python//python:defs.bzl", "py_binary") |
8 | | - |
9 | | -def run_tool(name, tool, data, env): |
10 | | - # TODO: Fix the driver file discovery in order to allow symlinks. |
11 | | - py_binary( |
12 | | - name = name, |
13 | | - main = "run_tool.py", |
14 | | - srcs = ["run_tool.py"], |
15 | | - args = ["$(location {})".format(tool)], |
16 | | - data = [tool] + data, |
17 | | - env = env, |
| 7 | +def _run_tool_impl(ctx): |
| 8 | + tool_files = ctx.attr.tool.files.to_list() |
| 9 | + if len(tool_files) != 1: |
| 10 | + fail("Expected 1 tool file, found {0}".format(len(tool_files))) |
| 11 | + ctx.actions.symlink( |
| 12 | + output = ctx.outputs.executable, |
| 13 | + target_file = tool_files[0], |
| 14 | + is_executable = True, |
18 | 15 | ) |
| 16 | + return [ |
| 17 | + DefaultInfo( |
| 18 | + runfiles = ctx.runfiles(files = ctx.files.data), |
| 19 | + ), |
| 20 | + RunEnvironmentInfo(environment = ctx.attr.env), |
| 21 | + ] |
| 22 | + |
| 23 | +run_tool = rule( |
| 24 | + doc = "Helper for running a tool in a filegroup.", |
| 25 | + implementation = _run_tool_impl, |
| 26 | + attrs = { |
| 27 | + "data": attr.label_list(allow_files = True), |
| 28 | + "env": attr.string_dict(), |
| 29 | + "tool": attr.label( |
| 30 | + allow_single_file = True, |
| 31 | + executable = True, |
| 32 | + cfg = "target", |
| 33 | + ), |
| 34 | + }, |
| 35 | + executable = True, |
| 36 | +) |
0 commit comments