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 = "1.26.4";
62 pyproject = true;
63 disabled = pythonOlder "3.9" || pythonAtLeast "3.13";
64
65 src = fetchPypi {
66 inherit pname version;
67 extension = "tar.gz";
68 hash = "sha256-KgKrqe0S5KxOs+qUIcQgMBoMZGDZgw10qd+H76SRIBA=";
69 };
70
71 patches =
72 [
73 # Disable `numpy/core/tests/test_umath.py::TestComplexFunctions::test_loss_of_precision[complex256]`
74 # on x86_64-darwin because it fails under Rosetta 2 due to issues with trig functions and
75 # 80-bit long double complex numbers.
76 ./disable-failing-long-double-test-Rosetta-2.patch
77 ]
78 # We patch cpython/distutils to fix https://bugs.python.org/issue1222585
79 # Patching of numpy.distutils is needed to prevent it from undoing the
80 # patch to distutils.
81 ++ lib.optionals python.hasDistutilsCxxPatch [ ./numpy-distutils-C++.patch ];
82
83 postPatch = ''
84 # fails with multiple errors because we are not using the pinned setuptools version
85 # see https://github.com/numpy/numpy/blob/v1.25.0/pyproject.toml#L7
86 # error: option --single-version-externally-managed not recognized
87 # TypeError: dist must be a Distribution instance
88 rm numpy/core/tests/test_cython.py
89
90 patchShebangs numpy/_build_utils/*.py
91
92 # remove needless reference to full Python path stored in built wheel
93 substituteInPlace numpy/meson.build \
94 --replace 'py.full_path()' "'python'"
95
96 substituteInPlace pyproject.toml \
97 --replace-fail "meson-python>=0.15.0,<0.16.0" "meson-python"
98 '';
99
100 nativeBuildInputs =
101 [
102 cython
103 gfortran
104 meson-python
105 pkg-config
106 ]
107 ++ lib.optionals (stdenv.isDarwin) [ xcbuild.xcrun ]
108 ++ lib.optionals (!stdenv.buildPlatform.canExecute stdenv.hostPlatform) [ mesonEmulatorHook ];
109
110 buildInputs = [
111 blas
112 lapack
113 ];
114
115 # Causes `error: argument unused during compilation: '-fno-strict-overflow'` due to `-Werror`.
116 hardeningDisable = lib.optionals stdenv.cc.isClang [ "strictoverflow" ];
117
118 # we default openblas to build with 64 threads
119 # if a machine has more than 64 threads, it will segfault
120 # see https://github.com/OpenMathLib/OpenBLAS/issues/2993
121 preConfigure = ''
122 sed -i 's/-faltivec//' numpy/distutils/system_info.py
123 export OMP_NUM_THREADS=$((NIX_BUILD_CORES > 64 ? 64 : NIX_BUILD_CORES))
124 '';
125
126 # HACK: copy mesonEmulatorHook's flags to the variable used by meson-python
127 postConfigure = ''
128 mesonFlags="$mesonFlags ''${mesonFlagsArray[@]}"
129 '';
130
131 preBuild = ''
132 ln -s ${cfg} site.cfg
133 '';
134
135 enableParallelBuilding = true;
136
137 nativeCheckInputs = [
138 pytest-xdist
139 pytestCheckHook
140 hypothesis
141 setuptools
142 typing-extensions
143 ];
144
145 preCheck = ''
146 cd "$out"
147 '';
148
149 # https://github.com/numpy/numpy/blob/a277f6210739c11028f281b8495faf7da298dbef/numpy/_pytesttester.py#L180
150 pytestFlagsArray = [
151 "-m"
152 "not\\ slow" # fast test suite
153 ];
154
155 # https://github.com/numpy/numpy/issues/24548
156 disabledTests =
157 lib.optionals stdenv.isi686 [
158 "test_new_policy" # AssertionError: assert False
159 "test_identityless_reduction_huge_array" # ValueError: Maximum allowed dimension exceeded
160 "test_float_remainder_overflow" # AssertionError: FloatingPointError not raised by divmod
161 "test_int" # AssertionError: selectedintkind(19): expected 16 but got -1
162 ]
163 ++ lib.optionals stdenv.isAarch32 [
164 "test_impossible_feature_enable" # AssertionError: Failed to generate error
165 "test_features" # AssertionError: Failure Detection
166 "test_new_policy" # AssertionError: assert False
167 "test_identityless_reduction_huge_array" # ValueError: Maximum allowed dimension exceeded
168 "test_unary_spurious_fpexception" # AssertionError: Got warnings: [<warnings.WarningMessage object at 0xd1197430>]
169 "test_int" # AssertionError: selectedintkind(19): expected 16 but got -1
170 "test_real" # AssertionError: selectedrealkind(16): expected 10 but got -1
171 "test_quad_precision" # AssertionError: selectedrealkind(32): expected 16 but got -1
172 "test_big_arrays" # ValueError: array is too big; `arr.size * arr.dtype.itemsize` is larger tha...
173 "test_multinomial_pvals_float32" # Failed: DID NOT RAISE <class 'ValueError'>
174 ]
175 ++ lib.optionals stdenv.isAarch64 [
176 "test_big_arrays" # OOM on a 16G machine
177 ]
178 ++ lib.optionals (stdenv.isDarwin && stdenv.isx86_64) [
179 # can fail on virtualized machines confused over their cpu identity
180 "test_dispatcher"
181 ];
182
183 passthru = {
184 # just for backwards compatibility
185 blas = blas.provider;
186 blasImplementation = blas.implementation;
187 inherit cfg;
188 tests = {
189 inherit sage;
190 };
191 };
192
193 # Disable test
194 # - test_large_file_support: takes a long time and can cause the machine to run out of disk space
195 env.NOSE_EXCLUDE = "test_large_file_support";
196
197 meta = {
198 changelog = "https://github.com/numpy/numpy/releases/tag/v${version}";
199 description = "Scientific tools for Python";
200 mainProgram = "f2py";
201 homepage = "https://numpy.org/";
202 license = lib.licenses.bsd3;
203 };
204}