FindJythonInterp.cmake
Go to the documentation of this file.
1 ##############################################################################
2 # @file FindJythonInterp.cmake
3 # @brief Find Jython interpreter.
4 #
5 # @par Output variables:
6 # <table border="0">
7 # <tr>
8 # @tp @b JYTHONINTERP_FOUND @endtp
9 # <td>Whether the Jython executable was found.</td>
10 # </tr>
11 # <tr>
12 # @tp @b JYTHON_EXECUTABLE @endtp
13 # <td>Path to the Jython interpreter.</td>
14 # </tr>
15 # <tr>
16 # @tp @b JYTHON_VERSION_STRING @endtp
17 # <td>Jython version found, e.g. 2.5.2.</td>
18 # </tr>
19 # <tr>
20 # @tp @b JYTHON_VERSION_MAJOR @endtp
21 # <td>Jython major version found, e.g. 2.</td>
22 # </tr>
23 # <tr>
24 # @tp @b JYTHON_VERSION_MINOR @endtp
25 # <td>Jython minor version found, e.g. 5.</td>
26 # </tr>
27 # <tr>
28 # @tp @b JYTHON_VERSION_PATCH @endtp
29 # <td>Jython patch version found, e.g. 2.</td>
30 # </tr>
31 # </table>
32 #
33 # @ingroup CMakeFindModules
34 ##############################################################################
35 
36 #=============================================================================
37 # Copyright 2011-2012 University of Pennsylvania
38 # Copyright 2013-2016 Andreas Schuh <andreas.schuh.84@gmail.com>
39 #
40 # Distributed under the OSI-approved BSD License (the "License");
41 # see accompanying file Copyright.txt for details.
42 #
43 # This software is distributed WITHOUT ANY WARRANTY; without even the
44 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
45 # See the License for more information.
46 #=============================================================================
47 # (To distribute this file outside of CMake, substitute the full
48 # License text for the above reference.)
49 
50 # ROOT
51 if (JYTHON_ROOT)
52  set(_JythonInterp_ROOT ${JYTHON_ROOT})
53 else ()
54  file(TO_CMAKE_PATH "$ENV{JYTHON_ROOT}" _JythonInterp_ROOT)
55 endif ()
56 
57 # HINTS
58 set(_JythonInterp_HINTS)
59 if (_JythonInterp_ROOT)
60  list(APPEND _JythonInterp_HINTS "${_JythonInterp_ROOT}/bin")
61 endif ()
62 
63 # PATHS
64 set(_JythonInterp_PATHS
65  /opt/local/bin
66  /usr/local/bin
67  /usr/bin
68 )
69 
70 # find jython executable
71 find_program(JYTHON_EXECUTABLE
72  NAMES jython
73  HINTS ${_JythonInterp_HINTS}
74  PATHS ${_JythonInterp_PATHS}
75 )
76 
77 mark_as_advanced(JYTHON_EXECUTABLE)
78 
79 # determine jython version
80 if (JYTHON_EXECUTABLE)
81  execute_process (COMMAND "${JYTHON_EXECUTABLE}" -c "import sys; sys.stdout.write(';'.join([str(x) for x in sys.version_info[:3]]))"
82  OUTPUT_VARIABLE _JYTHON_VERSION
83  RESULT_VARIABLE _JYTHON_VERSION_RESULT
84  ERROR_QUIET)
85  if (_JYTHON_VERSION_RESULT EQUAL 0)
86  string (REPLACE ";" "." JYTHON_VERSION_STRING "${_JYTHON_VERSION}")
87  list (GET _JYTHON_VERSION 0 JYTHON_VERSION_MAJOR)
88  list (GET _JYTHON_VERSION 1 JYTHON_VERSION_MINOR)
89  list (GET _JYTHON_VERSION 2 JYTHON_VERSION_PATCH)
90  if (JYTHON_VERSION_PATCH EQUAL 0)
91  # it's called "Jython 2.5", not "2.5.0"
92  string (REGEX REPLACE "\\.0$" "" JYTHON_VERSION_STRING "${JYTHON_VERSION_STRING}")
93  endif()
94  else ()
95  # sys.version predates sys.version_info, so use that
96  execute_process(COMMAND "${JYTHON_EXECUTABLE}" -c "import sys; sys.stdout.write(sys.version)"
97  OUTPUT_VARIABLE _JYTHON_VERSION
98  RESULT_VARIABLE _JYTHON_VERSION_RESULT
99  ERROR_QUIET)
100  if (_JYTHON_VERSION_RESULT EQUAL 0)
101  string (REGEX REPLACE " .*" "" JYTHON_VERSION_STRING "${_JYTHON_VERSION}")
102  string (REGEX REPLACE "^([0-9]+)\\.[0-9]+.*" "\\1" JYTHON_VERSION_MAJOR "${JYTHON_VERSION_STRING}")
103  string (REGEX REPLACE "^[0-9]+\\.([0-9])+.*" "\\1" JYTHON_VERSION_MINOR "${JYTHON_VERSION_STRING}")
104  if (JYTHON_VERSION_STRING MATCHES "^[0-9]+\\.[0-9]+\\.[0-9]+.*")
105  string (REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" JYTHON_VERSION_PATCH "${JYTHON_VERSION_STRING}")
106  else ()
107  set (JYTHON_VERSION_PATCH "0")
108  endif()
109  else ()
110  set (JYTHON_VERSION_STRING)
111  set (JYTHON_VERSION_MAJOR)
112  set (JYTHON_VERSION_MINOR)
113  set (JYTHON_VERSION_PATCH)
114  endif ()
115  endif ()
116  unset (_JYTHON_VERSION)
117  unset (_JYTHON_VERSION_RESULT)
118 endif ()
119 
120 # handle QUIET, REQUIRED, and [EXACT] VERSION arguments and set JYTHONINTERP_FOUND
121 include(FindPackageHandleStandardArgs)
122 find_package_handle_standard_args(
123  JythonInterp
124  REQUIRED_VARS
125  JYTHON_EXECUTABLE
126  VERSION_VAR
127  JYTHON_VERSION_STRING
128 )
129 
130 # unset local auxiliary variables
131 unset(_JythonInterp_ROOT)
132 unset(_JythonInterp_HINTS)
133 unset(_JythonInterp_PATHS)
cmake _JythonInterp_ROOT
cmake COMMAND
cmake JYTHON_VERSION_PATCH