Skip to content

Commit

Permalink
Add a Boolean library_evolution attribute to swift_library
Browse files Browse the repository at this point in the history
Historically, library evolution has been supported by a global flag that Apple framework rules transition on in order to build all dependencies with library evolution enabled. In retrospect, this is wrong; we shouldn't be automatically applying library evolution to the whole subgraph in this way. Instead, the owner of the framework should explicitly specify library evolution on the client-facing modules that make up their SDK.

The goal here is to ensure that framework owners carefully audit their public `deps` vs. implementation-only `private_deps` so that the `.swiftinterface` doesn't attempt to import anything that it shouldn't, especially with Swift 5.7 validating emitted interfaces after compilation by default.

This change only adds the new attribute; the flag is still honored while we have clients and rules_apple depending on it.

PiperOrigin-RevId: 486750877
(cherry picked from commit b9175e6)

Cherry-pick notes: With this change the private swiftinterface is enabled by `ctx.attr.library_evolution` as well. It can be disabled with `features = ["-swift.emit_private_swiftinterface”]`.

Signed-off-by: Brentley Jones <github@brentleyjones.com>
  • Loading branch information
allevato authored and brentleyjones committed Oct 4, 2024
1 parent 884373e commit 6beacf8
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 95 deletions.
10 changes: 6 additions & 4 deletions doc/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,8 @@ its transitive dependencies be propagated.

<pre>
swift_library(<a href="#swift_library-name">name</a>, <a href="#swift_library-deps">deps</a>, <a href="#swift_library-srcs">srcs</a>, <a href="#swift_library-data">data</a>, <a href="#swift_library-always_include_developer_search_paths">always_include_developer_search_paths</a>, <a href="#swift_library-alwayslink">alwayslink</a>, <a href="#swift_library-copts">copts</a>,
<a href="#swift_library-defines">defines</a>, <a href="#swift_library-generated_header_name">generated_header_name</a>, <a href="#swift_library-generates_header">generates_header</a>, <a href="#swift_library-linkopts">linkopts</a>, <a href="#swift_library-linkstatic">linkstatic</a>, <a href="#swift_library-module_name">module_name</a>,
<a href="#swift_library-package_name">package_name</a>, <a href="#swift_library-plugins">plugins</a>, <a href="#swift_library-private_deps">private_deps</a>, <a href="#swift_library-swiftc_inputs">swiftc_inputs</a>)
<a href="#swift_library-defines">defines</a>, <a href="#swift_library-generated_header_name">generated_header_name</a>, <a href="#swift_library-generates_header">generates_header</a>, <a href="#swift_library-library_evolution">library_evolution</a>, <a href="#swift_library-linkopts">linkopts</a>,
<a href="#swift_library-linkstatic">linkstatic</a>, <a href="#swift_library-module_name">module_name</a>, <a href="#swift_library-package_name">package_name</a>, <a href="#swift_library-plugins">plugins</a>, <a href="#swift_library-private_deps">private_deps</a>, <a href="#swift_library-swiftc_inputs">swiftc_inputs</a>)
</pre>

Compiles and links Swift code into a static library and Swift module.
Expand All @@ -400,6 +400,7 @@ Compiles and links Swift code into a static library and Swift module.
| <a id="swift_library-defines"></a>defines | A list of defines to add to the compilation command line.<br><br>Note that unlike C-family languages, Swift defines do not have values; they are simply identifiers that are either defined or undefined. So strings in this list should be simple identifiers, **not** `name=value` pairs.<br><br>Each string is prepended with `-D` and added to the command line. Unlike `copts`, these flags are added for the target and every target that depends on it, so use this attribute with caution. It is preferred that you add defines directly to `copts`, only using this feature in the rare case that a library needs to propagate a symbol up to those that depend on it. | List of strings | optional | `[]` |
| <a id="swift_library-generated_header_name"></a>generated_header_name | The name of the generated Objective-C interface header. This name must end with a `.h` extension and cannot contain any path separators.<br><br>If this attribute is not specified, then the default behavior is to name the header `${target_name}-Swift.h`.<br><br>This attribute is ignored if the toolchain does not support generating headers. | String | optional | `""` |
| <a id="swift_library-generates_header"></a>generates_header | If True, an Objective-C header will be generated for this target, in the same build package where the target is defined. By default, the name of the header is `${target_name}-Swift.h`; this can be changed using the `generated_header_name` attribute.<br><br>Targets should only set this attribute to True if they export Objective-C APIs. A header generated for a target that does not export Objective-C APIs will be effectively empty (except for a large amount of prologue and epilogue code) and this is generally wasteful because the extra file needs to be propagated in the build graph and, when explicit modules are enabled, extra actions must be executed to compile the Objective-C module for the generated header. | Boolean | optional | `False` |
| <a id="swift_library-library_evolution"></a>library_evolution | Indicates whether the Swift code should be compiled with library evolution mode enabled.<br><br>This attribute should be used to compile a module that will be distributed as part of a client-facing (non-implementation-only) module in a library or framework that will be distributed for use outside of the Bazel build graph. Setting this to true will compile the module with the `-library-evolution` flag and emit a `.swiftinterface` file as one of the compilation outputs. | Boolean | optional | `False` |
| <a id="swift_library-linkopts"></a>linkopts | Additional linker options that should be passed to the linker for the binary that depends on this target. These strings are subject to `$(location ...)` and ["Make" variable](https://docs.bazel.build/versions/master/be/make-variables.html) expansion. | List of strings | optional | `[]` |
| <a id="swift_library-linkstatic"></a>linkstatic | If True, the Swift module will be built for static linking. This will make all interfaces internal to the module that is being linked against and will inform the consuming module that the objects will be locally available (which may potentially avoid a PLT relocation). Set to `False` to build a `.so` or `.dll`. | Boolean | optional | `True` |
| <a id="swift_library-module_name"></a>module_name | The name of the Swift module being built.<br><br>If left unspecified, the module name will be computed based on the target's build label, by stripping the leading `//` and replacing `/`, `:`, and other non-identifier characters with underscores. | String | optional | `""` |
Expand Down Expand Up @@ -528,8 +529,8 @@ swift_proto_compiler(<a href="#swift_proto_compiler-name">name</a>, <a href="#sw
<pre>
swift_proto_library(<a href="#swift_proto_library-name">name</a>, <a href="#swift_proto_library-deps">deps</a>, <a href="#swift_proto_library-srcs">srcs</a>, <a href="#swift_proto_library-data">data</a>, <a href="#swift_proto_library-additional_compiler_deps">additional_compiler_deps</a>, <a href="#swift_proto_library-additional_compiler_info">additional_compiler_info</a>,
<a href="#swift_proto_library-always_include_developer_search_paths">always_include_developer_search_paths</a>, <a href="#swift_proto_library-alwayslink">alwayslink</a>, <a href="#swift_proto_library-compilers">compilers</a>, <a href="#swift_proto_library-copts">copts</a>, <a href="#swift_proto_library-defines">defines</a>,
<a href="#swift_proto_library-generated_header_name">generated_header_name</a>, <a href="#swift_proto_library-generates_header">generates_header</a>, <a href="#swift_proto_library-linkopts">linkopts</a>, <a href="#swift_proto_library-linkstatic">linkstatic</a>, <a href="#swift_proto_library-module_name">module_name</a>,
<a href="#swift_proto_library-package_name">package_name</a>, <a href="#swift_proto_library-plugins">plugins</a>, <a href="#swift_proto_library-protos">protos</a>, <a href="#swift_proto_library-swiftc_inputs">swiftc_inputs</a>)
<a href="#swift_proto_library-generated_header_name">generated_header_name</a>, <a href="#swift_proto_library-generates_header">generates_header</a>, <a href="#swift_proto_library-library_evolution">library_evolution</a>, <a href="#swift_proto_library-linkopts">linkopts</a>, <a href="#swift_proto_library-linkstatic">linkstatic</a>,
<a href="#swift_proto_library-module_name">module_name</a>, <a href="#swift_proto_library-package_name">package_name</a>, <a href="#swift_proto_library-plugins">plugins</a>, <a href="#swift_proto_library-protos">protos</a>, <a href="#swift_proto_library-swiftc_inputs">swiftc_inputs</a>)
</pre>

Generates a Swift static library from one or more targets producing `ProtoInfo`.
Expand Down Expand Up @@ -587,6 +588,7 @@ swift_proto_library(
| <a id="swift_proto_library-defines"></a>defines | A list of defines to add to the compilation command line.<br><br>Note that unlike C-family languages, Swift defines do not have values; they are simply identifiers that are either defined or undefined. So strings in this list should be simple identifiers, **not** `name=value` pairs.<br><br>Each string is prepended with `-D` and added to the command line. Unlike `copts`, these flags are added for the target and every target that depends on it, so use this attribute with caution. It is preferred that you add defines directly to `copts`, only using this feature in the rare case that a library needs to propagate a symbol up to those that depend on it. | List of strings | optional | `[]` |
| <a id="swift_proto_library-generated_header_name"></a>generated_header_name | The name of the generated Objective-C interface header. This name must end with a `.h` extension and cannot contain any path separators.<br><br>If this attribute is not specified, then the default behavior is to name the header `${target_name}-Swift.h`.<br><br>This attribute is ignored if the toolchain does not support generating headers. | String | optional | `""` |
| <a id="swift_proto_library-generates_header"></a>generates_header | If True, an Objective-C header will be generated for this target, in the same build package where the target is defined. By default, the name of the header is `${target_name}-Swift.h`; this can be changed using the `generated_header_name` attribute.<br><br>Targets should only set this attribute to True if they export Objective-C APIs. A header generated for a target that does not export Objective-C APIs will be effectively empty (except for a large amount of prologue and epilogue code) and this is generally wasteful because the extra file needs to be propagated in the build graph and, when explicit modules are enabled, extra actions must be executed to compile the Objective-C module for the generated header. | Boolean | optional | `False` |
| <a id="swift_proto_library-library_evolution"></a>library_evolution | Indicates whether the Swift code should be compiled with library evolution mode enabled.<br><br>This attribute should be used to compile a module that will be distributed as part of a client-facing (non-implementation-only) module in a library or framework that will be distributed for use outside of the Bazel build graph. Setting this to true will compile the module with the `-library-evolution` flag and emit a `.swiftinterface` file as one of the compilation outputs. | Boolean | optional | `False` |
| <a id="swift_proto_library-linkopts"></a>linkopts | Additional linker options that should be passed to the linker for the binary that depends on this target. These strings are subject to `$(location ...)` and ["Make" variable](https://docs.bazel.build/versions/master/be/make-variables.html) expansion. | List of strings | optional | `[]` |
| <a id="swift_proto_library-linkstatic"></a>linkstatic | If True, the Swift module will be built for static linking. This will make all interfaces internal to the module that is being linked against and will inform the consuming module that the objects will be locally available (which may potentially avoid a PLT relocation). Set to `False` to build a `.so` or `.dll`. | Boolean | optional | `True` |
| <a id="swift_proto_library-module_name"></a>module_name | The name of the Swift module being built.<br><br>If left unspecified, the module name will be computed based on the target's build label, by stripping the leading `//` and replacing `/`, `:`, and other non-identifier characters with underscores. | String | optional | `""` |
Expand Down
14 changes: 14 additions & 0 deletions swift/internal/attrs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,20 @@ def swift_library_rule_attrs(
),
swift_config_attrs(),
{
"library_evolution": attr.bool(
default = False,
doc = """\
Indicates whether the Swift code should be compiled with library evolution mode
enabled.
This attribute should be used to compile a module that will be distributed as
part of a client-facing (non-implementation-only) module in a library or
framework that will be distributed for use outside of the Bazel build graph.
Setting this to true will compile the module with the `-library-evolution` flag
and emit a `.swiftinterface` file as one of the compilation outputs.
""",
mandatory = False,
),
"alwayslink": attr.bool(
default = False,
doc = """\
Expand Down
13 changes: 11 additions & 2 deletions swift/swift_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,20 @@ def _swift_library_impl(ctx):
copts.extend(module_copts)

extra_features = []
if ctx.attr._config_emit_swiftinterface[BuildSettingInfo].value:

# TODO(b/239957001): Remove the global flag.
if (
ctx.attr.library_evolution or
ctx.attr._config_emit_swiftinterface[BuildSettingInfo].value
):
extra_features.append(SWIFT_FEATURE_ENABLE_LIBRARY_EVOLUTION)
extra_features.append(SWIFT_FEATURE_EMIT_SWIFTINTERFACE)

if ctx.attr._config_emit_private_swiftinterface[BuildSettingInfo].value:
# TODO(b/239957001): Remove the global flag.
if (
ctx.attr.library_evolution or
ctx.attr._config_emit_private_swiftinterface[BuildSettingInfo].value
):
extra_features.append(SWIFT_FEATURE_ENABLE_LIBRARY_EVOLUTION)
extra_features.append(SWIFT_FEATURE_EMIT_PRIVATE_SWIFTINTERFACE)

Expand Down
36 changes: 5 additions & 31 deletions test/fixtures/module_interface/BUILD
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
load("//swift:swift_binary.bzl", "swift_binary")
load("//swift:swift_import.bzl", "swift_import")
load("//swift:swift_library.bzl", "swift_library")
load("//test/fixtures:common.bzl", "FIXTURE_TAGS")
load(
"//test/rules:swift_library_artifact_collector.bzl",
"swift_library_artifact_collector",
)

package(
default_testonly = True,
Expand All @@ -23,32 +18,11 @@ swift_binary(

swift_import(
name = "toy_module",
archives = [":toy_outputs/libToyModule.a"],
archives = [
"//test/fixtures/module_interface/library:toy_outputs/libToyModule.a",
],
module_name = "ToyModule",
swiftdoc = ":toy_outputs/ToyModule.swiftdoc",
swiftinterface = ":toy_outputs/ToyModule.swiftinterface",
tags = FIXTURE_TAGS,
)

# Checking in pre-built artifacts like a `.swiftinterface` and static libraries
# would require different artifacts for every platform the test might run on.
# Instead, build it on-demand but forward the outputs using the "artifact
# collector" rule below to make them act as if they were pre-built outputs when
# referenced by the `swift_import` rule.

swift_library(
name = "toy_module_library",
srcs = ["ToyModule.swift"],
module_name = "ToyModule",
tags = FIXTURE_TAGS,
)

swift_library_artifact_collector(
name = "toy_module_artifact_collector",
static_library = "toy_outputs/libToyModule.a",
swiftdoc = "toy_outputs/ToyModule.swiftdoc",
swiftinterface = "toy_outputs/ToyModule.swiftinterface",
swiftdoc = "//test/fixtures/module_interface/library:toy_outputs/ToyModule.swiftdoc",
swiftinterface = "//test/fixtures/module_interface/library:toy_outputs/ToyModule.swiftinterface",
tags = FIXTURE_TAGS,
target = ":toy_module_library",
target_compatible_with = ["@platforms//os:macos"],
)
51 changes: 51 additions & 0 deletions test/fixtures/module_interface/library/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
load("//swift:swift_library.bzl", "swift_library")
load(
"//test/fixtures:common.bzl",
"FIXTURE_TAGS",
)
load(
"//test/rules:swift_library_artifact_collector.bzl",
"swift_library_artifact_collector",
)

package(
default_testonly = True,
default_visibility = ["//test:__subpackages__"],
)

licenses(["notice"])

# Checking in pre-built artifacts like a `.swiftinterface` and static libraries
# would require different artifacts for every platform the test might run on.
# Instead, build it on-demand but forward the outputs using the "artifact
# collector" rule below to make them act as if they were pre-built outputs when
# referenced by the `swift_import` rule.
#
# These must be in a separate package than the `swift_import` target because
# that rule propagates its pre-built inputs in `DefaultInfo`.

swift_library(
name = "toy_module_library",
srcs = ["ToyModule.swift"],
library_evolution = True,
module_name = "ToyModule",
tags = FIXTURE_TAGS,
)

swift_library_artifact_collector(
name = "toy_module_artifact_collector",
static_library = "toy_outputs/libToyModule.a",
swiftdoc = "toy_outputs/ToyModule.swiftdoc",
swiftinterface = "toy_outputs/ToyModule.swiftinterface",
tags = FIXTURE_TAGS,
target = ":toy_module_library",
target_compatible_with = ["@platforms//os:macos"],
)

swift_library(
name = "toy_module_library_without_library_evolution",
srcs = ["ToyModule.swift"],
library_evolution = False,
module_name = "ToyModuleNoEvolution",
tags = FIXTURE_TAGS,
)
29 changes: 3 additions & 26 deletions test/fixtures/private_swiftinterface/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ check them in directly.

load("//swift:swift_binary.bzl", "swift_binary")
load("//swift:swift_import.bzl", "swift_import")
load("//swift:swift_library.bzl", "swift_library")
load("//test/fixtures:common.bzl", "FIXTURE_TAGS")
load(
"//test/rules:swift_library_artifact_collector.bzl",
"swift_library_artifact_collector",
)

package(
default_testonly = True,
Expand All @@ -43,30 +38,12 @@ swift_binary(

swift_import(
name = "private_swiftinterface_import",
archives = [":private_swiftinterface_outputs/libPrivateSwiftInterface.a"],
archives = ["//test/fixtures/private_swiftinterface/library:private_swiftinterface_outputs/libPrivateSwiftInterface.a"],
module_name = "PrivateSwiftInterface",
swiftdoc = ":private_swiftinterface_outputs/PrivateSwiftInterface.swiftdoc",
swiftdoc = "//test/fixtures/private_swiftinterface/library:private_swiftinterface_outputs/PrivateSwiftInterface.swiftdoc",
# Using the private interface allows using both the normal and private interfaces of a module.
# Only one swiftinterface is allowed per module, so we can't use both at the same time.
# This tests that using the private interface allows a dependent module to use an `@_spi` symbol.
swiftinterface = ":private_swiftinterface_outputs/PrivateSwiftInterface.private.swiftinterface",
tags = FIXTURE_TAGS,
)

swift_library(
name = "private_swiftinterface",
srcs = ["Lib.swift"],
module_name = "PrivateSwiftInterface",
tags = FIXTURE_TAGS,
)

swift_library_artifact_collector(
name = "private_swiftinterface_artifact_collector",
private_swiftinterface = "private_swiftinterface_outputs/PrivateSwiftInterface.private.swiftinterface",
static_library = "private_swiftinterface_outputs/libPrivateSwiftInterface.a",
swiftdoc = "private_swiftinterface_outputs/PrivateSwiftInterface.swiftdoc",
swiftinterface = "private_swiftinterface_outputs/PrivateSwiftInterface.swiftinterface",
swiftinterface = "//test/fixtures/private_swiftinterface/library:private_swiftinterface_outputs/PrivateSwiftInterface.private.swiftinterface",
tags = FIXTURE_TAGS,
target = ":private_swiftinterface",
target_compatible_with = ["@platforms//os:macos"],
)
Loading

0 comments on commit 6beacf8

Please sign in to comment.