Skip to content

Commit

Permalink
pyo3-build-config: Build "abi3" extensions without an interpreter
Browse files Browse the repository at this point in the history
Ignore the build host Python interpreter configuration when
compiling portable "abi3" extension modules.
Such extensions should not depend on a concrete Python version
or build configuration by design.

Maturin already does this when building "abi3" extension wheels
by defining `PYO3_NO_PYTHON=1` for Cargo when an `abi3-py3*`
feature is detected.

Closes #2292
  • Loading branch information
ravenexp committed Apr 11, 2022
1 parent dc5a670 commit 809a3ec
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pyo3-build-config/src/impl_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,14 +674,14 @@ pub fn is_extension_module() -> bool {

/// Checks if we need to link to `libpython` for the current build target.
///
/// Must be called from a crate PyO3 build script.
/// Must be called from a PyO3 crate build script.
pub fn is_linking_libpython() -> bool {
is_linking_libpython_for_target(&target_triple_from_env())
}

/// Checks if we need to link to `libpython` for the target.
///
/// Must be called from a crate PyO3 build script.
/// Must be called from a PyO3 crate build script.
fn is_linking_libpython_for_target(target: &Triple) -> bool {
target.operating_system == OperatingSystem::Windows
|| target.environment == Environment::Android
Expand Down Expand Up @@ -1651,14 +1651,19 @@ pub fn make_cross_compile_config() -> Result<Option<InterpreterConfig>> {
/// Only used by `pyo3-build-config` build script.
#[allow(dead_code)]
pub fn make_interpreter_config() -> Result<InterpreterConfig> {
let host = Triple::host();
let abi3_version = get_abi3_version();

if have_python_interpreter() {
// See if we can skip the Python interpreter configuration detection.
// Unix "abi3" extension modules can be built without any interpreter,
// so do this by default just like maturin does.
let require_interpreter = abi3_version.is_none() || is_linking_libpython_for_target(&host);

if have_python_interpreter() && require_interpreter {
let mut interpreter_config = InterpreterConfig::from_interpreter(find_interpreter()?)?;
interpreter_config.fixup_for_abi3_version(abi3_version)?;
Ok(interpreter_config)
} else if let Some(version) = abi3_version {
let host = Triple::host();
Ok(default_abi3_config(&host, version))
} else {
bail!("An abi3-py3* feature must be specified when compiling without a Python interpreter.")
Expand Down

0 comments on commit 809a3ec

Please sign in to comment.