Skip to content

Commit 776ea02

Browse files
committed
Add build.rs
1 parent c148ad4 commit 776ea02

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

build.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use std::process::{Command, Stdio};
2+
use std::str;
3+
fn is_python2() -> bool {
4+
let script = r"
5+
import sys
6+
print(sys.version_info[0])
7+
";
8+
let out = Command::new("python")
9+
.args(&["-c", script])
10+
.stderr(Stdio::inherit())
11+
.output()
12+
.expect("Failed to run the python interpreter");
13+
let version = str::from_utf8(&out.stdout).unwrap();
14+
version.starts_with("2")
15+
}
16+
17+
fn cfg(python2: bool) {
18+
if python2 {
19+
println!("cargo:rustc-cfg=Py_2");
20+
} else {
21+
println!("cargo:rustc-cfg=Py_3");
22+
}
23+
}
24+
25+
fn main() {
26+
if cfg!(feature = "python3") {
27+
cfg(false);
28+
} else if cfg!(feature = "python2") {
29+
cfg(true);
30+
} else {
31+
cfg(is_python2());
32+
}
33+
}

0 commit comments

Comments
 (0)