kythe.io@v0.0.68-0.20240422202219-7225dbc01741/tools/build_rules/verifier_test/kzip_archive.bzl (about)

     1  # Copyright 2019 The Kythe Authors. All rights reserved.
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #   http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  """
    15  Rule for creating generic .kzip compilation units using kzip create.
    16  """
    17  
    18  def _key_pair(item):
    19      return "{}={}".format(*item)
    20  
    21  def _kythe_uri(corpus, language, path = None, root = None):
    22      uri = "kythe://{}?lang={}".format(corpus, language)
    23      if path:
    24          uri += "?path=" + path
    25      if root:
    26          uri += "?root=" + root
    27      return uri
    28  
    29  def _single_path(srcs):
    30      # If there is exactly one source, use that for the path.
    31      # This is primarily to be compatible with some existing uses of kzip create.
    32      if len(srcs) == 1:
    33          return srcs[0].path
    34      return None
    35  
    36  def _kzip_archive(ctx):
    37      args = ctx.actions.args()
    38      args.add("create")
    39      args.add("--uri", _kythe_uri(
    40          corpus = ctx.attr.corpus,
    41          language = ctx.attr.lang,
    42          path = _single_path(ctx.files.srcs),
    43          root = ctx.expand_location(ctx.attr.root),
    44      ))
    45      args.add("--output", ctx.outputs.kzip)
    46      args.add("--output_key", str(ctx.label))
    47      args.add_joined("--source_file", ctx.files.srcs, join_with = ",")
    48      args.add_joined("--required_input", ctx.files.deps, join_with = ",")
    49      args.add_all(
    50          [(k, ctx.expand_location(v)) for k, v in ctx.attr.env.items()],
    51          before_each = "--env",
    52          map_each = _key_pair,
    53      )
    54      args.add_all([ctx.expand_location(d) for d in ctx.attr.details], before_each = "--details")
    55      args.add_all("--rules", ctx.files.vnames_config)
    56      if ctx.attr.has_compile_errors:
    57          args.add("--has_compile_errors")
    58      if ctx.attr.working_directory:
    59          args.add("--working_directory", ctx.expand_location(ctx.attr.working_directory))
    60      if ctx.attr.entry_context:
    61          args.add("--entry_context", ctx.expand_location(ctx.attr.entry_context))
    62  
    63      # All arguments after a plain "--" will be passed as arguments in the compilation unit.
    64      # This must come last.
    65      args.add_all("--", [ctx.expand_location(a) for a in ctx.attr.args])
    66      ctx.actions.run(
    67          outputs = [ctx.outputs.kzip],
    68          inputs = ctx.files.srcs + ctx.files.deps + ctx.files.vnames_config,
    69          executable = ctx.executable._kzip,
    70          arguments = [args],
    71          mnemonic = "KzipCreate",
    72      )
    73  
    74  kzip_archive = rule(
    75      attrs = {
    76          "corpus": attr.string(
    77              doc = "The corpus to which this compilation belongs.",
    78              default = "kythe",
    79          ),
    80          "lang": attr.string(
    81              doc = "The language this compilation should be indexed as.",
    82              mandatory = True,
    83          ),
    84          "root": attr.string(
    85              doc = "The root to use, if any, in the compilation unit Vname.",
    86          ),
    87          "srcs": attr.label_list(
    88              doc = "List of source files.",
    89              allow_files = True,
    90              allow_empty = False,
    91          ),
    92          "deps": attr.label_list(
    93              doc = "List of non-source required_input files.",
    94              allow_files = True,
    95          ),
    96          "args": attr.string_list(
    97              doc = "List of command-line arguments for the compilation unit.",
    98          ),
    99          "working_directory": attr.string(
   100              doc = "Manually specify a working_directory to use in the compilation unit.",
   101          ),
   102          "env": attr.string_dict(
   103              doc = "Environment variables to specify in the compilation unit.",
   104          ),
   105          "entry_context": attr.string(
   106              doc = "Indexer-specific context to provide in the compilation unit.",
   107          ),
   108          "has_compile_errors": attr.bool(
   109              doc = "Whether to indicate compilation errors in the compilation unit.",
   110          ),
   111          "details": attr.string_list(
   112              doc = "JSON-encoded proto.Any details message to include.",
   113          ),
   114          "vnames_config": attr.label(
   115              allow_single_file = True,
   116              doc = "vnames.json file to use, if any.",
   117          ),
   118          "_kzip": attr.label(
   119              default = Label("//kythe/go/platform/tools/kzip"),
   120              allow_single_file = True,
   121              executable = True,
   122              cfg = "exec",
   123          ),
   124      },
   125      implementation = _kzip_archive,
   126      outputs = {
   127          "kzip": "%{name}.kzip",
   128      },
   129  )