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 mkdir -p "$out"
35 cp -r ${monorepoSrc}/cmake "$out"
36 cp -r ${monorepoSrc}/libclc "$out"
37 '');
38
39 sourceRoot = "${finalAttrs.src.name}/libclc";
40
41 outputs = [
42 "out"
43 "dev"
44 ];
45
46 patches = [
47 (getVersionFile "libclc/gnu-install-dirs.patch")
48 ]
49 # LLVM 19 changes how host tools are looked up.
50 # Need to remove NO_DEFAULT_PATH and the PATHS arguments for find_program
51 # so CMake can actually find the tools in nativeBuildInputs.
52 # https://github.com/llvm/llvm-project/pull/105969
53 ++ lib.optional (lib.versionAtLeast release_version "19") (
54 getVersionFile "libclc/use-default-paths.patch"
55 );
56
57 # cmake expects all required binaries to be in the same place, so it will not be able to find clang without the patch
58 postPatch =
59 lib.optionalString (lib.versionOlder release_version "19") ''
60 substituteInPlace CMakeLists.txt \
61 --replace-fail 'find_program( LLVM_CLANG clang PATHS ''${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )' \
62 'find_program( LLVM_CLANG clang PATHS "${buildLlvmTools.clang.cc}/bin" NO_DEFAULT_PATH )' \
63 --replace-fail 'find_program( LLVM_AS llvm-as PATHS ''${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )' \
64 'find_program( LLVM_AS llvm-as PATHS "${buildLlvmTools.llvm}/bin" NO_DEFAULT_PATH )' \
65 --replace-fail 'find_program( LLVM_LINK llvm-link PATHS ''${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )' \
66 'find_program( LLVM_LINK llvm-link PATHS "${buildLlvmTools.llvm}/bin" NO_DEFAULT_PATH )' \
67 --replace-fail 'find_program( LLVM_OPT opt PATHS ''${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )' \
68 'find_program( LLVM_OPT opt PATHS "${buildLlvmTools.llvm}/bin" NO_DEFAULT_PATH )' \
69 --replace-fail 'find_program( LLVM_SPIRV llvm-spirv PATHS ''${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )' \
70 'find_program( LLVM_SPIRV llvm-spirv PATHS "${spirv-llvm-translator}/bin" NO_DEFAULT_PATH )'
71 ''
72 + lib.optionalString (!stdenv.buildPlatform.canExecute stdenv.hostPlatform) (
73 if (lib.versionOlder release_version "19") then
74 ''
75 substituteInPlace CMakeLists.txt \
76 --replace-fail 'COMMAND prepare_builtins' \
77 'COMMAND ${buildLlvmTools.libclc.dev}/bin/prepare_builtins'
78 ''
79 else
80 ''
81 substituteInPlace CMakeLists.txt \
82 --replace-fail 'set( prepare_builtins_exe prepare_builtins )' \
83 'set( prepare_builtins_exe ${buildLlvmTools.libclc.dev}/bin/prepare_builtins )'
84 ''
85 );
86
87 nativeBuildInputs = [
88 cmake
89 ninja
90 python3
91 ]
92 ++ lib.optionals (lib.versionAtLeast release_version "19") [
93 clang-only
94 buildLlvmTools.llvm
95 spirv-llvm-translator
96 ];
97 buildInputs = [ llvm ];
98 strictDeps = true;
99
100 postInstall = ''
101 install -Dt $dev/bin prepare_builtins
102 '';
103
104 meta = with lib; {
105 homepage = "http://libclc.llvm.org/";
106 description = "Implementation of the library requirements of the OpenCL C programming language";
107 mainProgram = "prepare_builtins";
108 license = licenses.mit;
109 platforms = platforms.all;
110 };
111})