Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ fn configure_pyo3_env(
// Setup `PYO3_CONFIG_FILE` if we are cross compiling for pyo3 bindings
if let Some(interpreter) = python_interpreter {
// Target python interpreter isn't runnable when cross compiling
if interpreter.runnable && !force_target_abi {
if interpreter.runnable {
if bridge_model.is_pyo3() {
debug!(
"Setting PYO3_PYTHON to {}",
Expand Down
132 changes: 132 additions & 0 deletions test-crates/pyo3-cffi-build-script/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions test-crates/pyo3-cffi-build-script/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "pyo3-cffi-build-script"
version = "0.1.0"
edition = "2021"

[dependencies]
pyo3 = { version = "0.29.0", features = ["abi3"] }

[lib]
name = "pyo3_cffi_build_script"
crate-type = ["cdylib"]
21 changes: 21 additions & 0 deletions test-crates/pyo3-cffi-build-script/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! Models the `cryptography` project: a PyO3 extension whose build script shells
//! out to the build interpreter to run cffi. maturin must hand us that
//! interpreter via `PYO3_PYTHON`, even for abi3 builds.

use std::env;
use std::process::Command;

fn main() {
println!("cargo:rerun-if-env-changed=PYO3_PYTHON");
let python = env::var("PYO3_PYTHON")
.expect("PYO3_PYTHON is not set; maturin must hand abi3 builds an interpreter");
let status = Command::new(&python)
.args([
"-c",
"import cffi; ffi = cffi.FFI(); ffi.cdef('int answer(void);'); \
ffi.set_source('_pyo3_cffi_demo', 'int answer(void) { return 42; }')",
])
.status()
.expect("failed to run PYO3_PYTHON");
assert!(status.success(), "cffi codegen via PYO3_PYTHON failed");
}
7 changes: 7 additions & 0 deletions test-crates/pyo3-cffi-build-script/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use pyo3::prelude::*;

#[pymodule]
fn pyo3_cffi_build_script(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add("answer", 42)?;
Ok(())
}
34 changes: 34 additions & 0 deletions tests/common/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,40 @@ pub fn abi3_without_version() -> Result<()> {
Ok(())
}

pub fn pyo3_cffi_build_script() -> Result<()> {
let base = crate::common::test_python_path().map(PathBuf::from);
let venv_dir = crate::common::create_named_virtualenv("pyo3-cffi-build-script", base)?;
let venv_python = maturin::Target::from_target_triple(None)?.get_venv_python(&venv_dir);
crate::common::install_pip_packages(&venv_python, &["cffi"])?;
let python = venv_python
.to_str()
.context("venv python path is not utf-8")?;

// The first argument is ignored by clap
let cli = vec![
"build",
"--manifest-path",
"test-crates/pyo3-cffi-build-script/Cargo.toml",
"--quiet",
"--interpreter",
python,
"--target-dir",
"test-targets/wheels/pyo3_cffi_build_script",
];

let options = BuildOptions::try_parse_from(cli)?;
let build_context = options
.into_build_context()
.strip(Some(cfg!(feature = "faster-tests")))
.editable(false)
.build()?;
// Actually build the wheel so the crate's build.rs runs.
let wheels = BuildOrchestrator::new(&build_context).build_wheels();
assert!(wheels.is_ok());

Ok(())
}

pub fn abi3t_without_version() -> Result<()> {
let python = crate::common::test_python_path().unwrap_or_else(|| "python3".to_string());
// The first argument is ignored by clap
Expand Down
5 changes: 5 additions & 0 deletions tests/run/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ fn abi3_without_version() {
handle_result(other::abi3_without_version())
}

#[test]
fn pyo3_cffi_build_script() {
handle_result(other::pyo3_cffi_build_script())
}

#[test]
fn abi3t_without_version() {
// abi3t requires CPython >= 3.15 (PEP 803). On older runners the build
Expand Down
Loading