Repository navigation
Expand file tree
/
Copy pathbuild.rs
More file actions
58 lines (53 loc) · 2.49 KB
/
Copy pathbuild.rs
File metadata and controls
58 lines (53 loc) · 2.49 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
// pyo3 embeds a SHARED libpython and emits no rpath, so the dynamic loader
// binds whichever libpython3.x it finds first -- usually the system one, even
// when the build was configured against a different interpreter. the stdlib
// path is baked from the configured interpreter, so the two halves can come
// from different installs, and nothing says so until a node imports a module
// the mismatch hides: a python-build-standalone install (what mise provides)
// compiles `ssl` INTO libpython rather than shipping `_ssl.so` in lib-dynload,
// so pairing its stdlib with the system libpython leaves no reachable `_ssl`
// and every https url in a python node fails as "unknown url type: https".
//
// pinning the rpath to the configured interpreter's libdir keeps the library
// and the stdlib on it the same install.
//
// an interpreter living in a default loader directory needs no rpath -- the
// loader was always going to find that one -- so system pythons are left alone
// and the release workflow's containers build exactly as they did before.
use std::path::Path;
use std::process::Command;
const SYSCONFIG_QUERY: &str =
"import sysconfig; print(sysconfig.get_config_var('LIBDIR') or ''); \
print(sysconfig.get_config_var('Py_ENABLE_SHARED') or 0)";
/// libdirs the dynamic loader searches anyway. /usr/local/lib is the python:*
/// docker images' prefix, which the release workflow builds in.
const LOADER_DEFAULT_DIRS: [&str; 2] = ["/usr/", "/lib"];
fn main() {
println!("cargo:rerun-if-env-changed=PYO3_PYTHON");
if std::env::var_os("CARGO_FEATURE_PYTHON").is_none() {
return;
}
let python = std::env::var("PYO3_PYTHON").unwrap_or_else(|_| "python3".to_string());
if let Some(libdir) = shared_libdir(&python) {
println!("cargo:rustc-link-arg=-Wl,-rpath,{}", libdir);
}
}
/// the libdir of `python`, when it was built with a shared libpython and that
/// library is actually there to link against.
fn shared_libdir(python: &str) -> Option<String> {
let out = Command::new(python).arg("-c").arg(SYSCONFIG_QUERY).output().ok()?;
if !out.status.success() {
return None;
}
let text = String::from_utf8(out.stdout).ok()?;
let mut lines = text.lines();
let libdir = lines.next()?.trim().to_string();
let shared = lines.next()?.trim() != "0";
if libdir.is_empty() || !shared || !Path::new(&libdir).is_dir() {
return None;
}
if LOADER_DEFAULT_DIRS.iter().any(|d| libdir.starts_with(d)) {
return None;
}
Some(libdir)
}