Skip to content

Commit

Permalink
Add abi3-py* features
Browse files Browse the repository at this point in the history
  • Loading branch information
kngwyu committed Oct 31, 2020
1 parent 3b3ba4e commit b94755c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ rustversion = "1.0"
[features]
default = ["macros"]
macros = ["ctor", "indoc", "inventory", "paste", "pyo3cls", "unindent"]
# Use the Python limited API. See https://www.python.org/dev/peps/pep-0384/ for
# more.
# Use the Python limited API. See https://www.python.org/dev/peps/pep-0384/ for more.
abi3 = []
# With abi3, we can manually set the minimum Python version.
abi3-py36 = ["abi3"]
abi3-py37 = ["abi3-py36"]
abi3-py38 = ["abi3-py37"]
abi3-py39 = ["abi3-py38"]

# Optimizes PyObject to Vec conversion and so on.
nightly = []
Expand Down
25 changes: 20 additions & 5 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use std::{
str::FromStr,
};

const PY3_MIN_MINOR: u8 = 5;
/// Minimum required Python version.
const PY3_MIN_MINOR: u8 = 6;
/// Maximum Python version that can be used as minimum required Python version with abi3.
const ABI3_MAX_MIN_MINOR: u8 = 9;
const CFG_KEY: &str = "py_sys_config";

type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
Expand Down Expand Up @@ -770,12 +773,24 @@ fn configure(interpreter_config: &InterpreterConfig) -> Result<String> {
bail!("Python 2 is not supported");
}

if env::var_os("CARGO_FEATURE_ABI3").is_some() {
println!("cargo:rustc-cfg=Py_LIMITED_API");
fn get_abi3_min_python() -> Option<u8> {
for i in PY3_MIN_MINOR..=ABI3_MAX_MIN_MINOR {
if env::var_os(format!("CARGO_FEATURE_ABI{}", i)).is_some() {
return Some(i);
}
}
None
}

if let Some(minor) = interpreter_config.version.minor {
for i in 6..=minor {
let minor = if env::var_os("CARGO_FEATURE_ABI3").is_some() {
println!("cargo:rustc-cfg=Py_LIMITED_API");
get_abi3_min_python().or(interpreter_config.version.minor)
} else {
interpreter_config.version.minor
};

if let Some(minor) = minor {
for i in PY3_MIN_MINOR..=minor {
println!("cargo:rustc-cfg=Py_3_{}", i);
flags += format!("CFG_Py_3_{},", i).as_ref();
}
Expand Down

0 comments on commit b94755c

Please sign in to comment.