Specify consistent c++ standard between cxx and cxx-test-suite

This commit is contained in:
David Tolnay 2020-08-28 17:25:29 -07:00
parent 907debe8b4
commit de1cb777b2
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
8 changed files with 59 additions and 12 deletions

View File

@ -14,9 +14,10 @@ keywords = ["ffi"]
categories = ["development-tools::ffi", "api-bindings"]
[features]
default = [] # c++11
"c++14" = []
"c++17" = []
default = ["cxxbridge-flags/default"] # c++11
"c++14" = ["cxxbridge-flags/c++14"]
"c++17" = ["cxxbridge-flags/c++17"]
"c++20" = ["cxxbridge-flags/c++20"]
[dependencies]
cxxbridge-macro = { version = "=0.3.6", path = "macro" }
@ -24,6 +25,7 @@ link-cplusplus = "1.0"
[build-dependencies]
cc = "1.0.49"
cxxbridge-flags = { version = "=0.3.6", path = "flags", default-features = false }
[dev-dependencies]
cxx-build = { version = "=0.3.6", path = "gen/build" }
@ -32,7 +34,7 @@ rustversion = "1.0"
trybuild = { version = "1.0.33", features = ["diff"] }
[workspace]
members = ["demo-rs", "gen/build", "gen/cmd", "macro", "tests/ffi"]
members = ["demo-rs", "flags", "gen/build", "gen/cmd", "macro", "tests/ffi"]
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

View File

@ -3,13 +3,7 @@ fn main() {
.file("src/cxx.cc")
.cpp(true)
.cpp_link_stdlib(None) // linked via link-cplusplus crate
.flag_if_supported(if cfg!(feature = "c++17") {
"-std=c++17"
} else if cfg!(feature = "c++14") {
"-std=c++14"
} else {
"-std=c++11"
})
.flag_if_supported(cxxbridge_flags::STD)
.compile("cxxbridge03");
println!("cargo:rerun-if-changed=src/cxx.cc");
println!("cargo:rerun-if-changed=include/cxx.h");

17
flags/Cargo.toml Normal file
View File

@ -0,0 +1,17 @@
[package]
name = "cxxbridge-flags"
version = "0.3.6"
authors = ["David Tolnay <dtolnay@gmail.com>"]
edition = "2018"
license = "MIT OR Apache-2.0"
description = "Compiler configuration of the `cxx` crate (implementation detail)"
repository = "https://github.com/dtolnay/cxx"
[features]
default = [] # c++11
"c++14" = []
"c++17" = []
"c++20" = []
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

20
flags/src/impl.rs Normal file
View File

@ -0,0 +1,20 @@
#[allow(unused_assignments, unused_mut, unused_variables)]
pub const STD: &str = {
let mut flags = ["-std=c++11", "/std:c++11"];
#[cfg(feature = "c++14")]
(flags = ["-std=c++14", "/std:c++14"]);
#[cfg(feature = "c++17")]
(flags = ["-std=c++17", "/std:c++17"]);
#[cfg(feature = "c++20")]
(flags = ["-std=c++20", "/std:c++20"]);
let [mut flag, msvc_flag] = flags;
#[cfg(target_env = "msvc")]
(flag = msvc_flag);
flag
};

7
flags/src/lib.rs Normal file
View File

@ -0,0 +1,7 @@
//! This crate is an implementation detail of the `cxx` and `cxx-build` crates,
//! and does not expose any public API.
mod r#impl;
#[doc(hidden)]
pub use r#impl::*;

View File

@ -12,3 +12,4 @@ cxx = { path = "../.." }
[build-dependencies]
cxx-build = { path = "../../gen/build" }
cxxbridge-flags = { path = "../../flags" }

View File

@ -6,6 +6,6 @@ fn main() {
let sources = vec!["lib.rs", "module.rs"];
cxx_build::bridges(sources)
.file("tests.cc")
.flag_if_supported("-std=c++11")
.flag_if_supported(cxxbridge_flags::STD)
.compile("cxx-test-suite");
}

6
third-party/Cargo.lock generated vendored
View File

@ -70,6 +70,7 @@ dependencies = [
"cc",
"cxx-build",
"cxx-test-suite",
"cxxbridge-flags",
"cxxbridge-macro",
"link-cplusplus",
"rustversion",
@ -94,6 +95,7 @@ version = "0.0.0"
dependencies = [
"cxx",
"cxx-build",
"cxxbridge-flags",
]
[[package]]
@ -116,6 +118,10 @@ dependencies = [
"cxx-build",
]
[[package]]
name = "cxxbridge-flags"
version = "0.3.6"
[[package]]
name = "cxxbridge-macro"
version = "0.3.6"