Skip to content

Commit 2a20daa

Browse files
authored
Support "Make" variable expansion in copts and linkopts
Resolves #314.
1 parent 0e84c1e commit 2a20daa

File tree

4 files changed

+45
-24
lines changed

4 files changed

+45
-24
lines changed

doc/rules.md

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ please use one of the platform-specific application rules in
6363
<tr id="swift_binary.deps">
6464
<td><code>deps</code></td>
6565
<td>
66-
<p><code>List of <a hef="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>A list of targets that are dependencies of the target being built, which will be
66+
<p><code>List of <a href="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>A list of targets that are dependencies of the target being built, which will be
6767
linked into that target.</p>
6868
<p>If the Swift toolchain supports implementation-only imports (<code>private_deps</code> on
6969
<code>swift_library</code>), then targets in <code>deps</code> are treated as regular
@@ -83,12 +83,12 @@ Linux), those dependencies will be <strong>ignored.</strong></p></td>
8383
<tr id="swift_binary.srcs">
8484
<td><code>srcs</code></td>
8585
<td>
86-
<p><code>List of <a hef="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>A list of <code>.swift</code> source files that will be compiled into the library.</p></td>
86+
<p><code>List of <a href="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>A list of <code>.swift</code> source files that will be compiled into the library.</p></td>
8787
</tr>
8888
<tr id="swift_binary.data">
8989
<td><code>data</code></td>
9090
<td>
91-
<p><code>List of <a hef="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>The list of files needed by this target at runtime.</p>
91+
<p><code>List of <a href="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>The list of files needed by this target at runtime.</p>
9292
<p>Files and targets named in the <code>data</code> attribute will appear in the <code>*.runfiles</code>
9393
area of this target, if it has one. This may include data files needed by a
9494
binary or library, or other programs needed by it.</p></td>
@@ -152,7 +152,7 @@ good build result caching.</li>
152152
<tr id="swift_binary.swiftc_inputs">
153153
<td><code>swiftc_inputs</code></td>
154154
<td>
155-
<p><code>List of <a hef="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>Additional files that are referenced using <code>$(location ...)</code> in attributes that
155+
<p><code>List of <a href="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>Additional files that are referenced using <code>$(location ...)</code> in attributes that
156156
support location expansion.</p></td>
157157
</tr>
158158
</tbody>
@@ -214,7 +214,7 @@ any C++ declarations.
214214
<tr id="swift_c_module.deps">
215215
<td><code>deps</code></td>
216216
<td>
217-
<p><code>List of <a hef="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; required</code></p><p>A list of C targets (or anything propagating <code>CcInfo</code>) that are dependencies of
217+
<p><code>List of <a href="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; required</code></p><p>A list of C targets (or anything propagating <code>CcInfo</code>) that are dependencies of
218218
this target and whose headers may be referenced by the module map.</p></td>
219219
</tr>
220220
<tr id="swift_c_module.module_map">
@@ -330,14 +330,14 @@ swift_grpc_library(
330330
<tr id="swift_grpc_library.deps">
331331
<td><code>deps</code></td>
332332
<td>
333-
<p><code>List of <a hef="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>Exactly one <code>swift_proto_library</code> or <code>swift_grpc_library</code> target that contains
333+
<p><code>List of <a href="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>Exactly one <code>swift_proto_library</code> or <code>swift_grpc_library</code> target that contains
334334
the Swift protos used by the services being generated. Test stubs should depend
335335
on the <code>swift_grpc_library</code> implementing the service.</p></td>
336336
</tr>
337337
<tr id="swift_grpc_library.srcs">
338338
<td><code>srcs</code></td>
339339
<td>
340-
<p><code>List of <a hef="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>Exactly one <code>proto_library</code> target that defines the services being generated.</p></td>
340+
<p><code>List of <a href="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>Exactly one <code>proto_library</code> target that defines the services being generated.</p></td>
341341
</tr>
342342
<tr id="swift_grpc_library.flavor">
343343
<td><code>flavor</code></td>
@@ -381,7 +381,7 @@ Allows for the use of precompiled Swift modules as dependencies in other
381381
<tr id="swift_import.deps">
382382
<td><code>deps</code></td>
383383
<td>
384-
<p><code>List of <a hef="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>A list of targets that are dependencies of the target being built, which will be
384+
<p><code>List of <a href="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>A list of targets that are dependencies of the target being built, which will be
385385
linked into that target.</p>
386386
<p>If the Swift toolchain supports implementation-only imports (<code>private_deps</code> on
387387
<code>swift_library</code>), then targets in <code>deps</code> are treated as regular
@@ -401,15 +401,15 @@ Linux), those dependencies will be <strong>ignored.</strong></p></td>
401401
<tr id="swift_import.data">
402402
<td><code>data</code></td>
403403
<td>
404-
<p><code>List of <a hef="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>The list of files needed by this target at runtime.</p>
404+
<p><code>List of <a href="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>The list of files needed by this target at runtime.</p>
405405
<p>Files and targets named in the <code>data</code> attribute will appear in the <code>*.runfiles</code>
406406
area of this target, if it has one. This may include data files needed by a
407407
binary or library, or other programs needed by it.</p></td>
408408
</tr>
409409
<tr id="swift_import.archives">
410410
<td><code>archives</code></td>
411411
<td>
412-
<p><code>List of <a hef="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; required</code></p><p>The list of <code>.a</code> files provided to Swift targets that depend on this target.</p></td>
412+
<p><code>List of <a href="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; required</code></p><p>The list of <code>.a</code> files provided to Swift targets that depend on this target.</p></td>
413413
</tr>
414414
<tr id="swift_import.module_name">
415415
<td><code>module_name</code></td>
@@ -458,7 +458,7 @@ Compiles and links Swift code into a static library and Swift module.
458458
<tr id="swift_library.deps">
459459
<td><code>deps</code></td>
460460
<td>
461-
<p><code>List of <a hef="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>A list of targets that are dependencies of the target being built, which will be
461+
<p><code>List of <a href="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>A list of targets that are dependencies of the target being built, which will be
462462
linked into that target.</p>
463463
<p>If the Swift toolchain supports implementation-only imports (<code>private_deps</code> on
464464
<code>swift_library</code>), then targets in <code>deps</code> are treated as regular
@@ -478,12 +478,12 @@ Linux), those dependencies will be <strong>ignored.</strong></p></td>
478478
<tr id="swift_library.srcs">
479479
<td><code>srcs</code></td>
480480
<td>
481-
<p><code>List of <a hef="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>A list of <code>.swift</code> source files that will be compiled into the library.</p></td>
481+
<p><code>List of <a href="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>A list of <code>.swift</code> source files that will be compiled into the library.</p></td>
482482
</tr>
483483
<tr id="swift_library.data">
484484
<td><code>data</code></td>
485485
<td>
486-
<p><code>List of <a hef="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>The list of files needed by this target at runtime.</p>
486+
<p><code>List of <a href="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>The list of files needed by this target at runtime.</p>
487487
<p>Files and targets named in the <code>data</code> attribute will appear in the <code>*.runfiles</code>
488488
area of this target, if it has one. This may include data files needed by a
489489
binary or library, or other programs needed by it.</p></td>
@@ -503,7 +503,7 @@ conformance.</p></td>
503503
<td><code>copts</code></td>
504504
<td>
505505
<p><code>List of strings; optional</code></p><p>Additional compiler options that should be passed to <code>swiftc</code>. These strings are
506-
subject to <code>$(location ...)</code> expansion.</p></td>
506+
subject to <code>$(location ...)</code> and <code><a href="https://docs.bazel.build/versions/master/be/make-variables.html">"Make" variable</a></code> expansion.</p></td>
507507
</tr>
508508
<tr id="swift_library.defines">
509509
<td><code>defines</code></td>
@@ -533,6 +533,7 @@ or if the target has the <code>swift.no_generated_header</code> feature enabled.
533533
<td>
534534
<p><code>List of strings; optional</code></p><p>Additional linker options that should be passed to the linker for the binary
535535
that depends on this target. These strings are subject to <code>$(location ...)</code>
536+
and <code><a href="https://docs.bazel.build/versions/master/be/make-variables.html">"Make" variable</a></code> expansion.</p></td>
536537
expansion.</p></td>
537538
</tr>
538539
<tr id="swift_library.module_name">
@@ -546,7 +547,7 @@ non-identifier characters with underscores.</p></td>
546547
<tr id="swift_library.private_deps">
547548
<td><code>private_deps</code></td>
548549
<td>
549-
<p><code>List of <a hef="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>A list of targets that are implementation-only dependencies of the target being
550+
<p><code>List of <a href="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>A list of targets that are implementation-only dependencies of the target being
550551
built. Libraries/linker flags from these dependencies will be propagated to
551552
dependent for linking, but artifacts/flags required for compilation (such as
552553
.swiftmodule files, C headers, and search paths) will not be propagated.</p>
@@ -564,7 +565,7 @@ Linux), those dependencies will be <strong>ignored.</strong></p></td>
564565
<tr id="swift_library.swiftc_inputs">
565566
<td><code>swiftc_inputs</code></td>
566567
<td>
567-
<p><code>List of <a hef="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>Additional files that are referenced using <code>$(location ...)</code> in attributes that
568+
<p><code>List of <a href="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>Additional files that are referenced using <code>$(location ...)</code> in attributes that
568569
support location expansion.</p></td>
569570
</tr>
570571
</tbody>
@@ -614,7 +615,7 @@ symbol is defined; it is not repeated by the alias module.)
614615
<tr id="swift_module_alias.deps">
615616
<td><code>deps</code></td>
616617
<td>
617-
<p><code>List of <a hef="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>A list of targets that are dependencies of the target being built, which will be
618+
<p><code>List of <a href="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>A list of targets that are dependencies of the target being built, which will be
618619
linked into that target. Allowed kinds are <code>swift_import</code> and <code>swift_library</code>
619620
(or anything else propagating <code>SwiftInfo</code>).</p></td>
620621
</tr>
@@ -730,7 +731,7 @@ prevents unused modules from being loaded by `swiftc`.
730731
<tr id="swift_proto_library.deps">
731732
<td><code>deps</code></td>
732733
<td>
733-
<p><code>List of <a hef="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>Exactly one <code>proto_library</code> target (or any target that propagates a <code>proto</code>
734+
<p><code>List of <a href="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>Exactly one <code>proto_library</code> target (or any target that propagates a <code>proto</code>
734735
provider) from which the Swift library should be generated.</p></td>
735736
</tr>
736737
</tbody>
@@ -795,7 +796,7 @@ to your BUILD file's `package()` declaration instead of the individual targets.
795796
<tr id="swift_test.deps">
796797
<td><code>deps</code></td>
797798
<td>
798-
<p><code>List of <a hef="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>A list of targets that are dependencies of the target being built, which will be
799+
<p><code>List of <a href="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>A list of targets that are dependencies of the target being built, which will be
799800
linked into that target.</p>
800801
<p>If the Swift toolchain supports implementation-only imports (<code>private_deps</code> on
801802
<code>swift_library</code>), then targets in <code>deps</code> are treated as regular
@@ -815,12 +816,12 @@ Linux), those dependencies will be <strong>ignored.</strong></p></td>
815816
<tr id="swift_test.srcs">
816817
<td><code>srcs</code></td>
817818
<td>
818-
<p><code>List of <a hef="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>A list of <code>.swift</code> source files that will be compiled into the library.</p></td>
819+
<p><code>List of <a href="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>A list of <code>.swift</code> source files that will be compiled into the library.</p></td>
819820
</tr>
820821
<tr id="swift_test.data">
821822
<td><code>data</code></td>
822823
<td>
823-
<p><code>List of <a hef="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>The list of files needed by this target at runtime.</p>
824+
<p><code>List of <a href="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>The list of files needed by this target at runtime.</p>
824825
<p>Files and targets named in the <code>data</code> attribute will appear in the <code>*.runfiles</code>
825826
area of this target, if it has one. This may include data files needed by a
826827
binary or library, or other programs needed by it.</p></td>
@@ -884,7 +885,7 @@ good build result caching.</li>
884885
<tr id="swift_test.swiftc_inputs">
885886
<td><code>swiftc_inputs</code></td>
886887
<td>
887-
<p><code>List of <a hef="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>Additional files that are referenced using <code>$(location ...)</code> in attributes that
888+
<p><code>List of <a href="https://docs.bazel.build/versions/master/build-ref.html#labels">labels</a>; optional</code></p><p>Additional files that are referenced using <code>$(location ...)</code> in attributes that
888889
support location expansion.</p></td>
889890
</tr>
890891
</tbody>

swift/internal/attrs.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ A list of `.swift` source files that will be compiled into the library.
104104
"copts": attr.string_list(
105105
doc = """\
106106
Additional compiler options that should be passed to `swiftc`. These strings are
107-
subject to `$(location ...)` expansion.
107+
subject to `$(location ...)` and "Make" variable expansion.
108108
""",
109109
),
110110
"defines": attr.string_list(
@@ -248,7 +248,7 @@ def swift_library_rule_attrs(
248248
doc = """\
249249
Additional linker options that should be passed to the linker for the binary
250250
that depends on this target. These strings are subject to `$(location ...)`
251-
expansion.
251+
and "Make" variable expansion.
252252
""",
253253
),
254254
"alwayslink": attr.bool(

swift/internal/swift_library.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ load(
3535
"compact",
3636
"create_cc_info",
3737
"expand_locations",
38+
"expand_make_variables",
3839
"get_providers",
3940
)
4041
load("@bazel_skylib//lib:dicts.bzl", "dicts")
@@ -100,7 +101,9 @@ def _swift_library_impl(ctx):
100101
# These can't use additional_inputs since expand_locations needs targets,
101102
# not files.
102103
copts = expand_locations(ctx, ctx.attr.copts, ctx.attr.swiftc_inputs)
104+
copts = expand_make_variables(ctx, copts, "copts")
103105
linkopts = expand_locations(ctx, ctx.attr.linkopts, ctx.attr.swiftc_inputs)
106+
linkopts = expand_make_variables(ctx, linkopts, "linkopts")
104107
srcs = ctx.files.srcs
105108

106109
extra_features = []

swift/internal/utils.bzl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,23 @@ def expand_locations(ctx, values, targets = []):
142142
"""
143143
return [ctx.expand_location(value, targets) for value in values]
144144

145+
def expand_make_variables(ctx, values, attribute_name):
146+
"""Expands all references to Make variables in each of the given values.
147+
148+
Args:
149+
ctx: The rule context.
150+
values: A list of strings, which may contain Make variable placeholders.
151+
attribute_name: The attribute name string that will be presented in
152+
console when an error occurs.
153+
154+
Returns:
155+
A list of strings with Make variables placeholders filled in.
156+
"""
157+
return [
158+
ctx.expand_make_variables(attribute_name, value, {})
159+
for value in values
160+
]
161+
145162
def get_swift_executable_for_toolchain(ctx):
146163
"""Returns the Swift driver executable that the toolchain should use.
147164

0 commit comments

Comments
 (0)