1{
2 lib,
3 stdenv,
4 fetchPypi,
5 python,
6 pythonAtLeast,
7 pythonOlder,
8 buildPythonPackage,
9 writeTextFile,
10
11 # build-system
12 cython,
13 gfortran,
14 meson-python,
15 mesonEmulatorHook,
16 pkg-config,
17 xcbuild,
18
19 # native dependencies
20 blas,
21 lapack,
22
23 # Reverse dependency
24 sage,
25
26 # tests
27 hypothesis,
28 pytest-xdist,
29 pytestCheckHook,
30 setuptools,
31 typing-extensions,
32}:
33
34assert (!blas.isILP64) && (!lapack.isILP64);
35
36let
37 cfg = writeTextFile {
38 name = "site.cfg";
39 text = lib.generators.toINI { } {
40 ${blas.implementation} = {
41 include_dirs = "${lib.getDev blas}/include:${lib.getDev lapack}/include";
42 library_dirs = "${blas}/lib:${lapack}/lib";
43 runtime_library_dirs = "${blas}/lib:${lapack}/lib";
44 libraries = "lapack,lapacke,blas,cblas";
45 };
46 lapack = {
47 include_dirs = "${lib.getDev lapack}/include";
48 library_dirs = "${lapack}/lib";
49 runtime_library_dirs = "${lapack}/lib";
50 };
51 blas = {
52 include_dirs = "${lib.getDev blas}/include";
53 library_dirs = "${blas}/lib";
54 runtime_library_dirs = "${blas}/lib";
55 };
56 };
57 };
58in
59buildPythonPackage rec {
60 pname = "numpy";
61 version = "2.0.0";
62 pyproject = true;
63
64 disabled = pythonOlder "3.10";
65
66 src = fetchPypi {
67 inherit pname version;
68 extension = "tar.gz";
69 hash = "sha256-z10cnmg3+K+fkra9PobVE83BH2D9YhhcxJ7H0aujSGQ=";
70 };
71
72 patches = lib.optionals python.hasDistutilsCxxPatch [
73 # We patch cpython/distutils to fix https://bugs.python.org/issue1222585
74 # Patching of numpy.distutils is needed to prevent it from undoing the
75 # patch to distutils.
76 ./numpy-distutils-C++.patch
77 ];
78
79 postPatch = ''
80 # remove needless reference to full Python path stored in built wheel
81 substituteInPlace numpy/meson.build \
82 --replace-fail 'py.full_path()' "'python'"
83 '';
84
85 build-system =
86 [
87 cython
88 gfortran
89 meson-python
90 pkg-config
91 ]
92 ++ lib.optionals stdenv.isDarwin [ xcbuild.xcrun ]
93 ++ lib.optionals (!stdenv.buildPlatform.canExecute stdenv.hostPlatform) [ mesonEmulatorHook ];
94
95 # we default openblas to build with 64 threads
96 # if a machine has more than 64 threads, it will segfault
97 # see https://github.com/OpenMathLib/OpenBLAS/issues/2993
98 preConfigure = ''
99 sed -i 's/-faltivec//' numpy/distutils/system_info.py
100 export OMP_NUM_THREADS=$((NIX_BUILD_CORES > 64 ? 64 : NIX_BUILD_CORES))
101 '';
102
103 # HACK: copy mesonEmulatorHook's flags to the variable used by meson-python
104 postConfigure = ''
105 mesonFlags="$mesonFlags ''${mesonFlagsArray[@]}"
106 '';
107
108 buildInputs = [
109 blas
110 lapack
111 ];
112
113 preBuild = ''
114 ln -s ${cfg} site.cfg
115 '';
116
117 enableParallelBuilding = true;
118
119 nativeCheckInputs = [
120 hypothesis
121 pytestCheckHook
122 pytest-xdist
123 setuptools
124 typing-extensions
125 ];
126
127 preCheck = ''
128 pushd $out
129 '';
130
131 postCheck = ''
132 popd
133 '';
134
135 # https://github.com/numpy/numpy/blob/a277f6210739c11028f281b8495faf7da298dbef/numpy/_pytesttester.py#L180
136 pytestFlagsArray = [
137 "-m"
138 "not\\ slow" # fast test suite
139 ];
140
141 disabledTests =
142 lib.optionals (pythonAtLeast "3.13") [
143 # https://github.com/numpy/numpy/issues/26713
144 "test_iter_refcount"
145 ]
146 ++ lib.optionals stdenv.isAarch32 [
147 # https://github.com/numpy/numpy/issues/24548
148 "test_impossible_feature_enable" # AssertionError: Failed to generate error
149 "test_features" # AssertionError: Failure Detection
150 "test_new_policy" # AssertionError: assert False
151 "test_identityless_reduction_huge_array" # ValueError: Maximum allowed dimension exceeded
152 "test_unary_spurious_fpexception" # AssertionError: Got warnings: [<warnings.WarningMessage object at 0xd1197430>]
153 "test_int" # AssertionError: selectedintkind(19): expected 16 but got -1
154 "test_real" # AssertionError: selectedrealkind(16): expected 10 but got -1
155 "test_quad_precision" # AssertionError: selectedrealkind(32): expected 16 but got -1
156 "test_big_arrays" # ValueError: array is too big; `arr.size * arr.dtype.itemsize` is larger tha...
157 "test_multinomial_pvals_float32" # Failed: DID NOT RAISE <class 'ValueError'>
158 ]
159 ++ lib.optionals (stdenv.isDarwin && stdenv.isx86_64) [
160 # AssertionError: (np.int64(0), np.longdouble('9.9999999999999994515e-21'), np.longdouble('3.9696755572509052902e+20'), 'arctanh')
161 "test_loss_of_precision"
162 ];
163
164 passthru = {
165 # just for backwards compatibility
166 blas = blas.provider;
167 blasImplementation = blas.implementation;
168 inherit cfg;
169 tests = {
170 inherit sage;
171 };
172 };
173
174 meta = {
175 changelog = "https://github.com/numpy/numpy/releases/tag/v${version}";
176 description = "Scientific tools for Python";
177 homepage = "https://numpy.org/";
178 license = lib.licenses.bsd3;
179 };
180}