rust-lang / cargo

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

rustflags in cargo config.toml are wrongly quoted when calling rustc #11970

Closed daniestevez closed 1 year ago

daniestevez commented 1 year ago

Problem

I have noticed that setting a rustc codegen option such as -C target-feature=+avx2 in .cargo/config.toml wont' work. It appears that the flag is passed in quotes to rustc, which complains about this and terminates.

Steps

To reproduce:

  1. cargo new mycrate && cd mycrate
  2. Create .cargo/config.toml with the following contents:
    [target.x86_64-unknown-linux-gnu]
    rustflags = ["-C target-feature=+avx2"]
  3. Run cargo build. This will give the following:
    
    $ cargo build
    error: failed to run `rustc` to learn about target-specific information

Caused by: process didn't exit successfully: rustc - --crate-name ___ --print=file-names '-C target-feature=+avx2' --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=split-debuginfo --print=crate-name --print=cfg (exit status: 1) --- stderr error: unknown codegen option: target-feature


### Possible Solution(s)

_No response_

### Notes

It seems that the problem are the quotes (`'`) surrounding `-C target-feature=+avx2`. In fact, if I run the same without the quotes:
```rustc - --crate-name ___ --print=file-names -C target-feature=+avx2 --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=split-debuginfo --print=crate-name --print=cfg```
this stops waiting for input instead of failing, and on Ctrl+D prints the following and exits:

lib.rlib lib.so lib.so lib.a lib___.so /home/daniel/.rustup/toolchains/stable-x86_64-unknown-linux-gnu off packed unpacked


debug_assertions panic="unwind" proc_macro target_arch="x86_64" target_endian="little" target_env="gnu" target_family="unix" target_feature="avx" target_feature="avx2" target_feature="fxsr" target_feature="sse" target_feature="sse2" target_feature="sse3" target_feature="sse4.1" target_feature="ssse3" target_has_atomic="16" target_has_atomic="32" target_has_atomic="64" target_has_atomic="8" target_has_atomic="ptr" target_os="linux" target_pointer_width="64" target_vendor="unknown" unix


Flags passed in via the `RUSTFLAGS` env variable work, and they seem to be not surrounded in quotes:

$ RUSTFLAGS="-C target-feature=+avx2" cargo build --verbose Dirty mycrate v0.1.0 (/tmp/mycrate): the rustflags changed Compiling mycrate v0.1.0 (/tmp/mycrate) Running rustc --crate-name mycrate --edition=2021 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=158 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=b81e7d9b4e90224b -C extra-filename=-b81e7d9b4e90224b --out-dir /tmp/mycrate/target/debug/deps -C incremental=/tmp/mycrate/target/debug/incremental -L dependency=/tmp/mycrate/target/debug/deps -C target-feature=+avx2 Finished dev [unoptimized + debuginfo] target(s) in 0.11s


### Version

```text
cargo 1.68.2 (6feb7c9cf 2023-03-26)
release: 1.68.2
commit-hash: 6feb7c9cfc0c5604732dba75e4c3b2dbea38e8d8
commit-date: 2023-03-26
host: x86_64-unknown-linux-gnu
libgit2: 1.5.0 (sys:0.16.0 vendored)
libcurl: 7.86.0-DEV (sys:0.4.59+curl-7.86.0 vendored ssl:OpenSSL/1.1.1q)
os: Arch Linux [64-bit]
weihanglo commented 1 year ago

The config value may be an array of string or a space-separated string. IIRC both

should work. Could you give them an attempt?

daniestevez commented 1 year ago

Thanks! Both of those work correctly. Closing as solved.