|
| 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 | +"""Implementation of the `swift_synthesize_interface_aspect` aspect.""" |
| 16 | + |
| 17 | +load( |
| 18 | + "//swift/internal:features.bzl", |
| 19 | + "configure_features", |
| 20 | +) |
| 21 | +load( |
| 22 | + "//swift/internal:interface_synthesizing.bzl", |
| 23 | + "synthesize_interface", |
| 24 | +) |
| 25 | +load( |
| 26 | + "//swift/internal:toolchain_utils.bzl", |
| 27 | + "get_swift_toolchain", |
| 28 | + "use_swift_toolchain", |
| 29 | +) |
| 30 | +load(":providers.bzl", "SwiftInfo", "SwiftSynthesizedInterfaceInfo") |
| 31 | +load(":swift_clang_module_aspect.bzl", "swift_clang_module_aspect") |
| 32 | + |
| 33 | +visibility("public") |
| 34 | + |
| 35 | +def _get_swift_info(target): |
| 36 | + """Returns the SwiftInfo provider or None if it is not present.""" |
| 37 | + if SwiftInfo in target: |
| 38 | + return target[SwiftInfo] |
| 39 | + else: |
| 40 | + return None |
| 41 | + |
| 42 | +def _swift_synthesize_interface_aspect_impl(target, aspect_ctx): |
| 43 | + direct_outputs = [] |
| 44 | + synthesized_modules = [] |
| 45 | + |
| 46 | + swift_info = _get_swift_info(target) |
| 47 | + if swift_info and swift_info.direct_modules: |
| 48 | + swift_toolchain = get_swift_toolchain(aspect_ctx) |
| 49 | + feature_configuration = configure_features( |
| 50 | + ctx = aspect_ctx, |
| 51 | + swift_toolchain = swift_toolchain, |
| 52 | + requested_features = aspect_ctx.features, |
| 53 | + unsupported_features = aspect_ctx.disabled_features, |
| 54 | + ) |
| 55 | + |
| 56 | + if CcInfo in target: |
| 57 | + compilation_context = target[CcInfo].compilation_context |
| 58 | + else: |
| 59 | + compilation_context = cc_common.create_compilation_context() |
| 60 | + |
| 61 | + for module in swift_info.direct_modules: |
| 62 | + output_file = aspect_ctx.actions.declare_file( |
| 63 | + "{}.synthesized.swift".format(target.label.name), |
| 64 | + ) |
| 65 | + direct_outputs.append(output_file) |
| 66 | + synthesize_interface( |
| 67 | + actions = aspect_ctx.actions, |
| 68 | + compilation_contexts = [compilation_context], |
| 69 | + feature_configuration = feature_configuration, |
| 70 | + module_name = module.name, |
| 71 | + output_file = output_file, |
| 72 | + swift_infos = [swift_info], |
| 73 | + swift_toolchain = swift_toolchain, |
| 74 | + ) |
| 75 | + synthesized_modules.append( |
| 76 | + struct( |
| 77 | + module_name = module.name, |
| 78 | + synthesized_interface = output_file, |
| 79 | + ), |
| 80 | + ) |
| 81 | + |
| 82 | + transitive_synthesized_modules = [] |
| 83 | + for dep in getattr(aspect_ctx.rule.attr, "deps", []): |
| 84 | + if SwiftSynthesizedInterfaceInfo in dep: |
| 85 | + synth_info = dep[SwiftSynthesizedInterfaceInfo] |
| 86 | + transitive_synthesized_modules.append( |
| 87 | + synth_info.transitive_modules, |
| 88 | + ) |
| 89 | + |
| 90 | + return [ |
| 91 | + OutputGroupInfo( |
| 92 | + swift_synthesized_interface = depset(direct_outputs), |
| 93 | + ), |
| 94 | + SwiftSynthesizedInterfaceInfo( |
| 95 | + direct_modules = synthesized_modules, |
| 96 | + transitive_modules = depset( |
| 97 | + synthesized_modules, |
| 98 | + transitive = transitive_synthesized_modules, |
| 99 | + ), |
| 100 | + ), |
| 101 | + ] |
| 102 | + |
| 103 | +swift_synthesize_interface_aspect = aspect( |
| 104 | + attr_aspects = ["deps"], |
| 105 | + doc = """\ |
| 106 | + Synthesizes the Swift interface for the target to which it is applied. |
| 107 | +
|
| 108 | + This aspect invokes `swift-synthesize-interface` on the target to which |
| 109 | + it is applied and produces the output in a file located at |
| 110 | + `bazel-bin/<package_name>/<target_name>.synthesized.swift`. This output |
| 111 | + file can be obtained by requesting the output group named |
| 112 | + `swift_synthesized_interface` during the build. |
| 113 | +
|
| 114 | + The output group only contains the synthesized interface for the target |
| 115 | + to which the aspect is *directly* applied; it does not contain the |
| 116 | + synthesized interfaces for any transitive dependencies. If the full |
| 117 | + transitive closure of synthesized interfaces is needed, then clients |
| 118 | + should read the `SwiftSynthesizedInterfaceInfo` provider, which contains |
| 119 | + a `depset` with the full transitive closure. |
| 120 | + """, |
| 121 | + fragments = ["cpp"], |
| 122 | + implementation = _swift_synthesize_interface_aspect_impl, |
| 123 | + provides = [SwiftSynthesizedInterfaceInfo], |
| 124 | + requires = [swift_clang_module_aspect], |
| 125 | + toolchains = use_swift_toolchain(), |
| 126 | +) |
0 commit comments