Skip to content

[Bugfix] Pass -object_path_lto option to linker when LTO flags are enabled #1552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion swift/internal/linking.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ load(
":feature_names.bzl",
"SWIFT_FEATURE_LLD_GC_WORKAROUND",
"SWIFT_FEATURE_OBJC_LINK_FLAGS",
"SWIFT_FEATURE_FULL_LTO",
"SWIFT_FEATURE_THIN_LTO",
)
load(
":features.bzl",
Expand Down Expand Up @@ -202,7 +204,8 @@ def create_linking_context_from_compilation_outputs(
name = None,
swift_toolchain,
toolchain_type = SWIFT_TOOLCHAIN_TYPE,
user_link_flags = []):
user_link_flags = [],
bazel_bin_path = None):
"""Creates a linking context from the outputs of a Swift compilation.

On some platforms, this function will spawn additional post-compile actions
Expand Down Expand Up @@ -322,6 +325,30 @@ def create_linking_context_from_compilation_outputs(
),
)

full_lto_enabled = is_feature_enabled(
feature_configuration = feature_configuration,
feature_name = SWIFT_FEATURE_FULL_LTO,
)

thin_lto_enabled = is_feature_enabled(
feature_configuration = feature_configuration,
feature_name = SWIFT_FEATURE_THIN_LTO,
)

# When LTO is enabled, we need to pass `-object_path_lto` to the linker, where it can write one big object file after LTO
# or else build will produce invalid dSYM.
if (full_lto_enabled or thin_lto_enabled) and bazel_bin_path != None:
extra_linking_contexts.append(
cc_common.create_linking_context(
linker_inputs = depset([
cc_common.create_linker_input(
owner = label,
user_link_flags = depset(["-Wl,-object_path_lto,{}/{}_lto.o".format(bazel_bin_path, name)]),
),
]),
),
)

if include_dev_srch_paths != None and is_test != None:
fail("""\
Both `include_dev_srch_paths` and `is_test` cannot be specified. Please select \
Expand Down
3 changes: 1 addition & 2 deletions swift/swift_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ def _swift_library_impl(ctx):
swift_toolchain = swift_toolchain,
unsupported_features = ctx.disabled_features,
)

swift_infos = get_providers(deps, SwiftInfo)
private_swift_infos = get_providers(private_deps, SwiftInfo)

Expand Down Expand Up @@ -199,7 +198,6 @@ def _swift_library_impl(ctx):
module_context = compile_result.module_context
compilation_outputs = compile_result.compilation_outputs
supplemental_outputs = compile_result.supplemental_outputs

linking_context, linking_output = (
create_linking_context_from_compilation_outputs(
actions = ctx.actions,
Expand All @@ -221,6 +219,7 @@ def _swift_library_impl(ctx):
module_context = module_context,
swift_toolchain = swift_toolchain,
user_link_flags = linkopts,
bazel_bin_path = ctx.bin_dir.path,
)
)

Expand Down