Different rustflags for build.rs and main.rs

Hello,

I need to generate two builds from my crate:

In the first build, neither build.rs and main.rs should be compiled with target-cpu=znver1.

In the second build, only main.rs should be compiled with target-cpu=znver1.

Any ideas?

I tried export RUSTFLAGS="-C target-cpu=znver1", but this affects both main.rs and build.rs

I'd try two approaches:

  • cargo rustc -- -C target-cpu=znver1
    

    which, IIRC, ought to only apply those passed rust flags to the compilation of main.rs,

  • RUSTFLAGS=-Ctarget-cpu=znver1 \
    cargo build --target <host_triplet>
    
    • e.g.

      RUSTFLAGS=-Ctarget-cpu=znver1 \
      cargo build --target "$(rustc -vV | sed -n 's/^host: //p')"
      

    since the moment --target is passed, Cargo will refuse to pass RUSTFLAGS to host-specific Rust code, such as build.rs scripts and procedural macros.

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.