Identifying host os in cross-compiling

I am cross-compiling to wasm32-unknown-unknown and I would like to create a test for our CI. The problem is I can't find a standard way to identify the host_os and I tried to pass a --cfg to cargo test but it also doesn't seem possible.

I would be glad if someone point me to a documentation or maybe a workaround.

Here is the code I am trying to implement:

#[cfg(test)]
mod test {
    use super::Platform;

    use wasm_bindgen_test::wasm_bindgen_test;
    use wasm_bindgen_test::wasm_bindgen_test_configure;

    wasm_bindgen_test_configure!(run_in_browser);

    fn host_platform() -> Platform {
        #[cfg(host_os = "linux")]
        Platform::Linux
    }

    #[wasm_bindgen_test]
    fn platform() {
        assert_eq!(Platform::query(), host_platform())
    }
}
1 Like

You might need to write a build script and automatically optionally enable a run-in-browser feature, because that's guaranteed to be compiled+run on the host.

Cargo passes several useful environment variables to build scripts so they can know what the target and host platforms are.

Just in addition to the environment variables here, using plain old cfg! in build.rs should refer to the hosts cfg as well.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.