--- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -2305,7 +2305,7 @@ return crate.metadata.dirname return crate.output.dirname -def _portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, for_windows = False, for_darwin = False, flavor_msvc = False): +def _portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, for_windows = False, for_darwin = False, flavor_msvc = False, use_direct_driver = False): artifact = get_preferred_artifact(lib, use_pic) if ambiguous_libs and artifact.path in ambiguous_libs: artifact = ambiguous_libs[artifact.path] @@ -2344,6 +2344,11 @@ artifact.basename.startswith("test-") or artifact.basename.startswith("std-") ): return [] if for_darwin else ["-lstatic=%s" % get_lib_name(artifact)] + + if for_windows and use_direct_driver and not artifact.basename.endswith(".lib"): + return [ + "-Clink-arg={}".format(artifact.path), + ] if flavor_msvc: return [ @@ -2381,7 +2386,7 @@ ]) elif include_link_flags: get_lib_name = get_lib_name_for_windows if flavor_msvc else get_lib_name_default - ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, flavor_msvc = flavor_msvc)) + ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, flavor_msvc = flavor_msvc, use_direct_driver = use_direct_driver)) # Windows toolchains can inherit POSIX defaults like -pthread from C deps, # which fails to link with the MinGW/LLD toolchain. Drop them here. @@ -2558,17 +2563,25 @@ else: # For all other crate types we want to link C++ runtime library statically # (for example libstdc++.a or libc++.a). + runtime_libs = cc_toolchain.static_runtime_lib(feature_configuration = feature_configuration) args.add_all( - cc_toolchain.static_runtime_lib(feature_configuration = feature_configuration), + runtime_libs, map_each = _get_dirname, format_each = "-Lnative=%s", ) if include_link_flags: - args.add_all( - cc_toolchain.static_runtime_lib(feature_configuration = feature_configuration), - map_each = get_lib_name, - format_each = "-lstatic=%s", - ) + if toolchain.target_os == "windows" and use_direct_link_driver: + for runtime_lib in runtime_libs.to_list(): + if runtime_lib.basename.endswith(".lib"): + args.add(get_lib_name(runtime_lib), format = "-lstatic=%s") + else: + args.add(runtime_lib.path, format = "--codegen=link-arg=%s") + else: + args.add_all( + runtime_libs, + map_each = get_lib_name, + format_each = "-lstatic=%s", + ) def _get_dirname(file): """A helper function for `_add_native_link_flags`.