Skip to content

Commit

Permalink
Adjust windows CRT linking for the new /defaultlib linking in core.
Browse files Browse the repository at this point in the history
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.

[email protected]

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 <[email protected]>
Commit-Queue: danakj <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1292464}
NOKEYCHECK=True
GitOrigin-RevId: 714e31ddaca456ecf74d7f6f309447d6095f7ed4
  • Loading branch information
danakj authored and copybara-github committed Apr 25, 2024
1 parent dfdfe58 commit 3572630
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 30 deletions.
53 changes: 27 additions & 26 deletions config/win/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,9 @@ config("release_crt") {
if (is_component_build) {
cflags = [ "/MD" ]

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

if (use_custom_libcxx) {
# On Windows, including libcpmt[d]/msvcprt[d] explicitly links the C++
Expand All @@ -521,6 +522,10 @@ config("release_crt") {
}
} else {
cflags = [ "/MT" ]

# /MT specifies libcmt.lib as the CRT library, which is the static+release
# version. Rust needs to agree, so we tell it to use the static+release CRT
# as well. See https://github.com/rust-lang/rust/issues/39016.
rustflags = [ "-Ctarget-feature=+crt-static" ]

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

# /MDd specifies msvcrtd.lib as the CRT library. Rust needs to agree, so
# we specify it explicitly.
# Once https://github.com/rust-lang/rust/issues/39016 is resolved we should
# instead tell rustc which CRT to use (static/dynamic + release/debug). We
# can't support prebuilt stdlib in this path until then.
rustflags = [ "-Clink-arg=msvcrtd.lib" ]

# Disable libraries rustc links implicitly;
# see https://github.com/rust-lang/rust/pull/122268
rustflags += [
"-Clink-arg=/nodefaultlib:libcmt.lib",
# /MDd specifies msvcrtd.lib as the CRT library. Rust needs to agree, so we
# specify it explicitly. Rust defaults to the dynamic+release library, which
# we remove here, and then replace. See
# https://github.com/rust-lang/rust/issues/39016.
rustflags = [
"-Clink-arg=/nodefaultlib:msvcrt.lib",
"-Clink-arg=msvcrtd.lib",
]

if (use_custom_libcxx) {
Expand All @@ -554,8 +554,9 @@ config("dynamic_crt") {
} else {
cflags = [ "/MD" ]

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

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

# /MTd specifies libcmtd.lib as the CRT library. Rust needs to agree, so
# we specify it explicitly.
# Once https://github.com/rust-lang/rust/issues/39016 is resolved we should
# instead tell rustc which CRT to use (static/dynamic + release/debug). We
# can't support prebuilt stdlib in this path until then.
rustflags = [ "-Clink-arg=libcmtd.lib" ]

# Disable libraries rustc links implicitly;
# see https://github.com/rust-lang/rust/pull/122268
rustflags += [
# /MTd specifies libcmtd.lib as the CRT library. Rust needs to agree, so we
# specify it explicitly. We tell Rust that we're using the static CRT but
# remove the release library that it chooses, and replace with the debug
# library. See https://github.com/rust-lang/rust/issues/39016.
rustflags = [
"-Ctarget-feature=+crt-static",
"-Clink-arg=/nodefaultlib:libcmt.lib",
"-Clink-arg=/nodefaultlib:msvcrt.lib",
"-Clink-arg=libcmtd.lib",
]

if (use_custom_libcxx) {
ldflags = [ "/DEFAULTLIB:libcpmtd.lib" ]
}
} else {
cflags = [ "/MT" ]

# /MT specifies libcmt.lib as the CRT library, which is the static+release
# version. Rust needs to agree, so we tell it to use the static+release CRT
# as well. See https://github.com/rust-lang/rust/issues/39016.
rustflags = [ "-Ctarget-feature=+crt-static" ]

if (use_custom_libcxx) {
Expand Down
4 changes: 1 addition & 3 deletions rust/std/gnrt_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ remove_deps = ['allocator-api2']
[crate.libc]
# This target is #[no_core] when included by std, which is incompatible with
# profiling.
#
# Suppress link directives since we specify the deps in GN configs.
extra_kv = { include_coverage = false, no_link_directives = true }
extra_kv = { include_coverage = false }

[crate.profiler_builtins]
# The build script is used to compile a profiler-rt library, but we get
Expand Down
1 change: 0 additions & 1 deletion rust/std/rules/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1442,7 +1442,6 @@ cargo_crate("libc") {
rustflags = [
"--cfg=backtrace_in_libstd",
"-Zforce-unstable-if-unmarked",
"-Zlink-directives=false",
]
output_dir =
"$root_out_dir/local_rustc_sysroot/lib/rustlib/$rust_abi_target/lib/"
Expand Down

0 comments on commit 3572630

Please sign in to comment.