Skip to content

Commit 0e0e4ab

Browse files
authored
Support additional_linker_inputs in swift_binary (#1584)
1 parent 25ea7ca commit 0e0e4ab

File tree

7 files changed

+152
-10
lines changed

7 files changed

+152
-10
lines changed

doc/rules.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ On this page:
4141
## swift_binary
4242

4343
<pre>
44-
swift_binary(<a href="#swift_binary-name">name</a>, <a href="#swift_binary-deps">deps</a>, <a href="#swift_binary-srcs">srcs</a>, <a href="#swift_binary-data">data</a>, <a href="#swift_binary-copts">copts</a>, <a href="#swift_binary-defines">defines</a>, <a href="#swift_binary-env">env</a>, <a href="#swift_binary-linkopts">linkopts</a>, <a href="#swift_binary-malloc">malloc</a>, <a href="#swift_binary-module_name">module_name</a>,
45-
<a href="#swift_binary-package_name">package_name</a>, <a href="#swift_binary-plugins">plugins</a>, <a href="#swift_binary-stamp">stamp</a>, <a href="#swift_binary-swiftc_inputs">swiftc_inputs</a>)
44+
swift_binary(<a href="#swift_binary-name">name</a>, <a href="#swift_binary-deps">deps</a>, <a href="#swift_binary-srcs">srcs</a>, <a href="#swift_binary-data">data</a>, <a href="#swift_binary-additional_linker_inputs">additional_linker_inputs</a>, <a href="#swift_binary-copts">copts</a>, <a href="#swift_binary-defines">defines</a>, <a href="#swift_binary-env">env</a>, <a href="#swift_binary-linkopts">linkopts</a>,
45+
<a href="#swift_binary-malloc">malloc</a>, <a href="#swift_binary-module_name">module_name</a>, <a href="#swift_binary-package_name">package_name</a>, <a href="#swift_binary-plugins">plugins</a>, <a href="#swift_binary-stamp">stamp</a>, <a href="#swift_binary-swiftc_inputs">swiftc_inputs</a>)
4646
</pre>
4747

4848
Compiles and links Swift code into an executable binary.
@@ -68,6 +68,7 @@ please use one of the platform-specific application rules in
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 | `[]` |
6969
| <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 | `[]` |
71+
| <a id="swift_binary-additional_linker_inputs"></a>additional_linker_inputs | List of additional files needed by the linker.<br><br>These files will be passed to the linker when linking the binary target. Typically, these are linker scripts or other files referenced by `linkopts`. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
7172
| <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 | `[]` |
7273
| <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 | `[]` |
7374
| <a id="swift_binary-env"></a>env | Specifies additional environment variables to set when the test is executed by `bazel run` or `bazel test`.<br><br>The values of these environment variables are subject to `$(location)` and "Make variable" substitution.<br><br>NOTE: The environment variables are not set when you run the target outside of Bazel (for example, by manually executing the binary in `bazel-bin/`). | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | `{}` |
@@ -85,8 +86,8 @@ please use one of the platform-specific application rules in
8586
## swift_compiler_plugin
8687

8788
<pre>
88-
swift_compiler_plugin(<a href="#swift_compiler_plugin-name">name</a>, <a href="#swift_compiler_plugin-deps">deps</a>, <a href="#swift_compiler_plugin-srcs">srcs</a>, <a href="#swift_compiler_plugin-data">data</a>, <a href="#swift_compiler_plugin-copts">copts</a>, <a href="#swift_compiler_plugin-defines">defines</a>, <a href="#swift_compiler_plugin-linkopts">linkopts</a>, <a href="#swift_compiler_plugin-malloc">malloc</a>, <a href="#swift_compiler_plugin-module_name">module_name</a>,
89-
<a href="#swift_compiler_plugin-package_name">package_name</a>, <a href="#swift_compiler_plugin-plugins">plugins</a>, <a href="#swift_compiler_plugin-stamp">stamp</a>, <a href="#swift_compiler_plugin-swiftc_inputs">swiftc_inputs</a>)
89+
swift_compiler_plugin(<a href="#swift_compiler_plugin-name">name</a>, <a href="#swift_compiler_plugin-deps">deps</a>, <a href="#swift_compiler_plugin-srcs">srcs</a>, <a href="#swift_compiler_plugin-data">data</a>, <a href="#swift_compiler_plugin-additional_linker_inputs">additional_linker_inputs</a>, <a href="#swift_compiler_plugin-copts">copts</a>, <a href="#swift_compiler_plugin-defines">defines</a>, <a href="#swift_compiler_plugin-linkopts">linkopts</a>,
90+
<a href="#swift_compiler_plugin-malloc">malloc</a>, <a href="#swift_compiler_plugin-module_name">module_name</a>, <a href="#swift_compiler_plugin-package_name">package_name</a>, <a href="#swift_compiler_plugin-plugins">plugins</a>, <a href="#swift_compiler_plugin-stamp">stamp</a>, <a href="#swift_compiler_plugin-swiftc_inputs">swiftc_inputs</a>)
9091
</pre>
9192

9293
Compiles and links a Swift compiler plugin (for example, a macro).
@@ -157,6 +158,7 @@ swift_library(
157158
| <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 | `[]` |
158159
| <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 | `[]` |
159160
| <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 | `[]` |
161+
| <a id="swift_compiler_plugin-additional_linker_inputs"></a>additional_linker_inputs | List of additional files needed by the linker.<br><br>These files will be passed to the linker when linking the binary target. Typically, these are linker scripts or other files referenced by `linkopts`. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
160162
| <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 | `[]` |
161163
| <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 | `[]` |
162164
| <a id="swift_compiler_plugin-linkopts"></a>linkopts | Additional linker options that should be passed to `clang`. These strings are subject to `$(location ...)` expansion. | List of strings | optional | `[]` |
@@ -921,8 +923,8 @@ swift_binary(
921923
## swift_test
922924

923925
<pre>
924-
swift_test(<a href="#swift_test-name">name</a>, <a href="#swift_test-deps">deps</a>, <a href="#swift_test-srcs">srcs</a>, <a href="#swift_test-data">data</a>, <a href="#swift_test-copts">copts</a>, <a href="#swift_test-defines">defines</a>, <a href="#swift_test-discover_tests">discover_tests</a>, <a href="#swift_test-env">env</a>, <a href="#swift_test-env_inherit">env_inherit</a>, <a href="#swift_test-linkopts">linkopts</a>,
925-
<a href="#swift_test-malloc">malloc</a>, <a href="#swift_test-module_name">module_name</a>, <a href="#swift_test-package_name">package_name</a>, <a href="#swift_test-plugins">plugins</a>, <a href="#swift_test-stamp">stamp</a>, <a href="#swift_test-swiftc_inputs">swiftc_inputs</a>)
926+
swift_test(<a href="#swift_test-name">name</a>, <a href="#swift_test-deps">deps</a>, <a href="#swift_test-srcs">srcs</a>, <a href="#swift_test-data">data</a>, <a href="#swift_test-additional_linker_inputs">additional_linker_inputs</a>, <a href="#swift_test-copts">copts</a>, <a href="#swift_test-defines">defines</a>, <a href="#swift_test-discover_tests">discover_tests</a>, <a href="#swift_test-env">env</a>,
927+
<a href="#swift_test-env_inherit">env_inherit</a>, <a href="#swift_test-linkopts">linkopts</a>, <a href="#swift_test-malloc">malloc</a>, <a href="#swift_test-module_name">module_name</a>, <a href="#swift_test-package_name">package_name</a>, <a href="#swift_test-plugins">plugins</a>, <a href="#swift_test-stamp">stamp</a>, <a href="#swift_test-swiftc_inputs">swiftc_inputs</a>)
926928
</pre>
927929

928930
Compiles and links Swift code into an executable test target.
@@ -996,6 +998,7 @@ root of your workspace (i.e. `$(SRCROOT)`).
996998
| <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 | `[]` |
997999
| <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 | `[]` |
9981000
| <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 | `[]` |
1001+
| <a id="swift_test-additional_linker_inputs"></a>additional_linker_inputs | List of additional files needed by the linker.<br><br>These files will be passed to the linker when linking the binary target. Typically, these are linker scripts or other files referenced by `linkopts`. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
9991002
| <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 | `[]` |
10001003
| <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 | `[]` |
10011004
| <a id="swift_test-discover_tests"></a>discover_tests | Determines whether or not tests are automatically discovered in the binary. The default value is `True`.<br><br>Tests are discovered in a platform-specific manner. On Apple platforms, they are found using the XCTest framework's `XCTestSuite.default` accessor, which uses the Objective-C runtime to dynamically discover tests. On non-Apple platforms, discovery uses symbol graphs generated from dependencies to find classes and methods written in XCTest's style.<br><br>If tests are discovered, then you should not provide your own `main` entry point in the `swift_test` binary; the test runtime provides the entry point for you. If you set this attribute to `False`, then you are responsible for providing your own `main`. This allows you to write tests that use a framework other than Apple's `XCTest`. The only requirement of such a test is that it terminate with a zero exit code for success or a non-zero exit code for failure. | Boolean | optional | `True` |

swift/internal/binary_attrs.bzl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ def binary_rule_attrs(
5959
requires_srcs = False,
6060
),
6161
{
62+
"additional_linker_inputs": attr.label_list(
63+
allow_files = True,
64+
doc = """\
65+
List of additional files needed by the linker.
66+
67+
These files will be passed to the linker when linking the binary target.
68+
Typically, these are linker scripts or other files referenced by `linkopts`.
69+
""",
70+
mandatory = False,
71+
),
6272
"linkopts": attr.string_list(
6373
doc = """\
6474
Additional linker options that should be passed to `clang`. These strings are

swift/swift_binary.bzl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def _swift_binary_impl(ctx):
157157
binary_link_flags = expand_locations(
158158
ctx,
159159
ctx.attr.linkopts,
160-
ctx.attr.swiftc_inputs,
160+
ctx.attr.additional_linker_inputs,
161161
) + ctx.fragments.cpp.linkopts
162162

163163
# When linking the binary, make sure we use the correct entry point name.
@@ -178,7 +178,7 @@ def _swift_binary_impl(ctx):
178178

179179
linking_outputs = register_link_binary_action(
180180
actions = ctx.actions,
181-
additional_inputs = ctx.files.swiftc_inputs,
181+
additional_inputs = ctx.files.additional_linker_inputs,
182182
additional_linking_contexts = additional_linking_contexts,
183183
additional_outputs = additional_debug_outputs,
184184
feature_configuration = feature_configuration,
@@ -228,7 +228,7 @@ def _swift_binary_impl(ctx):
228228
environment = expand_locations(
229229
ctx,
230230
ctx.attr.env,
231-
ctx.attr.swiftc_inputs,
231+
ctx.attr.swiftc_inputs + ctx.attr.additional_linker_inputs,
232232
),
233233
),
234234
]
@@ -240,7 +240,7 @@ def _swift_binary_impl(ctx):
240240
linking_context, _ = (
241241
create_linking_context_from_compilation_outputs(
242242
actions = ctx.actions,
243-
additional_inputs = ctx.files.swiftc_inputs,
243+
additional_inputs = ctx.files.additional_linker_inputs,
244244
alwayslink = True,
245245
compilation_outputs = compilation_outputs,
246246
feature_configuration = feature_configuration,

test/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2+
load(":additional_linker_inputs_tests.bzl", "additional_linker_inputs_test_suite")
23
load(":ast_file_tests.bzl", "ast_file_test_suite")
34
load(":bzl_test.bzl", "bzl_test")
45
load(":cc_library_tests.bzl", "cc_library_test_suite")
@@ -30,6 +31,8 @@ load(":xctest_runner_tests.bzl", "xctest_runner_test_suite")
3031

3132
licenses(["notice"])
3233

34+
additional_linker_inputs_test_suite(name = "additional_linker_inputs")
35+
3336
ast_file_test_suite(name = "ast_file")
3437

3538
cc_library_test_suite(name = "cc_library")

0 commit comments

Comments
 (0)