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,