blob: ae3ef670cee2f8477abe871e0efc3f0e8fc5b252 [file] [log] [blame]
David Tolnayee907be2020-10-07 17:38:59 -07001use std::env;
2use std::path::Path;
David Tolnay1d0266a2020-11-11 12:00:20 -08003use std::process::Command;
David Tolnayee907be2020-10-07 17:38:59 -07004
David Tolnay7db73692019-10-20 14:51:12 -04005fn main() {
6 cc::Build::new()
David Tolnay736cbca2020-03-11 16:49:18 -07007 .file("src/cxx.cc")
Christopher Chalmersd24563d2020-05-08 19:23:46 +01008 .cpp(true)
David Tolnay110df7d2020-05-08 13:06:04 -07009 .cpp_link_stdlib(None) // linked via link-cplusplus crate
David Tolnayde1cb772020-08-28 17:25:29 -070010 .flag_if_supported(cxxbridge_flags::STD)
David Tolnay8f16ae72020-10-08 18:21:13 -070011 .compile("cxxbridge05");
David Tolnay1d0266a2020-11-11 12:00:20 -080012
David Tolnay736cbca2020-03-11 16:49:18 -070013 println!("cargo:rerun-if-changed=src/cxx.cc");
14 println!("cargo:rerun-if-changed=include/cxx.h");
David Tolnay585a9fe2020-08-30 21:03:38 -070015 println!("cargo:rustc-cfg=built_with_cargo");
David Tolnay1d0266a2020-11-11 12:00:20 -080016
David Tolnayee907be2020-10-07 17:38:59 -070017 if let Some(manifest_dir) = env::var_os("CARGO_MANIFEST_DIR") {
18 let cxx_h = Path::new(&manifest_dir).join("include").join("cxx.h");
19 println!("cargo:HEADER={}", cxx_h.to_string_lossy());
20 }
David Tolnay1d0266a2020-11-11 12:00:20 -080021
22 if let Some(rustc) = rustc_version() {
David Tolnaybc413be2020-11-11 12:13:49 -080023 if rustc.minor < 48 {
24 println!("cargo:warning=The cxx crate requires a rustc version 1.48.0 or newer.");
David Tolnay1d0266a2020-11-11 12:00:20 -080025 println!(
26 "cargo:warning=You appear to be building with: {}",
27 rustc.version,
28 );
29 }
30 }
31}
32
33struct RustVersion {
34 version: String,
35 minor: u32,
36}
37
38fn rustc_version() -> Option<RustVersion> {
39 let rustc = env::var_os("RUSTC")?;
40 let output = Command::new(rustc).arg("--version").output().ok()?;
41 let version = String::from_utf8(output.stdout).ok()?;
42 let mut pieces = version.split('.');
43 if pieces.next() != Some("rustc 1") {
44 return None;
45 }
46 let minor = pieces.next()?.parse().ok()?;
47 Some(RustVersion { version, minor })
David Tolnay7db73692019-10-20 14:51:12 -040048}