Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
Migrate Swift build rules to the new C++ LinkerInput APIs.
Browse files Browse the repository at this point in the history
Based partially on bazelbuild#512 by @benjaminp.

PiperOrigin-RevId: 341831304
  • Loading branch information
allevato authored and swiple-rules-gardener committed Nov 11, 2020
1 parent e4ad1cc commit bf9560d
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 66 deletions.
45 changes: 39 additions & 6 deletions swift/internal/linking.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,21 @@ def _register_static_library_link_action(
progress_message = "Linking {}".format(output.short_path),
)

def register_libraries_to_link(
def create_linker_input(
*,
actions,
alwayslink,
cc_feature_configuration,
compilation_outputs,
is_dynamic,
is_static,
library_name,
objects,
swift_toolchain):
"""Declares the requested libraries and registers actions to link them.
owner,
swift_toolchain,
additional_inputs = [],
user_link_flags = []):
"""Creates a linker input for a library to link and additional inputs/flags.
Args:
actions: The object used to register actions.
Expand All @@ -104,17 +109,26 @@ def register_libraries_to_link(
argument is ignored if `is_static` is False.
cc_feature_configuration: The C++ feature configuration to use when
constructing the action.
compilation_outputs: The compilation outputs from a Swift compile
action, as returned by `swift_common.compile`, or None.
is_dynamic: If True, declare and link a dynamic library.
is_static: If True, declare and link a static library.
library_name: The basename (without extension) of the libraries to
declare.
objects: A list of `File`s denoting object (`.o`) files that will be
linked.
owner: The `Label` of the target that owns this linker input.
swift_toolchain: The Swift toolchain provider to use when constructing
the action.
additional_inputs: A list of extra `File` inputs passed to the linking
action.
user_link_flags: A list of extra flags to pass to the linking command.
Returns:
A `LibraryToLink` object containing the libraries that were created.
A tuple containing two elements:
1. A `LinkerInput` object containing the library that was created.
2. The single `LibraryToLink` object that is inside the linker input.
"""
dynamic_library = None
if is_dynamic:
Expand All @@ -137,14 +151,26 @@ def register_libraries_to_link(
else:
static_library = None

return cc_common.create_library_to_link(
library_to_link = 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,
)
linker_input = cc_common.create_linker_input(
owner = owner,
libraries = depset([library_to_link]),
additional_inputs = depset(
compilation_outputs.linker_inputs + additional_inputs,
),
user_link_flags = depset(
compilation_outputs.linker_flags + user_link_flags,
),
)

return linker_input, library_to_link

def register_link_binary_action(
actions,
Expand All @@ -156,6 +182,7 @@ def register_link_binary_action(
name,
objects,
output_type,
owner,
stamp,
swift_toolchain,
user_link_flags):
Expand All @@ -178,6 +205,7 @@ def register_link_binary_action(
objects: A list of object (.o) files that will be passed to the linker.
output_type: A string indicating the output type; "executable" or
"dynamic_library".
owner: The `Label` of the target that owns this linker input.
stamp: A tri-state value (-1, 0, or 1) that specifies whether link
stamping is enabled. See `cc_common.link` for details about the
behavior of this argument.
Expand Down Expand Up @@ -223,7 +251,12 @@ def register_link_binary_action(

linking_contexts.append(
cc_common.create_linking_context(
user_link_flags = dep_link_flags,
linker_inputs = depset([
cc_common.create_linker_input(
owner = owner,
user_link_flags = depset(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 @@ -246,6 +246,7 @@ def _swift_linking_rule_impl(
name = ctx.label.name,
objects = objects_to_link,
output_type = "executable",
owner = ctx.label,
stamp = ctx.attr.stamp,
swift_toolchain = swift_toolchain,
user_link_flags = user_link_flags,
Expand Down
8 changes: 5 additions & 3 deletions swift/internal/swift_grpc_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ load(
"SWIFT_FEATURE_GENERATE_FROM_RAW_PROTO_FILES",
"SWIFT_FEATURE_NO_GENERATED_HEADER",
)
load(":linking.bzl", "register_libraries_to_link")
load(":linking.bzl", "create_linker_input")
load(
":proto_gen_utils.bzl",
"declare_generated_files",
Expand Down Expand Up @@ -292,16 +292,18 @@ def _swift_grpc_library_impl(ctx):
target_name = ctx.label.name,
)

library_to_link = register_libraries_to_link(
linker_input, library_to_link = create_linker_input(
actions = ctx.actions,
alwayslink = False,
cc_feature_configuration = swift_common.cc_feature_configuration(
feature_configuration = feature_configuration,
),
compilation_outputs = compilation_outputs,
is_dynamic = False,
is_static = True,
library_name = ctx.label.name,
objects = compilation_outputs.object_files,
owner = ctx.label,
swift_toolchain = swift_toolchain,
)

Expand All @@ -319,7 +321,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 Down
23 changes: 13 additions & 10 deletions swift/internal/swift_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +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_input = cc_common.create_linker_input(
owner = ctx.label,
libraries = depset([
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 = [
DefaultInfo(
Expand All @@ -59,7 +62,7 @@ def _swift_import_impl(ctx):
),
create_cc_info(
cc_infos = get_providers(deps, CcInfo),
libraries_to_link = libraries_to_link,
linker_inputs = [linker_input],
),
# Propagate an `Objc` provider so that Apple-specific rules like
# apple_binary` will link the imported library properly. Typically we'd
Expand Down
12 changes: 7 additions & 5 deletions swift/internal/swift_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ load(
"SWIFT_FEATURE_ENABLE_LIBRARY_EVOLUTION",
"SWIFT_FEATURE_SUPPORTS_PRIVATE_DEPS",
)
load(":linking.bzl", "register_libraries_to_link")
load(":linking.bzl", "create_linker_input")
load(":providers.bzl", "SwiftInfo", "SwiftToolchainInfo")
load(":swift_common.bzl", "swift_common")
load(
Expand Down Expand Up @@ -179,17 +179,21 @@ def _swift_library_impl(ctx):
else:
clang_module = None

library_to_link = register_libraries_to_link(
linker_input, library_to_link = create_linker_input(
actions = ctx.actions,
additional_inputs = additional_inputs,
alwayslink = ctx.attr.alwayslink,
cc_feature_configuration = swift_common.cc_feature_configuration(
feature_configuration = feature_configuration,
),
compilation_outputs = compilation_outputs,
is_dynamic = False,
is_static = True,
library_name = ctx.label.name,
objects = compilation_outputs.object_files,
owner = ctx.label,
swift_toolchain = swift_toolchain,
user_link_flags = linkopts,
)

direct_output_files = compact([
Expand All @@ -213,14 +217,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
8 changes: 5 additions & 3 deletions swift/internal/swift_module_alias.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ load(
"output_groups_from_compilation_outputs",
)
load(":derived_files.bzl", "derived_files")
load(":linking.bzl", "register_libraries_to_link")
load(":linking.bzl", "create_linker_input")
load(":providers.bzl", "SwiftInfo", "SwiftToolchainInfo")
load(":swift_common.bzl", "swift_common")
load(":utils.bzl", "compact", "create_cc_info", "get_providers")
Expand Down Expand Up @@ -72,16 +72,18 @@ def _swift_module_alias_impl(ctx):
target_name = ctx.label.name,
)

library_to_link = register_libraries_to_link(
linker_input, library_to_link = create_linker_input(
actions = ctx.actions,
alwayslink = False,
cc_feature_configuration = swift_common.cc_feature_configuration(
feature_configuration = feature_configuration,
),
compilation_outputs = compilation_outputs,
is_dynamic = False,
is_static = True,
library_name = ctx.label.name,
objects = compilation_outputs.object_files,
owner = ctx.label,
swift_toolchain = swift_toolchain,
)

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 Down
8 changes: 5 additions & 3 deletions swift/internal/swift_protoc_gen_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ load(
"SWIFT_FEATURE_GENERATE_FROM_RAW_PROTO_FILES",
"SWIFT_FEATURE_NO_GENERATED_HEADER",
)
load(":linking.bzl", "register_libraries_to_link")
load(":linking.bzl", "create_linker_input")
load(
":proto_gen_utils.bzl",
"declare_generated_files",
Expand Down Expand Up @@ -427,19 +427,21 @@ def _swift_protoc_gen_aspect_impl(target, aspect_ctx):
target_name = target.label.name,
)

library_to_link = register_libraries_to_link(
linker_input, library_to_link = create_linker_input(
actions = aspect_ctx.actions,
alwayslink = False,
cc_feature_configuration = swift_common.cc_feature_configuration(
feature_configuration = feature_configuration,
),
compilation_outputs = compilation_outputs,
is_dynamic = False,
is_static = True,
# Prevent conflicts with C++ protos in the same output directory,
# which use the `lib{name}.a` pattern. This will produce
# `lib{name}.swift.a` instead.
library_name = "{}.swift".format(target.label.name),
objects = compilation_outputs.object_files,
owner = target.label,
swift_toolchain = swift_toolchain,
)

Expand Down Expand Up @@ -515,7 +517,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 bf9560d

Please sign in to comment.