2021-02-05 17:12:51 +00:00
|
|
|
{ lib
|
|
|
|
, stdenv
|
2020-04-24 23:36:52 +00:00
|
|
|
, fetchFromGitHub
|
|
|
|
, fetchpatch
|
|
|
|
, cmake
|
|
|
|
, git
|
|
|
|
|
2021-12-06 16:07:01 +00:00
|
|
|
, llvmPackages_11
|
2020-04-24 23:36:52 +00:00
|
|
|
, spirv-llvm-translator
|
|
|
|
|
|
|
|
, buildWithPatches ? true
|
|
|
|
}:
|
|
|
|
|
|
|
|
let
|
2021-12-06 16:07:01 +00:00
|
|
|
llvmPkgs = llvmPackages_11 // {
|
2020-04-24 23:36:52 +00:00
|
|
|
inherit spirv-llvm-translator;
|
|
|
|
};
|
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
inherit (lib) getVersion;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
addPatches = component: pkg:
|
2021-02-05 17:12:51 +00:00
|
|
|
with builtins; with lib;
|
2020-04-24 23:36:52 +00:00
|
|
|
let path = "${passthru.patchesOut}/${component}";
|
|
|
|
in pkg.overrideAttrs (super: {
|
|
|
|
postPatch = (if super ? postPatch then super.postPatch + "\n" else "") + ''
|
|
|
|
for p in ${path}/*
|
|
|
|
do
|
|
|
|
patch -p1 -i "$p"
|
|
|
|
done
|
|
|
|
'';
|
|
|
|
});
|
|
|
|
|
|
|
|
passthru = rec {
|
2021-12-06 16:07:01 +00:00
|
|
|
spirv-llvm-translator = llvmPkgs.spirv-llvm-translator;
|
|
|
|
llvm = addPatches "llvm" llvmPkgs.llvm;
|
2021-05-20 23:08:51 +00:00
|
|
|
libclang = addPatches "clang" llvmPkgs.libclang;
|
|
|
|
|
|
|
|
clang-unwrapped = libclang.out;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
clang = llvmPkgs.clang.override {
|
|
|
|
cc = clang-unwrapped;
|
|
|
|
};
|
|
|
|
|
|
|
|
patchesOut = stdenv.mkDerivation rec {
|
|
|
|
pname = "opencl-clang-patches";
|
2021-02-05 17:12:51 +00:00
|
|
|
inherit (library) version src patches;
|
2021-12-06 16:07:01 +00:00
|
|
|
# Clang patches assume the root is the llvm root dir
|
|
|
|
# but clang root in nixpkgs is the clang sub-directory
|
|
|
|
postPatch = ''
|
|
|
|
for filename in patches/clang/*.patch; do
|
|
|
|
substituteInPlace "$filename" \
|
|
|
|
--replace "a/clang/" "a/" \
|
|
|
|
--replace "b/clang/" "b/"
|
|
|
|
done
|
|
|
|
'';
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
installPhase = ''
|
|
|
|
[ -d patches ] && cp -r patches/ $out || mkdir $out
|
2021-12-06 16:07:01 +00:00
|
|
|
mkdir -p $out/clang $out/llvm
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
library = let
|
2020-04-24 23:36:52 +00:00
|
|
|
inherit (llvmPkgs) llvm;
|
2021-05-20 23:08:51 +00:00
|
|
|
inherit (if buildWithPatches then passthru else llvmPkgs) libclang spirv-llvm-translator;
|
2020-04-24 23:36:52 +00:00
|
|
|
in
|
|
|
|
stdenv.mkDerivation rec {
|
|
|
|
pname = "opencl-clang";
|
2021-12-06 16:07:01 +00:00
|
|
|
version = "unstable-2021-06-22";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
inherit passthru;
|
|
|
|
|
|
|
|
src = fetchFromGitHub {
|
|
|
|
owner = "intel";
|
|
|
|
repo = "opencl-clang";
|
2021-12-06 16:07:01 +00:00
|
|
|
rev = "fd68f64b33e67d58f6c36b9e25c31c1178a1962a";
|
|
|
|
sha256 = "sha256-q1YPBb/LY67iEuQx1fMUQD/I7OsNfobW3yNfJxLXx3E=";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
patches = [
|
|
|
|
# Build script tries to find Clang OpenCL headers under ${llvm}
|
|
|
|
# Work around it by specifying that directory manually.
|
|
|
|
./opencl-headers-dir.patch
|
|
|
|
];
|
|
|
|
|
2021-05-20 23:08:51 +00:00
|
|
|
# Uses linker flags that are not supported on Darwin.
|
|
|
|
postPatch = lib.optionalString stdenv.isDarwin ''
|
|
|
|
sed -i -e '/SET_LINUX_EXPORTS_FILE/d' CMakeLists.txt
|
|
|
|
substituteInPlace CMakeLists.txt \
|
|
|
|
--replace '-Wl,--no-undefined' ""
|
|
|
|
'';
|
|
|
|
|
|
|
|
nativeBuildInputs = [ cmake git llvm.dev ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-05-20 23:08:51 +00:00
|
|
|
buildInputs = [ libclang llvm spirv-llvm-translator ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
cmakeFlags = [
|
|
|
|
"-DPREFERRED_LLVM_VERSION=${getVersion llvm}"
|
2021-05-20 23:08:51 +00:00
|
|
|
"-DOPENCL_HEADERS_DIR=${libclang.lib}/lib/clang/${getVersion libclang}/include/"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
"-DLLVMSPIRV_INCLUDED_IN_LLVM=OFF"
|
|
|
|
"-DSPIRV_TRANSLATOR_DIR=${spirv-llvm-translator}"
|
|
|
|
];
|
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
meta = with lib; {
|
2020-04-24 23:36:52 +00:00
|
|
|
homepage = "https://github.com/intel/opencl-clang/";
|
|
|
|
description = "A clang wrapper library with an OpenCL-oriented API and the ability to compile OpenCL C kernels to SPIR-V modules";
|
|
|
|
license = licenses.ncsa;
|
|
|
|
platforms = platforms.all;
|
|
|
|
maintainers = with maintainers; [ gloaming ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in
|
2021-02-05 17:12:51 +00:00
|
|
|
library
|