kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/tools/fyi/testdata/compile_commands.bzl (about)

     1  """Rule for generating compile_commands.json.in with appropriate include directories."""
     2  
     3  load("//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain", "use_cpp_toolchain")
     4  
     5  _TEMPLATE = """  {{
     6      "directory": "OUT_DIR",
     7      "command": "clang++ -c {filename} -std=c++11 -Wall -Werror -I. -IBASE_DIR {system_includes}",
     8      "file": "{filename}",
     9    }}"""
    10  
    11  def _compile_commands_impl(ctx):
    12      system_includes = " ".join([
    13          "-I{}".format(d)
    14          for d in find_cpp_toolchain(ctx).built_in_include_directories
    15      ])
    16      ctx.actions.write(
    17          output = ctx.outputs.compile_commands,
    18          content = "[\n{}]\n".format(",\n".join([
    19              _TEMPLATE.format(filename = name, system_includes = system_includes)
    20              for name in ctx.attr.filenames
    21          ])),
    22      )
    23  
    24  compile_commands = rule(
    25      attrs = {
    26          "filenames": attr.string_list(
    27              mandatory = True,
    28              allow_empty = False,
    29          ),
    30          # Do not add references, temporary attribute for find_cpp_toolchain.
    31          # See go/skylark-api-for-cc-toolchain for more details.
    32          "_cc_toolchain": attr.label(
    33              default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
    34          ),
    35      },
    36      doc = "Generates a compile_commannds.json.in template file.",
    37      outputs = {
    38          "compile_commands": "compile_commands.json.in",
    39      },
    40      toolchains = use_cpp_toolchain(),
    41      implementation = _compile_commands_impl,
    42  )