Skip to content

Commit

Permalink
Migrate to the modern Starlark linker input API. (#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminp committed Nov 6, 2020
1 parent 2279303 commit aff1754
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 76 deletions.
40 changes: 30 additions & 10 deletions swift/internal/linking.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,21 @@ def _register_static_library_link_action(
)

def register_libraries_to_link(
owning_label,
actions,
alwayslink,
cc_feature_configuration,
is_dynamic,
is_static,
library_name,
objects,
swift_toolchain):
swift_toolchain,
user_link_flags,
additional_inputs):
"""Declares the requested libraries and registers actions to link them.
Args:
owning_label: Label executing rule (i.e., ctx.label).
actions: The object used to register actions.
alwayslink: If True, create a static library that should be
always-linked (having a `.lo` extension instead of `.a`). This
Expand All @@ -120,9 +124,11 @@ def register_libraries_to_link(
linked.
swift_toolchain: The Swift toolchain provider to use when constructing
the action.
user_link_flags: Extra link flags to be passed with the library.
additional_inputs: Extra inputs for a link action involving the library.
Returns:
A `LibraryToLink` object containing the libraries that were created.
A `LinkerInput` object containing the libraries that were created.
"""
dynamic_library = None
if is_dynamic:
Expand All @@ -145,16 +151,24 @@ def register_libraries_to_link(
else:
static_library = None

return cc_common.create_library_to_link(
actions = actions,
alwayslink = alwayslink,
cc_toolchain = swift_toolchain.cc_toolchain_info,
feature_configuration = cc_feature_configuration,
pic_static_library = static_library,
dynamic_library = dynamic_library,
return cc_common.create_linker_input(
owner = owning_label,
libraries = depset(direct = [
cc_common.create_library_to_link(
actions = actions,
alwayslink = alwayslink,
cc_toolchain = swift_toolchain.cc_toolchain_info,
feature_configuration = cc_feature_configuration,
pic_static_library = static_library,
dynamic_library = dynamic_library,
),
]),
additional_inputs = depset(direct = additional_inputs),
user_link_flags = depset(direct = user_link_flags),
)

def register_link_binary_action(
owning_label,
actions,
additional_inputs,
additional_linking_contexts,
Expand All @@ -170,6 +184,7 @@ def register_link_binary_action(
"""Registers an action that invokes the linker to produce a binary.
Args:
owning_label: Label of the rule creating the link action.
actions: The object used to register actions.
additional_inputs: A list of additional inputs to the link action,
such as those used in `$(location ...)` substitution, linker
Expand Down Expand Up @@ -231,7 +246,12 @@ def register_link_binary_action(

linking_contexts.append(
cc_common.create_linking_context(
user_link_flags = dep_link_flags,
linker_inputs = depset(direct = [
cc_common.create_linker_input(
owner = owning_label,
user_link_flags = depset(direct = dep_link_flags),
),
])
),
)

Expand Down
1 change: 1 addition & 0 deletions swift/internal/swift_binary_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ def _swift_linking_rule_impl(
user_link_flags.extend(ctx.fragments.cpp.linkopts)

linking_outputs = register_link_binary_action(
owning_label = ctx.label,
actions = ctx.actions,
additional_inputs = additional_inputs_to_linker,
additional_linking_contexts = additional_linking_contexts,
Expand Down
11 changes: 7 additions & 4 deletions swift/internal/swift_grpc_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ def _swift_grpc_library_impl(ctx):
target_name = ctx.label.name,
)

library_to_link = register_libraries_to_link(
linker_input = register_libraries_to_link(
owning_label = ctx.label,
actions = ctx.actions,
alwayslink = False,
cc_feature_configuration = swift_common.cc_feature_configuration(
Expand All @@ -304,14 +305,16 @@ def _swift_grpc_library_impl(ctx):
library_name = ctx.label.name,
objects = compilation_outputs.object_files,
swift_toolchain = swift_toolchain,
additional_inputs = compilation_outputs.linker_inputs,
user_link_flags = compilation_outputs.linker_flags,
)

providers = [
DefaultInfo(
files = depset(direct = generated_files + compact([
compilation_outputs.swiftdoc,
compilation_outputs.swiftmodule,
library_to_link.pic_static_library,
linker_input.libraries[0].pic_static_library,
])),
),
OutputGroupInfo(**output_groups_from_compilation_outputs(
Expand All @@ -320,7 +323,7 @@ def _swift_grpc_library_impl(ctx):
create_cc_info(
cc_infos = get_providers(compile_deps, CcInfo),
compilation_outputs = compilation_outputs,
libraries_to_link = [library_to_link],
linker_inputs = [linker_input],
),
deps[0][SwiftProtoInfo],
swift_common.create_swift_info(
Expand All @@ -346,7 +349,7 @@ def _swift_grpc_library_impl(ctx):
link_inputs = compilation_outputs.linker_inputs,
linkopts = compilation_outputs.linker_flags,
module_map = compilation_outputs.generated_module_map,
static_archives = compact([library_to_link.pic_static_library]),
static_archives = compact([linker_input.libraries[0].pic_static_library]),
swiftmodules = [compilation_outputs.swiftmodule],
objc_header = compilation_outputs.generated_header,
))
Expand Down
22 changes: 13 additions & 9 deletions swift/internal/swift_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,18 @@ def _swift_import_impl(ctx):
unsupported_features = ctx.disabled_features,
)

libraries_to_link = [
cc_common.create_library_to_link(
actions = ctx.actions,
cc_toolchain = cc_toolchain,
feature_configuration = cc_feature_configuration,
static_library = archive,
)
for archive in archives
linker_inputs = [
cc_common.create_linker_input(
libraries = depset(direct = [
cc_common.create_library_to_link(
actions = ctx.actions,
cc_toolchain = cc_toolchain,
feature_configuration = cc_feature_configuration,
static_library = archive,
)
for archive in archives
]),
),
]

providers = [
Expand All @@ -59,7 +63,7 @@ def _swift_import_impl(ctx):
),
create_cc_info(
cc_infos = get_providers(deps, CcInfo),
libraries_to_link = libraries_to_link,
linker_inputs = linker_inputs,
),
# Propagate an `Objc` provider so that Apple-specific rules like
# apple_binary` will link the imported library properly. Typically we'd
Expand Down
13 changes: 7 additions & 6 deletions swift/internal/swift_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ def _swift_library_impl(ctx):
else:
clang_module = None

library_to_link = register_libraries_to_link(
linker_input = register_libraries_to_link(
owning_label = ctx.label,
actions = ctx.actions,
alwayslink = ctx.attr.alwayslink,
cc_feature_configuration = swift_common.cc_feature_configuration(
Expand All @@ -190,14 +191,16 @@ def _swift_library_impl(ctx):
library_name = ctx.label.name,
objects = compilation_outputs.object_files,
swift_toolchain = swift_toolchain,
user_link_flags = linkopts,
additional_inputs = compilation_outputs.linker_inputs,
)

direct_output_files = compact([
compilation_outputs.generated_header,
compilation_outputs.swiftdoc,
compilation_outputs.swiftinterface,
compilation_outputs.swiftmodule,
library_to_link.pic_static_library,
linker_input.libraries[0].pic_static_library,
])

providers = [
Expand All @@ -213,14 +216,12 @@ def _swift_library_impl(ctx):
compilation_outputs = compilation_outputs,
)),
create_cc_info(
additional_inputs = additional_inputs,
cc_infos = get_providers(deps, CcInfo),
compilation_outputs = compilation_outputs,
defines = ctx.attr.defines,
includes = [ctx.bin_dir.path],
libraries_to_link = [library_to_link],
linker_inputs = [linker_input],
private_cc_infos = get_providers(private_deps, CcInfo),
user_link_flags = linkopts,
),
coverage_common.instrumented_files_info(
ctx,
Expand Down Expand Up @@ -261,7 +262,7 @@ def _swift_library_impl(ctx):
link_inputs = compilation_outputs.linker_inputs + additional_inputs,
linkopts = compilation_outputs.linker_flags + linkopts,
module_map = compilation_outputs.generated_module_map,
static_archives = compact([library_to_link.pic_static_library]),
static_archives = compact([linker_input.libraries[0].pic_static_library]),
swiftmodules = [compilation_outputs.swiftmodule],
objc_header = compilation_outputs.generated_header,
))
Expand Down
12 changes: 7 additions & 5 deletions swift/internal/swift_module_alias.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ def _swift_module_alias_impl(ctx):
target_name = ctx.label.name,
)

library_to_link = register_libraries_to_link(
linker_input = register_libraries_to_link(
owning_label = ctx.label,
actions = ctx.actions,
alwayslink = False,
cc_feature_configuration = swift_common.cc_feature_configuration(
Expand All @@ -83,15 +84,16 @@ def _swift_module_alias_impl(ctx):
library_name = ctx.label.name,
objects = compilation_outputs.object_files,
swift_toolchain = swift_toolchain,
additional_inputs = compilation_outputs.linker_inputs,
)

providers = [
DefaultInfo(
files = depset(compact([
compilation_outputs.swiftdoc,
compilation_outputs.swiftmodule,
library_to_link.dynamic_library,
library_to_link.pic_static_library,
linker_input.libraries[0].dynamic_library,
linker_input.libraries[0].pic_static_library,
])),
),
OutputGroupInfo(**output_groups_from_compilation_outputs(
Expand All @@ -105,7 +107,7 @@ def _swift_module_alias_impl(ctx):
cc_infos = get_providers(deps, CcInfo),
compilation_outputs = compilation_outputs,
includes = [ctx.bin_dir.path],
libraries_to_link = [library_to_link],
linker_inputs = [linker_input],
),
swift_common.create_swift_info(
modules = [
Expand All @@ -130,7 +132,7 @@ def _swift_module_alias_impl(ctx):
link_inputs = compilation_outputs.linker_inputs,
linkopts = compilation_outputs.linker_flags,
module_map = compilation_outputs.generated_module_map,
static_archives = compact([library_to_link.pic_static_library]),
static_archives = compact([linker_input.libraries[0].pic_static_library]),
swiftmodules = [compilation_outputs.swiftmodule],
objc_header = compilation_outputs.generated_header,
))
Expand Down
11 changes: 7 additions & 4 deletions swift/internal/swift_protoc_gen_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,8 @@ def _swift_protoc_gen_aspect_impl(target, aspect_ctx):
target_name = target.label.name,
)

library_to_link = register_libraries_to_link(
linker_input = register_libraries_to_link(
owning_label = aspect_ctx.label,
actions = aspect_ctx.actions,
alwayslink = False,
cc_feature_configuration = swift_common.cc_feature_configuration(
Expand All @@ -442,6 +443,8 @@ def _swift_protoc_gen_aspect_impl(target, aspect_ctx):
library_name = "{}.swift".format(target.label.name),
objects = compilation_outputs.object_files,
swift_toolchain = swift_toolchain,
additional_inputs = compilation_outputs.linker_inputs,
user_link_flags = compilation_outputs.linker_flags,
)

# It's bad practice to attach providers you don't own to other targets,
Expand Down Expand Up @@ -477,9 +480,9 @@ def _swift_protoc_gen_aspect_impl(target, aspect_ctx):
objc_info_args["header"] = depset([
compilation_outputs.generated_header,
])
if library_to_link.pic_static_library:
if linker_input.libraries[0].pic_static_library:
objc_info_args["library"] = depset(
[library_to_link.pic_static_library],
[linker_input.libraries[0].pic_static_library],
order = "topological",
)
if compilation_outputs.linker_flags:
Expand Down Expand Up @@ -516,7 +519,7 @@ def _swift_protoc_gen_aspect_impl(target, aspect_ctx):
cc_infos = cc_infos,
compilation_outputs = compilation_outputs,
includes = includes,
libraries_to_link = [library_to_link],
linker_inputs = [linker_input],
),
objc_info = objc_info,
),
Expand Down
Loading

0 comments on commit aff1754

Please sign in to comment.