rustrust-cargo

How can I specifiy that a Cargo package may only be compiled on some platforms?


I'm writing a binary Rust crate that is specifically built around a command-line program that only exists on Linux. Is there a way I can specify that it may only compile on Linux, as a Windows or OS X version just wouldn't make any sense? What is the best approach here?


Solution

  • It's not necessary to have a build.rs just for this. Just use the compile_error macro:

    fn main() {
        #[cfg(not(target_os = "linux"))]
        compile_error!("Only Linux is supported");
    
        // your code
    }