Cargo --config KEY=VALUE is not effective

We have the hello world sample and added dependency rand like

cargo add rand 

then Cargo.toml looks like

$cat Cargo.toml

[package]
name = "hello"
version = "0.1.0"
edition = "2021"

[dependencies]
rand = "0.8.5"

and we are trying to pass the linker option for rustc through the cargo like

$cargo --config 'build.rustflags = ["-L /<path>/rust-1.70.0.0/x86_64-unknown-linux-gnu/bin/../lib/rustlib/aarch64-wrs-vxworks/lib/ -L <path>/rust-1.70.0.0/x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/"]' build  --verbose

rand has the dependency like libc and cargo try to build and fails like

Some errors have detailed explanations: E0408, E0412, E0425, E0463, E0531.
For more information about an error, try `rustc --explain E0408`.
error: could not compile `libc` (build script) due to 50 previous errors

rustc --crate-name build_script_build CARGO_HOME/.cargo/registry/src/index.crates.io-6f17d22bba15001f/libc-0.2.151/build.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=138 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=97b99bae06b5ff0f -C extra-filename=-97b99bae06b5ff0f --out-dir /<path>/rust/hello/target/debug/build/libc-97b99bae06b5ff0f -L dependency=/<path>/rust/hello/target/debug/deps --cap-lints allow

note that from 1.70.0 rustc or cargo needs to pass the "-L/rustlib/" target libs path manually and before pervious version are working fine without ,hence we tried to pass through cargo like --config or RUSTFLAGS ,which is not effective and cargo is not passing to rustc like mentioned above ...

any suggestions here to avoid these errors or how we make cargo search the target libs by default without manually intervention from command-line /RUSTFLAGS/ etc ?

like

export RUSTFLAGS="-L <path>/rust-1.70.0.0/x86_64-unknown-linux-gnu/bin/../lib/rustlib/aarch64-wrs-vxworks/lib/ -L <path>rust-1.70.0.0/x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/"

   Compiling libc v0.2.151
   Compiling cfg-if v1.0.0
   Compiling ppv-lite86 v0.2.17
     
  Running `rustc --crate-name cfg_if --edition=2018 <path>/rust/../../usr/rust/.cargo/registry/src/index.crates.io-6f17d22bba15001f/cfg-if-1.0.0/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=138 --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 -C metadata=1f5677bb9cb2f9d9 -C extra-filename=-1f5677bb9cb2f9d9 --out-dir <path>/rust/hello/target/x86_64-unknown-linux-gnu/debug/deps --target x86_64-unknown-linux-gnu -L dependency=<path>/rust/hello/target/x86_64-unknown-linux-gnu/debug/deps -L dependency=/<path>/rust/hello/target/debug/deps --cap-lints allow -L <path>/rust-1.70.0.0/x86_64-unknown-linux-gnu/bin/../lib/rustlib/aarch64-wrs-vxworks/lib/ -L<path>/rust-1.70.0.0/x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/`
  Running `rustc --crate-name ppv_lite86 --edition=2018 <path>/rust/../../usr/rust/.cargo/registry/src/index.crates.io-6f17d22bba15001f/ppv-lite86-0.2.17/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=138 --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 --cfg 'feature="simd"' --cfg 'feature="std"' -C metadata=3911f71223ba48c3 -C extra-filename=-3911f71223ba48c3 --out-dir <path>/hello/target/x86_64-unknown-linux-gnu/debug/deps --target x86_64-unknown-linux-gnu -L dependency=<path>/rust/hello/target/x86_64-unknown-linux-gnu/debug/deps -L dependency=/<path>/rust/hello/target/debug/deps --cap-lints allow -L /<path>/rust-1.70.0.0/x86_64-unknown-linux-gnu/bin/../lib/rustlib/aarch64-wrs-vxworks/lib/ -L /<path>/rust-1.70.0.0/x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/`
  Running `rustc --crate-name build_script_build <path>/rust/../../usr/rust/.cargo/registry/src/index.crates.io-6f17d22bba15001f/libc-0.2.151/build.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=138 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=97b99bae06b5ff0f -C extra-filename=-97b99bae06b5ff0f --out-dir /<path>/rust/hello/target/debug/build/libc-97b99bae06b5ff0f -L dependency=/<path>/rust/hello/target/debug/deps --cap-lints allow`
     

no luck also with

cargo rustc -- "-L /<path>/rust-1.70.0.0/x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/ "

from above cargo verbose ,you see that the library search was appended to CFG-IF and PPV-lite86 compilation ,but "-L " was not passed to LIBC compilation.

any inputs from experts will helps us to debug the issue further ,thanks in advance

Why are you trying to pass this rustc argument in the first place? Rustc will find the standard library on it's own and cargo will pass it for all dependencies you explicitly declare. You should never need to add an rlib dependency behind cargo's back.

You are missing the part with the actual error. Can you post that?

@bjorn3 ,thank you for coming back and yes rustc version like 1.64 was finding the std lib on its own ,but not with rustc 1.70.0 and issue we seen like

cargo build  --target aarch64-wrs-vxworks --verbose
   Compiling libc v0.2.151
   Compiling cfg-if v1.0.0
   Compiling ppv-lite86 v0.2.17
     Running `rustc --crate-name cfg_if --edition=2018 /folk/prj-rust-dev/umesh/vxworks/vsb/usr/rust/../../usr/rust/.cargo/registry/src/index.crates.io-6f17d22bba15001f/cfg-if-1.0.0/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=138 --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 -C metadata=94c41ef99b5090bf -C extra-filename=-94c41ef99b5090bf --out-dir /folk/prj-rust-dev/umesh/vxworks/vsb/usr/rust/hello/target/aarch64-wrs-vxworks/debug/deps --target aarch64-wrs-vxworks -L dependency=/folk/prj-rust-dev/umesh/vxworks/vsb/usr/rust/hello/target/aarch64-wrs-vxworks/debug/deps -L dependency=/folk/prj-rust-dev/umesh/vxworks/vsb/usr/rust/hello/target/debug/deps --cap-lints allow -L /folk/prj-rust-dev/umesh/vxworks/vx7-2309-features/compilers/rust-1.70.0.0/x86_64-unknown-linux-gnu/bin/../lib/rustlib/aarch64-wrs-vxworks/lib/`
     Running `rustc --crate-name ppv_lite86 --edition=2018 /folk/prj-rust-dev/umesh/vxworks/vsb/usr/rust/../../usr/rust/.cargo/registry/src/index.crates.io-6f17d22bba15001f/ppv-lite86-0.2.17/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=138 --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 --cfg 'feature="simd"' --cfg 'feature="std"' -C metadata=9c465ec463cac700 -C extra-filename=-9c465ec463cac700 --out-dir /folk/prj-rust-dev/umesh/vxworks/vsb/usr/rust/hello/target/aarch64-wrs-vxworks/debug/deps --target aarch64-wrs-vxworks -L dependency=/folk/prj-rust-dev/umesh/vxworks/vsb/usr/rust/hello/target/aarch64-wrs-vxworks/debug/deps -L dependency=/folk/prj-rust-dev/umesh/vxworks/vsb/usr/rust/hello/target/debug/deps --cap-lints allow -L /folk/prj-rust-dev/umesh/vxworks/vx7-2309-features/compilers/rust-1.70.0.0/x86_64-unknown-linux-gnu/bin/../lib/rustlib/aarch64-wrs-vxworks/lib/`

Running `rustc --crate-name build_script_build /folk/prj-rust-dev/umesh/vxworks/vsb/usr/rust/../../usr/rust/.cargo/registry/src/index.crates.io-6f17d22bba15001f/libc-0.2.151/build.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=138 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=97b99bae06b5ff0f -C extra-filename=-97b99bae06b5ff0f --out-dir /folk/prj-rust-dev/umesh/vxworks/vsb/usr/rust/hello/target/debug/build/libc-97b99bae06b5ff0f -L dependency=/folk/prj-rust-dev/umesh/vxworks/vsb/usr/rust/hello/target/debug/deps --cap-lints allow`
error[E0463]: can't find crate for `std`

error: cannot find macro `panic` in this scope
   --> /folk/prj-rust-dev/umesh/vxworks/vsb/usr/rust/../../usr/rust/.cargo/registry/src/index.crates.io-6f17d22bba15001f/libc-0.2.151/build.rs:192:25
    |
192 |                 None => panic!("Failed to get rustc version"),
    |                         ^^^^^
...
197 |     let rustc = otry!(env::var_os("RUSTC"));
    |                 --------------------------- in this macro invocation
    |
    = note: this error originates in the macro `otry` (in Nightly builds, run with -Z macro-backtrace for more info)

error: cannot find macro `panic` in this scope
   --> /folk/prj-rust-dev/umesh/vxworks/vsb/usr/rust/../../usr/rust/.cargo/registry/src/index.crates.io-6f17d22bba15001f/libc-0.2.151/build.rs:192:25
    |
192 |                 None => panic!("Failed to get rustc version"),
    |                         ^^^^^
...
210 |     let version = otry!(str::from_utf8(&output.stdout).ok());

What does rustc --print sysroot show?

The rustflags probably don't get applied to build scripts. This is normal behavior when cross-compiling.

@bjorn3 ,

What does rustc --print sysroot show?

/folk/prj-rust-dev/umesh/vxworks/vx7-2309-features

and it was expected like

/folk/prj-rust-dev/umesh/vxworks/vx7-2309-features/compilers/rust-1.70.0.0/x86_64-unknown-linux-gnu

does we can over-ride sysroot from cargo ,that has effect on build.rs file too ?

The rustflags probably don't get applied to build scripts.

ok ,but manual says other way like ever rustc or compiler invocation from cargo these flags are passed .

and by looking at code like cargo/src/cargo/core/compiler/build_context/target_info.rs at 69aea5b6f69add7c51cca939a79644080c0b0ba0 · rust-lang/cargo · GitHub ,you are right the RUSTFLAGS are not passed to build scripts and plugins .

You can pass --sysroot /path/to/sysroot to rustc to set the sysroot, but that doesn't help if you can't pass flags to rustc for build scripts. Something which I know of the top of my head that should work is to write a shell script which wraps rustc and passes --sysroot and then set the RUSTC env var to the path to this shell script. RUSTC is not ignored for build scripts when cross-compiling unlike RUSTFLAGS.

Did you set any flags in config.toml when compiling the toolchain? A while ago there was a change to how the sysroot is found. I don't remember if that change was already in 1.64 though, but if not it could be that this change breaks on a flag you set in config.toml.

Not exaclty not remembering making any changes to config.toml ,please let me know if you come across the sysroot changes like stated ,till then will using the wrapper script for rustc .

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.