Cargo build --verbose --target=i686-linux-android makes target_os NOT android, why?

If I build my project with

cargo build --verbose --target=i686-linux-android

where build.rs looks like this

fn main() {
    #[cfg(target_os = "linux")]
    {
        panic!("target_os is linux!!!!!!!!!!!!!");
    }

I get the panic at panic!("target_os is linux!!!!!!!!!!!!!");, but the target is android.

Why?

Build scripts and proc macros are compiled to run on the host, not the target. That is, target_os in that context is targeting the host. But you can learn about the target with environment variables instead of cfg:

https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts

3 Likes

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.