@@ -433,7 +433,7 @@ def get_linker_and_args(ctx, crate_type, toolchain, cc_toolchain, feature_config
433433 link_args = []
434434 link_env = {}
435435
436- if cc_toolchain and toolchain . linker_preference != "rust" :
436+ if cc_toolchain :
437437 if crate_type in ("bin" ) or add_flags_for_binary :
438438 is_linking_dynamic_library = False
439439 action_name = CPP_LINK_EXECUTABLE_ACTION_NAME
@@ -475,10 +475,58 @@ def get_linker_and_args(ctx, crate_type, toolchain, cc_toolchain, feature_config
475475 )
476476 ld_is_direct_driver = False
477477
478- if not ld and toolchain .linker :
478+ if not ld or toolchain .linker_preference == "rust" :
479479 ld = toolchain .linker .path
480480 ld_is_direct_driver = toolchain .linker_type == "direct"
481481
482+ # When using rust-lld directly, we still need library search paths from cc_toolchain
483+ # to find system libraries that rustc's stdlib depends on (like -lgcc_s, -lutil, etc.)
484+ # Filter link_args to only include flags that help locate libraries.
485+ if cc_toolchain and link_args :
486+ filtered_args = []
487+ skip_next = False
488+ for i , arg in enumerate (link_args ):
489+ if skip_next :
490+ skip_next = False
491+ continue
492+
493+ # Strip -Wl, prefix if using direct driver (it's only for compiler drivers)
494+ processed_arg = arg
495+ if ld_is_direct_driver and arg .startswith ("-Wl," ):
496+ # Remove -Wl, prefix and split on commas (e.g., "-Wl,-rpath,/path" -> ["-rpath", "/path"])
497+ # For now, we'll handle common cases; complex -Wl, args might need more sophisticated handling
498+ processed_arg = arg [4 :] # Strip "-Wl,"
499+
500+ # Handle macOS version flag: convert -mmacos-version-min=X.Y to -macos_version_min X.Y
501+ if processed_arg .startswith ("-mmacos-version-min=" ):
502+ version = processed_arg .split ("=" , 1 )[1 ]
503+ filtered_args .append ("-macos_version_min" )
504+ filtered_args .append (version )
505+ # Keep library search path flags
506+
507+ elif processed_arg .startswith ("-L" ):
508+ filtered_args .append (processed_arg )
509+ # Keep sysroot flags (as single or two-part arguments)
510+
511+ elif processed_arg == "--sysroot" or processed_arg .startswith ("--sysroot=" ):
512+ filtered_args .append (processed_arg )
513+ if processed_arg == "--sysroot" and i + 1 < len (link_args ):
514+ # Two-part argument, keep the next arg too
515+ filtered_args .append (link_args [i + 1 ])
516+ skip_next = True
517+
518+ # Keep dynamic linker flags
519+ elif processed_arg .startswith ("--dynamic-linker" ) or processed_arg == "--dynamic-linker" :
520+ filtered_args .append (processed_arg )
521+ if processed_arg == "--dynamic-linker" and i + 1 < len (link_args ):
522+ filtered_args .append (link_args [i + 1 ])
523+ skip_next = True
524+
525+ # Keep rpath-related flags
526+ elif processed_arg .startswith ("-rpath" ) or processed_arg .startswith ("--rpath" ):
527+ filtered_args .append (processed_arg )
528+ link_args = filtered_args
529+
482530 if not ld :
483531 fail ("No linker available for rustc. Either `rust_toolchain.linker` must be set or a `cc_toolchain` configured for the current configuration." )
484532
0 commit comments