| David Tolnay | ee907be | 2020-10-07 17:38:59 -0700 | [diff] [blame] | 1 | use std::env; |
| 2 | use std::path::Path; |
| David Tolnay | 1d0266a | 2020-11-11 12:00:20 -0800 | [diff] [blame] | 3 | use std::process::Command; |
| David Tolnay | ee907be | 2020-10-07 17:38:59 -0700 | [diff] [blame] | 4 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 5 | fn main() { |
| 6 | cc::Build::new() |
| David Tolnay | 736cbca | 2020-03-11 16:49:18 -0700 | [diff] [blame] | 7 | .file("src/cxx.cc") |
| Christopher Chalmers | d24563d | 2020-05-08 19:23:46 +0100 | [diff] [blame] | 8 | .cpp(true) |
| David Tolnay | 110df7d | 2020-05-08 13:06:04 -0700 | [diff] [blame] | 9 | .cpp_link_stdlib(None) // linked via link-cplusplus crate |
| David Tolnay | de1cb77 | 2020-08-28 17:25:29 -0700 | [diff] [blame] | 10 | .flag_if_supported(cxxbridge_flags::STD) |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 11 | .compile("cxxbridge05"); |
| David Tolnay | 1d0266a | 2020-11-11 12:00:20 -0800 | [diff] [blame] | 12 | |
| David Tolnay | 736cbca | 2020-03-11 16:49:18 -0700 | [diff] [blame] | 13 | println!("cargo:rerun-if-changed=src/cxx.cc"); |
| 14 | println!("cargo:rerun-if-changed=include/cxx.h"); |
| David Tolnay | 585a9fe | 2020-08-30 21:03:38 -0700 | [diff] [blame] | 15 | println!("cargo:rustc-cfg=built_with_cargo"); |
| David Tolnay | 1d0266a | 2020-11-11 12:00:20 -0800 | [diff] [blame] | 16 | |
| David Tolnay | ee907be | 2020-10-07 17:38:59 -0700 | [diff] [blame] | 17 | 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 Tolnay | 1d0266a | 2020-11-11 12:00:20 -0800 | [diff] [blame] | 21 | |
| 22 | if let Some(rustc) = rustc_version() { |
| David Tolnay | bc413be | 2020-11-11 12:13:49 -0800 | [diff] [blame^] | 23 | if rustc.minor < 48 { |
| 24 | println!("cargo:warning=The cxx crate requires a rustc version 1.48.0 or newer."); |
| David Tolnay | 1d0266a | 2020-11-11 12:00:20 -0800 | [diff] [blame] | 25 | println!( |
| 26 | "cargo:warning=You appear to be building with: {}", |
| 27 | rustc.version, |
| 28 | ); |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | struct RustVersion { |
| 34 | version: String, |
| 35 | minor: u32, |
| 36 | } |
| 37 | |
| 38 | fn 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 Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 48 | } |