@@ -541,7 +541,7 @@ def get_linker_and_args(ctx, crate_type, toolchain, cc_toolchain, feature_config
541541
542542 return ld , ld_is_direct_driver , link_args , link_env
543543
544- def _symlink_for_ambiguous_lib (actions , toolchain , crate_info , lib ):
544+ def symlink_for_ambiguous_lib (actions , toolchain , crate_info , lib ):
545545 """Constructs a disambiguating symlink for a library dependency.
546546
547547 Args:
@@ -560,9 +560,9 @@ def _symlink_for_ambiguous_lib(actions, toolchain, crate_info, lib):
560560
561561 # Take the absolute value of hash() since it could be negative.
562562 path_hash = abs (hash (lib .path ))
563- lib_name = get_lib_name_for_windows (lib ) if toolchain .target_os . startswith ( "windows" ) else get_lib_name_default (lib )
563+ lib_name = get_lib_name_for_windows (lib ) if toolchain .target_abi == "msvc" else get_lib_name_default (lib )
564564
565- if toolchain .target_os . startswith ( "windows" ) :
565+ if toolchain .target_abi == "msvc" :
566566 prefix = ""
567567 extension = ".lib"
568568 elif lib_name .endswith (".pic" ):
@@ -657,8 +657,8 @@ def _disambiguate_libs(actions, toolchain, crate_info, dep_info, use_pic):
657657 if name in visited_libs :
658658 old_path = visited_libs [name ].path
659659 if old_path not in ambiguous_libs :
660- ambiguous_libs [old_path ] = _symlink_for_ambiguous_lib (actions , toolchain , crate_info , visited_libs [name ])
661- ambiguous_libs [artifact .path ] = _symlink_for_ambiguous_lib (actions , toolchain , crate_info , artifact )
660+ ambiguous_libs [old_path ] = symlink_for_ambiguous_lib (actions , toolchain , crate_info , visited_libs [name ])
661+ ambiguous_libs [artifact .path ] = symlink_for_ambiguous_lib (actions , toolchain , crate_info , artifact )
662662
663663 visited_libs [name ] = artifact
664664 return ambiguous_libs
@@ -1500,7 +1500,7 @@ def rustc_compile_action(
15001500 pdb_file = None
15011501 dsym_folder = None
15021502 if crate_info .type in ("cdylib" , "bin" ) and not experimental_use_cc_common_link :
1503- if toolchain .target_os == "windows " and compilation_mode .strip_level == "none" :
1503+ if toolchain .target_abi == "msvc " and compilation_mode .strip_level == "none" :
15041504 pdb_file = ctx .actions .declare_file (crate_info .output .basename [:- len (crate_info .output .extension )] + "pdb" , sibling = crate_info .output )
15051505 action_outputs .append (pdb_file )
15061506 elif toolchain .target_os in ["macos" , "darwin" ]:
@@ -1643,6 +1643,7 @@ def rustc_compile_action(
16431643 compilation_outputs = compilation_outputs ,
16441644 name = output_relative_to_package ,
16451645 stamp = ctx .attr .stamp ,
1646+ main_output = crate_info .output ,
16461647 output_type = "executable" if crate_info .type == "bin" else "dynamic_library" ,
16471648 additional_outputs = additional_linker_outputs ,
16481649 )
@@ -2204,7 +2205,26 @@ def _get_crate_dirname(crate):
22042205 """
22052206 return crate .output .dirname
22062207
2207- def _portable_link_flags (lib , use_pic , ambiguous_libs , get_lib_name , for_windows = False , for_darwin = False , flavor_msvc = False ):
2208+ def portable_link_flags (
2209+ lib ,
2210+ use_pic ,
2211+ ambiguous_libs ,
2212+ get_lib_name ,
2213+ for_darwin = False ,
2214+ flavor_msvc = False ):
2215+ """_summary_
2216+
2217+ Args:
2218+ lib (_type_): _description_
2219+ use_pic (_type_): _description_
2220+ ambiguous_libs (_type_): _description_
2221+ get_lib_name (_type_): _description_
2222+ for_darwin (bool, optional): _description_. Defaults to False.
2223+ flavor_msvc (bool, optional): _description_. Defaults to False.
2224+
2225+ Returns:
2226+ _type_: _description_
2227+ """
22082228 artifact = get_preferred_artifact (lib , use_pic )
22092229 if ambiguous_libs and artifact .path in ambiguous_libs :
22102230 artifact = ambiguous_libs [artifact .path ]
@@ -2244,17 +2264,11 @@ def _portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, for_windows
22442264 ):
22452265 return [] if for_darwin else ["-lstatic=%s" % get_lib_name (artifact )]
22462266
2247- if for_windows :
2248- if flavor_msvc :
2249- return [
2250- "-lstatic=%s" % get_lib_name (artifact ),
2251- "-Clink-arg={}" .format (artifact .basename ),
2252- ]
2253- else :
2254- return [
2255- "-lstatic=%s" % get_lib_name (artifact ),
2256- "-Clink-arg=-l{}" .format (artifact .basename ),
2257- ]
2267+ if flavor_msvc :
2268+ return [
2269+ "-lstatic=%s" % get_lib_name (artifact ),
2270+ "-Clink-arg={}" .format (artifact .basename ),
2271+ ]
22582272 else :
22592273 return [
22602274 "-lstatic=%s" % get_lib_name (artifact ),
@@ -2285,8 +2299,15 @@ def _make_link_flags_windows(make_link_flags_args, flavor_msvc, use_direct_drive
22852299 ("-Clink-arg=%s--no-whole-archive" % prefix ),
22862300 ])
22872301 elif include_link_flags :
2288- ret .extend (_portable_link_flags (lib , use_pic , ambiguous_libs , get_lib_name_for_windows , for_windows = True , flavor_msvc = flavor_msvc ))
2289- _add_user_link_flags (ret , linker_input )
2302+ get_lib_name = get_lib_name_for_windows if flavor_msvc else get_lib_name_default
2303+ ret .extend (portable_link_flags (lib , use_pic , ambiguous_libs , get_lib_name , flavor_msvc = flavor_msvc ))
2304+
2305+ # Windows toolchains can inherit POSIX defaults like -pthread from C deps,
2306+ # which fails to link with the MinGW/LLD toolchain. Drop them here.
2307+ for flag in linker_input .user_link_flags :
2308+ if flag in ("-pthread" , "-lpthread" ):
2309+ continue
2310+ ret .append ("--codegen=link-arg={}" .format (flag ))
22902311 return ret
22912312
22922313def _make_link_flags_windows_msvc (make_link_flags_args , use_direct_driver ):
@@ -2306,7 +2327,7 @@ def _make_link_flags_darwin(make_link_flags_args, use_direct_driver):
23062327 ("-Clink-arg=%s%s" % (prefix , get_preferred_artifact (lib , use_pic ).path )),
23072328 ])
23082329 elif include_link_flags :
2309- ret .extend (_portable_link_flags (lib , use_pic , ambiguous_libs , get_lib_name_default , for_darwin = True ))
2330+ ret .extend (portable_link_flags (lib , use_pic , ambiguous_libs , get_lib_name_default , for_darwin = True ))
23102331 _add_user_link_flags (ret , linker_input )
23112332 return ret
23122333
@@ -2322,7 +2343,7 @@ def _make_link_flags_default(make_link_flags_args, use_direct_driver):
23222343 ("-Clink-arg=%s--no-whole-archive" % prefix ),
23232344 ])
23242345 elif include_link_flags :
2325- ret .extend (_portable_link_flags (lib , use_pic , ambiguous_libs , get_lib_name_default ))
2346+ ret .extend (portable_link_flags (lib , use_pic , ambiguous_libs , get_lib_name_default ))
23262347 _add_user_link_flags (ret , linker_input )
23272348 return ret
23282349
@@ -2365,19 +2386,18 @@ def _get_make_link_flag_funcs(target_os, target_abi, use_direct_link_driver):
23652386 - callable: The function for producing link args.
23662387 - callable: The function for formatting link library names.
23672388 """
2389+ get_lib_name = get_lib_name_default
2390+
23682391 if target_os == "windows" :
2369- make_link_flags_windows_msvc = _make_link_flags_windows_msvc_direct if use_direct_link_driver else _make_link_flags_windows_msvc_indirect
2370- make_link_flags_windows_gnu = _make_link_flags_windows_gnu_direct if use_direct_link_driver else _make_link_flags_windows_gnu_indirect
2371- make_link_flags = make_link_flags_windows_msvc if target_abi == "msvc" else make_link_flags_windows_gnu
2372- get_lib_name = get_lib_name_for_windows
2392+ if target_abi == "msvc" :
2393+ make_link_flags = _make_link_flags_windows_msvc_direct if use_direct_link_driver else _make_link_flags_windows_msvc_indirect
2394+ get_lib_name = get_lib_name_for_windows
2395+ else :
2396+ make_link_flags = _make_link_flags_windows_gnu_direct if use_direct_link_driver else _make_link_flags_windows_gnu_indirect
23732397 elif target_os .startswith (("mac" , "darwin" , "ios" )):
2374- make_link_flags_darwin = _make_link_flags_darwin_direct if use_direct_link_driver else _make_link_flags_darwin_indirect
2375- make_link_flags = make_link_flags_darwin
2376- get_lib_name = get_lib_name_default
2398+ make_link_flags = _make_link_flags_darwin_direct if use_direct_link_driver else _make_link_flags_darwin_indirect
23772399 else :
2378- make_link_flags_default = _make_link_flags_default_direct if use_direct_link_driver else _make_link_flags_default_indirect
2379- make_link_flags = make_link_flags_default
2380- get_lib_name = get_lib_name_default
2400+ make_link_flags = _make_link_flags_default_direct if use_direct_link_driver else _make_link_flags_default_indirect
23812401
23822402 return (make_link_flags , get_lib_name )
23832403
0 commit comments