1{
2 lib,
3 stdenv,
4 version,
5 runCommand,
6 monorepoSrc,
7 llvm,
8 buildPackages,
9 buildLlvmTools,
10 ninja,
11 cmake,
12 python3,
13 release_version,
14 getVersionFile,
15}:
16let
17 spirv-llvm-translator = buildPackages.spirv-llvm-translator.override {
18 inherit (buildLlvmTools) llvm;
19 };
20
21 # The build requires an unwrapped clang but wrapped clang++ thus we need to
22 # split the unwrapped clang out to prevent the build from finding the
23 # unwrapped clang++
24 clang-only = runCommand "clang-only" { } ''
25 mkdir -p "$out"/bin
26 ln -s "${lib.getExe' buildLlvmTools.clang.cc "clang"}" "$out"/bin
27 '';
28in
29stdenv.mkDerivation (finalAttrs: {
30 pname = "libclc";
31 inherit version;
32
33 src = runCommand "libclc-src-${version}" { inherit (monorepoSrc) passthru; } (
34 ''
35 mkdir -p "$out"
36 ''
37 + lib.optionalString (lib.versionAtLeast release_version "14") ''
38 cp -r ${monorepoSrc}/cmake "$out"
39 ''
40 + ''
41 cp -r ${monorepoSrc}/libclc "$out"
42 ''
43 );
44
45 sourceRoot = "${finalAttrs.src.name}/libclc";
46
47 outputs = [
48 "out"
49 "dev"
50 ];
51
52 patches = [
53 ./libclc-gnu-install-dirs.patch
54 ]
55 # LLVM 19 changes how host tools are looked up.
56 # Need to remove NO_DEFAULT_PATH and the PATHS arguments for find_program
57 # so CMake can actually find the tools in nativeBuildInputs.
58 # https://github.com/llvm/llvm-project/pull/105969
59 ++ lib.optional (lib.versionAtLeast release_version "19") (
60 getVersionFile "libclc/use-default-paths.patch"
61 );
62
63 # cmake expects all required binaries to be in the same place, so it will not be able to find clang without the patch
64 postPatch =
65 lib.optionalString (lib.versionOlder release_version "19") ''
66 substituteInPlace CMakeLists.txt \
67 --replace-fail 'find_program( LLVM_CLANG clang PATHS ''${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )' \
68 'find_program( LLVM_CLANG clang PATHS "${buildLlvmTools.clang.cc}/bin" NO_DEFAULT_PATH )' \
69 --replace-fail 'find_program( LLVM_AS llvm-as PATHS ''${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )' \
70 'find_program( LLVM_AS llvm-as PATHS "${buildLlvmTools.llvm}/bin" NO_DEFAULT_PATH )' \
71 --replace-fail 'find_program( LLVM_LINK llvm-link PATHS ''${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )' \
72 'find_program( LLVM_LINK llvm-link PATHS "${buildLlvmTools.llvm}/bin" NO_DEFAULT_PATH )' \
73 --replace-fail 'find_program( LLVM_OPT opt PATHS ''${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )' \
74 'find_program( LLVM_OPT opt PATHS "${buildLlvmTools.llvm}/bin" NO_DEFAULT_PATH )' \
75 --replace-fail 'find_program( LLVM_SPIRV llvm-spirv PATHS ''${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )' \
76 'find_program( LLVM_SPIRV llvm-spirv PATHS "${spirv-llvm-translator}/bin" NO_DEFAULT_PATH )'
77 ''
78 + lib.optionalString (!stdenv.buildPlatform.canExecute stdenv.hostPlatform) (
79 if (lib.versionOlder release_version "19") then
80 ''
81 substituteInPlace CMakeLists.txt \
82 --replace-fail 'COMMAND prepare_builtins' \
83 'COMMAND ${buildLlvmTools.libclc.dev}/bin/prepare_builtins'
84 ''
85 else
86 ''
87 substituteInPlace CMakeLists.txt \
88 --replace-fail 'set( prepare_builtins_exe prepare_builtins )' \
89 'set( prepare_builtins_exe ${buildLlvmTools.libclc.dev}/bin/prepare_builtins )'
90 ''
91 );
92
93 nativeBuildInputs = [
94 cmake
95 ninja
96 python3
97 ]
98 ++ lib.optional (lib.versionAtLeast release_version "19") [
99 clang-only
100 buildLlvmTools.llvm
101 spirv-llvm-translator
102 ];
103 buildInputs = [ llvm ];
104 strictDeps = true;
105
106 postInstall = ''
107 install -Dt $dev/bin prepare_builtins
108 '';
109
110 meta = with lib; {
111 homepage = "http://libclc.llvm.org/";
112 description = "Implementation of the library requirements of the OpenCL C programming language";
113 mainProgram = "prepare_builtins";
114 license = licenses.mit;
115 platforms = platforms.all;
116 };
117})