Skip to content

Commit 582d9c5

Browse files
committed
build: move print-config into environment variable
1 parent 3d3dacf commit 582d9c5

File tree

5 files changed

+58
-45
lines changed

5 files changed

+58
-45
lines changed

build.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{env, process::Command};
22

33
use pyo3_build_config::{
4-
bail, ensure,
4+
bail, cargo_env_var, ensure, env_var,
55
errors::{Context, Result},
66
InterpreterConfig, PythonImplementation, PythonVersion,
77
};
@@ -22,7 +22,10 @@ fn ensure_python_version(interpreter_config: &InterpreterConfig) -> Result<()> {
2222

2323
fn ensure_target_architecture(interpreter_config: &InterpreterConfig) -> Result<()> {
2424
// Try to check whether the target architecture matches the python library
25-
let rust_target = match env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap().as_str() {
25+
let rust_target = match cargo_env_var("CARGO_CFG_TARGET_POINTER_WIDTH")
26+
.unwrap()
27+
.as_str()
28+
{
2629
"64" => "64-bit",
2730
"32" => "32-bit",
2831
x => bail!("unexpected Rust target pointer width: {}", x),
@@ -55,14 +58,14 @@ fn ensure_target_architecture(interpreter_config: &InterpreterConfig) -> Result<
5558
}
5659

5760
fn get_rustc_link_lib(config: &InterpreterConfig) -> Result<String> {
58-
let link_name = if env::var_os("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
61+
let link_name = if cargo_env_var("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
5962
if config.abi3 {
6063
// Link against python3.lib for the stable ABI on Windows.
6164
// See https://www.python.org/dev/peps/pep-0384/#linkage
6265
//
6366
// This contains only the limited ABI symbols.
6467
"pythonXY:python3".to_owned()
65-
} else if env::var_os("CARGO_CFG_TARGET_ENV").unwrap() == "gnu" {
68+
} else if cargo_env_var("CARGO_CFG_TARGET_ENV").unwrap() == "gnu" {
6669
// https://packages.msys2.org/base/mingw-w64-python
6770
format!(
6871
"pythonXY:python{}.{}",
@@ -103,8 +106,8 @@ fn rustc_minor_version() -> Option<u32> {
103106
}
104107

105108
fn emit_cargo_configuration(interpreter_config: &InterpreterConfig) -> Result<()> {
106-
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
107-
let is_extension_module = env::var_os("CARGO_FEATURE_EXTENSION_MODULE").is_some();
109+
let target_os = cargo_env_var("CARGO_CFG_TARGET_OS").unwrap();
110+
let is_extension_module = cargo_env_var("CARGO_FEATURE_EXTENSION_MODULE").is_some();
108111
match (is_extension_module, target_os.as_str()) {
109112
(_, "windows") => {
110113
// always link on windows, even with extension module
@@ -144,7 +147,7 @@ fn emit_cargo_configuration(interpreter_config: &InterpreterConfig) -> Result<()
144147
_ => {}
145148
}
146149

147-
if env::var_os("CARGO_FEATURE_AUTO_INITIALIZE").is_some() {
150+
if cargo_env_var("CARGO_FEATURE_AUTO_INITIALIZE").is_some() {
148151
if !interpreter_config.shared {
149152
bail!(
150153
"The `auto-initialize` feature is enabled, but your python installation only supports \
@@ -179,6 +182,9 @@ fn emit_cargo_configuration(interpreter_config: &InterpreterConfig) -> Result<()
179182
/// (including `pyo3-macros-backend` during macro expansion).
180183
fn configure_pyo3() -> Result<()> {
181184
let interpreter_config = pyo3_build_config::make_interpreter_config()?;
185+
if env_var("PYO3_PRINT_CONFIG").map_or(false, |os_str| os_str == "1") {
186+
print_config_and_exit(&interpreter_config);
187+
}
182188
ensure_python_version(&interpreter_config)?;
183189
ensure_target_architecture(&interpreter_config)?;
184190
emit_cargo_configuration(&interpreter_config)?;
@@ -207,6 +213,20 @@ fn configure_pyo3() -> Result<()> {
207213
Ok(())
208214
}
209215

216+
fn print_config_and_exit(config: &InterpreterConfig) {
217+
println!("\n-- PYO3_PRINT_CONFIG=1 is set, printing configuration and halting compile --");
218+
println!("implementation: {}", config.implementation);
219+
println!("interpreter version: {}", config.version);
220+
println!("interpreter path: {:?}", config.executable);
221+
println!("libdir: {:?}", config.libdir);
222+
println!("shared: {}", config.shared);
223+
println!("base prefix: {:?}", config.base_prefix);
224+
println!("ld_version: {:?}", config.ld_version);
225+
println!("pointer width: {:?}", config.calcsize_pointer);
226+
227+
std::process::exit(101);
228+
}
229+
210230
fn main() {
211231
// Print out error messages using display, to get nicer formatting.
212232
if let Err(e) = configure_pyo3() {

guide/src/building_and_distribution.md

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,34 @@ You can override the Python interpreter by setting the `PYO3_PYTHON` environment
1717

1818
Once the Python interpreter is located, `pyo3-build-config` executes it to query the information in the `sysconfig` module which is needed to configure the rest of the compilation.
1919

20-
To validate the configuration which PyO3 will use, you can run the `print-config` binary from the `pyo3-build-config` crate. An example usage of this binary is shown below:
21-
22-
```bash
23-
# (First, clone the PyO3 git repository)
24-
$ cd path/to/pyo3/repository
25-
$ cd pyo3-build-config
26-
$ cargo run --bin print-config
27-
cargo:rerun-if-env-changed=PYO3_PYTHON
28-
cargo:rerun-if-env-changed=VIRTUAL_ENV
29-
cargo:rerun-if-env-changed=CONDA_PREFIX
30-
cargo:rerun-if-env-changed=PATH
31-
implementation: CPython
32-
interpreter version: 3.8
33-
interpreter path: Some("/usr/bin/python")
34-
libdir: Some("/usr/lib")
35-
shared: true
36-
base prefix: Some("/usr")
37-
ld_version: Some("3.8")
38-
pointer width: Some(8)
20+
To validate the configuration which PyO3 will use, you can run a compilation with the environment variable `PYO3_PRINT_CONFIG=1` set. An example output of doing this is shown below:
21+
22+
```console
23+
$ PYO3_PRINT_CONFIG=1 cargo build
24+
Compiling pyo3 v0.14.1 (/home/david/dev/pyo3)
25+
error: failed to run custom build command for `pyo3 v0.14.1 (/home/david/dev/pyo3)`
26+
27+
Caused by:
28+
process didn't exit successfully: `/home/david/dev/pyo3/target/debug/build/pyo3-7a8cf4fe22e959b7/build-script-build` (exit status: 101)
29+
--- stdout
30+
cargo:rerun-if-env-changed=PYO3_CROSS
31+
cargo:rerun-if-env-changed=PYO3_CROSS_LIB_DIR
32+
cargo:rerun-if-env-changed=PYO3_CROSS_PYTHON_VERSION
33+
cargo:rerun-if-env-changed=PYO3_PYTHON
34+
cargo:rerun-if-env-changed=VIRTUAL_ENV
35+
cargo:rerun-if-env-changed=CONDA_PREFIX
36+
cargo:rerun-if-env-changed=PATH
37+
cargo:rerun-if-env-changed=PYO3_PRINT_CONFIG
38+
39+
-- PYO3_PRINT_CONFIG=1 is set, printing configuration and halting compile --
40+
implementation: CPython
41+
interpreter version: 3.8
42+
interpreter path: Some("/usr/bin/python")
43+
libdir: Some("/usr/lib")
44+
shared: true
45+
base prefix: Some("/usr")
46+
ld_version: Some("3.8")
47+
pointer width: Some(8)
3948
```
4049

4150
## Building Python extension modules

pyo3-build-config/src/bin/print-config.rs

Lines changed: 0 additions & 16 deletions
This file was deleted.

pyo3-build-config/src/impl_.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ const ABI3_MAX_MINOR: u8 = 9;
2525
/// Gets an environment variable owned by cargo.
2626
///
2727
/// Environment variables set by cargo are expected to be valid UTF8.
28-
fn cargo_env_var(var: &str) -> Option<String> {
28+
pub fn cargo_env_var(var: &str) -> Option<String> {
2929
env::var_os(var).map(|os_string| os_string.to_str().unwrap().into())
3030
}
3131

3232
/// Gets an external environment variable, and registers the build script to rerun if
3333
/// the variable changes.
34-
fn env_var(var: &str) -> Option<OsString> {
34+
pub fn env_var(var: &str) -> Option<OsString> {
3535
println!("cargo:rerun-if-env-changed={}", var);
3636
env::var_os(var)
3737
}

pyo3-build-config/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub use impl_::{
2727

2828
// Used in PyO3's build.rs
2929
#[doc(hidden)]
30-
pub use impl_::make_interpreter_config;
30+
pub use impl_::{cargo_env_var, env_var, make_interpreter_config};
3131

3232
/// Reads the configuration written by PyO3's build.rs
3333
///

0 commit comments

Comments
 (0)