nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{ lib
2, stdenv
3, fetchFromGitHub
4, fetchpatch
5, cmake
6, git
7
8, llvmPackages_11
9, spirv-llvm-translator
10
11, buildWithPatches ? true
12}:
13
14let
15 llvmPkgs = llvmPackages_11 // {
16 inherit spirv-llvm-translator;
17 };
18
19 inherit (lib) getVersion;
20
21 addPatches = component: pkg:
22 with builtins; with lib;
23 let path = "${passthru.patchesOut}/${component}";
24 in pkg.overrideAttrs (super: {
25 postPatch = (if super ? postPatch then super.postPatch + "\n" else "") + ''
26 for p in ${path}/*
27 do
28 patch -p1 -i "$p"
29 done
30 '';
31 });
32
33 passthru = rec {
34 spirv-llvm-translator = llvmPkgs.spirv-llvm-translator;
35 llvm = addPatches "llvm" llvmPkgs.llvm;
36 libclang = addPatches "clang" llvmPkgs.libclang;
37
38 clang-unwrapped = libclang.out;
39
40 clang = llvmPkgs.clang.override {
41 cc = clang-unwrapped;
42 };
43
44 patchesOut = stdenv.mkDerivation rec {
45 pname = "opencl-clang-patches";
46 inherit (library) version src patches;
47 # Clang patches assume the root is the llvm root dir
48 # but clang root in nixpkgs is the clang sub-directory
49 postPatch = ''
50 for filename in patches/clang/*.patch; do
51 substituteInPlace "$filename" \
52 --replace "a/clang/" "a/" \
53 --replace "b/clang/" "b/"
54 done
55 '';
56
57 installPhase = ''
58 [ -d patches ] && cp -r patches/ $out || mkdir $out
59 mkdir -p $out/clang $out/llvm
60 '';
61 };
62 };
63
64 library = let
65 inherit (llvmPkgs) llvm;
66 inherit (if buildWithPatches then passthru else llvmPkgs) libclang spirv-llvm-translator;
67 in
68 stdenv.mkDerivation rec {
69 pname = "opencl-clang";
70 version = "unstable-2022-03-16";
71
72 inherit passthru;
73
74 src = fetchFromGitHub {
75 owner = "intel";
76 repo = "opencl-clang";
77 rev = "bbdd1587f577397a105c900be114b56755d1f7dc";
78 sha256 = "sha256-qEZoQ6h4XAvSnJ7/gLXBb1qrzeYa6Jp6nij9VFo8MwQ=";
79 };
80
81 patches = [
82 # Build script tries to find Clang OpenCL headers under ${llvm}
83 # Work around it by specifying that directory manually.
84 ./opencl-headers-dir.patch
85 ];
86
87 # Uses linker flags that are not supported on Darwin.
88 postPatch = lib.optionalString stdenv.isDarwin ''
89 sed -i -e '/SET_LINUX_EXPORTS_FILE/d' CMakeLists.txt
90 substituteInPlace CMakeLists.txt \
91 --replace '-Wl,--no-undefined' ""
92 '';
93
94 nativeBuildInputs = [ cmake git llvm.dev ];
95
96 buildInputs = [ libclang llvm spirv-llvm-translator ];
97
98 cmakeFlags = [
99 "-DPREFERRED_LLVM_VERSION=${getVersion llvm}"
100 "-DOPENCL_HEADERS_DIR=${libclang.lib}/lib/clang/${getVersion libclang}/include/"
101
102 "-DLLVMSPIRV_INCLUDED_IN_LLVM=OFF"
103 "-DSPIRV_TRANSLATOR_DIR=${spirv-llvm-translator}"
104 ];
105
106 meta = with lib; {
107 homepage = "https://github.com/intel/opencl-clang/";
108 description = "A clang wrapper library with an OpenCL-oriented API and the ability to compile OpenCL C kernels to SPIR-V modules";
109 license = licenses.ncsa;
110 platforms = platforms.all;
111 maintainers = with maintainers; [ gloaming ];
112 };
113 };
114in
115 library