From cdc329e816017cdaa4709a87bc9013fb1c17e026 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thi=20Do=C3=A3n?= Date: Thu, 13 Aug 2020 15:19:19 +0900 Subject: [PATCH] Use a parameter file when linking Swift libraries when the host machine is macOS. This is to prevent the linking failures if a `swift_library` target has too many files that can cause its linking args to become longer than the system's command length limits. This does not change the behavior when building on Linux yet, since `ar` does not support passing arguments using a parameter file. --- swift/internal/linking.bzl | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/swift/internal/linking.bzl b/swift/internal/linking.bzl index 6d4336478..fa5b08e65 100644 --- a/swift/internal/linking.bzl +++ b/swift/internal/linking.bzl @@ -22,6 +22,18 @@ load( ) load(":derived_files.bzl", "derived_files") +def _is_apple_platform_toolchain(swift_toolchain): + """Returns `True` if the given Swift toolchain is for Apple platforms. + + Args: + swift_toolchain: A `SwiftToolchainInfo` provider representing a Swift + compiler toolchain. + + Returns: + `True` if the given Swift toolchain is for Apple platforms. + """ + return swift_toolchain.system_name == "darwin" + def _register_static_library_link_action( actions, cc_feature_configuration, @@ -58,7 +70,17 @@ def _register_static_library_link_action( ) args = actions.args() args.add_all(command_line) - args.add_all(objects) + filelist_args = actions.args() + + # Use a parameter file when building on macOS. `ar` on Linux does not + # support this yet, so we're passing parameters to the command line as is. + if _is_apple_platform_toolchain(swift_toolchain): + args.add("-filelist") + filelist_args.set_param_file_format("multiline") + filelist_args.use_param_file("%s", use_always = True) + filelist_args.add_all(objects) + else: + args.add_all(objects) env = cc_common.get_environment_variables( action_name = CPP_LINK_STATIC_LIBRARY_ACTION_NAME, @@ -73,7 +95,7 @@ def _register_static_library_link_action( execution_requirements = {req: "1" for req in execution_requirements_list} actions.run( - arguments = [args], + arguments = [args, filelist_args], env = env, executable = archiver_path, execution_requirements = execution_requirements,