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