1 use std::env; 2 use std::path::{Path, PathBuf}; 3 use std::process::Command; 4 main()5fn main() { 6 let manifest_dir_opt = env::var_os("CARGO_MANIFEST_DIR").map(PathBuf::from); 7 let manifest_dir = manifest_dir_opt.as_deref().unwrap_or(Path::new("")); 8 9 cc::Build::new() 10 .file(manifest_dir.join("src/cxx.cc")) 11 .cpp(true) 12 .cpp_link_stdlib(None) // linked via link-cplusplus crate 13 .std(cxxbridge_flags::STD) 14 .warnings_into_errors(cfg!(deny_warnings)) 15 .compile("cxxbridge1"); 16 17 println!("cargo:rerun-if-changed=src/cxx.cc"); 18 println!("cargo:rerun-if-changed=include/cxx.h"); 19 println!("cargo:rustc-cfg=built_with_cargo"); 20 21 if let Some(manifest_dir) = &manifest_dir_opt { 22 let cxx_h = manifest_dir.join("include").join("cxx.h"); 23 println!("cargo:HEADER={}", cxx_h.to_string_lossy()); 24 } 25 26 if let Some(rustc) = rustc_version() { 27 if rustc.minor < 60 { 28 println!("cargo:warning=The cxx crate requires a rustc version 1.60.0 or newer."); 29 println!( 30 "cargo:warning=You appear to be building with: {}", 31 rustc.version, 32 ); 33 } 34 } 35 } 36 37 struct RustVersion { 38 version: String, 39 minor: u32, 40 } 41 rustc_version() -> Option<RustVersion>42fn rustc_version() -> Option<RustVersion> { 43 let rustc = env::var_os("RUSTC")?; 44 let output = Command::new(rustc).arg("--version").output().ok()?; 45 let version = String::from_utf8(output.stdout).ok()?; 46 let mut pieces = version.split('.'); 47 if pieces.next() != Some("rustc 1") { 48 return None; 49 } 50 let minor = pieces.next()?.parse().ok()?; 51 Some(RustVersion { version, minor }) 52 } 53