Skip to content

Commit 67b6938

Browse files
authored
Pull in various Windows improvements (#3940)
closes #3794
1 parent c5958b9 commit 67b6938

7 files changed

Lines changed: 409 additions & 42 deletions

File tree

rust/platform/triple_mappings.bzl

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ _CPU_ARCH_TO_BUILTIN_PLAT_SUFFIX = {
121121
"le32": None,
122122
"mips": None,
123123
"mipsel": None,
124-
"powerpc": "ppc",
125-
"powerpc64": None,
124+
"powerpc": "ppc32",
125+
"powerpc64": "ppc",
126126
"powerpc64le": "ppc64le",
127127
"riscv32": "riscv32",
128128
"riscv32imc": "riscv32",
@@ -154,17 +154,17 @@ _SYSTEM_TO_BUILTIN_SYS_SUFFIX = {
154154
"linux": "linux",
155155
"macos": "osx",
156156
"nacl": None,
157-
"netbsd": None,
157+
"netbsd": "netbsd",
158158
"nixos": "nixos",
159159
"none": "none",
160160
"nto": "qnx",
161161
"openbsd": "openbsd",
162162
"solaris": None,
163163
"uefi": "uefi",
164164
"unknown": None,
165-
"wasi": None,
166-
"wasip1": None,
167-
"wasip2": None,
165+
"wasi": "wasi",
166+
"wasip1": "wasi",
167+
"wasip2": "wasi",
168168
"windows": "windows",
169169
}
170170

@@ -179,9 +179,11 @@ _SYSTEM_TO_BINARY_EXT = {
179179
"ios": "",
180180
"linux": "",
181181
"macos": "",
182+
"netbsd": "",
182183
"nixos": "",
183184
"none": "",
184185
"nto": "",
186+
"threads": ".wasm",
185187
"uefi": ".efi",
186188
# This is currently a hack allowing us to have the proper
187189
# generated extension for the wasm target, similarly to the
@@ -204,9 +206,11 @@ _SYSTEM_TO_STATICLIB_EXT = {
204206
"ios": ".a",
205207
"linux": ".a",
206208
"macos": ".a",
209+
"netbsd": ".a",
207210
"nixos": ".a",
208211
"none": ".a",
209212
"nto": ".a",
213+
"threads": "",
210214
"uefi": ".lib",
211215
"unknown": "",
212216
"wasi": "",
@@ -226,9 +230,11 @@ _SYSTEM_TO_DYLIB_EXT = {
226230
"ios": ".dylib",
227231
"linux": ".so",
228232
"macos": ".dylib",
233+
"netbsd": ".so",
229234
"nixos": ".so",
230235
"none": ".so",
231236
"nto": ".a",
237+
"threads": ".wasm",
232238
"uefi": "", # UEFI doesn't have dynamic linking
233239
"unknown": ".wasm",
234240
"wasi": ".wasm",
@@ -284,7 +290,12 @@ _SYSTEM_TO_STDLIB_LINKFLAGS = {
284290
"wasi": [],
285291
"wasip1": [],
286292
"wasip2": [],
287-
"windows": ["advapi32.lib", "ws2_32.lib", "userenv.lib", "Bcrypt.lib"],
293+
"windows": {
294+
# see https://github.com/rust-lang/rust/blob/c4aa646f15e40bd3e64ddb5017b7b89b3646ac99/src/tools/run-make-support/src/external_deps/c_cxx_compiler/extras.rs#L14-L23
295+
"gnu": ["-lws2_32", "-luserenv", "-lbcrypt", "-lntdll", "-lsynchronization"],
296+
"gnullvm": ["-lws2_32", "-luserenv", "-lbcrypt", "-lntdll", "-lsynchronization"],
297+
"msvc": ["advapi32.lib", "ws2_32.lib", "userenv.lib", "Bcrypt.lib"],
298+
},
288299
}
289300

290301
def cpu_arch_to_constraints(cpu_arch, *, system = None, abi = None):
@@ -410,8 +421,24 @@ def system_to_staticlib_ext(system):
410421
def system_to_binary_ext(system):
411422
return _SYSTEM_TO_BINARY_EXT[system]
412423

413-
def system_to_stdlib_linkflags(system):
414-
return _SYSTEM_TO_STDLIB_LINKFLAGS[system]
424+
def system_to_stdlib_linkflags(system, abi = None):
425+
"""_summary_
426+
427+
Args:
428+
system (_type_): _description_
429+
abi (_type_, optional): _description_. Defaults to None.
430+
431+
Returns:
432+
_type_: _description_
433+
"""
434+
flags = _SYSTEM_TO_STDLIB_LINKFLAGS[system]
435+
if type(flags) == "list":
436+
return flags
437+
if abi:
438+
for prefix, abi_flags in flags.items():
439+
if prefix and abi.startswith(prefix):
440+
return abi_flags
441+
return flags.get(None, [])
415442

416443
def triple_to_constraint_set(target_triple):
417444
"""Returns a set of constraints for a given platform triple

rust/private/repository_utils.bzl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,13 @@ def BUILD_for_rust_toolchain(
417417
str: A rendered template of a `rust_toolchain` declaration
418418
"""
419419
if stdlib_linkflags == None:
420-
stdlib_linkflags = ", ".join(['"%s"' % x for x in system_to_stdlib_linkflags(target_triple.system)])
420+
stdlib_linkflags = ", ".join([
421+
'"%s"' % x
422+
for x in system_to_stdlib_linkflags(
423+
target_triple.system,
424+
target_triple.abi,
425+
)
426+
])
421427

422428
rustfmt_label = None
423429
if include_rustfmt:

rust/private/rustc.bzl

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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

22922313
def _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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
load(":windows_lib_name_test.bzl", "windows_lib_name_test_suite")
2+
3+
windows_lib_name_test_suite(name = "windows_lib_name_test_suite")

0 commit comments

Comments
 (0)