forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.rs
More file actions
71 lines (63 loc) · 2.47 KB
/
build.rs
File metadata and controls
71 lines (63 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use std::env;
use std::path::{Path, PathBuf};
use cpython_build_helper::print_linker_args;
fn main() {
print_linker_args();
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let srcdir = manifest_dir
.parent()
.and_then(Path::parent)
.expect("expected Modules/cpython-sys to live under the source tree");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let builddir = env::var("PYTHON_BUILD_DIR").ok();
if gil_disabled(srcdir, builddir.as_deref()) {
println!("cargo:rustc-cfg=py_gil_disabled");
}
generate_c_api_bindings(srcdir, builddir.as_deref(), out_path.as_path());
// TODO(emmatyping): generate bindings to the internal parser API
// The parser includes things slightly differently, so we should generate
// it's bindings independently
//generate_parser_bindings(srcdir, &out_path.as_path());
}
fn gil_disabled(srcdir: &Path, builddir: Option<&str>) -> bool {
let mut candidates = Vec::new();
if let Some(build) = builddir {
candidates.push(PathBuf::from(build));
}
candidates.push(srcdir.to_path_buf());
for base in candidates {
let path = base.join("pyconfig.h");
if let Ok(contents) = std::fs::read_to_string(&path)
&& contents.contains("Py_GIL_DISABLED 1")
{
return true;
}
}
false
}
fn generate_c_api_bindings(srcdir: &Path, builddir: Option<&str>, out_path: &Path) {
let mut builder = bindgen::Builder::default().header("wrapper.h");
// Always search the source dir and the public headers.
let mut include_dirs = vec![srcdir.to_path_buf(), srcdir.join("Include")];
// Include the build directory if provided; out-of-tree builds place
// the generated pyconfig.h there.
if let Some(build) = builddir {
include_dirs.push(PathBuf::from(build));
}
for dir in include_dirs {
builder = builder.clang_arg(format!("-I{}", dir.display()));
}
let bindings = builder
.allowlist_function("_?Py.*")
.allowlist_type("_?Py.*")
.allowlist_var("_?Py.*")
.blocklist_type("^PyMethodDef$")
.blocklist_type("PyObject")
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Unable to generate bindings");
// Write the bindings to the $OUT_DIR/c_api.rs file.
bindings
.write_to_file(out_path.join("c_api.rs"))
.expect("Couldn't write bindings!");
}