Skip to content

Commit

Permalink
Test whether c++11 is supported and enable it if so (#46)
Browse files Browse the repository at this point in the history
* Test whether c++11 is supported and enable it if so

* Turns warnings into errors when testing for flag
  • Loading branch information
jean-airoldie authored Jul 8, 2024
1 parent 061e966 commit 2b3db09
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,36 @@ mod glibc {
}
}

mod cxx11 {
use std::{
env,
path::{Path, PathBuf},
};

// Attempt to compile a c program that links has the c++11 flag to determine
// whether it is supported.
pub(crate) fn has_cxx11() -> bool {
let src = Path::new(env!("CARGO_MANIFEST_DIR")).join("src/trivial.c");
println!("cargo:rerun-if-changed={}", src.display());

let dest =
PathBuf::from(env::var("OUT_DIR").unwrap()).join("has_cxx11");

cc::Build::new()
.warnings(true)
.warnings_into_errors(true)
.std("c++11")
.get_compiler()
.to_command()
.arg(src)
.arg("-o")
.arg(dest)
.status()
.expect("failed to execute gcc")
.success()
}
}

/// The location of a library.
#[derive(Debug, Clone)]
pub struct LibLocation {
Expand Down Expand Up @@ -441,6 +471,16 @@ impl Build {
if has_strlcpy {
build.define("ZMQ_HAVE_STRLCPY", "1");
}

// MSVC does not support c++11, since c++14 is the minimum.
if !target.contains("msvc") {
// Enable c++11 if possible to fix issue 45
// (https://github.com/jean-airoldie/zeromq-src-rs/issues/45).
if cxx11::has_cxx11() {
build.std("c++11");
}
}

let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let lib_dir = out_dir.join("lib");

Expand Down
3 changes: 3 additions & 0 deletions src/trivial.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int main(void) {
return 0;
}

0 comments on commit 2b3db09

Please sign in to comment.