1{ lib
2, stdenv
3, pythonAtLeast
4, pythonOlder
5, fetchPypi
6, python
7, buildPythonPackage
8, setuptools
9, numpy
10, llvmlite
11, libcxx
12, importlib-metadata
13, substituteAll
14, runCommand
15, fetchpatch
16
17# CUDA-only dependencies:
18, addOpenGLRunpath ? null
19, cudaPackages ? {}
20
21# CUDA flags:
22, cudaSupport ? false
23}:
24
25let
26 inherit (cudaPackages) cudatoolkit;
27in buildPythonPackage rec {
28 version = "0.56.4";
29 pname = "numba";
30 format = "setuptools";
31 disabled = pythonOlder "3.6" || pythonAtLeast "3.11";
32
33 src = fetchPypi {
34 inherit pname version;
35 hash = "sha256-Mtn+9BLIFIPX7+DOts9NMxD96LYkqc7MoA95BXOslu4=";
36 };
37
38 postPatch = ''
39 substituteInPlace setup.py \
40 --replace 'max_numpy_run_version = "1.24"' 'max_numpy_run_version = "1.25"'
41 substituteInPlace numba/__init__.py \
42 --replace "elif numpy_version > (1, 23):" "elif numpy_version > (1, 24):"
43 '';
44
45 env.NIX_CFLAGS_COMPILE = lib.optionalString stdenv.isDarwin "-I${lib.getDev libcxx}/include/c++/v1";
46
47 nativeBuildInputs = [
48 numpy
49 ] ++ lib.optionals cudaSupport [
50 addOpenGLRunpath
51 ];
52
53 propagatedBuildInputs = [
54 numpy
55 llvmlite
56 setuptools
57 ] ++ lib.optionals (pythonOlder "3.9") [
58 importlib-metadata
59 ] ++ lib.optionals cudaSupport [
60 cudatoolkit
61 cudatoolkit.lib
62 ];
63
64 patches = [
65 # fix failure in test_cache_invalidate (numba.tests.test_caching.TestCache)
66 # remove when upgrading past version 0.56
67 (fetchpatch {
68 name = "fix-test-cache-invalidate-readonly.patch";
69 url = "https://github.com/numba/numba/commit/993e8c424055a7677b2755b184fc9e07549713b9.patch";
70 hash = "sha256-IhIqRLmP8gazx+KWIyCxZrNLMT4jZT8CWD3KcH4KjOo=";
71 })
72 # Backport numpy 1.24 support from https://github.com/numba/numba/pull/8691
73 ./numpy-1.24.patch
74 ] ++ lib.optionals cudaSupport [
75 (substituteAll {
76 src = ./cuda_path.patch;
77 cuda_toolkit_path = cudatoolkit;
78 cuda_toolkit_lib_path = cudatoolkit.lib;
79 })
80 ];
81
82 postFixup = lib.optionalString cudaSupport ''
83 find $out -type f \( -name '*.so' -or -name '*.so.*' \) | while read lib; do
84 addOpenGLRunpath "$lib"
85 patchelf --set-rpath "${cudatoolkit}/lib:${cudatoolkit.lib}/lib:$(patchelf --print-rpath "$lib")" "$lib"
86 done
87 '';
88
89 # run a smoke test in a temporary directory so that
90 # a) Python picks up the installed library in $out instead of the build files
91 # b) we have somewhere to put $HOME so some caching tests work
92 # c) it doesn't take 6 CPU hours for the full suite
93 checkPhase = ''
94 runHook preCheck
95
96 pushd $(mktemp -d)
97 HOME=. ${python.interpreter} -m numba.runtests -m $NIX_BUILD_CORES numba.tests.test_usecases
98 popd
99
100 runHook postCheck
101 '';
102
103 pythonImportsCheck = [
104 "numba"
105 ];
106
107 passthru.tests = {
108 # CONTRIBUTOR NOTE: numba also contains CUDA tests, though these cannot be run in
109 # this sandbox environment. Consider running similar commands to those below outside the
110 # sandbox manually if you have the appropriate hardware; support will be detected
111 # and the corresponding tests enabled automatically.
112 # Also, the full suite currently does not complete on anything but x86_64-linux.
113 fullSuite = runCommand "${pname}-test" {} ''
114 pushd $(mktemp -d)
115 # pip and python in $PATH is needed for the test suite to pass fully
116 PATH=${python.withPackages (p: [ p.numba p.pip ])}/bin:$PATH
117 HOME=$PWD python -m numba.runtests -m $NIX_BUILD_CORES
118 popd
119 touch $out # stop Nix from complaining no output was generated and failing the build
120 '';
121 };
122
123 meta = with lib; {
124 description = "Compiling Python code using LLVM";
125 homepage = "https://numba.pydata.org/";
126 license = licenses.bsd2;
127 maintainers = with maintainers; [ fridh ];
128 };
129}