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