'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?



Solution 1:[1]

The build.rs script is compiled and run locally and thus its #[cfg(...)] attributes will reflect the local system. If you want to know the operating system that you're ultimately building for, use the CARGO_CFG_TARGET_OS environment variable.

Others can be seen in Environment variables Cargo sets for build scripts in the Rust Reference.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kmdreko