Search code examples
rustbuildrust-cargo

Why does defining RUSTFLAGS cause rustflags in .cargo/config to be ignored?


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?


Solution

  • 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:

    1. RUSTFLAGS environment variable.

    2. All matching target.<triple>.rustflags and target.<cfg>.rustflags config entries joined together.

    3. 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