kythe.io@v0.0.68-0.20240422202219-7225dbc01741/tools/build_rules/extra_aspects/cxx/aspect.bzl (about)

     1  # Copyright 2023 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  """C++ extraction aspect definition."""
    16  
    17  load("@rules_proto//proto:defs.bzl", "ProtoInfo")
    18  load(
    19      "//tools/build_rules/extra_aspects:config.bzl",
    20      "KytheExtractorConfigInfo",
    21      "run_configured_extractor",
    22  )
    23  
    24  KytheCxxCompilationInfo = provider("C++ KytheCompilationInfo", fields = ["kzips"])
    25  
    26  _KytheCxxProtoCompilationInfo = provider("", fields = ["kzips"])
    27  
    28  _COMMON_ATTRS = {
    29      "_config": attr.label(
    30          providers = [KytheExtractorConfigInfo],
    31          default = Label("//tools/build_rules/extra_aspects/cxx:extractor-config"),
    32      ),
    33  }
    34  
    35  def _transitive_kzips(ctx, provider = _KytheCxxProtoCompilationInfo):
    36      return [
    37          dep[provider].kzips
    38          for dep in ctx.rule.attr.deps
    39          if provider in dep and dep[provider].kzips
    40      ]
    41  
    42  def _extract_target(target, ctx, transitive):
    43      config = ctx.attr._config[KytheExtractorConfigInfo]
    44      kzips = [run_configured_extractor(target, ctx, config)]
    45      if transitive:
    46          kzips += _transitive_kzips(ctx)
    47      return depset(transitive = kzips)
    48  
    49  def _extract_proto_impl(target, ctx):
    50      return [_KytheCxxProtoCompilationInfo(kzips = _extract_target(
    51          target,
    52          ctx,
    53          transitive = True,
    54      ))]
    55  
    56  _extract_proto_aspect = aspect(
    57      implementation = _extract_proto_impl,
    58      attr_aspects = ["deps"],
    59      # Only call the aspect on proto_library-like targets.
    60      required_providers = [ProtoInfo],
    61      # We need the actions provided by the aspect, rather than the provider itself,
    62      # but CcProtoInfo aspect isn't exported from the rules.
    63      required_aspect_providers = [[CcInfo]],
    64      attrs = _COMMON_ATTRS,
    65  )
    66  
    67  def _extract_impl(target, ctx):
    68      config = ctx.attr._config[KytheExtractorConfigInfo].action_selection_config
    69      return [KytheCxxCompilationInfo(kzips = _extract_target(
    70          target,
    71          ctx,
    72          transitive = [] == config.aspect_rule_attrs.get(ctx.rule.kind),
    73      ))]
    74  
    75  extract_cxx_aspect = aspect(
    76      implementation = _extract_impl,
    77      requires = [_extract_proto_aspect],
    78      attrs = _COMMON_ATTRS,
    79      provides = [KytheCxxCompilationInfo],
    80  )