nixpkgs/pkgs/development/python-modules/numpy/2.nix

223 lines
7.1 KiB
Nix

{
lib,
stdenv,
fetchFromGitHub,
python,
pythonAtLeast,
buildPythonPackage,
writeTextFile,
# build-system
cython,
gfortran,
meson-python,
mesonEmulatorHook,
pkg-config,
# native dependencies
blas,
coreutils,
lapack,
openmpCheckPhaseHook,
# Reverse dependency
sage,
# tests
hypothesis,
pytest-xdist,
pytestCheckHook,
setuptools,
typing-extensions,
}:
# Verify these are compatible
assert blas.isILP64 == lapack.isILP64;
buildPythonPackage (finalAttrs: {
pname = "numpy";
version = "2.5.0";
pyproject = true;
src = fetchFromGitHub {
owner = "numpy";
repo = "numpy";
tag = "v${finalAttrs.version}";
fetchSubmodules = true;
hash = "sha256-RiC1dLoDamK5B2VzHBL0V//K/Vix25q11wNGcl3Witk=";
};
patches = lib.optionals python.hasDistutilsCxxPatch [
# We patch cpython/distutils to fix https://bugs.python.org/issue1222585
# Patching of numpy.distutils is needed to prevent it from undoing the
# patch to distutils.
./numpy-distutils-C++.patch
];
postPatch = ''
# remove needless reference to full Python path stored in built wheel
substituteInPlace numpy/meson.build \
--replace-fail 'py.full_path()' "'python'"
# Test_POWER_Features::test_features - FileNotFoundError: [Errno 2] No such file or directory: '/bin/true'
substituteInPlace numpy/_core/tests/test_cpu_features.py \
--replace-fail '/bin/true' '${lib.getExe' coreutils "true"}'
'';
mesonFlags = [
# See https://numpy.org/devdocs/building/blas_lapack.html
"-Duse-ilp64=${if blas.isILP64 then "true" else "false"}"
]
++ lib.optionals (!stdenv.hostPlatform.isLoongArch64) [
# This is required to support CPUs with feature-sets earlier than x86-64-v2
# (before 2009). This will still build optimizations for newer features, but
# allow for importing with older machines. See:
#
# - https://github.com/NixOS/nixpkgs/issues/496822
# - https://numpy.org/devdocs/reference/simd/build-options.html
# - https://github.com/numpy/numpy/issues/31073
#
# NOTE: It is possible to enable CPU features based upon attributes defined
# in `lib/systems/architectures.nix`, but that might trigger tons of
# rebuilds on old x86_64 CPU machines, and it will be too complex for
# maintenance.
(lib.mesonOption "cpu-baseline" "none")
];
build-system = [
cython
gfortran
meson-python
pkg-config
]
++ lib.optionals (!stdenv.buildPlatform.canExecute stdenv.hostPlatform) [ mesonEmulatorHook ];
buildInputs = [
blas
lapack
];
preBuild = ''
ln -s ${finalAttrs.finalPackage.passthru.cfg} site.cfg
'';
enableParallelBuilding = true;
nativeCheckInputs = [
hypothesis
pytestCheckHook
pytest-xdist
setuptools
typing-extensions
];
# Enables any dependent package to easily find numpy.pc via standard
# pkg-config call. The `$out/include` symlink matches what's written in the
# numpy.pc file.
postInstall = ''
ln -s $out/${python.sitePackages}/numpy/_core/lib/pkgconfig $out/lib/pkgconfig
ln -s ${placeholder "out"}/${finalAttrs.passthru.coreIncludeInnerDir} $out/include
'';
propagatedNativeBuildInputs = [
openmpCheckPhaseHook
];
preCheck = ''
pushd $out
# For numpy-config executable to be available during tests
export PATH=$PATH:$out/bin
'';
postCheck = ''
popd
'';
# https://github.com/numpy/numpy/blob/a277f6210739c11028f281b8495faf7da298dbef/numpy/_pytesttester.py#L180
disabledTestMarks = [
"slow" # fast test suite
];
disabledTests = [
# Tries to import numpy.distutils.msvccompiler, removed in setuptools 74.0
"test_api_importable"
]
++ lib.optionals (pythonAtLeast "3.13") [
# https://github.com/numpy/numpy/issues/26713
"test_iter_refcount"
]
++ lib.optionals stdenv.hostPlatform.isAarch32 [
# https://github.com/numpy/numpy/issues/24548
"test_impossible_feature_enable" # AssertionError: Failed to generate error
"test_features" # AssertionError: Failure Detection
"test_new_policy" # AssertionError: assert False
"test_identityless_reduction_huge_array" # ValueError: Maximum allowed dimension exceeded
"test_unary_spurious_fpexception" # AssertionError: Got warnings: [<warnings.WarningMessage object at 0xd1197430>]
"test_int" # AssertionError: selectedintkind(19): expected 16 but got -1
"test_real" # AssertionError: selectedrealkind(16): expected 10 but got -1
"test_quad_precision" # AssertionError: selectedrealkind(32): expected 16 but got -1
"test_big_arrays" # ValueError: array is too big; `arr.size * arr.dtype.itemsize` is larger tha...
"test_multinomial_pvals_float32" # Failed: DID NOT RAISE <class 'ValueError'>
]
++ lib.optionals stdenv.hostPlatform.isRiscV64 [
"test_floor_division_errors" # FloatingPointError: invalid value encountered in floor_divide
"test_unary_spurious_fpexception" # AssertionError: Got warnings: [<warnings.WarningMessage ...>]
]
++ lib.optionals (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isx86_64) [
# AssertionError: (np.int64(0), np.longdouble('9.9999999999999994515e-21'), np.longdouble('3.9696755572509052902e+20'), 'arctanh')
"test_loss_of_precision"
]
++ lib.optionals (stdenv.hostPlatform.isPower64 && stdenv.hostPlatform.isBigEndian) [
# https://github.com/numpy/numpy/issues/29918
"test_sq_cases"
]
++ lib.optionals (stdenv.hostPlatform ? gcc.arch) [
# remove if https://github.com/numpy/numpy/issues/27460 is resolved
"test_validate_transcendentals"
];
passthru = {
# just for backwards compatibility
blas = blas.provider;
blasImplementation = blas.implementation;
buildConfig = {
${blas.implementation} = {
include_dirs = "${lib.getDev blas}/include:${lib.getDev lapack}/include";
library_dirs = "${blas}/lib:${lapack}/lib";
runtime_library_dirs = "${blas}/lib:${lapack}/lib";
libraries = "lapack,lapacke,blas,cblas";
};
lapack = {
include_dirs = "${lib.getDev lapack}/include";
library_dirs = "${lapack}/lib";
runtime_library_dirs = "${lapack}/lib";
};
blas = {
include_dirs = "${lib.getDev blas}/include";
library_dirs = "${blas}/lib";
runtime_library_dirs = "${blas}/lib";
};
};
cfg = writeTextFile {
name = "site.cfg";
text = lib.generators.toINI { } finalAttrs.finalPackage.buildConfig;
};
# Need to be defined with two variables for postInstall to use `placeholder
# "out"` where here we have to use `${finalAttrs.finalPackage}`, that would
# cause infinite recursion if used in postInstall too.
coreIncludeInnerDir = "${python.sitePackages}/numpy/_core/include";
coreIncludeDir = "${finalAttrs.finalPackage}/${finalAttrs.finalPackage.passthru.coreIncludeInnerDir}";
tests = {
inherit sage;
};
};
meta = {
changelog = "https://github.com/numpy/numpy/releases/tag/${finalAttrs.src.tag}";
description = "Scientific tools for Python";
homepage = "https://numpy.org/";
license = lib.licenses.bsd3;
maintainers = with lib.maintainers; [ doronbehar ];
};
})