1{ lib
2, fetchPypi
3, fetchpatch
4, python
5, buildPythonPackage
6, gfortran
7, hypothesis
8, pytest
9, typing-extensions
10, blas
11, lapack
12, writeTextFile
13, cython
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.23.3";
44 format = "setuptools";
45 disabled = pythonOlder "3.7";
46
47 src = fetchPypi {
48 inherit pname version;
49 extension = "tar.gz";
50 hash = "sha256-Ub9JwM0dUr4KJAqmbzRYr8S5XYmT0tBPDZH6YMEK9s0=";
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 ];
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 checkInputs = [
79 pytest
80 hypothesis
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}