<commit_before># Copyright 2018 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""An aspect attached to `proto_library` targets to generate Swift artifacts."""

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load(":api.bzl", "swift_common")
load(":features.bzl", "SWIFT_FEATURE_ENABLE_TESTING", "SWIFT_FEATURE_NO_GENERATED_HEADER")
load(":linking.bzl", "register_libraries_to_link")
load(
    ":proto_gen_utils.bzl",
    "declare_generated_files",
    "extract_generated_dir_path",
    "register_module_mapping_write_action",
)
load(":providers.bzl", "SwiftInfo", "SwiftProtoInfo", "SwiftToolchainInfo")
load(":utils.bzl", "compact", "create_cc_info", "get_providers", "workspace_relative_path")

# The paths of well known type protos that should not be generated by the aspect
# because they are already included in the SwiftProtobuf runtime. The plugin
# provides the mapping from these protos to the SwiftProtobuf module for us.
# TODO(b/63389580): Once we migrate to proto_lang_toolchain, this information
# can go in the blacklisted_protos list instead.
_WELL_KNOWN_TYPE_PATHS = [
    "google/protobuf/any.proto",
    "google/protobuf/api.proto",
    "google/protobuf/duration.proto",
    "google/protobuf/empty.proto",
    "google/protobuf/field_mask.proto",
    "google/protobuf/source_context.proto",
    "google/protobuf/struct.proto",
    "google/protobuf/timestamp.proto",
    "google/protobuf/type.proto",
    "google/protobuf/wrappers.proto",
]

SwiftProtoCcInfo = provider(
    doc = """
Wraps a `CcInfo` provider added to a `proto_library` through the Swift proto aspect.

This is necessary because `proto_library` targets already propagate a `CcInfo` provider for C++
protos, so the Swift proto aspect cannot directly attach its own. (It's also not good practice
to attach providers that you don't own to arbitrary targets, because you don't know how those
targets might change in the future.) The `swift_proto_library` rule will pick up this provider
and return the underlying `CcInfo` provider as its own.

This provider is an implementation detail not meant to be used by clients.
""",
    fields = {
        "cc_info": "The underlying `CcInfo` provider.",
        "objc_info": "The underlying `apple_common.Objc` provider.",
    },
)

def _filter_out_well_known_types(srcs):
    """Returns the given list of files, excluding any well-known type protos.

    Args:
      srcs: A list of `.proto` files.

    Returns:
      The given list of files with any well-known type protos (those living under
      the `google.protobuf` package) removed.
    """
    return [
        f
        for f in srcs
        if workspace_relative_path(f) not in _WELL_KNOWN_TYPE_PATHS
    ]

def _register_pbswift_generate_action(
        label,
        actions,
        direct_srcs,
        transitive_descriptor_sets,
        module_mapping_file,
        mkdir_and_run,
        protoc_executable,
        protoc_plugin_executable):
    """Registers the actions that generate `.pb.swift` files from `.proto` files.

    Args:<commit_msg>Add descriptor.proto to the list of bundled proto files.<commit_after># Copyright 2018 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""An aspect attached to `proto_library` targets to generate Swift artifacts."""

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load(":api.bzl", "swift_common")
load(":features.bzl", "SWIFT_FEATURE_ENABLE_TESTING", "SWIFT_FEATURE_NO_GENERATED_HEADER")
load(":linking.bzl", "register_libraries_to_link")
load(
    ":proto_gen_utils.bzl",
    "declare_generated_files",
    "extract_generated_dir_path",
    "register_module_mapping_write_action",
)
load(":providers.bzl", "SwiftInfo", "SwiftProtoInfo", "SwiftToolchainInfo")
load(":utils.bzl", "compact", "create_cc_info", "get_providers", "workspace_relative_path")

# The paths of proto files bundled with the runtime. This is mainly the well
# known type protos, but also includes descriptor.proto to make generation of
# files that include options easier. These files should not be generated by
# the aspect because they are already included in the SwiftProtobuf runtime.
# The plugin provides the mapping from these protos to the SwiftProtobuf
# module for us.
# TODO(b/63389580): Once we migrate to proto_lang_toolchain, this information
# can go in the blacklisted_protos list instead.
_RUNTIME_BUNDLED_PROTO_FILES = [
    "google/protobuf/any.proto",
    "google/protobuf/api.proto",
    "google/protobuf/descriptor.proto",
    "google/protobuf/duration.proto",
    "google/protobuf/empty.proto",
    "google/protobuf/field_mask.proto",
    "google/protobuf/source_context.proto",
    "google/protobuf/struct.proto",
    "google/protobuf/timestamp.proto",
    "google/protobuf/type.proto",
    "google/protobuf/wrappers.proto",
]

SwiftProtoCcInfo = provider(
    doc = """
Wraps a `CcInfo` provider added to a `proto_library` through the Swift proto aspect.

This is necessary because `proto_library` targets already propagate a `CcInfo` provider for C++
protos, so the Swift proto aspect cannot directly attach its own. (It's also not good practice
to attach providers that you don't own to arbitrary targets, because you don't know how those
targets might change in the future.) The `swift_proto_library` rule will pick up this provider
and return the underlying `CcInfo` provider as its own.

This provider is an implementation detail not meant to be used by clients.
""",
    fields = {
        "cc_info": "The underlying `CcInfo` provider.",
        "objc_info": "The underlying `apple_common.Objc` provider.",
    },
)

def _filter_out_well_known_types(srcs):
    """Returns the given list of files, excluding any well-known type protos.

    Args:
      srcs: A list of `.proto` files.

    Returns:
      The given list of files with any well-known type protos (those living under
      the `google.protobuf` package) removed.
    """
    return [
        f
        for f in srcs
        if workspace_relative_path(f) not in _RUNTIME_BUNDLED_PROTO_FILES
    ]

def _register_pbswift_generate_action(
        label,
        actions,
        direct_srcs,
        transitive_descriptor_sets,
        module_mapping_file,
        mkdir_and_run,
        protoc_executable,
        protoc_plugin_executable):
    """Registers the actions that generate `.pb.swift` files from `.proto` files.

    Args:
{'fraction_non_alphanumeric': 0.09109311740890688, 'fraction_numerical': 0.004318488529014845, 'mean_word_length': 4.072553045859, 'pattern_counts': {'":': 16, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '813246', 'n_tokens_mistral': 2213, 'n_tokens_neox': 2128, 'n_words': 758}