Why is this `--cfg` only getting passed when I don't cross-compile?

In this PR, I introduce put proc macro code behind a feature gate:

if !cfg!(zerocopy_derive_union_into_bytes) {
    ...
}

Our CI tests assume that this --cfg is passed. We have a custom cargo wrapper that sets various --cfgs, and I update that wrapper to pass this --cfg.

Bizarrely, this works and all of the tests pass except when cross-compiling. When cross-compiling, even just via cargo check, somehow the --cfg doesn't make its way to the proc macro during compilation.

My question is: why might cross-compilation cause --cfg arguments not to be propagated when compiling a proc macro?

Steps to reproduce:

  1. Take a look at this PR
  2. Check out the PR by checking out this branch
  3. Run ./cargo.sh +nightly check --tests --package zerocopy-derive (this should succeed)
  4. Run ./cargo.sh +nightly check --tests --package zerocopy-derive --target i686-unknown-linux-gnu (this should fail)

Note: The same failure mode is observed with vanilla Cargo; you can also replace steps (3) and (4) with:

  1. RUSTFLAGS='--cfg zerocopy_derive_union_into_bytes' cargo +$(./cargo.sh --version nightly) check --tests --package zerocopy-derive
  2. RUSTFLAGS='--cfg zerocopy_derive_union_into_bytes' cargo +$(./cargo.sh --version nightly) check --tests --package zerocopy-derive --target i686-unknown-linux-gnu

There is some special behavior in Cargo around RUSTFLAGS and cross compilation: Unstable Features - The Cargo Book

Ah gotcha, thanks!

@jswrenn ended up suggesting that we just emit code which contains #[cfg(...)] so that it's interpreted by the normal compilation pass, which turned out to work.

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.