Skip to content

Commit 3572630

Browse files
danakjcopybara-github
authored andcommitted
Adjust windows CRT linking for the new /defaultlib linking in core.
This more strictly follows the recipe in rust-lang/rust#122268 (comment) And we remove the -Zlink-directives=false for building the libc crate, as that crate no longer provides the CRT link directives. The link directives are now in core, and are done through /defaultlib so that we can remove it in the command line with /nodefaultlib. This allows us to control linking entirely through our GN linking rules, and not during stdlib compilation, giving us support for prebuilt Rust stdlib as well. R=thakis@chromium.org Bug: 5476668 Change-Id: I1466c4f721a9d8cc9ac3465c2e15f866b731a738 Cq-Include-Trybots: luci.chromium.try:android-rust-arm32-rel,android-rust-arm64-rel,android-rust-arm64-dbg,linux-rust-x64-dbg,linux-rust-x64-rel,mac-rust-x64-dbg,win-rust-x64-dbg,win-rust-x64-rel Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5477889 Reviewed-by: Nico Weber <thakis@chromium.org> Commit-Queue: danakj <danakj@chromium.org> Cr-Commit-Position: refs/heads/main@{#1292464} NOKEYCHECK=True GitOrigin-RevId: 714e31ddaca456ecf74d7f6f309447d6095f7ed4
1 parent dfdfe58 commit 3572630

File tree

3 files changed

+28
-30
lines changed

3 files changed

+28
-30
lines changed

config/win/BUILD.gn

+27-26
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,9 @@ config("release_crt") {
511511
if (is_component_build) {
512512
cflags = [ "/MD" ]
513513

514-
# The - in front of crt-static turns the feature off.
515-
rustflags = [ "-Ctarget-feature=-crt-static" ]
514+
# /MD specifies msvcrt.lib as the CRT library, which is the dynamic+release
515+
# version. Rust needs to agree, and its default mode is dynamic+release, so
516+
# we do nothing here. See https://github.com/rust-lang/rust/issues/39016.
516517

517518
if (use_custom_libcxx) {
518519
# On Windows, including libcpmt[d]/msvcprt[d] explicitly links the C++
@@ -521,6 +522,10 @@ config("release_crt") {
521522
}
522523
} else {
523524
cflags = [ "/MT" ]
525+
526+
# /MT specifies libcmt.lib as the CRT library, which is the static+release
527+
# version. Rust needs to agree, so we tell it to use the static+release CRT
528+
# as well. See https://github.com/rust-lang/rust/issues/39016.
524529
rustflags = [ "-Ctarget-feature=+crt-static" ]
525530

526531
if (use_custom_libcxx) {
@@ -534,18 +539,13 @@ config("dynamic_crt") {
534539
# This pulls in the DLL debug CRT and defines _DEBUG
535540
cflags = [ "/MDd" ]
536541

537-
# /MDd specifies msvcrtd.lib as the CRT library. Rust needs to agree, so
538-
# we specify it explicitly.
539-
# Once https://github.com/rust-lang/rust/issues/39016 is resolved we should
540-
# instead tell rustc which CRT to use (static/dynamic + release/debug). We
541-
# can't support prebuilt stdlib in this path until then.
542-
rustflags = [ "-Clink-arg=msvcrtd.lib" ]
543-
544-
# Disable libraries rustc links implicitly;
545-
# see https://github.com/rust-lang/rust/pull/122268
546-
rustflags += [
547-
"-Clink-arg=/nodefaultlib:libcmt.lib",
542+
# /MDd specifies msvcrtd.lib as the CRT library. Rust needs to agree, so we
543+
# specify it explicitly. Rust defaults to the dynamic+release library, which
544+
# we remove here, and then replace. See
545+
# https://github.com/rust-lang/rust/issues/39016.
546+
rustflags = [
548547
"-Clink-arg=/nodefaultlib:msvcrt.lib",
548+
"-Clink-arg=msvcrtd.lib",
549549
]
550550

551551
if (use_custom_libcxx) {
@@ -554,8 +554,9 @@ config("dynamic_crt") {
554554
} else {
555555
cflags = [ "/MD" ]
556556

557-
# The - in front of crt-static turns the feature off.
558-
rustflags = [ "-Ctarget-feature=-crt-static" ]
557+
# /MD specifies msvcrt.lib as the CRT library, which is the dynamic+release
558+
# version. Rust needs to agree, and its default mode is dynamic+release, so
559+
# we do nothing here. See https://github.com/rust-lang/rust/issues/39016.
559560

560561
if (use_custom_libcxx) {
561562
ldflags = [ "/DEFAULTLIB:msvcprt.lib" ]
@@ -568,25 +569,25 @@ config("static_crt") {
568569
# This pulls in the static debug CRT and defines _DEBUG
569570
cflags = [ "/MTd" ]
570571

571-
# /MTd specifies libcmtd.lib as the CRT library. Rust needs to agree, so
572-
# we specify it explicitly.
573-
# Once https://github.com/rust-lang/rust/issues/39016 is resolved we should
574-
# instead tell rustc which CRT to use (static/dynamic + release/debug). We
575-
# can't support prebuilt stdlib in this path until then.
576-
rustflags = [ "-Clink-arg=libcmtd.lib" ]
577-
578-
# Disable libraries rustc links implicitly;
579-
# see https://github.com/rust-lang/rust/pull/122268
580-
rustflags += [
572+
# /MTd specifies libcmtd.lib as the CRT library. Rust needs to agree, so we
573+
# specify it explicitly. We tell Rust that we're using the static CRT but
574+
# remove the release library that it chooses, and replace with the debug
575+
# library. See https://github.com/rust-lang/rust/issues/39016.
576+
rustflags = [
577+
"-Ctarget-feature=+crt-static",
581578
"-Clink-arg=/nodefaultlib:libcmt.lib",
582-
"-Clink-arg=/nodefaultlib:msvcrt.lib",
579+
"-Clink-arg=libcmtd.lib",
583580
]
584581

585582
if (use_custom_libcxx) {
586583
ldflags = [ "/DEFAULTLIB:libcpmtd.lib" ]
587584
}
588585
} else {
589586
cflags = [ "/MT" ]
587+
588+
# /MT specifies libcmt.lib as the CRT library, which is the static+release
589+
# version. Rust needs to agree, so we tell it to use the static+release CRT
590+
# as well. See https://github.com/rust-lang/rust/issues/39016.
590591
rustflags = [ "-Ctarget-feature=+crt-static" ]
591592

592593
if (use_custom_libcxx) {

rust/std/gnrt_config.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ remove_deps = ['allocator-api2']
6464
[crate.libc]
6565
# This target is #[no_core] when included by std, which is incompatible with
6666
# profiling.
67-
#
68-
# Suppress link directives since we specify the deps in GN configs.
69-
extra_kv = { include_coverage = false, no_link_directives = true }
67+
extra_kv = { include_coverage = false }
7068

7169
[crate.profiler_builtins]
7270
# The build script is used to compile a profiler-rt library, but we get

rust/std/rules/BUILD.gn

-1
Original file line numberDiff line numberDiff line change
@@ -1442,7 +1442,6 @@ cargo_crate("libc") {
14421442
rustflags = [
14431443
"--cfg=backtrace_in_libstd",
14441444
"-Zforce-unstable-if-unmarked",
1445-
"-Zlink-directives=false",
14461445
]
14471446
output_dir =
14481447
"$root_out_dir/local_rustc_sysroot/lib/rustlib/$rust_abi_target/lib/"

0 commit comments

Comments
 (0)