I'd like to pass a custom --cfg flag to rustc via Cargo. Normally, I can do this by putting this in .cargo/config for a given crate:
[build]
rustflags = "--cfg procmacro2_semver_exempt"
However, in one of my crates, I am using a custom compilation target, and that seems to break rustflags;
[build]
target = "avr-atmega32u4.json"
rustflags = "--cfg procmacro2_semver_exempt"
[unstable]
build-std = ["core"]
[env]
AVR_CPU_FREQUENCY_HZ = "16_000_000"
In this case, the config flag is not passed to rustc. I've tried moving rustflags into a target-specific section:
[target.avr-atmega32u4.json]
rustflags = "--cfg procmacro2_semver_exempt"
but this doesn't seem to have any effect either.
Related
I have this as my ./cargo/config:
[target.aarch64-unknown-linux-gnu]
linker = "aarch64-none-linux-gnu-gcc"
rustflags = ["-C", "target-feature=+crt-static"]
I have defined RUSTFLAGS in build.sh like so:
export RUSTFLAGS='--cfg chip_type="es"'
When I do:
cargo build --target=aarch64-unknown-linux-gnu
I find that the "-C", "target-feature=+crt-static" is not included. How do I solve this problem?
As can be seen from Cargo Configuration on build.rustflags:
There are three mutually exclusive sources of extra flags. They are checked in order, with the first one being used:
RUSTFLAGS environment variable.
All matching target.<triple>.rustflags and target.<cfg>.rustflags config entries joined together.
build.rustflags config value.
So this new build.sh code solves my problem:
RUSTFLAGS='--cfg chip_type="es" '$RUSTFLAGS
RUSTFLAGS='-C target-feature=+crt-static '$RUSTFLAGS
export RUSTFLAGS
According to the archival notice in wasm-gc,
The wasm-pack (and wasm-bindgen) project will already run [--gc-targets] by default for you, so there's no need to run it again.
How do we specify this in Cargo.toml explictly?
Rustc passes arguments with
-C, --codegen OPT[=VALUE]
Set a codegen option
The codegen option you want link-arg so you'd use it like -C link-arg=--gc-targets you can set that up in your Cargo.toml with,
[[bin]]
rustflags = [
"-C", "link-arg=--gc-targets",
]
Alternatively, you can now set this up directly with the new experimental extra-link-arg option in Cargo.
I'd like to link with -lm. Right now, I'm doing it this way:
let _link_lm = f64::sin(3.0);
I tried putting this in .cargo/config:
[build]
rustflags = ["-C", "link-args=-lm"]
This doesn't dynamically link the library.
Also, using cargo:rustc-link-lib=m in a build script is the same as calling cargo rustc -- -lm which does not work either.
I check that the library is not linked with ldd.
It matters to link the library because this is for a JIT compiler which can call these functions by fetching them using dlsym.
How I can link to this library without calling one of its functions?
It turns out that rustc call the linker with -Wl,--as-needed, so the solution for me was to disable this option:
[build]
rustflags = ["-C", "link-arg=-Wl,--no-as-needed", "-C", "link-arg=-lm"]
The program I'm writing runs much faster when the -C target-cpu=native flag is passed to rustc. I want to give users a simple, platform-independent way to enable this when compiling, so I added a Cargo feature cpu_native = [] in Cargo.toml and created this Cargo config in my project:
[target.'cfg(cpu_native)']
rustflags = ["-C", "target-cpu=native"]
However, this has no effect on my program, and passing --features cpu_native to Cargo does not even trigger a recompile. Changing to the following Cargo config does force re-compilation with faster instructions:
[build]
rustflags = ["-C", "target-cpu=native"]
However, this will compile with target-cpu=native with the default Cargo features, which was not what I wanted. From the Cargo book, what I want seems to be possible, but I don't see what I'm doing wrong.
I don't think this is supported (yet?). I enhanced Cargo to print out what config flags are checked against when resolving:
[
Name("debug_assertions"),
Name("proc_macro"),
KeyPair("target_arch", "x86_64"),
KeyPair("target_endian", "little"),
KeyPair("target_env", ""),
KeyPair("target_family", "unix"),
KeyPair("target_os", "macos"),
KeyPair("target_pointer_width", "64"),
Name("unix"),
]
[target.'cfg(cpu_native)']
This is the incorrect syntax for a Cargo feature; it would normally be cfg(feature = "cpu_native").
With Cargo, I can set a project's development settings to use parallel code generation:
[profile.dev]
codegen-units = 8
According to the documentation, it should be possible to put this into ~/.cargo/config to apply this setting to all projects. This doesn't work for me: it seems that the .cargo/config file isn't used at all. Is there any way to apply such configuration to every project I compile?
You can set rustflags for all builds or per target in your .cargo/config file.
[build] # or [target.$triple]
rustflags = ["-Ccodegen-units=4"]
To be clear, this will set the codegen-units for all your projects (covered by this .cargo/config) regardless of profile.
To make sure it's actually set, you can also set verbose output in the same file. This will show each rustc command with flags that cargo invokes.
[term]
verbose = true
A workaround is to create a script to be called instead of cargo
#!/bin/bash
if [[ $* != *--release* ]]; then
# dev build
export RUSTFLAGS="-C codegen-units=8"
fi
cargo "$#"
If you use the full path to cargo on the script, you can create a alias
alias cargo=/path/to/script
and just call cargo.