run_tool.bzl 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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(
  19. environment = ctx.attr.env |
  20. {"CARBON_ARGV0_OVERRIDE": tool_files[0].short_path},
  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. mandatory = True,
  34. ),
  35. },
  36. executable = True,
  37. )