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 # LLVM 19 changes how host tools are looked up.
55 # Need to remove NO_DEFAULT_PATH and the PATHS arguments for find_program
56 # so CMake can actually find the tools in nativeBuildInputs.
57 # https://github.com/llvm/llvm-project/pull/105969
58 ++ lib.optional (lib.versionAtLeast release_version "19") (
59 getVersionFile "libclc/use-default-paths.patch"
60 );
61
62 # cmake expects all required binaries to be in the same place, so it will not be able to find clang without the patch
63 postPatch =
64 lib.optionalString (lib.versionOlder release_version "19") ''
65 substituteInPlace CMakeLists.txt \
66 --replace-fail 'find_program( LLVM_CLANG clang PATHS ''${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )' \
67 'find_program( LLVM_CLANG clang PATHS "${buildLlvmTools.clang.cc}/bin" NO_DEFAULT_PATH )' \
68 --replace-fail 'find_program( LLVM_AS llvm-as PATHS ''${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )' \
69 'find_program( LLVM_AS llvm-as PATHS "${buildLlvmTools.llvm}/bin" NO_DEFAULT_PATH )' \
70 --replace-fail 'find_program( LLVM_LINK llvm-link PATHS ''${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )' \
71 'find_program( LLVM_LINK llvm-link PATHS "${buildLlvmTools.llvm}/bin" NO_DEFAULT_PATH )' \
72 --replace-fail 'find_program( LLVM_OPT opt PATHS ''${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )' \
73 'find_program( LLVM_OPT opt PATHS "${buildLlvmTools.llvm}/bin" NO_DEFAULT_PATH )' \
74 --replace-fail 'find_program( LLVM_SPIRV llvm-spirv PATHS ''${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )' \
75 'find_program( LLVM_SPIRV llvm-spirv PATHS "${spirv-llvm-translator}/bin" NO_DEFAULT_PATH )'
76 ''
77 + lib.optionalString (!stdenv.buildPlatform.canExecute stdenv.hostPlatform) (
78 if (lib.versionOlder release_version "19") then
79 ''
80 substituteInPlace CMakeLists.txt \
81 --replace-fail 'COMMAND prepare_builtins' \
82 'COMMAND ${buildLlvmTools.libclc.dev}/bin/prepare_builtins'
83 ''
84 else
85 ''
86 substituteInPlace CMakeLists.txt \
87 --replace-fail 'set( prepare_builtins_exe prepare_builtins )' \
88 'set( prepare_builtins_exe ${buildLlvmTools.libclc.dev}/bin/prepare_builtins )'
89 ''
90 );
91
92 nativeBuildInputs =
93 [
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})