|
| 1 | +# Copyright 2025 The Bazel 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 | +"""Functions relating to synthesizing Swift interfaces from non-Swift targets.""" |
| 16 | + |
| 17 | +load("//swift:providers.bzl", "SwiftInfo") |
| 18 | +load(":action_names.bzl", "SWIFT_ACTION_SYNTHESIZE_INTERFACE") |
| 19 | +load(":actions.bzl", "run_toolchain_action") |
| 20 | +load(":utils.bzl", "merge_compilation_contexts") |
| 21 | + |
| 22 | +def synthesize_interface( |
| 23 | + *, |
| 24 | + actions, |
| 25 | + compilation_contexts, |
| 26 | + feature_configuration, |
| 27 | + module_name, |
| 28 | + output_file, |
| 29 | + swift_infos, |
| 30 | + swift_toolchain): |
| 31 | + """Extracts the symbol graph from a Swift module. |
| 32 | +
|
| 33 | + Args: |
| 34 | + actions: The object used to register actions. |
| 35 | + compilation_contexts: A list of `CcCompilationContext`s that represent |
| 36 | + C/Objective-C requirements of the target being compiled, such as |
| 37 | + Swift-compatible preprocessor defines, header search paths, and so |
| 38 | + forth. These are typically retrieved from the `CcInfo` providers of |
| 39 | + a target's dependencies. |
| 40 | + feature_configuration: The Swift feature configuration. |
| 41 | + module_name: The name of the module whose symbol graph should be |
| 42 | + extracted. |
| 43 | + output_file: A `File` into which the synthesized interface will be |
| 44 | + written. |
| 45 | + swift_infos: A list of `SwiftInfo` providers from dependencies of the |
| 46 | + target being compiled. This should include both propagated and |
| 47 | + non-propagated (implementation-only) dependencies. |
| 48 | + swift_toolchain: The `SwiftToolchainInfo` provider of the toolchain. |
| 49 | + """ |
| 50 | + merged_compilation_context = merge_compilation_contexts( |
| 51 | + transitive_compilation_contexts = compilation_contexts + [ |
| 52 | + cc_info.compilation_context |
| 53 | + for cc_info in swift_toolchain.implicit_deps_providers.cc_infos |
| 54 | + ], |
| 55 | + ) |
| 56 | + merged_swift_info = SwiftInfo( |
| 57 | + swift_infos = ( |
| 58 | + swift_infos + swift_toolchain.implicit_deps_providers.swift_infos |
| 59 | + ), |
| 60 | + ) |
| 61 | + |
| 62 | + # Flattening this `depset` is necessary because we need to extract the |
| 63 | + # module maps or precompiled modules out of structured values and do so |
| 64 | + # conditionally. |
| 65 | + transitive_modules = merged_swift_info.transitive_modules.to_list() |
| 66 | + |
| 67 | + transitive_swiftmodules = [] |
| 68 | + for module in transitive_modules: |
| 69 | + swift_module = module.swift |
| 70 | + if swift_module: |
| 71 | + transitive_swiftmodules.append(swift_module.swiftmodule) |
| 72 | + |
| 73 | + prerequisites = struct( |
| 74 | + bin_dir = feature_configuration._bin_dir, |
| 75 | + cc_compilation_context = merged_compilation_context, |
| 76 | + genfiles_dir = feature_configuration._genfiles_dir, |
| 77 | + is_swift = True, |
| 78 | + module_name = module_name, |
| 79 | + output_file = output_file, |
| 80 | + target_label = feature_configuration._label, |
| 81 | + transitive_modules = transitive_modules, |
| 82 | + transitive_swiftmodules = transitive_swiftmodules, |
| 83 | + ) |
| 84 | + |
| 85 | + run_toolchain_action( |
| 86 | + actions = actions, |
| 87 | + action_name = SWIFT_ACTION_SYNTHESIZE_INTERFACE, |
| 88 | + feature_configuration = feature_configuration, |
| 89 | + outputs = [output_file], |
| 90 | + prerequisites = prerequisites, |
| 91 | + progress_message = ( |
| 92 | + "Synthesizing Swift interface for {}".format(module_name) |
| 93 | + ), |
| 94 | + swift_toolchain = swift_toolchain, |
| 95 | + ) |
0 commit comments