Skip to content

Commit 0f312e7

Browse files
authored
Merge pull request #268 from dtolnay/flags
Specify consistent c++ standard between cxx and cxx-test-suite
2 parents 907debe + de1cb77 commit 0f312e7

File tree

8 files changed

+59
-12
lines changed

8 files changed

+59
-12
lines changed

Cargo.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@ keywords = ["ffi"]
1414
categories = ["development-tools::ffi", "api-bindings"]
1515

1616
[features]
17-
default = [] # c++11
18-
"c++14" = []
19-
"c++17" = []
17+
default = ["cxxbridge-flags/default"] # c++11
18+
"c++14" = ["cxxbridge-flags/c++14"]
19+
"c++17" = ["cxxbridge-flags/c++17"]
20+
"c++20" = ["cxxbridge-flags/c++20"]
2021

2122
[dependencies]
2223
cxxbridge-macro = { version = "=0.3.6", path = "macro" }
2324
link-cplusplus = "1.0"
2425

2526
[build-dependencies]
2627
cc = "1.0.49"
28+
cxxbridge-flags = { version = "=0.3.6", path = "flags", default-features = false }
2729

2830
[dev-dependencies]
2931
cxx-build = { version = "=0.3.6", path = "gen/build" }
@@ -32,7 +34,7 @@ rustversion = "1.0"
3234
trybuild = { version = "1.0.33", features = ["diff"] }
3335

3436
[workspace]
35-
members = ["demo-rs", "gen/build", "gen/cmd", "macro", "tests/ffi"]
37+
members = ["demo-rs", "flags", "gen/build", "gen/cmd", "macro", "tests/ffi"]
3638

3739
[package.metadata.docs.rs]
3840
targets = ["x86_64-unknown-linux-gnu"]

build.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@ fn main() {
33
.file("src/cxx.cc")
44
.cpp(true)
55
.cpp_link_stdlib(None) // linked via link-cplusplus crate
6-
.flag_if_supported(if cfg!(feature = "c++17") {
7-
"-std=c++17"
8-
} else if cfg!(feature = "c++14") {
9-
"-std=c++14"
10-
} else {
11-
"-std=c++11"
12-
})
6+
.flag_if_supported(cxxbridge_flags::STD)
137
.compile("cxxbridge03");
148
println!("cargo:rerun-if-changed=src/cxx.cc");
159
println!("cargo:rerun-if-changed=include/cxx.h");

flags/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "cxxbridge-flags"
3+
version = "0.3.6"
4+
authors = ["David Tolnay <dtolnay@gmail.com>"]
5+
edition = "2018"
6+
license = "MIT OR Apache-2.0"
7+
description = "Compiler configuration of the `cxx` crate (implementation detail)"
8+
repository = "https://github.com/dtolnay/cxx"
9+
10+
[features]
11+
default = [] # c++11
12+
"c++14" = []
13+
"c++17" = []
14+
"c++20" = []
15+
16+
[package.metadata.docs.rs]
17+
targets = ["x86_64-unknown-linux-gnu"]

flags/src/impl.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#[allow(unused_assignments, unused_mut, unused_variables)]
2+
pub const STD: &str = {
3+
let mut flags = ["-std=c++11", "/std:c++11"];
4+
5+
#[cfg(feature = "c++14")]
6+
(flags = ["-std=c++14", "/std:c++14"]);
7+
8+
#[cfg(feature = "c++17")]
9+
(flags = ["-std=c++17", "/std:c++17"]);
10+
11+
#[cfg(feature = "c++20")]
12+
(flags = ["-std=c++20", "/std:c++20"]);
13+
14+
let [mut flag, msvc_flag] = flags;
15+
16+
#[cfg(target_env = "msvc")]
17+
(flag = msvc_flag);
18+
19+
flag
20+
};

flags/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//! This crate is an implementation detail of the `cxx` and `cxx-build` crates,
2+
//! and does not expose any public API.
3+
4+
mod r#impl;
5+
6+
#[doc(hidden)]
7+
pub use r#impl::*;

tests/ffi/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ cxx = { path = "../.." }
1212

1313
[build-dependencies]
1414
cxx-build = { path = "../../gen/build" }
15+
cxxbridge-flags = { path = "../../flags" }

tests/ffi/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ fn main() {
66
let sources = vec!["lib.rs", "module.rs"];
77
cxx_build::bridges(sources)
88
.file("tests.cc")
9-
.flag_if_supported("-std=c++11")
9+
.flag_if_supported(cxxbridge_flags::STD)
1010
.compile("cxx-test-suite");
1111
}

third-party/Cargo.lock

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)