1{
2 lib,
3 stdenv,
4 fetchFromGitHub,
5 fetchpatch,
6 fetchurl,
7 # build
8 cmake,
9 ctags,
10 python3Packages,
11 swig,
12 # math
13 eigen,
14 blas,
15 lapack,
16 glpk,
17 # data
18 protobuf,
19 json_c,
20 libxml2,
21 hdf5,
22 curl,
23 # compression
24 libarchive,
25 bzip2,
26 xz,
27 snappy,
28 lzo,
29 # more math
30 nlopt,
31 lp_solve,
32 colpack,
33 # extra support
34 pythonSupport ? false,
35 opencvSupport ? false,
36 opencv ? null,
37 withSvmLight ? false,
38}:
39
40assert pythonSupport -> python3Packages != null;
41assert opencvSupport -> opencv != null;
42
43assert (!blas.isILP64) && (!lapack.isILP64);
44
45let
46 pname = "shogun";
47 version = "6.1.4";
48
49 rxcppVersion = "4.0.0";
50 gtestVersion = "1.8.0";
51
52 srcs = {
53 toolbox = fetchFromGitHub {
54 owner = "shogun-toolbox";
55 repo = "shogun";
56 rev = "shogun_${version}";
57 hash = "sha256-38aULxK50wQ2+/ERosSpRyBmssmYSGv5aaWfWSlrSRc=";
58 fetchSubmodules = true;
59 };
60
61 # The CMake external projects expect the packed archives
62 rxcpp = fetchurl {
63 url = "https://github.com/Reactive-Extensions/RxCpp/archive/v${rxcppVersion}.tar.gz";
64 sha256 = "sha256-UOc5WrG8KgAA3xJsaSCjbdPE7gSnFJay9MEK31DWUXg=";
65 };
66
67 gtest = fetchurl {
68 url = "https://github.com/google/googletest/archive/release-${gtestVersion}.tar.gz";
69 sha256 = "sha256-WKb0J3yivIVlIis7vVihd2CenEiOinJkk1m6UUUNt9g=";
70 };
71 };
72in
73
74stdenv.mkDerivation (finalAttrs: {
75 inherit pname version;
76
77 outputs = [
78 "out"
79 "dev"
80 "doc"
81 ];
82
83 src = srcs.toolbox;
84
85 patches = [
86 # Fix compile errors with GCC 9+
87 # https://github.com/shogun-toolbox/shogun/pull/4811
88 (fetchpatch {
89 url = "https://github.com/shogun-toolbox/shogun/commit/c8b670be4790e0f06804b048a6f3d77c17c3ee95.patch";
90 sha256 = "sha256-MxsR3Y2noFQevfqWK3nmX5iK4OVWeKBl5tfeDNgjcXk=";
91 })
92 (fetchpatch {
93 url = "https://github.com/shogun-toolbox/shogun/commit/5aceefd9fb0e2132c354b9a0c0ceb9160cc9b2f7.patch";
94 sha256 = "sha256-AgJJKQA8vc5oKaTQDqMdwBR4hT4sn9+uW0jLe7GteJw=";
95 })
96
97 # Fix virtual destruction
98 (fetchpatch {
99 url = "https://github.com/shogun-toolbox/shogun/commit/ef0e4dc1cc4a33c9e6b17a108fa38a436de2d7ee.patch";
100 sha256 = "sha256-a9Rm0ytqkSAgC3dguv8m3SwOSipb+VByBHHdmV0d63w=";
101 })
102 ./fix-virtual-destruction.patch
103
104 # Fix compile errors with json-c
105 # https://github.com/shogun-toolbox/shogun/pull/4104
106 (fetchpatch {
107 url = "https://github.com/shogun-toolbox/shogun/commit/365ce4c4c700736d2eec8ba6c975327a5ac2cd9b.patch";
108 sha256 = "sha256-OhEWwrHtD/sOcjHmPY/C9zJ8ruww8yXrRcTw38nGEJU=";
109 })
110
111 # Fix compile errors with Eigen 3.4
112 ./eigen-3.4.patch
113
114 ]
115 ++ lib.optional (!withSvmLight) ./svmlight-scrubber.patch;
116
117 nativeBuildInputs = [
118 cmake
119 swig
120 ctags
121 ]
122 ++ (with python3Packages; [
123 python
124 jinja2
125 ply
126 ]);
127
128 buildInputs = [
129 eigen
130 blas
131 lapack
132 glpk
133 protobuf
134 json_c
135 libxml2
136 hdf5
137 curl
138 libarchive
139 bzip2
140 xz
141 snappy
142 lzo
143 nlopt
144 lp_solve
145 colpack
146 ]
147 ++ lib.optionals pythonSupport (
148 with python3Packages;
149 [
150 python
151 numpy
152 ]
153 )
154 ++ lib.optional opencvSupport opencv;
155
156 cmakeFlags =
157 let
158 excludeTestsRegex = lib.concatStringsSep "|" [
159 # segfault
160 "SerializationXML"
161 "TrainedModelSerialization"
162 # broken by openblas 0.3.21
163 "mathematics_lapack"
164 # fails on aarch64
165 "LinearTimeMMD"
166 "QuadraticTimeMMD"
167 "SGVectorTest"
168 "Statistics"
169 # hangs on aarch64
170 "PRange"
171 # these take too long on CI
172 "evaluation_cross_validation"
173 "modelselection_combined_kernel"
174 "modelselection_grid_search"
175 ];
176 in
177 [
178 (lib.cmakeBool "BUILD_META_EXAMPLES" true)
179 (lib.cmakeBool "CMAKE_DISABLE_FIND_PACKAGE_ARPACK" true)
180 (lib.cmakeBool "CMAKE_DISABLE_FIND_PACKAGE_ARPREC" true)
181 (lib.cmakeBool "CMAKE_DISABLE_FIND_PACKAGE_CPLEX" true)
182 (lib.cmakeBool "CMAKE_DISABLE_FIND_PACKAGE_Mosek" true)
183 (lib.cmakeBool "CMAKE_DISABLE_FIND_PACKAGE_TFLogger" true)
184 (lib.cmakeBool "CMAKE_DISABLE_FIND_PACKAGE_ViennaCL" true)
185 (lib.cmakeFeature "CMAKE_CTEST_ARGUMENTS" "--exclude-regex;'${excludeTestsRegex}'")
186 (lib.cmakeBool "ENABLE_TESTING" finalAttrs.finalPackage.doCheck)
187 (lib.cmakeBool "DISABLE_META_INTEGRATION_TESTS" true)
188 (lib.cmakeBool "TRAVIS_DISABLE_META_CPP" true)
189 (lib.cmakeBool "INTERFACE_PYTHON" pythonSupport)
190 (lib.cmakeBool "OpenCV" opencvSupport)
191 (lib.cmakeBool "USE_SVMLIGHT" withSvmLight)
192 ];
193
194 CXXFLAGS = "-faligned-new";
195
196 doCheck = true;
197
198 postUnpack = ''
199 mkdir -p $sourceRoot/third_party/{rxcpp,GoogleMock}
200 ln -s ${srcs.rxcpp} $sourceRoot/third_party/rxcpp/v${rxcppVersion}.tar.gz
201 ln -s ${srcs.gtest} $sourceRoot/third_party/GoogleMock/release-${gtestVersion}.tar.gz
202 '';
203
204 postPatch = ''
205 # Fix preprocessing SVMlight code
206 sed -i \
207 -e 's@#ifdef SVMLIGHT@#ifdef USE_SVMLIGHT@' \
208 -e '/^#ifdef USE_SVMLIGHT/,/^#endif/ s@#endif@#endif //USE_SVMLIGHT@' \
209 src/shogun/kernel/string/CommUlongStringKernel.cpp
210 sed -i -e 's/#if USE_SVMLIGHT/#ifdef USE_SVMLIGHT/' src/interfaces/swig/Machine.i
211 sed -i -e 's@// USE_SVMLIGHT@//USE_SVMLIGHT@' src/interfaces/swig/Transfer.i
212 sed -i -e 's@/\* USE_SVMLIGHT \*/@//USE_SVMLIGHT@' src/interfaces/swig/Transfer_includes.i
213 ''
214 + lib.optionalString (!withSvmLight) ''
215 # Run SVMlight scrubber
216 patchShebangs scripts/light-scrubber.sh
217 echo "removing SVMlight code"
218 ./scripts/light-scrubber.sh
219 '';
220
221 postInstall = ''
222 mkdir -p $doc/share/doc/shogun/examples
223 mv $out/share/shogun/examples/cpp $doc/share/doc/shogun/examples
224 cp ../examples/undocumented/libshogun/*.cpp $doc/share/doc/shogun/examples/cpp
225 rm -r $out/share
226 '';
227
228 postFixup = ''
229 # CMake incorrectly calculates library path from dev prefix
230 substituteInPlace $dev/lib/cmake/shogun/ShogunTargets-release.cmake \
231 --replace-fail "\''${_IMPORT_PREFIX}/lib/" "$out/lib/"
232 '';
233
234 meta = with lib; {
235 description = "Toolbox which offers a wide range of efficient and unified machine learning methods";
236 homepage = "http://shogun-toolbox.org/";
237 license = if withSvmLight then licenses.unfree else licenses.gpl3Plus;
238 maintainers = with maintainers; [
239 edwtjo
240 smancill
241 ];
242 };
243})