FindWeka.cmake
Go to the documentation of this file.
1 ##############################################################################
2 # @file FindWeka.cmake
3 # @brief Find Weka (http://www.cs.waikato.ac.nz/ml/weka/) package.
4 #
5 # @par Input variables:
6 # <table border="0">
7 # <tr>
8 # @tp @b Weka_DIR @endtp
9 # <td>The Weka package files are searched under the specified root
10 # directory. If they are not found there, the default search paths
11 # are considered. This variable can also be set as environment variable.</td>
12 # </tr>
13 # <tr>
14 # @tp @b WEKA_DIR @endtp
15 # <td>Alternative environment variable for @c Weka_DIR.</td>
16 # </tr>
17 # <tr>
18 # @tp @b Weka_PACKAGES_DIR @endtp
19 # <td>Directory where additional Weka packages are installed. If they are
20 # not found there or if this variable is not set, this module will look
21 # in the standard installation directories. This variable can also be set
22 # as environment variable.</td>
23 # </tr>
24 # <tr>
25 # @tp @b WEKA_PACKAGES_DIR @endtp
26 # <td>Alternative environment variable for @c Weka_PACKAGES_DIR.</td>
27 # </tr>
28 # <tr>
29 # @tp <b>Weka_&lt;package&gt;_DIR</b> @endtp
30 # <td>The path of the given Weka package, i.e., the directory containing the
31 # <tt>&lt;package&gt;.jar</tt> file or a subdirectory named <tt>weka</tt>
32 # with the uncompressed <tt>.class</tt> files of the package.</td>
33 # </tr>
34 # <tr>
35 # @tp @b Weka_FIND_COMPONENTS @endtp
36 # <td>The @c COMPONENTS argument(s) of the find_package() command can
37 # be used to also look for additionally installed Weka packages.</td>
38 # </tr>
39 # <tr>
40 # @tp @b Weka_FIND_OPTIONAL_COMPONENTS @endtp
41 # <td>The @c OPTIONAL_COMPONENTS argument(s) of the find_package() command can
42 # be used to also look for additionally installed Weka packages.</td>
43 # </tr>
44 # <tr>
45 # @tp @b Weka_ADDITIONAL_VERSIONS @endtp
46 # <td>List of version numbers that should be taken into account when
47 # searching for Weka.</td>
48 # </tr>
49 # </table>
50 #
51 # @par Output variables:
52 # <table border="0">
53 # <tr>
54 # @tp @b Weka_FOUND @endtp
55 # <td>Whether the package was found and the following CMake variables are valid.</td>
56 # </tr>
57 # <tr>
58 # @tp @b Weka_HAS_PACKAGE_MANAGER @endtp
59 # <td>Whether the found version of Weka has a package manager.</td>
60 # </tr>
61 # <tr>
62 # @tp @b Weka_CLASSPATH @endtp
63 # <td>The path of the found <tt>weka.jar</tt> file.</td>
64 # </tr>
65 # <tr>
66 # @tp @b Weka_PACKAGES_CLASSPATH @endtp
67 # <td>The @c CLASSPATH of all found additional Weka packages (non-cached).</td>
68 # </tr>
69 # <tr>
70 # @tp @b Weka_CLASSPATHS @endtp
71 # <td>Combination of both @c Weka_CLASSPATH and @c Weka_PACKAGES_CLASSPATH.</td>
72 # </tr>
73 # </table>
74 #
75 # @ingroup CMakeFindModules
76 ##############################################################################
77 
78 #=============================================================================
79 # Copyright 2011-2012 University of Pennsylvania
80 # Copyright 2013-2016 Andreas Schuh <andreas.schuh.84@gmail.com>
81 #
82 # Distributed under the OSI-approved BSD License (the "License");
83 # see accompanying file Copyright.txt for details.
84 #
85 # This software is distributed WITHOUT ANY WARRANTY; without even the
86 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
87 # See the License for more information.
88 #=============================================================================
89 # (To distribute this file outside of CMake, substitute the full
90 # License text for the above reference.)
91 
92 # ============================================================================
93 # helpers
94 # ============================================================================
95 
96 # ----------------------------------------------------------------------------
97 # @brief Determine if this Weka version has a package manager.
98 #
99 # @returns This macro sets the Weka_HAS_PACKAGE_MANAGER variable.
101  if (Weka_VERSION_MAJOR AND Weka_VERSION_MINOR)
102  if (Weka_VERSION_MAJOR EQUAL 3)
103  if (Weka_VERSION_MINOR GREATER 7 OR (Weka_VERSION_MINOR EQUAL 7 AND Weka_VERSION_PATCH GREATER 1))
104  set (Weka_HAS_PACKAGE_MANAGER TRUE)
105  else ()
106  set (Weka_HAS_PACKAGE_MANAGER FALSE)
107  endif ()
108  elseif (Weka_VERSION_MAJOR GREATER 2)
109  set (Weka_HAS_PACKAGE_MANAGER TRUE)
110  else ()
111  set (Weka_HAS_PACKAGE_MANAGER FALSE)
112  endif ()
113  else ()
114  set (Weka_HAS_PACKAGE_MANAGER TRUE)
115  endif ()
116 endmacro ()
117 
118 # ----------------------------------------------------------------------------
119 ## @brief Get list of Weka packages.
120 #
121 # @param [out] PACKAGES Name of variable storing the list of packages.
122 # @param [in] WHICH Argument to -list-packages option of Weka package
123 # manager, i.e., "all", "available", or "installed".
124 function (weka_list_packages PACKAGES WHICH)
125  string (TOLOWER "${WHICH}" WHICH) # ignore case of argument
126  set (PKGS)
127  if (NOT JAVA_EXECUTABLE)
128  set (JAVA_EXECUTABLE java)
129  endif ()
130  if (Weka_VERSION_MAJOR AND Weka_VERSION_MINOR)
131  if (Weka_VERSION_MAJOR EQUAL 3)
132  if (Weka_VERSION_MINOR GREATER 7 OR (Weka_VERSION_MINOR EQUAL 7 AND Weka_VERSION_PATCH GREATER 1))
133  set (HAS_PACKAGE_MANAGER TRUE)
134  else ()
135  set (HAS_PACKAGE_MANAGER FALSE)
136  endif ()
137  elseif (Weka_VERSION_MAJOR GREATER 2)
138  set (HAS_PACKAGE_MANAGER TRUE)
139  else ()
140  set (HAS_PACKAGE_MANAGER FALSE)
141  endif ()
142  else ()
143  set (HAS_PACKAGE_MANAGER TRUE)
144  endif ()
145  if (HAS_PACKAGE_MANAGER)
146  if (Weka_CLASSPATH)
147  execute_process (
148  COMMAND "${JAVA_EXECUTABLE}"
149  -classpath "${Weka_CLASSPATH}"
150  weka.core.WekaPackageManager
151  -list-packages "${WHICH}"
152  RESULT_VARIABLE STATUS
153  OUTPUT_VARIABLE STDOUT
154  ERROR_VARIABLE STDERR
155  )
156  if (STATUS EQUAL 0)
157  string (REPLACE ";" "," STDOUT "${STDOUT}")
158  string (REPLACE "\n" ";" STDOUT "${STDOUT}")
159  foreach (LINE IN LISTS STDOUT)
160  if (LINE MATCHES "^(-+|[0-9]+(\\.[0-9]+(\\.[0-9]+)))[ \t]+[0-9]+(\\.[0-9]+(\\.[0-9]+))[ \t]+([a-zA-Z_-]+):")
161  list (APPEND PKGS "${CMAKE_MATCH_6}")
162  endif ()
163  endforeach ()
164  else ()
165  message (WARNING "Failed to retrieve list of ${WHICH} Weka packages! The command\n"
166  "\"${JAVA_EXECUTABLE}\" -classpath \"${Weka_CLASSPATH}\" weka.core.WekaPackageManager -list-packages ${WHICH}\n"
167  "failed with the error:\n${STDERR}")
168  endif ()
169  else ()
170  message (WARNING "Cannot retrieve list of Weka packages because Weka_CLASSPATH is not set!")
171  endif ()
172  endif ()
173  set (${PACKAGES} "${PKGS}" PARENT_SCOPE)
174 endfunction ()
175 
176 # ----------------------------------------------------------------------------
177 ## @brief Get Weka version.
178 #
179 # @param [out] VERSION Version string of Weka or 0.0.0.
180 # @param [out] MAJOR Major version of Weka or 0.
181 # @param [out] MINOR Minor version of Weka or 0.
182 # @param [out] PATCH Patch number of Weka or 0.
183 function (weka_get_version VERSION MAJOR MINOR PATCH)
184  set (VERSION_STRING "0.0.0")
185  set (VERSION_MAJOR 0)
186  set (VERSION_MINOR 0)
187  set (VERSION_PATCH 0)
188  if (NOT JAVA_EXECUTABLE)
189  set (JAVA_EXECUTABLE java)
190  endif ()
191  if (Weka_CLASSPATH)
192  execute_process (
193  COMMAND "${JAVA_EXECUTABLE}" -classpath "${Weka_CLASSPATH}" weka.core.Version
194  RESULT_VARIABLE RETVAL
195  OUTPUT_VARIABLE STDOUT
196  ERROR_VARIABLE STDERR
197  )
198  if (STDOUT MATCHES "^([0-9]+)\\.([0-9]+)\\.([0-9]+)")
199  set (VERSION_STRING "${CMAKE_MATCH_0}")
200  set (VERSION_MAJOR "${CMAKE_MATCH_1}")
201  set (VERSION_MINOR "${CMAKE_MATCH_2}")
202  set (VERSION_PATCH "${CMAKE_MATCH_3}")
203  else ()
204  message (WARNING "Failed to version of Weka installation. The command\n"
205  "\"${JAVA_EXECUTABLE}\" -classpath \"${Weka_CLASSPATH}\" weka.core.Version\n"
206  "failed with the error:\n${STDERR}")
207  endif ()
208  endif ()
209  set (${VERSION} "${VERSION_STRING}" PARENT_SCOPE)
210  set (${MAJOR} "${VERSION_MAJOR}" PARENT_SCOPE)
211  set (${MINOR} "${VERSION_MINOR}" PARENT_SCOPE)
212  set (${PATCH} "${VERSION_PATCH}" PARENT_SCOPE)
213 endfunction ()
214 
215 # ============================================================================
216 # main
217 # ============================================================================
218 
219 # ----------------------------------------------------------------------------
220 # initialize search
221 if (NOT Weka_DIR)
222  if (NOT $ENV{WEKAHOME} STREQUAL "")
223  set (Weka_DIR "$ENV{WEKAHOME}" CACHE PATH "Installation directory of Weka." FORCE)
224  elseif (NOT $ENV{Weka_DIR} STREQUAL "")
225  set (Weka_DIR "$ENV{Weka_DIR}" CACHE PATH "Installation directory of Weka." FORCE)
226  else ()
227  set (Weka_DIR "$ENV{WEKA_DIR}" CACHE PATH "Installation directory of Weka." FORCE)
228  endif ()
229 endif ()
230 
231 set (Weka_HINTS)
232 if (CMAKE_HOST_APPLE)
233  # TODO seach /Applications/weka-3-7-6.app/Contents and
234  # $ENV{HOME}/Applications/weka-3-7-6.app/Contents
235  # directories for all requested/known Weka versions
236 endif ()
237 string (REPLACE ":" ";" Weka_ENV_CLASSPATH "$ENV{CLASSPATH}")
238 foreach (Weka_CLSDIR IN LISTS Weka_ENV_CLASSPATH)
239  if (Weka_CLSDIR MATCHES "\\.jar")
240  get_filename_component (Weka_CLSDIR "${Weka_CLSDIR}" PATH)
241  endif ()
242  list (APPEND Weka_HINTS "${Weka_CLSDIR}")
243 endforeach ()
244 unset (Weka_ENV_CLASSPATH)
245 
246 #-----------------------------------------------------------------------------
247 # find weka.jar
248 if (Weka_DIR)
249  if (Weka_CLASSPATH AND NOT Weka_CLASSPATH MATCHES "^${Weka_DIR}/")
250  set_property (CACHE Weka_CLASSPATH PROPERTY VALUE "Weka_CLASSPATH-NOTFOUND")
251  endif ()
252 
253  find_file (
254  Weka_CLASSPATH
255  NAMES weka.jar
256  HINTS "${Weka_DIR}"
257  PATH_SUFFIXES "weka"
258  "Contents/Resources/Java"
259  "Resources/Java"
260  DOC "The Java library of the Weka package (weka.jar)."
261  NO_DEFAULT_PATH
262  )
263 else ()
264  find_file (
265  Weka_CLASSPATH
266  NAMES weka.jar
267  HINTS ${Weka_HINTS}
268  PATH_SUFFIXES "weka"
269  "Contents/Resources/Java"
270  "Resources/Java"
271  DOC "The Java library of the Weka package (weka.jar)."
272  )
273  if (Weka_CLASSPATH)
274  get_filename_component (Weka_DIR "${Weka_CLASSPATH}" PATH)
275  if (EXISTS "${Weka_DIR}/../WekaManual.pdf") # weka.jar is in weka/ subdirectory
276  get_filename_component (Weka_DIR "${Weka_DIR}" PATH)
277  elseif (Weka_DIR MATCHES "/Contents/Resources/Java$")
278  string (REGEX REPLACE "/Contents/Resources/Java$" "" Weka_DIR "${Weka_DIR}")
279  endif ()
280  set (Weka_DIR "${Weka_DIR}" CACHE PATH "Installation directory of Weka." FORCE)
281  endif ()
282 endif ()
283 mark_as_advanced (Weka_CLASSPATH)
284 
285 #-------------------------------------------------------------
286 # determine Weka version
287 weka_get_version (Weka_VERSION_STRING Weka_VERSION_MAJOR Weka_VERSION_MINOR Weka_VERSION_PATCH)
289 
290 #-------------------------------------------------------------
291 # find Weka packages
292 if (Weka_HAS_PACKAGE_MANAGER)
293  if (NOT Weka_PACKAGES_DIR AND Weka_DIR)
294  if (IS_DIRECTORY "${Weka_DIR}/packages")
295  set (Weka_PACKAGES_DIR "${Weka_DIR}/packages" CACHE PATH "Installation directory of Weka packages." FORCE)
296  elseif (IS_DIRECTORY "$ENV{HOME}/wekafiles/packages")
297  set (Weka_PACKAGES_DIR "$ENV{HOME}/wekafiles/packages" CACHE PATH "Installation directory of Weka packages." FORCE)
298  endif ()
299  endif ()
300  set (Weka_REQUIRED_PACKAGE_VARS)
301  if (Weka_FIND_COMPONENTS)
302  foreach (Weka_PKG IN LISTS Weka_FIND_COMPONENTS)
303  list (APPEND Weka_REQUIRED_PACKAGE_VARS "Weka_${Weka_PKG}_DIR")
304  endforeach ()
305  elseif (Weka_CLASSPATH)
306  weka_list_packages (Weka_FIND_COMPONENTS installed)
307  endif ()
308  foreach (Weka_PKG IN LISTS Weka_FIND_COMPONENTS Weka_FIND_OPTIONAL_COMPONENTS)
309  set (Weka_${Weka_PKG}_DIR "Weka_${Weka_PKG}_DIR-NOTFOUND" CACHE PATH "Directory of ${Weka_PKG} Weka package." FORCE)
310  mark_as_advanced (Weka_${Weka_PKG}_DIR)
311  if (IS_DIRECTORY "${Weka_PACKAGES_DIR}/${Weka_PKG}")
312  if (EXISTS "${Weka_PACKAGES_DIR}/${Weka_PKG}/${Weka_PKG}.jar")
313  set (Weka_${Weka_PKG}_CLASSPATH "${Weka_PACKAGES_DIR}/${Weka_PKG}/${Weka_PKG}.jar")
314  elseif (IS_DIRECTORY "${Weka_PACKAGES_DIR}/${Weka_PKG}/weka")
315  set (Weka_${Weka_PKG}_CLASSPATH "${Weka_PACKAGES_DIR}/${Weka_PKG}")
316  elseif (IS_DIRECTORY "${Weka_PACKAGES_DIR}/${Weka_PACKAGE}/src/main/java/weka")
317  set (Weka_${Weka_PKG}_CLASSPATH "${Weka_PACKAGES_DIR}/${Weka_PKG}/src/main/java")
318  endif ()
319  endif ()
320  if (Weka_${Weka_PKG}_CLASSPATH)
321  list (APPEND Weka_PACKAGES_CLASSPATH "${Weka_${Weka_PKG}_CLASSPATH}")
322  set (Weka_${Weka_PKG}_DIR "${Weka_PACKAGES_DIR}/${Weka_PKG}" CACHE PATH "Directory of ${Weka_PKG} Weka package." FORCE)
323  endif ()
324  endforeach ()
325  unset (Weka_PKG)
326 else ()
327  if (DEFINED Weka_PACKAGES_DIR)
328  get_property (Weka_PACKAGES_DIR_SET CACHE Weka_PACKAGES_DIR PROPERTY TYPE SET)
329  if (Weka_PACKAGES_DIR_SET)
330  set_property (CACHE Weka_PACKAGES_DIR PROPERTY TYPE INTERNAL)
331  set_property (CACHE Weka_PACKAGES_DIR PROPERTY VALUE Weka_PACKAGES_DIR-NOTFOUND)
332  endif ()
333  unset (Weka_PACKAGES_DIR_SET)
334  endif ()
335  if (Weka_FIND_COMPONENTS AND Weka_FIND_REQUIRED)
336  message (WARNING "This Weka version has no package manager and consists only of the core weka.jar package."
337  " The components required may not be available with this Weka version. Make sure that you"
338  " have installed and selected the proper Weka version required by this software package."
339  " Further, check the Weka_DIR variable and set it accordingly if it points to the wrong"
340  " installation of Weka.")
341  endif ()
342 endif ()
343 
344 # ----------------------------------------------------------------------------
345 # Weka_CLASSPATHS
346 set (Weka_CLASSPATHS)
347 if (Weka_CLASSPATH)
348  list (APPEND Weka_CLASSPATHS "${Weka_CLASSPATH}")
349 endif ()
350 if (Weka_PACKAGES_CLASSPATH)
351  list (APPEND Weka_CLASSPATHS ${Weka_PACKAGES_CLASSPATH})
352 endif ()
353 
354 # ----------------------------------------------------------------------------
355 # debugging
356 if (BASIS_DEBUG AND COMMAND basis_dump_variables)
357  basis_dump_variables ("${CMAKE_CURRENT_BINARY_DIR}/FindWekaVariables.cmake")
358 endif ()
359 
360 # ----------------------------------------------------------------------------
361 # handle the QUIETLY and REQUIRED arguments and set *_FOUND to TRUE
362 # if all listed variables are found or TRUE
363 include (FindPackageHandleStandardArgs)
364 
365 find_package_handle_standard_args (
366  Weka
367  REQUIRED_VARS Weka_CLASSPATH ${Weka_REQUIRED_PACKAGE_VARS}
368  VERSION_VAR ${Weka_VERSION_STRING}
369 )
370 
371 unset (Weka_HINTS)
372 unset (Weka_REQUIRED_PACKAGE_VARS)
const unsigned int VERSION_MAJOR
The major version number.
Definition: basis.cxx:41
cmake Weka_PACKAGES_DIR
Definition: FindWeka.cmake:295
function is(in result, in expected, in name)
Test whether a given result is equal to the expected result.
cmake STDERR
string VERSION
Project version.
Definition: utilities.sh:81
cmake RETVAL
function weka_get_version(out VERSION, out MAJOR, out MINOR, out PATCH)
Get Weka version.
cmake Weka_DIR
Definition: FindWeka.cmake:223
cmake COMMAND
macro weka_has_package_manager()
cmake VALUE
const unsigned int VERSION_MINOR
The minor version number.
Definition: basis.cxx:42
const unsigned int VERSION_PATCH
The patch number.
Definition: basis.cxx:43
cmake STDOUT
if(oldcoutbuf)
def main(argv)
Definition: which.py:305
function get_filename_component(inout ARGN)
Fixes CMake&#39;s get_filename_component() command.
function weka_list_packages(out PACKAGES, in WHICH)
Get list of Weka packages.