Skip to content

Commit aff1754

Browse files
authored
Migrate to the modern Starlark linker input API. (#512)
See bazelbuild/bazel#10860.
1 parent 2279303 commit aff1754

11 files changed

+100
-76
lines changed

swift/internal/linking.bzl

+30-10
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,21 @@ def _register_static_library_link_action(
9595
)
9696

9797
def register_libraries_to_link(
98+
owning_label,
9899
actions,
99100
alwayslink,
100101
cc_feature_configuration,
101102
is_dynamic,
102103
is_static,
103104
library_name,
104105
objects,
105-
swift_toolchain):
106+
swift_toolchain,
107+
user_link_flags,
108+
additional_inputs):
106109
"""Declares the requested libraries and registers actions to link them.
107110
108111
Args:
112+
owning_label: Label executing rule (i.e., ctx.label).
109113
actions: The object used to register actions.
110114
alwayslink: If True, create a static library that should be
111115
always-linked (having a `.lo` extension instead of `.a`). This
@@ -120,9 +124,11 @@ def register_libraries_to_link(
120124
linked.
121125
swift_toolchain: The Swift toolchain provider to use when constructing
122126
the action.
127+
user_link_flags: Extra link flags to be passed with the library.
128+
additional_inputs: Extra inputs for a link action involving the library.
123129
124130
Returns:
125-
A `LibraryToLink` object containing the libraries that were created.
131+
A `LinkerInput` object containing the libraries that were created.
126132
"""
127133
dynamic_library = None
128134
if is_dynamic:
@@ -145,16 +151,24 @@ def register_libraries_to_link(
145151
else:
146152
static_library = None
147153

148-
return cc_common.create_library_to_link(
149-
actions = actions,
150-
alwayslink = alwayslink,
151-
cc_toolchain = swift_toolchain.cc_toolchain_info,
152-
feature_configuration = cc_feature_configuration,
153-
pic_static_library = static_library,
154-
dynamic_library = dynamic_library,
154+
return cc_common.create_linker_input(
155+
owner = owning_label,
156+
libraries = depset(direct = [
157+
cc_common.create_library_to_link(
158+
actions = actions,
159+
alwayslink = alwayslink,
160+
cc_toolchain = swift_toolchain.cc_toolchain_info,
161+
feature_configuration = cc_feature_configuration,
162+
pic_static_library = static_library,
163+
dynamic_library = dynamic_library,
164+
),
165+
]),
166+
additional_inputs = depset(direct = additional_inputs),
167+
user_link_flags = depset(direct = user_link_flags),
155168
)
156169

157170
def register_link_binary_action(
171+
owning_label,
158172
actions,
159173
additional_inputs,
160174
additional_linking_contexts,
@@ -170,6 +184,7 @@ def register_link_binary_action(
170184
"""Registers an action that invokes the linker to produce a binary.
171185
172186
Args:
187+
owning_label: Label of the rule creating the link action.
173188
actions: The object used to register actions.
174189
additional_inputs: A list of additional inputs to the link action,
175190
such as those used in `$(location ...)` substitution, linker
@@ -231,7 +246,12 @@ def register_link_binary_action(
231246

232247
linking_contexts.append(
233248
cc_common.create_linking_context(
234-
user_link_flags = dep_link_flags,
249+
linker_inputs = depset(direct = [
250+
cc_common.create_linker_input(
251+
owner = owning_label,
252+
user_link_flags = depset(direct = dep_link_flags),
253+
),
254+
])
235255
),
236256
)
237257

swift/internal/swift_binary_test.bzl

+1
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ def _swift_linking_rule_impl(
237237
user_link_flags.extend(ctx.fragments.cpp.linkopts)
238238

239239
linking_outputs = register_link_binary_action(
240+
owning_label = ctx.label,
240241
actions = ctx.actions,
241242
additional_inputs = additional_inputs_to_linker,
242243
additional_linking_contexts = additional_linking_contexts,

swift/internal/swift_grpc_library.bzl

+7-4
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ def _swift_grpc_library_impl(ctx):
293293
target_name = ctx.label.name,
294294
)
295295

296-
library_to_link = register_libraries_to_link(
296+
linker_input = register_libraries_to_link(
297+
owning_label = ctx.label,
297298
actions = ctx.actions,
298299
alwayslink = False,
299300
cc_feature_configuration = swift_common.cc_feature_configuration(
@@ -304,14 +305,16 @@ def _swift_grpc_library_impl(ctx):
304305
library_name = ctx.label.name,
305306
objects = compilation_outputs.object_files,
306307
swift_toolchain = swift_toolchain,
308+
additional_inputs = compilation_outputs.linker_inputs,
309+
user_link_flags = compilation_outputs.linker_flags,
307310
)
308311

309312
providers = [
310313
DefaultInfo(
311314
files = depset(direct = generated_files + compact([
312315
compilation_outputs.swiftdoc,
313316
compilation_outputs.swiftmodule,
314-
library_to_link.pic_static_library,
317+
linker_input.libraries[0].pic_static_library,
315318
])),
316319
),
317320
OutputGroupInfo(**output_groups_from_compilation_outputs(
@@ -320,7 +323,7 @@ def _swift_grpc_library_impl(ctx):
320323
create_cc_info(
321324
cc_infos = get_providers(compile_deps, CcInfo),
322325
compilation_outputs = compilation_outputs,
323-
libraries_to_link = [library_to_link],
326+
linker_inputs = [linker_input],
324327
),
325328
deps[0][SwiftProtoInfo],
326329
swift_common.create_swift_info(
@@ -346,7 +349,7 @@ def _swift_grpc_library_impl(ctx):
346349
link_inputs = compilation_outputs.linker_inputs,
347350
linkopts = compilation_outputs.linker_flags,
348351
module_map = compilation_outputs.generated_module_map,
349-
static_archives = compact([library_to_link.pic_static_library]),
352+
static_archives = compact([linker_input.libraries[0].pic_static_library]),
350353
swiftmodules = [compilation_outputs.swiftmodule],
351354
objc_header = compilation_outputs.generated_header,
352355
))

swift/internal/swift_import.bzl

+13-9
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,18 @@ def _swift_import_impl(ctx):
3838
unsupported_features = ctx.disabled_features,
3939
)
4040

41-
libraries_to_link = [
42-
cc_common.create_library_to_link(
43-
actions = ctx.actions,
44-
cc_toolchain = cc_toolchain,
45-
feature_configuration = cc_feature_configuration,
46-
static_library = archive,
47-
)
48-
for archive in archives
41+
linker_inputs = [
42+
cc_common.create_linker_input(
43+
libraries = depset(direct = [
44+
cc_common.create_library_to_link(
45+
actions = ctx.actions,
46+
cc_toolchain = cc_toolchain,
47+
feature_configuration = cc_feature_configuration,
48+
static_library = archive,
49+
)
50+
for archive in archives
51+
]),
52+
),
4953
]
5054

5155
providers = [
@@ -59,7 +63,7 @@ def _swift_import_impl(ctx):
5963
),
6064
create_cc_info(
6165
cc_infos = get_providers(deps, CcInfo),
62-
libraries_to_link = libraries_to_link,
66+
linker_inputs = linker_inputs,
6367
),
6468
# Propagate an `Objc` provider so that Apple-specific rules like
6569
# apple_binary` will link the imported library properly. Typically we'd

swift/internal/swift_library.bzl

+7-6
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ def _swift_library_impl(ctx):
179179
else:
180180
clang_module = None
181181

182-
library_to_link = register_libraries_to_link(
182+
linker_input = register_libraries_to_link(
183+
owning_label = ctx.label,
183184
actions = ctx.actions,
184185
alwayslink = ctx.attr.alwayslink,
185186
cc_feature_configuration = swift_common.cc_feature_configuration(
@@ -190,14 +191,16 @@ def _swift_library_impl(ctx):
190191
library_name = ctx.label.name,
191192
objects = compilation_outputs.object_files,
192193
swift_toolchain = swift_toolchain,
194+
user_link_flags = linkopts,
195+
additional_inputs = compilation_outputs.linker_inputs,
193196
)
194197

195198
direct_output_files = compact([
196199
compilation_outputs.generated_header,
197200
compilation_outputs.swiftdoc,
198201
compilation_outputs.swiftinterface,
199202
compilation_outputs.swiftmodule,
200-
library_to_link.pic_static_library,
203+
linker_input.libraries[0].pic_static_library,
201204
])
202205

203206
providers = [
@@ -213,14 +216,12 @@ def _swift_library_impl(ctx):
213216
compilation_outputs = compilation_outputs,
214217
)),
215218
create_cc_info(
216-
additional_inputs = additional_inputs,
217219
cc_infos = get_providers(deps, CcInfo),
218220
compilation_outputs = compilation_outputs,
219221
defines = ctx.attr.defines,
220222
includes = [ctx.bin_dir.path],
221-
libraries_to_link = [library_to_link],
223+
linker_inputs = [linker_input],
222224
private_cc_infos = get_providers(private_deps, CcInfo),
223-
user_link_flags = linkopts,
224225
),
225226
coverage_common.instrumented_files_info(
226227
ctx,
@@ -261,7 +262,7 @@ def _swift_library_impl(ctx):
261262
link_inputs = compilation_outputs.linker_inputs + additional_inputs,
262263
linkopts = compilation_outputs.linker_flags + linkopts,
263264
module_map = compilation_outputs.generated_module_map,
264-
static_archives = compact([library_to_link.pic_static_library]),
265+
static_archives = compact([linker_input.libraries[0].pic_static_library]),
265266
swiftmodules = [compilation_outputs.swiftmodule],
266267
objc_header = compilation_outputs.generated_header,
267268
))

swift/internal/swift_module_alias.bzl

+7-5
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ def _swift_module_alias_impl(ctx):
7272
target_name = ctx.label.name,
7373
)
7474

75-
library_to_link = register_libraries_to_link(
75+
linker_input = register_libraries_to_link(
76+
owning_label = ctx.label,
7677
actions = ctx.actions,
7778
alwayslink = False,
7879
cc_feature_configuration = swift_common.cc_feature_configuration(
@@ -83,15 +84,16 @@ def _swift_module_alias_impl(ctx):
8384
library_name = ctx.label.name,
8485
objects = compilation_outputs.object_files,
8586
swift_toolchain = swift_toolchain,
87+
additional_inputs = compilation_outputs.linker_inputs,
8688
)
8789

8890
providers = [
8991
DefaultInfo(
9092
files = depset(compact([
9193
compilation_outputs.swiftdoc,
9294
compilation_outputs.swiftmodule,
93-
library_to_link.dynamic_library,
94-
library_to_link.pic_static_library,
95+
linker_input.libraries[0].dynamic_library,
96+
linker_input.libraries[0].pic_static_library,
9597
])),
9698
),
9799
OutputGroupInfo(**output_groups_from_compilation_outputs(
@@ -105,7 +107,7 @@ def _swift_module_alias_impl(ctx):
105107
cc_infos = get_providers(deps, CcInfo),
106108
compilation_outputs = compilation_outputs,
107109
includes = [ctx.bin_dir.path],
108-
libraries_to_link = [library_to_link],
110+
linker_inputs = [linker_input],
109111
),
110112
swift_common.create_swift_info(
111113
modules = [
@@ -130,7 +132,7 @@ def _swift_module_alias_impl(ctx):
130132
link_inputs = compilation_outputs.linker_inputs,
131133
linkopts = compilation_outputs.linker_flags,
132134
module_map = compilation_outputs.generated_module_map,
133-
static_archives = compact([library_to_link.pic_static_library]),
135+
static_archives = compact([linker_input.libraries[0].pic_static_library]),
134136
swiftmodules = [compilation_outputs.swiftmodule],
135137
objc_header = compilation_outputs.generated_header,
136138
))

swift/internal/swift_protoc_gen_aspect.bzl

+7-4
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,8 @@ def _swift_protoc_gen_aspect_impl(target, aspect_ctx):
428428
target_name = target.label.name,
429429
)
430430

431-
library_to_link = register_libraries_to_link(
431+
linker_input = register_libraries_to_link(
432+
owning_label = aspect_ctx.label,
432433
actions = aspect_ctx.actions,
433434
alwayslink = False,
434435
cc_feature_configuration = swift_common.cc_feature_configuration(
@@ -442,6 +443,8 @@ def _swift_protoc_gen_aspect_impl(target, aspect_ctx):
442443
library_name = "{}.swift".format(target.label.name),
443444
objects = compilation_outputs.object_files,
444445
swift_toolchain = swift_toolchain,
446+
additional_inputs = compilation_outputs.linker_inputs,
447+
user_link_flags = compilation_outputs.linker_flags,
445448
)
446449

447450
# It's bad practice to attach providers you don't own to other targets,
@@ -477,9 +480,9 @@ def _swift_protoc_gen_aspect_impl(target, aspect_ctx):
477480
objc_info_args["header"] = depset([
478481
compilation_outputs.generated_header,
479482
])
480-
if library_to_link.pic_static_library:
483+
if linker_input.libraries[0].pic_static_library:
481484
objc_info_args["library"] = depset(
482-
[library_to_link.pic_static_library],
485+
[linker_input.libraries[0].pic_static_library],
483486
order = "topological",
484487
)
485488
if compilation_outputs.linker_flags:
@@ -516,7 +519,7 @@ def _swift_protoc_gen_aspect_impl(target, aspect_ctx):
516519
cc_infos = cc_infos,
517520
compilation_outputs = compilation_outputs,
518521
includes = includes,
519-
libraries_to_link = [library_to_link],
522+
linker_inputs = [linker_input],
520523
),
521524
objc_info = objc_info,
522525
),

0 commit comments

Comments
 (0)