Skip to content

Commit 2ccf1d0

Browse files
committed
Build configuration to select the target API version
1 parent e5ed4a9 commit 2ccf1d0

File tree

6 files changed

+216
-2
lines changed

6 files changed

+216
-2
lines changed

Cargo.toml

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ authors = [
77
]
88
description = "PyO3-based Rust bindings of the NumPy C-API"
99
documentation = "https://docs.rs/numpy"
10-
edition = "2021"
11-
rust-version = "1.83"
10+
edition.workspace = true
11+
rust-version.workspace = true
1212
repository = "https://github.com/PyO3/rust-numpy"
1313
categories = ["api-bindings", "development-tools::ffi", "science"]
1414
keywords = ["python", "numpy", "ffi", "pyo3"]
@@ -20,6 +20,29 @@ exclude = [
2020
"x.py",
2121
]
2222

23+
[features]
24+
default = ["target-npy119"]
25+
26+
# Default and minimum supported version are chosen to match the content of
27+
# header `numpy/_core/include/numpy/numpyconfig.h`` in the first available
28+
# version of numpy v2.
29+
target-npy115 = ["numpy-build-config/target-npy115"]
30+
target-npy116 = ["numpy-build-config/target-npy116"]
31+
target-npy117 = ["numpy-build-config/target-npy117"]
32+
target-npy118 = ["numpy-build-config/target-npy118"]
33+
target-npy119 = ["numpy-build-config/target-npy119"]
34+
target-npy120 = ["numpy-build-config/target-npy120"]
35+
target-npy121 = ["numpy-build-config/target-npy121"]
36+
target-npy122 = ["numpy-build-config/target-npy122"]
37+
target-npy123 = ["numpy-build-config/target-npy123"]
38+
target-npy124 = ["numpy-build-config/target-npy124"]
39+
target-npy125 = ["numpy-build-config/target-npy125"]
40+
target-npy20 = ["numpy-build-config/target-npy20"]
41+
target-npy21 = ["numpy-build-config/target-npy21"]
42+
target-npy22 = ["numpy-build-config/target-npy22"]
43+
target-npy23 = ["numpy-build-config/target-npy23"]
44+
target-npy24 = ["numpy-build-config/target-npy24"]
45+
2346
[dependencies]
2447
half = { version = "2.0", default-features = false, optional = true }
2548
libc = "0.2"
@@ -37,6 +60,7 @@ nalgebra = { version = ">=0.30, <0.35", default-features = false, features = ["s
3760

3861
[build-dependencies]
3962
pyo3-build-config = { version = "0.28", features = ["resolve-config"]}
63+
numpy-build-config = { path = "numpy-build-config", version = "0.28.0" }
4064

4165
[package.metadata.docs.rs]
4266
all-features = true
@@ -47,3 +71,10 @@ elided-lifetimes-in-paths = "deny"
4771

4872
[lints.clippy]
4973
needless-lifetimes = "allow"
74+
75+
[workspace.package]
76+
edition = "2021"
77+
rust-version = "1.83"
78+
79+
[workspace]
80+
members = ["numpy-build-config"]

build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
fn main() {
22
pyo3_build_config::use_pyo3_cfgs();
3+
numpy_build_config::use_numpy_cfgs();
34
}

numpy-build-config/Cargo.toml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[package]
2+
name = "numpy-build-config"
3+
version = "0.28.0"
4+
authors = [
5+
"The rust-numpy Project Developers",
6+
"PyO3 Project and Contributors <https://github.com/PyO3>",
7+
]
8+
description = "Build configuration for the numpy crate"
9+
edition = "2021"
10+
rust-version = "1.83"
11+
repository = "https://github.com/PyO3/rust-numpy"
12+
license = "BSD-2-Clause"
13+
14+
[dependencies]
15+
16+
[features]
17+
default = []
18+
target-npy115 = []
19+
target-npy116 = ["target-npy115"]
20+
target-npy117 = ["target-npy116"]
21+
target-npy118 = ["target-npy117"]
22+
target-npy119 = ["target-npy118"]
23+
target-npy120 = ["target-npy119"]
24+
target-npy121 = ["target-npy120"]
25+
target-npy122 = ["target-npy121"]
26+
target-npy123 = ["target-npy122"]
27+
target-npy124 = ["target-npy123"]
28+
target-npy125 = ["target-npy124"]
29+
target-npy20 = ["target-npy125"]
30+
target-npy21 = ["target-npy20"]
31+
target-npy22 = ["target-npy21"]
32+
target-npy23 = ["target-npy22"]
33+
target-npy24 = ["target-npy23"]

numpy-build-config/LICENSE

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
BSD 2-Clause License
2+
3+
Copyright (c) 2017, Toshiki Teramura
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

numpy-build-config/src/impl_.rs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#[derive(Debug, Clone, Copy)]
2+
pub struct NumpyVersion {
3+
pub minor: u32,
4+
pub major: u32,
5+
}
6+
7+
#[allow(non_snake_case)]
8+
impl NumpyVersion {
9+
const fn V1(minor: u32) -> Self {
10+
Self { major: 1, minor }
11+
}
12+
const fn V2(minor: u32) -> Self {
13+
Self { major: 2, minor }
14+
}
15+
}
16+
17+
impl NumpyVersion {
18+
/// An iterator over supported versions of numpy API.
19+
pub fn supported() -> impl Iterator<Item = Self> {
20+
SUPPORTED_VERSIONS.iter().copied()
21+
}
22+
23+
/// An iterator over enabled versions of numpy API.
24+
pub fn enabled() -> impl Iterator<Item = Self> {
25+
ENABLED_VERSIONS.iter().copied()
26+
}
27+
}
28+
29+
const SUPPORTED_VERSIONS: &[NumpyVersion] = &[
30+
NumpyVersion::V1(15),
31+
NumpyVersion::V1(16),
32+
NumpyVersion::V1(17),
33+
NumpyVersion::V1(18),
34+
NumpyVersion::V1(19),
35+
NumpyVersion::V1(20),
36+
NumpyVersion::V1(21),
37+
NumpyVersion::V1(22),
38+
NumpyVersion::V1(23),
39+
NumpyVersion::V1(24),
40+
NumpyVersion::V1(25),
41+
NumpyVersion::V2(0),
42+
NumpyVersion::V2(1),
43+
NumpyVersion::V2(2),
44+
NumpyVersion::V2(3),
45+
NumpyVersion::V2(4),
46+
];
47+
48+
const ENABLED_VERSIONS: &[NumpyVersion] = &[
49+
#[cfg(feature = "target-npy115")]
50+
NumpyVersion::V1(15), // 0x0000000c
51+
#[cfg(any(
52+
feature = "target-npy116",
53+
feature = "target-npy117",
54+
feature = "target-npy118",
55+
feature = "target-npy119"
56+
))]
57+
NumpyVersion::V1(16), // 0x0000000d
58+
#[cfg(any(
59+
feature = "target-npy116",
60+
feature = "target-npy117",
61+
feature = "target-npy118",
62+
feature = "target-npy119"
63+
))]
64+
NumpyVersion::V1(17), // 0x0000000d
65+
#[cfg(any(
66+
feature = "target-npy116",
67+
feature = "target-npy117",
68+
feature = "target-npy118",
69+
feature = "target-npy119"
70+
))]
71+
NumpyVersion::V1(18), // 0x0000000d
72+
#[cfg(any(
73+
feature = "target-npy116",
74+
feature = "target-npy117",
75+
feature = "target-npy118",
76+
feature = "target-npy119"
77+
))]
78+
NumpyVersion::V1(19), // 0x0000000d
79+
#[cfg(any(feature = "target-npy120", feature = "target-npy121"))]
80+
NumpyVersion::V1(20), // 0x0000000e
81+
#[cfg(any(feature = "target-npy120", feature = "target-npy121"))]
82+
NumpyVersion::V1(21), // 0x0000000e
83+
#[cfg(feature = "target-npy122")]
84+
NumpyVersion::V1(22), // 0x0000000f
85+
#[cfg(any(feature = "target-npy123", feature = "target-npy124"))]
86+
NumpyVersion::V1(23), // 0x00000010
87+
#[cfg(any(feature = "target-npy123", feature = "target-npy124"))]
88+
NumpyVersion::V1(24), // 0x00000010
89+
#[cfg(feature = "target-npy125")]
90+
NumpyVersion::V1(25), // 0x00000011
91+
#[cfg(feature = "target-npy20")]
92+
NumpyVersion::V2(0), // 0x00000012
93+
#[cfg(any(feature = "target-npy21", feature = "target-npy22"))]
94+
NumpyVersion::V2(1), // 0x00000013
95+
#[cfg(any(feature = "target-npy21", feature = "target-npy22"))]
96+
NumpyVersion::V2(2), // 0x00000013
97+
#[cfg(feature = "target-npy23")]
98+
NumpyVersion::V2(3), // 0x00000014
99+
#[cfg(feature = "target-npy24")]
100+
NumpyVersion::V2(4), // 0x00000015
101+
];

numpy-build-config/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use self::impl_::NumpyVersion;
2+
3+
mod impl_;
4+
5+
pub fn use_numpy_cfgs() {
6+
print_expected_features();
7+
print_enabled_features();
8+
}
9+
10+
fn print_expected_features() {
11+
for version in NumpyVersion::supported() {
12+
println!(
13+
"cargo:rustc-check-cfg=cfg(Numpy_{}_{})",
14+
version.major, version.minor
15+
);
16+
}
17+
}
18+
19+
fn print_enabled_features() {
20+
for version in NumpyVersion::enabled() {
21+
println!("cargo:rustc-cfg=Numpy_{}_{}", version.major, version.minor);
22+
}
23+
}

0 commit comments

Comments
 (0)