1{
2 lib,
3 stdenv,
4 pythonAtLeast,
5 pythonOlder,
6 fetchFromGitHub,
7 python,
8 buildPythonPackage,
9 setuptools,
10 numpy,
11 llvmlite,
12 libcxx,
13 importlib-metadata,
14 substituteAll,
15 runCommand,
16 symlinkJoin,
17 writers,
18 numba,
19
20 config,
21
22 # CUDA-only dependencies:
23 addDriverRunpath,
24 autoAddDriverRunpath,
25 cudaPackages,
26
27 # CUDA flags:
28 cudaSupport ? config.cudaSupport,
29}:
30
31let
32 cudatoolkit = cudaPackages.cuda_nvcc;
33in
34buildPythonPackage rec {
35 # Using an untagged version, with numpy 1.25 support, when it's released
36 # also drop the versioneer patch in postPatch
37 version = "0.59.1";
38 pname = "numba";
39 pyproject = true;
40
41 disabled = pythonOlder "3.8" || pythonAtLeast "3.13";
42
43 src = fetchFromGitHub {
44 owner = "numba";
45 repo = "numba";
46 rev = "refs/tags/${version}";
47 # Upstream uses .gitattributes to inject information about the revision
48 # hash and the refname into `numba/_version.py`, see:
49 #
50 # - https://git-scm.com/docs/gitattributes#_export_subst and
51 # - https://github.com/numba/numba/blame/5ef7c86f76a6e8cc90e9486487294e0c34024797/numba/_version.py#L25-L31
52 #
53 # Hence this hash may change if GitHub / Git will change it's behavior.
54 # Hopefully this will not happen until the next release. We are fairly sure
55 # that upstream relies on those strings to be valid, that's why we don't
56 # use `forceFetchGit = true;`.` If in the future we'll observe the hash
57 # changes too often, we can always use forceFetchGit, and inject the
58 # relevant strings ourselves, using `sed` commands, in extraPostFetch.
59 hash = "sha256-4udpgLLHbHNtxPiYVkj+gxAjTWV3ClZOv98Y313/qbc=";
60 };
61
62 postPatch = ''
63 substituteInPlace numba/cuda/cudadrv/driver.py \
64 --replace-fail \
65 "dldir = [" \
66 "dldir = [ '${addDriverRunpath.driverLink}/lib', "
67 '';
68
69 env.NIX_CFLAGS_COMPILE = lib.optionalString stdenv.isDarwin "-I${lib.getDev libcxx}/include/c++/v1";
70
71 nativeBuildInputs =
72 [ numpy ]
73 ++ lib.optionals cudaSupport [
74 autoAddDriverRunpath
75 cudaPackages.cuda_nvcc
76 ];
77
78 buildInputs = lib.optionals cudaSupport [ cudaPackages.cuda_cudart ];
79
80 propagatedBuildInputs = [
81 numpy
82 llvmlite
83 setuptools
84 ] ++ lib.optionals (pythonOlder "3.9") [ importlib-metadata ];
85
86 patches = lib.optionals cudaSupport [
87 (substituteAll {
88 src = ./cuda_path.patch;
89 cuda_toolkit_path = cudatoolkit;
90 cuda_toolkit_lib_path = lib.getLib cudatoolkit;
91 })
92 ];
93
94 # run a smoke test in a temporary directory so that
95 # a) Python picks up the installed library in $out instead of the build files
96 # b) we have somewhere to put $HOME so some caching tests work
97 # c) it doesn't take 6 CPU hours for the full suite
98 checkPhase = ''
99 runHook preCheck
100
101 pushd $(mktemp -d)
102 HOME=. ${python.interpreter} -m numba.runtests -m $NIX_BUILD_CORES numba.tests.test_usecases
103 popd
104
105 runHook postCheck
106 '';
107
108 pythonImportsCheck = [ "numba" ];
109
110 passthru.testers.cuda-detect =
111 writers.writePython3Bin "numba-cuda-detect"
112 { libraries = [ (numba.override { cudaSupport = true; }) ]; }
113 ''
114 from numba import cuda
115 cuda.detect()
116 '';
117 passthru.tests = {
118 # CONTRIBUTOR NOTE: numba also contains CUDA tests, though these cannot be run in
119 # this sandbox environment. Consider running similar commands to those below outside the
120 # sandbox manually if you have the appropriate hardware; support will be detected
121 # and the corresponding tests enabled automatically.
122 # Also, the full suite currently does not complete on anything but x86_64-linux.
123 fullSuite = runCommand "${pname}-test" { } ''
124 pushd $(mktemp -d)
125 # pip and python in $PATH is needed for the test suite to pass fully
126 PATH=${
127 python.withPackages (p: [
128 p.numba
129 p.pip
130 ])
131 }/bin:$PATH
132 HOME=$PWD python -m numba.runtests -m $NIX_BUILD_CORES
133 popd
134 touch $out # stop Nix from complaining no output was generated and failing the build
135 '';
136 };
137
138 meta = with lib; {
139 description = "Compiling Python code using LLVM";
140 homepage = "https://numba.pydata.org/";
141 license = licenses.bsd2;
142 mainProgram = "numba";
143 };
144}