Skip to content

Commit

Permalink
Update build script to use export-config from pyo3-build-config.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashley Anderson committed Jan 8, 2022
1 parent ed84dce commit 27ca891
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ openssl-sys = "0.9.72"

[build-dependencies]
cc = "1.0.72"
# TODO: update after merge/release in pyo3

# TODO: update after merge/release in pyo3
[build-dependencies.pyo3-build-config]
git = "https://github.com/aganders3/pyo3"
branch = "export-conf"
Expand Down
81 changes: 20 additions & 61 deletions src/rust/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::env;
use std::io::Write;
use std::fs;
use std::path::{Path, MAIN_SEPARATOR};
use std::process::{Command, Stdio};
use std::process::Command;

use pyo3_build_config::InterpreterConfig;

fn main() {
let target = env::var("TARGET").unwrap();
Expand All @@ -19,10 +21,11 @@ fn main() {
}
}

let pyo3_interpreter =
InterpreterConfig::from_pyo3_export_config().expect("could not find pyo3 interpreter");

let out_dir = env::var("OUT_DIR").unwrap();
// FIXME: maybe pyo3-build-config should provide a way to do this?
let python = env::var("PYO3_PYTHON").unwrap_or_else(|_| "python3".to_string());
println!("cargo:rerun-if-changed=../_cffi_src/");

let python_path = match env::var("PYTHONPATH") {
Ok(mut val) => {
if cfg!(target_os = "windows") {
Expand All @@ -35,30 +38,20 @@ fn main() {
}
Err(_) => format!("..{}", MAIN_SEPARATOR),
};
let output = Command::new(&python)
.env("PYTHONPATH", python_path)
.env("OUT_DIR", &out_dir)
.arg("../_cffi_src/build_openssl.py")
.output()

println!("cargo:rerun-if-changed=../_cffi_src/");
pyo3_interpreter
.run_python_script_with_envs(
&fs::read_to_string("../_cffi_src/build_openssl.py")
.expect("failed to read build_openssl.py"),
vec![("PYTHONPATH", python_path), ("OUT_DIR", out_dir.clone())],
)
.expect("failed to execute build_openssl.py");
if !output.status.success() {
panic!(
"failed to run build_openssl.py, stdout: \n{}\nstderr: \n{}\n",
String::from_utf8(output.stdout).unwrap(),
String::from_utf8(output.stderr).unwrap()
);
}

let python_impl = run_python_script(
&python,
"import platform; print(platform.python_implementation(), end='')",
)
.unwrap();
let python_include = run_python_script(
&python,
"import sysconfig; print(sysconfig.get_path('include'), end='')",
)
.unwrap();
let python_impl = pyo3_interpreter.implementation.to_string();
let python_include = pyo3_interpreter
.run_python_script("import sysconfig; print(sysconfig.get_path('include'), end='')")
.unwrap();
let openssl_include =
std::env::var_os("DEP_OPENSSL_INCLUDE").expect("unable to find openssl include path");
let openssl_c = Path::new(&out_dir).join("_openssl.c");
Expand All @@ -84,40 +77,6 @@ fn main() {
build.compile("_openssl.a");
}

/// Run a python script using the specified interpreter binary.
fn run_python_script(interpreter: impl AsRef<Path>, script: &str) -> Result<String, String> {
let interpreter = interpreter.as_ref();
let out = Command::new(interpreter)
.env("PYTHONIOENCODING", "utf-8")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.spawn()
.and_then(|mut child| {
child
.stdin
.as_mut()
.expect("piped stdin")
.write_all(script.as_bytes())?;
child.wait_with_output()
});

match out {
Err(err) => Err(format!(
"failed to run the Python interpreter at {}: {}",
interpreter.display(),
err
)),
Ok(ok) if !ok.status.success() => Err(format!(
"Python script failed: {}",
String::from_utf8(ok.stderr).expect("failed to parse Python script output as utf-8")
)),
Ok(ok) => Ok(
String::from_utf8(ok.stdout).expect("failed to parse Python script output as utf-8")
),
}
}

fn macos_link_search_path() -> Option<String> {
let output = Command::new("clang")
.arg("--print-search-dirs")
Expand Down

0 comments on commit 27ca891

Please sign in to comment.