1{ lib
2, fetchPypi
3, python
4, buildPythonPackage
5, gfortran
6, hypothesis
7, pytest
8, typing-extensions
9, blas
10, lapack
11, writeTextFile
12, cython
13, pythonOlder
14}:
15
16assert (!blas.isILP64) && (!lapack.isILP64);
17
18let
19 cfg = writeTextFile {
20 name = "site.cfg";
21 text = (lib.generators.toINI {} {
22 ${blas.implementation} = {
23 include_dirs = "${lib.getDev blas}/include:${lib.getDev lapack}/include";
24 library_dirs = "${blas}/lib:${lapack}/lib";
25 runtime_library_dirs = "${blas}/lib:${lapack}/lib";
26 libraries = "lapack,lapacke,blas,cblas";
27 };
28 lapack = {
29 include_dirs = "${lib.getDev lapack}/include";
30 library_dirs = "${lapack}/lib";
31 runtime_library_dirs = "${lapack}/lib";
32 };
33 blas = {
34 include_dirs = "${lib.getDev blas}/include";
35 library_dirs = "${blas}/lib";
36 runtime_library_dirs = "${blas}/lib";
37 };
38 });
39 };
40in buildPythonPackage rec {
41 pname = "numpy";
42 version = "1.24.2";
43 format = "setuptools";
44 disabled = pythonOlder "3.7";
45
46 src = fetchPypi {
47 inherit pname version;
48 extension = "tar.gz";
49 hash = "sha256-ADqfUw6IDLLNF3y6GvciC5qkLe+cSvwqL8Pua+frKyI=";
50 };
51
52 patches = lib.optionals python.hasDistutilsCxxPatch [
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 ./numpy-distutils-C++.patch
57 ];
58
59 nativeBuildInputs = [ gfortran cython ];
60 buildInputs = [ blas lapack ];
61
62 # we default openblas to build with 64 threads
63 # if a machine has more than 64 threads, it will segfault
64 # see https://github.com/xianyi/OpenBLAS/issues/2993
65 preConfigure = ''
66 sed -i 's/-faltivec//' numpy/distutils/system_info.py
67 export NPY_NUM_BUILD_JOBS=$NIX_BUILD_CORES
68 export OMP_NUM_THREADS=$((NIX_BUILD_CORES > 64 ? 64 : NIX_BUILD_CORES))
69 '';
70
71 preBuild = ''
72 ln -s ${cfg} site.cfg
73 '';
74
75 enableParallelBuilding = true;
76
77 nativeCheckInputs = [
78 pytest
79 # "hypothesis" indirectly depends on numpy to build its documentation.
80 (hypothesis.override { enableDocumentation = false; })
81 typing-extensions
82 ];
83
84 checkPhase = ''
85 runHook preCheck
86 pushd "$out"
87 ${python.interpreter} -c 'import numpy; numpy.test("fast", verbose=10)'
88 popd
89 runHook postCheck
90 '';
91
92 passthru = {
93 # just for backwards compatibility
94 blas = blas.provider;
95 blasImplementation = blas.implementation;
96 inherit cfg;
97 };
98
99 # Disable test
100 # - test_large_file_support: takes a long time and can cause the machine to run out of disk space
101 NOSE_EXCLUDE="test_large_file_support";
102
103 meta = {
104 description = "Scientific tools for Python";
105 homepage = "https://numpy.org/";
106 license = lib.licenses.bsd3;
107 maintainers = with lib.maintainers; [ fridh ];
108 };
109}