Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

- Unreleased
- Adjust struct definitions to match the selected NumPy C API version (#534)
- Add features to select the NumPy C API version (#534)
- Fix PyArray_DTypeMeta definition when Py_LIMITED_API is disabled (#532)

- v0.28.0
Expand Down
35 changes: 33 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ authors = [
]
description = "PyO3-based Rust bindings of the NumPy C-API"
documentation = "https://docs.rs/numpy"
edition = "2021"
rust-version = "1.83"
edition.workspace = true
rust-version.workspace = true
repository = "https://github.com/PyO3/rust-numpy"
categories = ["api-bindings", "development-tools::ffi", "science"]
keywords = ["python", "numpy", "ffi", "pyo3"]
Expand All @@ -20,6 +20,29 @@ exclude = [
"x.py",
]

[features]
default = ["target-npy119"]

# Default and minimum supported version are chosen to match the content of
# header `numpy/_core/include/numpy/numpyconfig.h`` in the first available
# version of numpy v2.
target-npy115 = ["numpy-build-config/target-npy115"]
target-npy116 = ["numpy-build-config/target-npy116"]
target-npy117 = ["numpy-build-config/target-npy117"]
target-npy118 = ["numpy-build-config/target-npy118"]
target-npy119 = ["numpy-build-config/target-npy119"]
target-npy120 = ["numpy-build-config/target-npy120"]
target-npy121 = ["numpy-build-config/target-npy121"]
target-npy122 = ["numpy-build-config/target-npy122"]
target-npy123 = ["numpy-build-config/target-npy123"]
target-npy124 = ["numpy-build-config/target-npy124"]
target-npy125 = ["numpy-build-config/target-npy125"]
target-npy20 = ["numpy-build-config/target-npy20"]
target-npy21 = ["numpy-build-config/target-npy21"]
target-npy22 = ["numpy-build-config/target-npy22"]
target-npy23 = ["numpy-build-config/target-npy23"]
target-npy24 = ["numpy-build-config/target-npy24"]

[dependencies]
half = { version = "2.0", default-features = false, optional = true }
libc = "0.2"
Expand All @@ -37,6 +60,7 @@ nalgebra = { version = ">=0.30, <0.35", default-features = false, features = ["s

[build-dependencies]
pyo3-build-config = { version = "0.28", features = ["resolve-config"]}
numpy-build-config = { path = "numpy-build-config", version = "0.28.0" }

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

[lints.clippy]
needless-lifetimes = "allow"

[workspace.package]
edition = "2021"
rust-version = "1.83"

[workspace]
members = ["numpy-build-config"]
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
fn main() {
pyo3_build_config::use_pyo3_cfgs();
numpy_build_config::use_numpy_cfgs();
}
59 changes: 20 additions & 39 deletions examples/linalg/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions numpy-build-config/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "numpy-build-config"
version = "0.28.0"
authors = [
"The rust-numpy Project Developers",
"PyO3 Project and Contributors <https://github.com/PyO3>",
]
description = "Build configuration for the numpy crate"
edition = "2021"
rust-version = "1.83"
repository = "https://github.com/PyO3/rust-numpy"
license = "BSD-2-Clause"

[dependencies]

[features]
default = []
target-npy115 = []
target-npy116 = ["target-npy115"]
target-npy117 = ["target-npy116"]
target-npy118 = ["target-npy117"]
target-npy119 = ["target-npy118"]
target-npy120 = ["target-npy119"]
target-npy121 = ["target-npy120"]
target-npy122 = ["target-npy121"]
target-npy123 = ["target-npy122"]
target-npy124 = ["target-npy123"]
target-npy125 = ["target-npy124"]
target-npy20 = ["target-npy125"]
target-npy21 = ["target-npy20"]
target-npy22 = ["target-npy21"]
target-npy23 = ["target-npy22"]
target-npy24 = ["target-npy23"]
25 changes: 25 additions & 0 deletions numpy-build-config/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
BSD 2-Clause License

Copyright (c) 2017, Toshiki Teramura
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
101 changes: 101 additions & 0 deletions numpy-build-config/src/impl_.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#[derive(Debug, Clone, Copy)]
pub struct NumpyVersion {
pub minor: u32,
pub major: u32,
}

#[allow(non_snake_case)]
impl NumpyVersion {
const fn V1(minor: u32) -> Self {
Self { major: 1, minor }
}
const fn V2(minor: u32) -> Self {
Self { major: 2, minor }
}
}

impl NumpyVersion {
/// An iterator over supported versions of numpy API.
pub fn supported() -> impl Iterator<Item = Self> {
SUPPORTED_VERSIONS.iter().copied()
}

/// An iterator over enabled versions of numpy API.
pub fn enabled() -> impl Iterator<Item = Self> {
ENABLED_VERSIONS.iter().copied()
}
}

const SUPPORTED_VERSIONS: &[NumpyVersion] = &[
NumpyVersion::V1(15),
NumpyVersion::V1(16),
NumpyVersion::V1(17),
NumpyVersion::V1(18),
NumpyVersion::V1(19),
NumpyVersion::V1(20),
NumpyVersion::V1(21),
NumpyVersion::V1(22),
NumpyVersion::V1(23),
NumpyVersion::V1(24),
NumpyVersion::V1(25),
NumpyVersion::V2(0),
NumpyVersion::V2(1),
NumpyVersion::V2(2),
NumpyVersion::V2(3),
NumpyVersion::V2(4),
];

const ENABLED_VERSIONS: &[NumpyVersion] = &[
#[cfg(feature = "target-npy115")]
NumpyVersion::V1(15), // 0x0000000c
#[cfg(any(
feature = "target-npy116",
feature = "target-npy117",
feature = "target-npy118",
feature = "target-npy119"
))]
NumpyVersion::V1(16), // 0x0000000d
#[cfg(any(
feature = "target-npy116",
feature = "target-npy117",
feature = "target-npy118",
feature = "target-npy119"
))]
NumpyVersion::V1(17), // 0x0000000d
#[cfg(any(
feature = "target-npy116",
feature = "target-npy117",
feature = "target-npy118",
feature = "target-npy119"
))]
NumpyVersion::V1(18), // 0x0000000d
#[cfg(any(
feature = "target-npy116",
feature = "target-npy117",
feature = "target-npy118",
feature = "target-npy119"
))]
NumpyVersion::V1(19), // 0x0000000d
#[cfg(any(feature = "target-npy120", feature = "target-npy121"))]
NumpyVersion::V1(20), // 0x0000000e
#[cfg(any(feature = "target-npy120", feature = "target-npy121"))]
NumpyVersion::V1(21), // 0x0000000e
#[cfg(feature = "target-npy122")]
NumpyVersion::V1(22), // 0x0000000f
#[cfg(any(feature = "target-npy123", feature = "target-npy124"))]
NumpyVersion::V1(23), // 0x00000010
#[cfg(any(feature = "target-npy123", feature = "target-npy124"))]
NumpyVersion::V1(24), // 0x00000010
#[cfg(feature = "target-npy125")]
NumpyVersion::V1(25), // 0x00000011
#[cfg(feature = "target-npy20")]
NumpyVersion::V2(0), // 0x00000012
#[cfg(any(feature = "target-npy21", feature = "target-npy22"))]
NumpyVersion::V2(1), // 0x00000013
#[cfg(any(feature = "target-npy21", feature = "target-npy22"))]
NumpyVersion::V2(2), // 0x00000013
#[cfg(feature = "target-npy23")]
NumpyVersion::V2(3), // 0x00000014
#[cfg(feature = "target-npy24")]
NumpyVersion::V2(4), // 0x00000015
];
Loading
Loading