1{ lib
2, fetchPypi
3, python
4, buildPythonPackage
5, gfortran
6, hypothesis
7, pytest
8, blas
9, lapack
10, writeTextFile
11, cython
12, setuptoolsBuildHook
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.21.2";
43 format = "pyproject.toml";
44 disabled = pythonOlder "3.7";
45
46 src = fetchPypi {
47 inherit pname version;
48 extension = "zip";
49 sha256 = "423216d8afc5923b15df86037c6053bf030d15cc9e3224206ef868c2d63dd6dc";
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 setuptoolsBuildHook ];
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 # Workaround flakey compiler feature detection
76 # https://github.com/numpy/numpy/issues/19624
77 hardeningDisable = [ "strictoverflow" ];
78
79 enableParallelBuilding = true;
80
81 checkInputs = [
82 pytest
83 hypothesis
84 ];
85
86 checkPhase = ''
87 runHook preCheck
88 pushd dist
89 ${python.interpreter} -c 'import numpy; numpy.test("fast", verbose=10)'
90 popd
91 runHook postCheck
92 '';
93
94 passthru = {
95 # just for backwards compatibility
96 blas = blas.provider;
97 blasImplementation = blas.implementation;
98 inherit cfg;
99 };
100
101 # Disable test
102 # - test_large_file_support: takes a long time and can cause the machine to run out of disk space
103 NOSE_EXCLUDE="test_large_file_support";
104
105 meta = {
106 description = "Scientific tools for Python";
107 homepage = "https://numpy.org/";
108 license = lib.licenses.bsd3;
109 maintainers = with lib.maintainers; [ fridh ];
110 };
111}