{ lib, stdenv, llvmPackages, ninja, cmake, hwloc, ocl-icd, opencl-headers, getconf, pkg-config, spirv-llvm-translator, spirv-tools, libxml2, # required for statically linked llvm python3, writableTmpDirAsHomeHook, writeText, runCommand, opencl-cts, staticLLVM ? true, enableICD ? true, enableSPIRV ? true, enableHWLOC ? true, enableRemoteServer ? false, enableRemoteClient ? false, lttng-ust, enableLTTNG ? false, onetbb, enableTBB ? false, src, buildPackages, }: let buildLlvmPackages = buildPackages.llvmPackages; in llvmPackages.stdenv.mkDerivation (finalAttrs: { pname = "pocl"; version = "nightly"; inherit src; patches = [ ./cmake-fix.patch ]; cmakeFlags = [ # TODO: all these are broken when cross compiling. Upstream has refactored # all the cmake infra for cross compilation, but it's not in a release yet (lib.cmakeOptionType "filepath" "WITH_LLVM_CONFIG" ( lib.getExe' buildLlvmPackages.llvm.dev "llvm-config" )) (lib.cmakeOptionType "filepath" "CLANG" (lib.getExe' buildLlvmPackages.clangUseLLVM "clang")) (lib.cmakeOptionType "filepath" "CLANGXX" (lib.getExe' buildLlvmPackages.clangUseLLVM "clang++")) (lib.cmakeOptionType "path" "CLANG_RESOURCE_DIR" "${llvmPackages.stdenv.cc}/resource-root") (lib.cmakeBool "ENABLE_TESTS" finalAttrs.finalPackage.doCheck) (lib.cmakeBool "ENABLE_EXAMPLES" finalAttrs.finalPackage.doCheck) (lib.cmakeBool "ENABLE_RELOCATION" true) (lib.cmakeBool "ENABLE_POCL_BUILDING" true) (lib.cmakeBool "POCL_ICD_ABSOLUTE_PATH" true) (lib.cmakeBool "ENABLE_TBB_DEVICE" enableTBB) (lib.cmakeBool "ENABLE_ICD" enableICD) (lib.cmakeBool "ENABLE_HWLOC" enableHWLOC) (lib.cmakeBool "ENABLE_LTTNG" enableLTTNG) (lib.cmakeBool "ENABLE_REMOTE_CLIENT" enableRemoteClient) (lib.cmakeBool "ENABLE_REMOTE_SERVER" enableRemoteServer) (lib.cmakeBool "ENABLE_LLVM_FILECHECKS" false) # TODO: fix filecheck tests # avoid the runtime linker pulling in a different llvm e.g. from graphics drivers (lib.cmakeBool "STATIC_LLVM" staticLLVM) ] ++ lib.optionals stdenv.hostPlatform.isx86_64 [ (lib.cmakeFeature "KERNELLIB_HOST_CPU_VARIANTS" "distro") ]; # Fixes error: ld: cannot find -lgcc / -lgcc_s when linking from libpocl on # host # Should match the flags from : "${targetPackages.stdenv.cc}/nix-support/cc-ldflags # But, we cannot use the file here without doing import from derivation preConfigure = let inherit (stdenv) targetPlatform hostPlatform; gccForLibs = stdenv.cc.cc; gccForLibs_solib = lib.getLib gccForLibs + lib.optionalString (targetPlatform != hostPlatform) "/${targetPlatform.config}"; in '' cmakeFlagsArray+=( -DEXTRA_HOST_LD_FLAGS="-L${gccForLibs}/lib/gcc/${targetPlatform.config}/${gccForLibs.version} -L${gccForLibs_solib}/lib" ) ''; nativeBuildInputs = [ cmake getconf ninja pkg-config python3 ]; buildInputs = [ opencl-headers llvmPackages.libclang llvmPackages.llvm ] ++ lib.optionals staticLLVM [ libxml2 ] ++ lib.optionals enableHWLOC [ hwloc ] ++ lib.optionals enableTBB [ onetbb ] ++ lib.optionals enableICD [ ocl-icd ] ++ lib.optionals enableLTTNG [ lttng-ust ] ++ lib.optionals enableSPIRV [ (spirv-llvm-translator.override { inherit (llvmPackages) llvm; }) spirv-tools ]; nativeCheckInputs = [ writableTmpDirAsHomeHook # needed for POCL_CACHE_DIR ]; nativeInstallCheckInputs = [ writableTmpDirAsHomeHook # needed for POCL_CACHE_DIR ]; doInstallCheck = true; installCheckPhase = lib.optionalString enableICD '' runHook preInstallCheck export OCL_ICD_VENDORS=$out/etc/OpenCL/vendors $out/bin/poclcc -o poclcc.cl.pocl $src/examples/poclcc/poclcc.cl runHook postInstallCheck ''; setupHook = lib.optionalDrvAttr enableICD ( writeText "setup-hook" '' addToSearchPath OCL_ICD_VENDORS @out@/etc/OpenCL/vendors '' ); passthru = let self = finalAttrs.finalPackage; in { withICD = enableICD; test = { # Run builtin tests (causes a rebuild) self = self.overrideAttrs { doCheck = true; }; # Run basic test from Khronos OpenCL Conformance Test Suite # WARN: despite its name, test_basic is very exhaustive, and can take more # than 1 hour in a marenostrum5 node. cts = assert enableICD; lib.genAttrs [ "api" "basic" "c11_atomics" "printf" "svm" ] ( name: runCommand "pocl-cts-test_${name}" { nativeBuildInputs = [ self ]; requiredSystemFeatures = [ "sys-devices" ]; env = { POCL_DEBUG = "error,warn"; POCL_CACHE_DIR = "/build/pocl_cache"; }; } "${opencl-cts}/bin/Release/test_${name} | tee $out" ); }; }; env.HWLOC_SYNTHETIC = lib.optionalDrvAttr enableHWLOC "node:1 core:1 pu:1"; propagatedBuildInputs = [ stdenv.cc.cc ]; meta = with lib; { description = "A portable open source (MIT-licensed) implementation of the OpenCL standard"; homepage = "http://portablecl.org"; license = licenses.mit; maintainers = with maintainers; [ leixb jansol xddxdd bsc.abonerib ]; platforms = platforms.linux ++ platforms.darwin; }; })