Skip to content

Commit 6291111

Browse files
allevatobrentleyjones
authored andcommitted
Update docs to say that a source file should only be in a single swift_* target, and a swift_overlay should only be referenced by a single other target's aspect_hints
PiperOrigin-RevId: 664809810 (cherry picked from commit c593bc4) Signed-off-by: Brentley Jones <[email protected]>
1 parent 9af31cc commit 6291111

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

doc/rules.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ please use one of the platform-specific application rules in
6666
| :------------- | :------------- | :------------- | :------------- | :------------- |
6767
| <a id="swift_binary-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
6868
| <a id="swift_binary-deps"></a>deps | A list of targets that are dependencies of the target being built, which will be linked into that target.<br><br>If the Swift toolchain supports implementation-only imports (`private_deps` on `swift_library`), then targets in `deps` are treated as regular (non-implementation-only) imports that are propagated both to their direct and indirect (transitive) dependents.<br><br>Allowed kinds of dependencies are:<br><br>* `swift_library` (or anything propagating `SwiftInfo`)<br><br>* `cc_library` and `objc_library` (or anything propagating `CcInfo`) | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
69-
| <a id="swift_binary-srcs"></a>srcs | A list of `.swift` source files that will be compiled into the library. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
69+
| <a id="swift_binary-srcs"></a>srcs | A list of `.swift` source files that will be compiled into the library.<br><br>Except in very rare circumstances, a Swift source file should only appear in a single `swift_*` target. Adding the same source file to multiple `swift_*` targets can lead to binary bloat and/or symbol collisions. If specific sources need to be shared by multiple targets, consider factoring them out into their own `swift_library` instead. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
7070
| <a id="swift_binary-data"></a>data | The list of files needed by this target at runtime.<br><br>Files and targets named in the `data` attribute will appear in the `*.runfiles` area of this target, if it has one. This may include data files needed by a binary or library, or other programs needed by it. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
7171
| <a id="swift_binary-copts"></a>copts | Additional compiler options that should be passed to `swiftc`. These strings are subject to `$(location ...)` and ["Make" variable](https://docs.bazel.build/versions/master/be/make-variables.html) expansion. | List of strings | optional | `[]` |
7272
| <a id="swift_binary-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 | `[]` |
@@ -154,7 +154,7 @@ swift_library(
154154
| :------------- | :------------- | :------------- | :------------- | :------------- |
155155
| <a id="swift_compiler_plugin-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
156156
| <a id="swift_compiler_plugin-deps"></a>deps | A list of targets that are dependencies of the target being built, which will be linked into that target.<br><br>If the Swift toolchain supports implementation-only imports (`private_deps` on `swift_library`), then targets in `deps` are treated as regular (non-implementation-only) imports that are propagated both to their direct and indirect (transitive) dependents.<br><br>Allowed kinds of dependencies are:<br><br>* `swift_library` (or anything propagating `SwiftInfo`)<br><br>* `cc_library` and `objc_library` (or anything propagating `CcInfo`) | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
157-
| <a id="swift_compiler_plugin-srcs"></a>srcs | A list of `.swift` source files that will be compiled into the library. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
157+
| <a id="swift_compiler_plugin-srcs"></a>srcs | A list of `.swift` source files that will be compiled into the library.<br><br>Except in very rare circumstances, a Swift source file should only appear in a single `swift_*` target. Adding the same source file to multiple `swift_*` targets can lead to binary bloat and/or symbol collisions. If specific sources need to be shared by multiple targets, consider factoring them out into their own `swift_library` instead. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
158158
| <a id="swift_compiler_plugin-data"></a>data | The list of files needed by this target at runtime.<br><br>Files and targets named in the `data` attribute will appear in the `*.runfiles` area of this target, if it has one. This may include data files needed by a binary or library, or other programs needed by it. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
159159
| <a id="swift_compiler_plugin-copts"></a>copts | Additional compiler options that should be passed to `swiftc`. These strings are subject to `$(location ...)` and ["Make" variable](https://docs.bazel.build/versions/master/be/make-variables.html) expansion. | List of strings | optional | `[]` |
160160
| <a id="swift_compiler_plugin-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 | `[]` |
@@ -467,7 +467,7 @@ Compiles and links Swift code into a static library and Swift module.
467467
| :------------- | :------------- | :------------- | :------------- | :------------- |
468468
| <a id="swift_library-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
469469
| <a id="swift_library-deps"></a>deps | A list of targets that are dependencies of the target being built, which will be linked into that target.<br><br>If the Swift toolchain supports implementation-only imports (`private_deps` on `swift_library`), then targets in `deps` are treated as regular (non-implementation-only) imports that are propagated both to their direct and indirect (transitive) dependents.<br><br>Allowed kinds of dependencies are:<br><br>* `swift_library` (or anything propagating `SwiftInfo`)<br><br>* `cc_library` and `objc_library` (or anything propagating `CcInfo`) | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
470-
| <a id="swift_library-srcs"></a>srcs | A list of `.swift` source files that will be compiled into the library. | <a href="https://bazel.build/concepts/labels">List of labels</a> | required | |
470+
| <a id="swift_library-srcs"></a>srcs | A list of `.swift` source files that will be compiled into the library.<br><br>Except in very rare circumstances, a Swift source file should only appear in a single `swift_*` target. Adding the same source file to multiple `swift_*` targets can lead to binary bloat and/or symbol collisions. If specific sources need to be shared by multiple targets, consider factoring them out into their own `swift_library` instead. | <a href="https://bazel.build/concepts/labels">List of labels</a> | required | |
471471
| <a id="swift_library-data"></a>data | The list of files needed by this target at runtime.<br><br>Files and targets named in the `data` attribute will appear in the `*.runfiles` area of this target, if it has one. This may include data files needed by a binary or library, or other programs needed by it. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
472472
| <a id="swift_library-always_include_developer_search_paths"></a>always_include_developer_search_paths | If `True`, the developer framework search paths will be added to the compilation command. This enables a Swift module to access `XCTest` without having to mark the target as `testonly = True`. | Boolean | optional | `False` |
473473
| <a id="swift_library-alwayslink"></a>alwayslink | If `False`, any binary that depends (directly or indirectly) on this Swift module will only link in all the object files for the files listed in `srcs` when there is a direct symbol reference.<br><br>Swift protocol conformances don't create linker references. Likewise, if the Swift code has Objective-C classes/methods, their usage does not always result in linker references.<br><br>_"All the object files"_ for this module is also somewhat fuzzy. Unlike C, C++, and Objective-C, where each source file results in a `.o` file; for Swift the number of .o files depends on the compiler options (`-wmo`/`-whole-module-optimization`, `-num-threads`). That makes relying on linker reference more fragile, and any individual .swift file in `srcs` may/may not get picked up based on the linker references to other files that happen to get batched into a single `.o` by the compiler options used.<br><br>Swift Package Manager always passes the individual `.o` files to the linker instead of using intermediate static libraries, so it effectively is the same as `alwayslink = True`. | Boolean | optional | `True` |
@@ -682,14 +682,20 @@ overlay target must explicitly depend on them as well. This means that an
682682
overlay can have a different set of dependencies than the underlying module, if
683683
desired.
684684

685+
There is a tight coupling between a Swift overlay and the C/Objective-C module
686+
to which it is being applied, so a specific `swift_overlay` target should only
687+
be referenced by the `aspect_hints` of a single `objc_library` or `cc_library`
688+
target. Referencing a `swift_overlay` from multiple targets' `aspect_hints` is
689+
almost always an anti-pattern.
690+
685691
**ATTRIBUTES**
686692

687693

688694
| Name | Description | Type | Mandatory | Default |
689695
| :------------- | :------------- | :------------- | :------------- | :------------- |
690696
| <a id="swift_overlay-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
691697
| <a id="swift_overlay-deps"></a>deps | A list of targets that are dependencies of the target being built, which will be linked into that target.<br><br>If the Swift toolchain supports implementation-only imports (`private_deps` on `swift_library`), then targets in `deps` are treated as regular (non-implementation-only) imports that are propagated both to their direct and indirect (transitive) dependents.<br><br>Allowed kinds of dependencies are:<br><br>* `swift_library` (or anything propagating `SwiftInfo`)<br><br>* `cc_library` and `objc_library` (or anything propagating `CcInfo`) | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
692-
| <a id="swift_overlay-srcs"></a>srcs | A list of `.swift` source files that will be compiled into the library. | <a href="https://bazel.build/concepts/labels">List of labels</a> | required | |
698+
| <a id="swift_overlay-srcs"></a>srcs | A list of `.swift` source files that will be compiled into the library.<br><br>Except in very rare circumstances, a Swift source file should only appear in a single `swift_*` target. Adding the same source file to multiple `swift_*` targets can lead to binary bloat and/or symbol collisions. If specific sources need to be shared by multiple targets, consider factoring them out into their own `swift_library` instead. | <a href="https://bazel.build/concepts/labels">List of labels</a> | required | |
693699
| <a id="swift_overlay-always_include_developer_search_paths"></a>always_include_developer_search_paths | If `True`, the developer framework search paths will be added to the compilation command. This enables a Swift module to access `XCTest` without having to mark the target as `testonly = True`. | Boolean | optional | `False` |
694700
| <a id="swift_overlay-alwayslink"></a>alwayslink | If `False`, any binary that depends (directly or indirectly) on this Swift module will only link in all the object files for the files listed in `srcs` when there is a direct symbol reference.<br><br>Swift protocol conformances don't create linker references. Likewise, if the Swift code has Objective-C classes/methods, their usage does not always result in linker references.<br><br>_"All the object files"_ for this module is also somewhat fuzzy. Unlike C, C++, and Objective-C, where each source file results in a `.o` file; for Swift the number of .o files depends on the compiler options (`-wmo`/`-whole-module-optimization`, `-num-threads`). That makes relying on linker reference more fragile, and any individual .swift file in `srcs` may/may not get picked up based on the linker references to other files that happen to get batched into a single `.o` by the compiler options used.<br><br>Swift Package Manager always passes the individual `.o` files to the linker instead of using intermediate static libraries, so it effectively is the same as `alwayslink = True`. | Boolean | optional | `True` |
695701
| <a id="swift_overlay-copts"></a>copts | Additional compiler options that should be passed to `swiftc`. These strings are subject to `$(location ...)` and ["Make" variable](https://docs.bazel.build/versions/master/be/make-variables.html) expansion. | List of strings | optional | `[]` |
@@ -810,7 +816,7 @@ swift_proto_library(
810816
| :------------- | :------------- | :------------- | :------------- | :------------- |
811817
| <a id="swift_proto_library-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
812818
| <a id="swift_proto_library-deps"></a>deps | A list of targets that are dependencies of the target being built, which will be linked into that target.<br><br>If the Swift toolchain supports implementation-only imports (`private_deps` on `swift_library`), then targets in `deps` are treated as regular (non-implementation-only) imports that are propagated both to their direct and indirect (transitive) dependents.<br><br>Allowed kinds of dependencies are:<br><br>* `swift_library` (or anything propagating `SwiftInfo`)<br><br>* `cc_library` and `objc_library` (or anything propagating `CcInfo`) | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
813-
| <a id="swift_proto_library-srcs"></a>srcs | A list of `.swift` source files that will be compiled into the library. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
819+
| <a id="swift_proto_library-srcs"></a>srcs | A list of `.swift` source files that will be compiled into the library.<br><br>Except in very rare circumstances, a Swift source file should only appear in a single `swift_*` target. Adding the same source file to multiple `swift_*` targets can lead to binary bloat and/or symbol collisions. If specific sources need to be shared by multiple targets, consider factoring them out into their own `swift_library` instead. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
814820
| <a id="swift_proto_library-data"></a>data | The list of files needed by this target at runtime.<br><br>Files and targets named in the `data` attribute will appear in the `*.runfiles` area of this target, if it has one. This may include data files needed by a binary or library, or other programs needed by it. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
815821
| <a id="swift_proto_library-additional_compiler_deps"></a>additional_compiler_deps | List of additional dependencies required by the generated Swift code at compile time, whose SwiftProtoInfo will be ignored.<br><br>Allowed kinds of dependencies are:<br><br>* `swift_library` (or anything propagating `SwiftInfo`)<br><br>* `cc_library` and `objc_library` (or anything propagating `CcInfo`) | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
816822
| <a id="swift_proto_library-additional_compiler_info"></a>additional_compiler_info | Dictionary of additional information passed to the compiler targets. See the documentation of the respective compiler rules for more information on which fields are accepted and how they are used. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | `{}` |
@@ -987,7 +993,7 @@ root of your workspace (i.e. `$(SRCROOT)`).
987993
| :------------- | :------------- | :------------- | :------------- | :------------- |
988994
| <a id="swift_test-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
989995
| <a id="swift_test-deps"></a>deps | A list of targets that are dependencies of the target being built, which will be linked into that target.<br><br>If the Swift toolchain supports implementation-only imports (`private_deps` on `swift_library`), then targets in `deps` are treated as regular (non-implementation-only) imports that are propagated both to their direct and indirect (transitive) dependents.<br><br>Allowed kinds of dependencies are:<br><br>* `swift_library` (or anything propagating `SwiftInfo`)<br><br>* `cc_library` and `objc_library` (or anything propagating `CcInfo`) | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
990-
| <a id="swift_test-srcs"></a>srcs | A list of `.swift` source files that will be compiled into the library. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
996+
| <a id="swift_test-srcs"></a>srcs | A list of `.swift` source files that will be compiled into the library.<br><br>Except in very rare circumstances, a Swift source file should only appear in a single `swift_*` target. Adding the same source file to multiple `swift_*` targets can lead to binary bloat and/or symbol collisions. If specific sources need to be shared by multiple targets, consider factoring them out into their own `swift_library` instead. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
991997
| <a id="swift_test-data"></a>data | The list of files needed by this target at runtime.<br><br>Files and targets named in the `data` attribute will appear in the `*.runfiles` area of this target, if it has one. This may include data files needed by a binary or library, or other programs needed by it. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
992998
| <a id="swift_test-copts"></a>copts | Additional compiler options that should be passed to `swiftc`. These strings are subject to `$(location ...)` and ["Make" variable](https://docs.bazel.build/versions/master/be/make-variables.html) expansion. | List of strings | optional | `[]` |
993999
| <a id="swift_test-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 | `[]` |

0 commit comments

Comments
 (0)