-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
91 lines (76 loc) · 2.37 KB
/
build.rs
File metadata and controls
91 lines (76 loc) · 2.37 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//! Buildscript, required by some glue modules for initialisation.
//! Will do nothing if no glue modules are enabled.
#[cfg(feature = "js")]
extern crate napi_build;
#[cfg(feature = "py")]
extern crate pyo3_build_config;
#[cfg(feature = "py")]
extern crate pyo3_introspection;
/// The main method of the buildscript, required by some glue modules.
fn main() {
#[cfg(feature = "js")]
{
napi_build::setup();
}
#[cfg(feature = "py")]
{
pyo3_build_config::add_extension_module_link_args();
// The Python introspection step requires an already-built cdylib, which
// does not exist during the first clean build. Make it an opt-in second pass.
println!("cargo:rerun-if-env-changed=CODEMP_PY_GENHINTS");
if std::env::var("CODEMP_PY_GENHINTS").as_deref() != Ok("1") {
return;
}
let out_dir = std::env::var("OUT_DIR").expect("unreachable");
let dylib = if let Ok("windows") = std::env::var("CARGO_CFG_TARGET_OS").as_deref() {
"libcodemp.dll"
} else if let Ok("macos") = std::env::var("CARGO_CFG_TARGET_OS").as_deref() {
"libcodemp.dylib"
} else {
"libcodemp.so"
};
let lib_path = std::path::Path::new(&out_dir)
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.join(dylib);
if !lib_path.exists() {
println!(
"cargo:warning=skipping python postprocess, cdylib missing at {}",
lib_path.display()
);
return;
}
let module_hints = pyo3_introspection::introspect_cdylib(lib_path, "codemplib")
.expect("could not extract embedded type hints.");
let output = pyo3_introspection::module_stub_files(&module_hints);
println!("{output:?}");
let root_dir = std::env::var("CARGO_MANIFEST_DIR").expect("unreachable");
let pydist = std::path::Path::new(&root_dir)
.join("dist")
.join("py")
.join("src")
.join("autocodemp");
let pyicontent = output
.get_key_value(std::path::Path::new("__init__.pyi"))
.unwrap()
.1;
let pyinit = "\
from .codemplib import *
__doc__ = codemplib.__doc__
if hasattr(codemplib, '__all__'):
__all__ = codemplib.__all__";
let _ = std::fs::write(pydist.join("__init__.py"), pyinit);
let _ = std::fs::write(pydist.join("codemplib.pyi"), pyicontent);
}
#[cfg(feature = "lua")]
{
if let Ok("macos") = std::env::var("CARGO_CFG_TARGET_OS").as_deref() {
println!("cargo:rustc-cdylib-link-arg=-undefined");
println!("cargo:rustc-cdylib-link-arg=dynamic_lookup");
}
}
}