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