rust-lang / cargo

The Rust package manager
https://doc.rust-lang.org/cargo
Apache License 2.0
12.74k stars 2.42k forks source link

`profile-rustflags` does not properly handle `-Ctarget-feature=+crt-static` #11980

Open Systemcluster opened 1 year ago

Systemcluster commented 1 year ago

Problem

Specifying rustflags = ["-Ctarget-feature=+crt-static"] in Cargo.toml does not properly propagate the setting and results in inconsistencies.

For example, when using mimalloc with this setting, linking results in a conflict between MSVCRT and libucrt:

error: linking with `link.exe` failed: exit code: 1120
...
= note: LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
        LINK : warning LNK4217: symbol '_errno' defined in 'libucrt.lib(errno.obj)' is imported by 'liblibmimalloc_sys-cdc40389aa3ebb06.rlib(static.o)' in function '_mi_arena_alloc_aligned'
        LINK : warning LNK4217: symbol 'abort' defined in 'libucrt.lib(abort.obj)' is imported by 'liblibmimalloc_sys-cdc40389aa3ebb06.rlib(static.o)' in function 'mi_heap_alloc_new_n'
        LINK : warning LNK4217: symbol '__acrt_iob_func' defined in 'libucrt.lib(_file.obj)' is imported by 'liblibmimalloc_sys-cdc40389aa3ebb06.rlib(static.o)' in function '_mi_fputs'
        LINK : warning LNK4217: symbol '__stdio_common_vsprintf' defined in 'libucrt.lib(output.obj)' is imported by 'liblibmimalloc_sys-cdc40389aa3ebb06.rlib(static.o)' in function 'mi_vfprintf'
        liblibmimalloc_sys-cdc40389aa3ebb06.rlib(static.o) : error LNK2019: unresolved external symbol __imp__wgetenv referenced in function mi_wdupenv_s
        liblibmimalloc_sys-cdc40389aa3ebb06.rlib(static.o) : error LNK2019: unresolved external symbol __imp_strtol referenced in function mi_option_init
        liblibmimalloc_sys-cdc40389aa3ebb06.rlib(static.o) : error LNK2019: unresolved external symbol __imp_getenv referenced in function mi_dupenv_s
        liblibmimalloc_sys-cdc40389aa3ebb06.rlib(static.o) : error LNK2019: unresolved external symbol __imp_fputs referenced in function _mi_prim_out_stderr
        D:\Development\rust-profile-rustflags-test\target\release\deps\profile_rustflags_test.exe : fatal error LNK1120: 4 unresolved externals

Steps

Create an example project with this Cargo.toml:

cargo-features = ["profile-rustflags"]

[package]
name = "profile-rustflags-test"
version = "0.1.0"
edition = "2021"
publish = false

[profile.release]
rustflags = ["-Ctarget-feature=+crt-static"]

[dependencies]
mimalloc = { version = "0.1", default-features = false }

Use mimalloc in the main.rs:

use mimalloc::MiMalloc;
#[global_allocator]
static ALLOCATOR: MiMalloc = MiMalloc;

pub fn main() {
    println!("Hello, world!");
}

Compile with cargo build --release. It will result in the above error during the linking step.

Possible Solution(s)

No response

Notes

Adding rustflags = ["-Ctarget-feature=+crt-static"] to the [build] section in .cargo/config.toml works fine, with or without the profile rustflags.

Version

cargo 1.71.0-nightly (84b7041fd 2023-04-13)
release: 1.71.0-nightly
commit-hash: 84b7041fd2745ee6b3b4a150314f81aabb78e6b2
commit-date: 2023-04-13
host: x86_64-pc-windows-msvc
libgit2: 1.6.3 (sys:0.17.0 vendored)
libcurl: 8.0.1-DEV (sys:0.4.61+curl-8.0.1 vendored ssl:Schannel)
os: Windows 10.0.22621 (Windows 10 Pro) [64-bit]
Systemcluster commented 1 year ago

A smaller crate to reproduce this with is sys-info, which only depends on cc and libc. Reproduction setup: https://github.com/Systemcluster/rust-profile-rustflags-issue

This issue occurs with the nightly-x86_64-pc-windows-msvc toolchain, I have not tried non-msvc toolchains or non-default linkers.