configure_cmake_file.bzl 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. """A Starlark implementation of a CMake-like configure_file rule."""
  5. def _configure_cmake_file_impl(ctx):
  6. """Implementation for the configure_cmake_file rule."""
  7. # Flatten the defines dictionary into a list of command-line arguments
  8. # for the implementation script:
  9. #
  10. # ["--defines", KEY1, VAL1, "--defines", KEY2, VAL2]
  11. define_args = []
  12. for key, value in ctx.attr.defines.items():
  13. define_args.append("--defines")
  14. define_args.append(key)
  15. define_args.append(value)
  16. ctx.actions.run(
  17. executable = ctx.executable._impl_script,
  18. arguments = [
  19. "--src",
  20. ctx.file.src.path,
  21. "--out",
  22. ctx.outputs.out.path,
  23. ] + define_args,
  24. inputs = depset([ctx.file.src, ctx.executable._impl_script]),
  25. outputs = [ctx.outputs.out],
  26. mnemonic = "ConfigureCmakeFile",
  27. progress_message = "Configuring file: %{label}",
  28. )
  29. return [DefaultInfo(files = depset([ctx.outputs.out]))]
  30. configure_cmake_file = rule(
  31. implementation = _configure_cmake_file_impl,
  32. attrs = {
  33. "defines": attr.string_dict(
  34. mandatory = True,
  35. doc = "A dictionary of key-value definitions to substitute.",
  36. ),
  37. "out": attr.output(
  38. mandatory = True,
  39. doc = "The generated output file.",
  40. ),
  41. "src": attr.label(
  42. allow_single_file = True,
  43. mandatory = True,
  44. doc = "The input '.in' template file.",
  45. ),
  46. "_impl_script": attr.label(
  47. default = Label("//toolchain/runtimes:configure_cmake_file_impl"),
  48. allow_files = True,
  49. executable = True,
  50. cfg = "exec",
  51. ),
  52. },
  53. doc = """
  54. A rule that performs CMake-style configuration of an input file.
  55. This rule processes an input file (`.in`) and generates an output file
  56. based on a dictionary of definitions. It provides emulation
  57. of the most commonly used aspects of CMake's `configure_file` command:
  58. https://cmake.org/cmake/help/latest/command/configure_file.html
  59. Notable aspects not implemented are the following:
  60. * Substitution of cache values using `$CACHE{VAR}` syntax.
  61. * Substitution of environment variables using `$ENV{VAR}` syntax.
  62. """,
  63. )