From 16136736723a9ab62f79420bcdf26f69f225f943 Mon Sep 17 00:00:00 2001 From: jean-airoldie <25088801+jean-airoldie@users.noreply.github.com> Date: Sat, 6 Jul 2024 01:41:54 -0400 Subject: [PATCH] Test whether c++11 is supported and enable it if so --- src/lib.rs | 39 +++++++++++++++++++++++++++++++++++++++ src/trivial.c | 3 +++ 2 files changed, 42 insertions(+) create mode 100644 src/trivial.c diff --git a/src/lib.rs b/src/lib.rs index e63a0ee..9a55627 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -84,6 +84,35 @@ 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(false) + .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 { @@ -441,6 +470,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"); diff --git a/src/trivial.c b/src/trivial.c new file mode 100644 index 0000000..9b6bdc2 --- /dev/null +++ b/src/trivial.c @@ -0,0 +1,3 @@ +int main(void) { + return 0; +}