Skip to content

Provide a way for downstream crates to access the build configuration #1548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
davidhewitt opened this issue Apr 7, 2021 · 2 comments
Closed

Comments

@davidhewitt
Copy link
Member

In examples/pyo3-pytests we need to detect the Python version and PyPy so that some tests can be conditionally compiled. We do this by invoking the Python interpreter:

use std::process::Command;
fn main() {
let out = Command::new("python")
.args(&["-c", "import sys; import platform; print(sys.version_info[1]); print(platform.python_implementation())"])
.output()
.expect("python version did not print");
let output = String::from_utf8_lossy(&out.stdout);
let mut lines = output.trim().lines();
println!("{}", output);
let version: u8 = lines
.next()
.unwrap()
.parse()
.expect("python version was not parsed");
let implementation = lines.next().unwrap();
for each in 6..version {
println!("cargo:rustc-cfg=Py_3_{}", each);
}
if implementation == "PyPy" {
println!("cargo:rustc-cfg=PyPy");
}
}

This has some downsides that we should try to fix:

  • It's using different logic to detect Python compared to PyO3's main build.rs, which could lead to misconfiguration in certain situations. (e.g. it doesn't check PYO3_PYTHON, nor will it work when cross-compiling.)
  • Users have to copy this pattern by hand into every crate which needs to support multiple Python versions.

e.g. I've seen a similar trick in the wild in orjson (which is where I borrowed this from) https://github.com/ijl/orjson/blob/master/build.rs

I think we can solve this by splitting out a lot of PyO3's build.rs into a new crate pyo3-build-config which we would use in [build-dependencies]. Downstream crates like pyo3-pytests and orjson could then use this crate also in [build-dependencies] to access the exact same configuration which PyO3 has loaded in their own build.rs files.

I've started playing around with something of this form; this issue is a reminder for me to finish it off.

@davidhewitt
Copy link
Member Author

cc @m-ou-se I wonder if this would "resolve" m-ou-se/inline-python#27 by making it easier for inline-python to request linking to libpython in its build script. (I'm thinking with this change, inline-python's build script could access the pyo3 build config.)

@davidhewitt
Copy link
Member Author

Resolved in #1622

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant