···
1
+
{ config, stdenv, lib, fetchFromGitHub, cmake, gtest, doCheck ? true
2
+
, cudaSupport ? config.cudaSupport or false, openclSupport ? false, mpiSupport ? false, javaWrapper ? false, hdfsSupport ? false
3
+
, rLibrary ? false, cudaPackages, opencl-headers, ocl-icd, boost, llvmPackages, openmpi, openjdk, swig, hadoop, R, rPackages }:
5
+
assert doCheck -> mpiSupport != true;
6
+
assert openclSupport -> cudaSupport != true;
7
+
assert cudaSupport -> openclSupport != true;
9
+
stdenv.mkDerivation rec {
10
+
pnameBase = "lightgbm";
11
+
# prefix with r when building the R library
12
+
# The R package build results in a special binary file
13
+
# that contains a subset of the .so file use for the CLI
14
+
# and python version. In general, the CRAN version from
15
+
# nixpkgs's r-modules should be used, but this non-standard
16
+
# build allows for enabling CUDA support and other features
17
+
# which aren't included in the CRAN release. Build with:
18
+
# nix-build -E "with (import $NIXPKGS{}); \
20
+
# lgbm = lightgbm.override{rLibrary = true; doCheck = false;}; \
22
+
# rWrapper.override{ packages = [ lgbm ]; }"
23
+
pname = lib.optionalString rLibrary "r-" + pnameBase;
26
+
src = fetchFromGitHub {
27
+
owner = "microsoft";
29
+
rev = "v${version}";
30
+
fetchSubmodules = true;
31
+
hash = "sha256-QRuBbMVtD5J5ECw+bAp57bWaRc/fATMcTq+AKikhj1I=";
34
+
nativeBuildInputs = [ cmake ]
35
+
++ lib.optionals stdenv.isDarwin [ llvmPackages.openmp ]
36
+
++ lib.optionals openclSupport [ opencl-headers ocl-icd boost ]
37
+
++ lib.optionals mpiSupport [ openmpi ]
38
+
++ lib.optionals hdfsSupport [ hadoop ]
39
+
++ lib.optionals (hdfsSupport || javaWrapper) [ openjdk ]
40
+
++ lib.optionals javaWrapper [ swig ]
41
+
++ lib.optionals rLibrary [ R ];
43
+
buildInputs = [ gtest ]
44
+
++ lib.optional cudaSupport cudaPackages.cudatoolkit;
46
+
propagatedBuildInputs = lib.optionals rLibrary [
47
+
rPackages.data_table
53
+
# Skip APPLE in favor of linux build for .so files
55
+
export PROJECT_SOURCE_DIR=./
56
+
substituteInPlace CMakeLists.txt \
57
+
--replace "find_package(GTest CONFIG)" "find_package(GTest REQUIRED)" \
58
+
--replace "OpenCL_INCLUDE_DIRS}" "OpenCL_INCLUDE_DIRS}" \
59
+
--replace "elseif(APPLE)" "elseif(APPLESKIP)"
61
+
external_libs/compute/include/boost/compute/cl.hpp \
62
+
external_libs/compute/include/boost/compute/cl_ext.hpp \
63
+
--replace "include <OpenCL/" "include <CL/"
64
+
substituteInPlace build_r.R \
65
+
--replace "file.path(getwd(), \"lightgbm_r\")" "'$out/tmp'" \
67
+
"install_args <- c(\"CMD\", \"INSTALL\", \"--no-multiarch\", \"--with-keep.source\", tarball)" \
68
+
"install_args <- c(\"CMD\", \"INSTALL\", \"--no-multiarch\", \"--with-keep.source\", \"-l $out/library\", tarball)"
71
+
cmakeFlags = lib.optionals doCheck [ "-DBUILD_CPP_TEST=ON" ]
72
+
++ lib.optionals cudaSupport [ "-DUSE_CUDA=1" "-DCMAKE_CXX_COMPILER=${cudaPackages.cudatoolkit.cc}/bin/cc" ]
73
+
++ lib.optionals openclSupport [ "-DUSE_GPU=ON" ]
74
+
++ lib.optionals mpiSupport [ "-DUSE_MPI=ON" ]
75
+
++ lib.optionals hdfsSupport [
77
+
"-DHDFS_LIB=${hadoop}/lib/hadoop-3.3.1/lib/native/libhdfs.so"
78
+
"-DHDFS_INCLUDE_DIR=${hadoop}/lib/hadoop-3.3.1/include" ]
79
+
++ lib.optionals javaWrapper [ "-DUSE_SWIG=ON" ]
80
+
++ lib.optionals rLibrary [ "-D__BUILD_FOR_R=ON" ];
82
+
configurePhase = lib.optionals rLibrary ''
83
+
export R_LIBS_SITE="$out/library:$R_LIBS_SITE''${R_LIBS_SITE:+:}"
86
+
# set the R package buildPhase to null because lightgbm has a
87
+
# custom builder script that builds and installs in one step
88
+
buildPhase = lib.optionals rLibrary ''
95
+
'' + lib.optionalString (!rLibrary) ''
99
+
cp -r ../include $out
100
+
install -Dm755 ../lib_lightgbm.so $out/lib/lib_lightgbm.so
101
+
install -Dm755 ../lightgbm $out/bin/lightgbm
102
+
'' + lib.optionalString javaWrapper ''
105
+
cp -r lightgbmlib.jar $out
107
+
'' + lib.optionalString javaWrapper ''
110
+
cp -r lightgbmlib.jar $out
111
+
'' + lib.optionalString rLibrary ''
115
+
mkdir $out/library/lightgbm
116
+
'' + lib.optionalString (rLibrary && (!openclSupport)) ''
119
+
'' + lib.optionalString (rLibrary && openclSupport) ''
120
+
Rscript build_r.R --use-gpu \
121
+
--opencl-library=${ocl-icd}/lib/libOpenCL.so \
122
+
--boost-librarydir=${boost}
125
+
runHook postInstall
128
+
postFixup = lib.optionalString rLibrary ''
129
+
if test -e $out/nix-support/propagated-build-inputs; then
130
+
ln -s $out/nix-support/propagated-build-inputs $out/nix-support/propagated-user-env-packages
136
+
"LightGBM is a gradient boosting framework that uses tree based learning algorithms.";
137
+
homepage = "https://github.com/microsoft/LightGBM";
138
+
license = licenses.mit;
139
+
platforms = platforms.unix;
140
+
maintainers = with maintainers; [ nviets ];