Cross compiling OS-X to Linux

The advice I’ve found on the web has been:

rustup target add x86_64-unknown-linux-gnu
cargo build --target=x86_64-unknown-linux-gnu

But alas the target_os still seems to be ‘macos’ when evaluating (which is generating bindgen) - is there some additional step I am missing?

Where does target_os come from? According to the Cargo docs build scripts are just given the target triple and have to reparse it themselves.

Running cargo build --target x86_64-apple-darwin from linux and printing TARGET in the build script I do get "x86_64-apple-darwin".

Oh, are you talking about cfg(target_os = "linux")? That is done at compile time of the script, so will always be the host OS in a build script since the build script is being compiled to run on your current host. You need to read the TARGET environment variable and use that to determine the target OS if you need to know it in the build script. I would expect there to be some crates for helping with that.

Actually, looking for a crate to help I ran across which implies that there should be a CARGO_CFG_TARGET_OS environment variable, which does seem to work in my test.


I thought it might have been something like that.

Thanks env::var("CARGO_CFG_TARGET_OS") works a treat.