Skip to content

Commit

Permalink
Migrate to the modern Starlark linker input API.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminp committed Oct 27, 2020
1 parent 320d0a3 commit 9dbcdbf
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 76 deletions.
37 changes: 27 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,13 +151,20 @@ 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(
Expand Down Expand Up @@ -231,7 +244,11 @@ 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(
user_link_flags = dep_link_flags,
),
])
),
)

Expand Down
10 changes: 6 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,15 @@ 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,
)

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 +322,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 +348,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
10 changes: 6 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,7 @@ 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,
)

# It's bad practice to attach providers you don't own to other targets,
Expand Down Expand Up @@ -477,9 +479,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 +518,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
49 changes: 16 additions & 33 deletions swift/internal/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,20 @@ def collect_cc_libraries(
"""
libraries = []

# TODO(https://github.com/bazelbuild/bazel/issues/8118): Remove once flag is
# flipped.
libraries_to_link = cc_info.linking_context.libraries_to_link
if hasattr(libraries_to_link, "to_list"):
libraries_to_link = libraries_to_link.to_list()

for library in libraries_to_link:
if include_pic_static:
if library.pic_static_library:
libraries.append(library.pic_static_library)
elif library.static_library:
for li in cc_info.linking_context.linker_inputs.to_list():
for library in li.libraries:
if include_pic_static:
if library.pic_static_library:
libraries.append(library.pic_static_library)
elif library.static_library:
libraries.append(library.static_library)
elif include_static and library.static_library:
libraries.append(library.static_library)
elif include_static and library.static_library:
libraries.append(library.static_library)

if include_dynamic and library.dynamic_library:
libraries.append(library.dynamic_library)
if include_interface and library.interface_library:
libraries.append(library.interface_library)
if include_dynamic and library.dynamic_library:
libraries.append(library.dynamic_library)
if include_interface and library.interface_library:
libraries.append(library.interface_library)

return libraries

Expand All @@ -74,19 +69,15 @@ def compact(sequence):
return [item for item in sequence if item != None]

def create_cc_info(
additional_inputs = [],
cc_infos = [],
compilation_outputs = None,
defines = [],
includes = [],
libraries_to_link = [],
private_cc_infos = [],
user_link_flags = []):
linker_inputs = [],
private_cc_infos = []):
"""Creates a `CcInfo` provider from Swift compilation info and deps.
Args:
additional_inputs: A list of additional files that should be passed as
inputs to the final link action.
cc_infos: A list of `CcInfo` providers from public dependencies, whose
compilation and linking contexts should both be merged into the new
provider.
Expand All @@ -96,32 +87,24 @@ def create_cc_info(
context.
includes: The list of include paths to insert into the compilation
context.
libraries_to_link: A list of `LibraryToLink` objects that represent the
linker_inputs: A list of `LinkerInput` objects that represent the
libraries that should be linked into the final binary.
private_cc_infos: A list of `CcInfo` providers from private
(implementation-only) dependencies, whose linking contexts should be
merged into the new provider but whose compilation contexts should
be excluded.
user_link_flags: A list of flags that should be passed to the final link
action.
Returns:
A new `CcInfo`.
"""
all_additional_inputs = list(additional_inputs)
all_user_link_flags = list(user_link_flags)
all_headers = []
if compilation_outputs:
all_additional_inputs.extend(compilation_outputs.linker_inputs)
all_user_link_flags.extend(compilation_outputs.linker_flags)
all_headers = compact([compilation_outputs.generated_header])

local_cc_infos = [
CcInfo(
linking_context = cc_common.create_linking_context(
additional_inputs = all_additional_inputs,
libraries_to_link = libraries_to_link,
user_link_flags = all_user_link_flags,
linker_inputs = depset(direct = linker_inputs),
),
compilation_context = cc_common.create_compilation_context(
defines = depset(defines),
Expand Down
Loading

0 comments on commit 9dbcdbf

Please sign in to comment.