run_tool.bzl 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. # Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. # Exceptions. See /LICENSE for license information.
  3. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. """Supports running a tool from the install filegroup."""
  5. def _run_tool_impl(ctx):
  6. tool_files = ctx.attr.tool.files.to_list()
  7. if len(tool_files) != 1:
  8. fail("Expected 1 tool file, found {0}".format(len(tool_files)))
  9. ctx.actions.symlink(
  10. output = ctx.outputs.executable,
  11. target_file = tool_files[0],
  12. is_executable = True,
  13. )
  14. return [
  15. DefaultInfo(
  16. runfiles = ctx.runfiles(files = ctx.files.data),
  17. ),
  18. RunEnvironmentInfo(environment = ctx.attr.env),
  19. ]
  20. run_tool = rule(
  21. doc = "Helper for running a tool in a filegroup.",
  22. implementation = _run_tool_impl,
  23. attrs = {
  24. "data": attr.label_list(allow_files = True),
  25. "env": attr.string_dict(),
  26. "tool": attr.label(
  27. allow_single_file = True,
  28. executable = True,
  29. cfg = "target",
  30. ),
  31. },
  32. executable = True,
  33. )