Skip to content

Commit

Permalink
make abi3 feature apply to config imported from PYO3_BUILD_CONFIG
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Aug 28, 2024
1 parent 5720228 commit bdf63a5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
28 changes: 6 additions & 22 deletions pyo3-build-config/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod errors;
use std::{env, path::Path};

use errors::{Context, Result};
use impl_::{env_var, make_interpreter_config, InterpreterConfig};
use impl_::{make_interpreter_config, InterpreterConfig};

fn configure(interpreter_config: Option<InterpreterConfig>, name: &str) -> Result<bool> {
let target = Path::new(&env::var_os("OUT_DIR").unwrap()).join(name);
Expand All @@ -29,28 +29,12 @@ fn configure(interpreter_config: Option<InterpreterConfig>, name: &str) -> Resul
}
}

/// If PYO3_CONFIG_FILE is set, copy it into the crate.
fn config_file() -> Result<Option<InterpreterConfig>> {
if let Some(path) = env_var("PYO3_CONFIG_FILE") {
let path = Path::new(&path);
println!("cargo:rerun-if-changed={}", path.display());
// Absolute path is necessary because this build script is run with a cwd different to the
// original `cargo build` instruction.
ensure!(
path.is_absolute(),
"PYO3_CONFIG_FILE must be an absolute path"
);

let interpreter_config = InterpreterConfig::from_path(path)
.context("failed to parse contents of PYO3_CONFIG_FILE")?;
Ok(Some(interpreter_config))
} else {
Ok(None)
}
}

fn generate_build_configs() -> Result<()> {
let configured = configure(config_file()?, "pyo3-build-config-file.txt")?;
// If PYO3_CONFIG_FILE is set, copy it into the crate.
let configured = configure(
InterpreterConfig::from_pyo3_config_file_env().transpose()?,
"pyo3-build-config-file.txt",
)?;

if configured {
// Don't bother trying to find an interpreter on the host system
Expand Down
28 changes: 28 additions & 0 deletions pyo3-build-config/src/impl_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,34 @@ print("ext_suffix", get_config_var("EXT_SUFFIX"))
})
}

/// Import an externally-provided config file.
///
/// The `abi3` features, if set, may apply an `abi3` constraint to the Python version.
pub(super) fn from_pyo3_config_file_env() -> Option<Result<Self>> {
cargo_env_var("PYO3_CONFIG_FILE").map(|path| {
let path = Path::new(&path);
println!("cargo:rerun-if-changed={}", path.display());
// Absolute path is necessary because this build script is run with a cwd different to the
// original `cargo build` instruction.
ensure!(
path.is_absolute(),
"PYO3_CONFIG_FILE must be an absolute path"
);

let mut config = InterpreterConfig::from_path(path)
.context("failed to parse contents of PYO3_CONFIG_FILE")?;
// If the abi3 feature is enabled, the minimum Python version is constrained by the abi3
// feature.
//
// TODO: abi3 is a property of the build mode, not the interpreter. Should this be
// removed from `InterpreterConfig`?
config.abi3 |= is_abi3();
config.fixup_for_abi3_version(get_abi3_version())?;

Ok(config)
})
}

#[doc(hidden)]
pub fn from_path(path: impl AsRef<Path>) -> Result<Self> {
let path = path.as_ref();
Expand Down

0 comments on commit bdf63a5

Please sign in to comment.