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 Nov 12, 2020
1 parent 3b3ba4e commit 9895282
Show file tree
Hide file tree
Showing 2 changed files with 20 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
19 changes: 14 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_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,18 @@ fn configure(interpreter_config: &InterpreterConfig) -> Result<String> {
bail!("Python 2 is not supported");
}

if env::var_os("CARGO_FEATURE_ABI3").is_some() {
let minor = if env::var_os("CARGO_FEATURE_ABI3").is_some() {
println!("cargo:rustc-cfg=Py_LIMITED_API");
}
// Check any `abi3-py3*` feature is set. If not, use the interpreter version.
(PY3_MIN_MINOR..=ABI3_MAX_MINOR)
.find_map(|i| env::var_os(format!("CARGO_FEATURE_ABI3_PY3{}", i)).map(|_| i))
.or(interpreter_config.version.minor)
} else {
interpreter_config.version.minor
};

if let Some(minor) = interpreter_config.version.minor {
for i in 6..=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 9895282

Please sign in to comment.