|
| 1 | +"""Create a tool with runfiles and a rule which uses it. |
| 2 | +
|
| 3 | +A rule may register an action that uses a tool which requires external files |
| 4 | +during runtime. This demonstrates best practices for handling such a scenario. |
| 5 | +""" |
| 6 | + |
| 7 | +def _tool_impl(ctx): |
| 8 | + # Runfiles expansion for tools (executables that are to be run |
| 9 | + # as part of other build actions) is tricky and error-prone. |
| 10 | + |
| 11 | + # There is a {rulename}.runfiles directory adjacent to the tool's |
| 12 | + # executable file which contains all runfiles. This is not guaranteed |
| 13 | + # to be relative to the directory in which the executable file is run. |
| 14 | + runfiles_path = "$0.runfiles/" |
| 15 | + |
| 16 | + # Each runfile under the runfiles path resides under a directory with |
| 17 | + # with the same name as its workspace. |
| 18 | + data_file_root = runfiles_path + ctx.workspace_name + "/" |
| 19 | + |
| 20 | + data_file_path = data_file_root + ctx.files._data[0].path |
| 21 | + |
| 22 | + # Alternatively, one can use the root_symlinks parameter of `runfiles` |
| 23 | + # to create a symlink rooted directly under the {rulename}.runfiles |
| 24 | + # directory. |
| 25 | + my_runfiles = ctx.runfiles(files = [ctx.files._data[0]], |
| 26 | + root_symlinks = {"data_dep" : ctx.files._data[0]}) |
| 27 | + |
| 28 | + # Even root symlinks are under the runfiles path. |
| 29 | + data_dep_path = runfiles_path + "data_dep" |
| 30 | + |
| 31 | + # Thus the example directory structure is: |
| 32 | + # runfiles/tool (executable) |
| 33 | + # runfiles/tool.runfiles/ |
| 34 | + # data_dep (symlink to data.txt) |
| 35 | + # <workspace_name>/ |
| 36 | + # runfiles/ |
| 37 | + # udata.txt |
| 38 | + |
| 39 | + # Create the output executable file with command as its content. |
| 40 | + ctx.actions.write( |
| 41 | + output = ctx.outputs.executable, |
| 42 | + # Simple example, effectively puts the contents of data.txt into |
| 43 | + # the output twice (read once via symlink, once via normal file). |
| 44 | + content = "#!/bin/bash\ncat %s %s > $1" % (data_file_path, data_dep_path), |
| 45 | + is_executable = True, |
| 46 | + ) |
| 47 | + |
| 48 | + return [DefaultInfo( |
| 49 | + # The tool itself should just declare `runfiles`. The build |
| 50 | + # system will automatically create a `files_to_run` object |
| 51 | + # from the result of this declaration (used later). |
| 52 | + runfiles = my_runfiles, |
| 53 | + )] |
| 54 | + |
| 55 | +tool = rule( |
| 56 | + implementation = _tool_impl, |
| 57 | + executable = True, |
| 58 | + attrs = { |
| 59 | + "_data": attr.label( |
| 60 | + allow_files = True, |
| 61 | + default = "//runfiles:data.txt"), |
| 62 | + }, |
| 63 | +) |
| 64 | + |
| 65 | +def _tool_user_impl(ctx): |
| 66 | + my_out = ctx.actions.declare_file(ctx.attr.name + "_out") |
| 67 | + |
| 68 | + # If the tool dependency attribute was declared with `executable = True`, |
| 69 | + # then the tool's file can be found under `ctx.executable.<attr_name>`. |
| 70 | + # If this file is passed to `ctx.actions.run()`, the runfiles for this file |
| 71 | + # are automatically added to the action. |
| 72 | + tool = ctx.executable.tool |
| 73 | + |
| 74 | + ctx.actions.run( |
| 75 | + outputs = [my_out], |
| 76 | + executable = tool, |
| 77 | + arguments = [my_out.path] |
| 78 | + ) |
| 79 | + |
| 80 | + return [DefaultInfo(files = depset([my_out]))] |
| 81 | + |
| 82 | +tool_user = rule( |
| 83 | + implementation = _tool_user_impl, |
| 84 | + attrs = { |
| 85 | + "tool": attr.label(mandatory = True, executable = True, cfg = "host"), |
| 86 | + }, |
| 87 | + |
| 88 | +) |
0 commit comments