BASIS  r3148
FindPythonInterp.cmake
Go to the documentation of this file.
00001 ##############################################################################
00002 # @file  FindPythonInterp.cmake
00003 # @brief Find Python interpreter.
00004 #
00005 # @par Input variables:
00006 # <table border="0">
00007 #   <tr>
00008 #     @tp @b Python_ADDITIONAL_VERSIONS @endtp
00009 #     <td>List of version numbers that should be taken into account when
00010 #         searching for Python.</td>
00011 #   </tr>
00012 # </table>
00013 #
00014 # @par Output variables:
00015 # <table border="0">
00016 #   <tr>
00017 #     @tp @b PYTHONINTERP_FOUND @endtp
00018 #     <td>Was the Python executable found.</td>
00019 #   </tr>
00020 #   <tr>
00021 #     @tp @b PYTHON_EXECUTABLE @endtp
00022 #     <td>Path to the Python interpreter.</td>
00023 #   </tr>
00024 #   <tr>
00025 #     @tp @b PYTHON_VERSION_STRING @endtp
00026 #     <td>Python version found e.g. 2.5.2.</td>
00027 #   </tr>
00028 #   <tr>
00029 #     @tp @b PYTHON_VERSION_MAJOR @endtp
00030 #     <td>Python major version found e.g. 2.</td>
00031 #   </tr>
00032 #   <tr>
00033 #     @tp @b PYTHON_VERSION_MINOR @endtp
00034 #     <td>Python minor version found e.g. 5.</td>
00035 #   </tr>
00036 #   <tr>
00037 #     @tp @b PYTHON_VERSION_PATCH @endtp
00038 #     <td>Python patch version found e.g. 2.</td>
00039 #   </tr>
00040 # </table>
00041 #
00042 # @note This module has been copied from the Git repository of CMake on
00043 #       4/12/2012, i.e., before the release of CMake 2.8.8. Once CMake 2.8.8
00044 #       or any version is available for all major platforms, consider to
00045 #       remove this module from the BASIS package.
00046 #
00047 # @ingroup CMakeFindModules
00048 ##############################################################################
00049 
00050 #=============================================================================
00051 # Copyright 2005-2010 Kitware, Inc.
00052 # Copyright 2011 Bjoern Ricks <bjoern.ricks@gmail.com>
00053 # Copyright 2012 Rolf Eike Beer <eike@sf-mail.de>
00054 #
00055 # Redistribution and use in source and binary forms, with or without
00056 # modification, are permitted provided that the following conditions
00057 # are met:
00058 #
00059 # * Redistributions of source code must retain the above copyright
00060 #   notice, this list of conditions and the following disclaimer.
00061 #
00062 # * Redistributions in binary form must reproduce the above copyright
00063 #   notice, this list of conditions and the following disclaimer in the
00064 #   documentation and/or other materials provided with the distribution.
00065 #
00066 # * Neither the names of Kitware, Inc., the Insight Software Consortium,
00067 #   nor the names of their contributors may be used to endorse or promote
00068 #   products derived from this software without specific prior written
00069 #   permission.
00070 #
00071 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00072 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00073 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00074 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00075 # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00076 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00077 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00078 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00079 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00080 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00081 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00082 #=============================================================================
00083 
00084 unset(_Python_NAMES)
00085 
00086 set(_PYTHON1_VERSIONS 1.6 1.5)
00087 set(_PYTHON2_VERSIONS 2.7 2.6 2.5 2.4 2.3 2.2 2.1 2.0)
00088 set(_PYTHON3_VERSIONS 3.3 3.2 3.1 3.0)
00089 
00090 if(PythonInterp_FIND_VERSION)
00091     if(PythonInterp_FIND_VERSION MATCHES "^[0-9]+\\.[0-9]+(\\.[0-9]+.*)?$")
00092         string(REGEX REPLACE "^([0-9]+\\.[0-9]+).*" "\\1" _PYTHON_FIND_MAJ_MIN "${PythonInterp_FIND_VERSION}")
00093         string(REGEX REPLACE "^([0-9]+).*" "\\1" _PYTHON_FIND_MAJ "${_PYTHON_FIND_MAJ_MIN}")
00094         list(APPEND _Python_NAMES python${_PYTHON_FIND_MAJ_MIN} python${_PYTHON_FIND_MAJ})
00095         unset(_PYTHON_FIND_OTHER_VERSIONS)
00096         if(NOT PythonInterp_FIND_VERSION_EXACT)
00097             foreach(_PYTHON_V ${_PYTHON${_PYTHON_FIND_MAJ}_VERSIONS})
00098                 if(NOT _PYTHON_V VERSION_LESS _PYTHON_FIND_MAJ_MIN)
00099                     list(APPEND _PYTHON_FIND_OTHER_VERSIONS ${_PYTHON_V})
00100                 endif()
00101              endforeach()
00102         endif(NOT PythonInterp_FIND_VERSION_EXACT)
00103         unset(_PYTHON_FIND_MAJ_MIN)
00104         unset(_PYTHON_FIND_MAJ)
00105     else(PythonInterp_FIND_VERSION MATCHES "^[0-9]+\\.[0-9]+(\\.[0-9]+.*)?$")
00106         list(APPEND _Python_NAMES python${PythonInterp_FIND_VERSION})
00107         set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON${PythonInterp_FIND_VERSION}_VERSIONS})
00108     endif(PythonInterp_FIND_VERSION MATCHES "^[0-9]+\\.[0-9]+(\\.[0-9]+.*)?$")
00109 else(PythonInterp_FIND_VERSION)
00110     set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON3_VERSIONS} ${_PYTHON2_VERSIONS} ${_PYTHON1_VERSIONS})
00111 endif(PythonInterp_FIND_VERSION)
00112 
00113 list(APPEND _Python_NAMES python)
00114 
00115 # Search for the current active python version first
00116 find_program(PYTHON_EXECUTABLE NAMES ${_Python_NAMES})
00117 
00118 # Set up the versions we know about, in the order we will search. Always add
00119 # the user supplied additional versions to the front.
00120 set(_Python_VERSIONS
00121   ${Python_ADDITIONAL_VERSIONS}
00122   ${_PYTHON_FIND_OTHER_VERSIONS}
00123   )
00124 
00125 unset(_PYTHON_FIND_OTHER_VERSIONS)
00126 unset(_PYTHON1_VERSIONS)
00127 unset(_PYTHON2_VERSIONS)
00128 unset(_PYTHON3_VERSIONS)
00129 
00130 # Search for newest python version if python executable isn't found
00131 if(NOT PYTHON_EXECUTABLE)
00132     foreach(_CURRENT_VERSION ${_Python_VERSIONS})
00133       set(_Python_NAMES python${_CURRENT_VERSION})
00134       if(WIN32)
00135         list(APPEND _Python_NAMES python)
00136       endif()
00137       find_program(PYTHON_EXECUTABLE
00138         NAMES ${_Python_NAMES}
00139         PATHS [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]
00140         )
00141     endforeach()
00142 endif()
00143 
00144 # determine python version string
00145 if(PYTHON_EXECUTABLE)
00146     execute_process(COMMAND "${PYTHON_EXECUTABLE}" -E -c
00147                             "import sys; sys.stdout.write(';'.join([str(x) for x in sys.version_info[:3]]))"
00148                     OUTPUT_VARIABLE _VERSION
00149                     RESULT_VARIABLE _PYTHON_VERSION_RESULT
00150                     ERROR_QUIET)
00151     if(NOT _PYTHON_VERSION_RESULT)
00152         string(REPLACE ";" "." PYTHON_VERSION_STRING "${_VERSION}")
00153         list(GET _VERSION 0 PYTHON_VERSION_MAJOR)
00154         list(GET _VERSION 1 PYTHON_VERSION_MINOR)
00155         list(GET _VERSION 2 PYTHON_VERSION_PATCH)
00156         if(PYTHON_VERSION_PATCH EQUAL 0)
00157             # it's called "Python 2.7", not "2.7.0"
00158             string(REGEX REPLACE "\\.0$" "" PYTHON_VERSION_STRING "${PYTHON_VERSION_STRING}")
00159         endif()
00160     else()
00161         # sys.version predates sys.version_info, so use that
00162         execute_process(COMMAND "${PYTHON_EXECUTABLE}" -E -c "import sys; sys.stdout.write(sys.version)"
00163                         OUTPUT_VARIABLE _VERSION
00164                         RESULT_VARIABLE _PYTHON_VERSION_RESULT
00165                         ERROR_QUIET)
00166         if(NOT _PYTHON_VERSION_RESULT)
00167             string(REGEX REPLACE " .*" "" PYTHON_VERSION_STRING "${_VERSION}")
00168             string(REGEX REPLACE "^([0-9]+)\\.[0-9]+.*" "\\1" PYTHON_VERSION_MAJOR "${PYTHON_VERSION_STRING}")
00169             string(REGEX REPLACE "^[0-9]+\\.([0-9])+.*" "\\1" PYTHON_VERSION_MINOR "${PYTHON_VERSION_STRING}")
00170             if(PYTHON_VERSION_STRING MATCHES "^[0-9]+\\.[0-9]+\\.[0-9]+.*")
00171                 string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" PYTHON_VERSION_PATCH "${PYTHON_VERSION_STRING}")
00172             else()
00173                 set(PYTHON_VERSION_PATCH "0")
00174             endif()
00175         else()
00176             # sys.version was first documented for Python 1.5, so assume
00177             # this is older.
00178             set(PYTHON_VERSION_STRING "1.4")
00179             set(PYTHON_VERSION_MAJOR "1")
00180             set(PYTHON_VERSION_MINOR "4")
00181             set(PYTHON_VERSION_PATCH "0")
00182         endif()
00183     endif()
00184     unset(_PYTHON_VERSION_RESULT)
00185     unset(_VERSION)
00186 endif(PYTHON_EXECUTABLE)
00187 
00188 # handle the QUIETLY and REQUIRED arguments and set PYTHONINTERP_FOUND to TRUE if
00189 # all listed variables are TRUE
00190 include(FindPackageHandleStandardArgs)
00191 FIND_PACKAGE_HANDLE_STANDARD_ARGS(PythonInterp REQUIRED_VARS PYTHON_EXECUTABLE VERSION_VAR PYTHON_VERSION_STRING)
00192 
00193 mark_as_advanced(PYTHON_EXECUTABLE)