BASIS  r3148
FindSphinx.cmake
Go to the documentation of this file.
00001 ##############################################################################
00002 # @file  FindSphinx.cmake
00003 # @brief Find Sphinx documentation build tools.
00004 #
00005 # @par Input variables:
00006 # <table border="0">
00007 #   <tr>
00008 #     @tp @b Sphinx_DIR @endtp
00009 #     <td>Installation directory of Sphinx tools. Can also be set as environment variable.</td>
00010 #   </tr>
00011 #   <tr>
00012 #     @tp @b SPHINX_DIR @endtp
00013 #     <td>Alternative environment variable for @c Sphinx_DIR.</td>
00014 #   </tr>
00015 #   <tr>
00016 #     @tp @b Sphinx_FIND_COMPONENTS @endtp
00017 #     <td>Sphinx build tools to look for, i.e., 'apidoc' and/or 'build'.</td>
00018 #   </tr>
00019 # </table>
00020 #
00021 # @par Output variables:
00022 # <table border="0">
00023 #   <tr>
00024 #     @tp @b Sphinx_FOUND @endtp
00025 #     <td>Whether all or only the requested Sphinx build tools were found.</td>
00026 #   </tr>
00027 #   <tr>
00028 #     @tp @b SPHINX_FOUND @endtp
00029 #     <td>Alias for @c Sphinx_FOUND.<td>
00030 #   </tr>
00031 #   <tr>
00032 #     @tp @b SPHINX_EXECUTABLE @endtp
00033 #     <td>Non-cached alias for @c Sphinx-build_EXECUTABLE.</td>
00034 #   </tr>
00035 #   <tr>
00036 #     @tp @b Sphinx_PYTHON_EXECUTABLE @endtp
00037 #     <td>Python executable used to run sphinx-build. This is either the
00038 #         by default found Python interpreter or a specific version as
00039 #         specified by the shebang (#!) of the sphinx-build script.</td>
00040 #   </tr>
00041 #   <tr>
00042 #     @tp @b Sphinx_PYTHON_OPTIONS @endtp
00043 #     <td>A list of Python options extracted from the shebang (#!) of the
00044 #         sphinx-build script. The -E option is added by this module
00045 #         if the Python executable is not the system default to avoid
00046 #         problems with a differing setting of the @c PYTHONHOME.</td>
00047 #   </tr>
00048 #   <tr>
00049 #     @tp @b Sphinx-build_EXECUTABLE @endtp
00050 #     <td>Absolute path of the found sphinx-build tool.</td>
00051 #   </tr>
00052 #   <tr>
00053 #     @tp @b Sphinx-apidoc_EXECUTABLE @endtp
00054 #     <td>Absolute path of the found sphinx-apidoc tool.</td>
00055 #   </tr>
00056 #   <tr>
00057 #     @tp @b Sphinx_VERSION_STRING @endtp
00058 #     <td>Sphinx version found e.g. 1.1.2.</td>
00059 #   </tr>
00060 #   <tr>
00061 #     @tp @b Sphinx_VERSION_MAJOR @endtp
00062 #     <td>Sphinx major version found e.g. 1.</td>
00063 #   </tr>
00064 #   <tr>
00065 #     @tp @b Sphinx_VERSION_MINOR @endtp
00066 #     <td>Sphinx minor version found e.g. 1.</td>
00067 #   </tr>
00068 #   <tr>
00069 #     @tp @b Sphinx_VERSION_PATCH @endtp
00070 #     <td>Sphinx patch version found e.g. 2.</td>
00071 #   </tr>
00072 # </table>
00073 #
00074 # @ingroup CMakeFindModules
00075 ##############################################################################
00076 
00077 set (_Sphinx_REQUIRED_VARS)
00078 
00079 # ----------------------------------------------------------------------------
00080 # initialize search
00081 if (NOT Sphinx_DIR)
00082   if (NOT $ENV{Sphinx_DIR} STREQUAL "")
00083     set (Sphinx_DIR "$ENV{Sphinx_DIR}" CACHE PATH "Installation prefix of Sphinx (docutils)." FORCE)
00084   else ()
00085     set (Sphinx_DIR "$ENV{SPHINX_DIR}" CACHE PATH "Installation prefix of Sphinx (docutils)." FORCE)
00086   endif ()
00087 endif ()
00088 
00089 # ----------------------------------------------------------------------------
00090 # default components to look for
00091 if (NOT Sphinx_FIND_COMPONENTS)
00092   set (Sphinx_FIND_COMPONENTS "build" "apidoc")
00093 elseif (NOT Sphinx_FIND_COMPONENTS MATCHES "^(build|apidoc)$")
00094   message (FATAL_ERROR "Invalid Sphinx component in: ${Sphinx_FIND_COMPONENTS}")
00095 endif ()
00096 
00097 # ----------------------------------------------------------------------------
00098 # find components, i.e., build tools
00099 foreach (_Sphinx_TOOL IN LISTS Sphinx_FIND_COMPONENTS)
00100   if (Sphinx_DIR)
00101     find_program (
00102       Sphinx-${_Sphinx_TOOL}_EXECUTABLE
00103       NAMES         sphinx-${_Sphinx_TOOL} sphinx-${_Sphinx_TOOL}.py
00104       HINTS         "${Sphinx_DIR}"
00105       PATH_SUFFIXES bin
00106       DOC           "The sphinx-${_Sphinx_TOOL} Python script."
00107       NO_DEFAULT_PATH
00108     )
00109   else ()
00110     find_program (
00111       Sphinx-${_Sphinx_TOOL}_EXECUTABLE
00112       NAMES sphinx-${_Sphinx_TOOL} sphinx-${_Sphinx_TOOL}.py
00113       DOC   "The sphinx-${_Sphinx_TOOL} Python script."
00114     )
00115   endif ()
00116   mark_as_advanced (Sphinx-${_Sphinx_TOOL}_EXECUTABLE)
00117   list (APPEND _Sphinx_REQUIRED_VARS Sphinx-${_Sphinx_TOOL}_EXECUTABLE)
00118 endforeach ()
00119 
00120 # ----------------------------------------------------------------------------
00121 # determine Python executable used by Sphinx
00122 if (Sphinx-build_EXECUTABLE)
00123   # extract python executable from shebang of sphinx-build
00124   find_package (PythonInterp QUIET)
00125   set (Sphinx_PYTHON_EXECUTABLE "${PYTHON_EXECUTABLE}")
00126   set (Sphinx_PYTHON_OPTIONS)
00127   file (STRINGS "${Sphinx-build_EXECUTABLE}" FIRST_LINE LIMIT_COUNT 1)
00128   if (FIRST_LINE MATCHES "^#!(.*/python.*)") # does not match "#!/usr/bin/env python" !
00129     string (REGEX REPLACE "^ +| +$" "" Sphinx_PYTHON_EXECUTABLE "${CMAKE_MATCH_1}")
00130     if (Sphinx_PYTHON_EXECUTABLE MATCHES "([^ ]+) (.*)")
00131       set (Sphinx_PYTHON_EXECUTABLE "${CMAKE_MATCH_1}")
00132       string (REGEX REPLACE " +" ";" Sphinx_PYTHON_OPTIONS "${CMAKE_MATCH_2}")
00133     endif ()
00134   endif ()
00135   # this is done to avoid problems with multiple Python versions being installed
00136   # remember: CMake command if(STR EQUAL STR) is bad and may cause many troubles !
00137   string (REGEX REPLACE "([.+*?^$])" "\\\\\\1" _Sphinx_PYTHON_EXECUTABLE_RE "${PYTHON_EXECUTABLE}")
00138   list (FIND Sphinx_PYTHON_OPTIONS -E IDX)
00139   if (IDX EQUAL -1 AND NOT Sphinx_PYTHON_EXECUTABLE MATCHES "^${_Sphinx_PYTHON_EXECUTABLE_RE}$")
00140     list (INSERT Sphinx_PYTHON_OPTIONS 0 -E)
00141   endif ()
00142   unset (_Sphinx_PYTHON_EXECUTABLE_RE)
00143 endif ()
00144 
00145 # ----------------------------------------------------------------------------
00146 # determine Sphinx version
00147 if (Sphinx-build_EXECUTABLE)
00148   # intentionally use invalid -h option here as the help that is shown then
00149   # will include the Sphinx version information
00150   if (Sphinx_PYTHON_EXECUTABLE)
00151     execute_process (
00152       COMMAND "${Sphinx_PYTHON_EXECUTABLE}" ${Sphinx_PYTHON_OPTIONS} "${Sphinx-build_EXECUTABLE}" -h
00153       OUTPUT_VARIABLE _Sphinx_VERSION
00154       ERROR_VARIABLE  _Sphinx_VERSION
00155     )
00156   elseif (UNIX)
00157     execute_process (
00158       COMMAND "${Sphinx-build_EXECUTABLE}" -h
00159       OUTPUT_VARIABLE _Sphinx_VERSION
00160       ERROR_VARIABLE  _Sphinx_VERSION
00161     )
00162   endif ()
00163   if (_Sphinx_VERSION MATCHES "Sphinx v([0-9]+\\.[0-9]+\\.[0-9]+)")
00164     set (Sphinx_VERSION_STRING "${CMAKE_MATCH_1}")
00165     string (REPLACE "." ";" _Sphinx_VERSION "${Sphinx_VERSION_STRING}")
00166     list(GET _Sphinx_VERSION 0 Sphinx_VERSION_MAJOR)
00167     list(GET _Sphinx_VERSION 1 Sphinx_VERSION_MINOR)
00168     list(GET _Sphinx_VERSION 2 Sphinx_VERSION_PATCH)
00169     if (Sphinx_VERSION_PATCH EQUAL 0)
00170       string (REGEX REPLACE "\\.0$" "" Sphinx_VERSION_STRING "${Sphinx_VERSION_STRING}")
00171     endif ()
00172   endif()
00173 endif ()
00174 
00175 # ----------------------------------------------------------------------------
00176 # compatibility with FindPythonInterp.cmake and FindPerl.cmake
00177 set (SPHINX_EXECUTABLE "${Sphinx-build_EXECUTABLE}")
00178 
00179 # ----------------------------------------------------------------------------
00180 # handle the QUIETLY and REQUIRED arguments and set SPHINX_FOUND to TRUE if
00181 # all listed variables are TRUE
00182 include (FindPackageHandleStandardArgs)
00183 FIND_PACKAGE_HANDLE_STANDARD_ARGS (
00184   Sphinx
00185   REQUIRED_VARS
00186     ${_Sphinx_REQUIRED_VARS}
00187   VERSION_VAR
00188     Sphinx_VERSION_STRING
00189 )
00190 
00191 # ----------------------------------------------------------------------------
00192 # set Sphinx_DIR
00193 if (NOT Sphinx_DIR AND Sphinx-build_EXECUTABLE)
00194   get_filename_component (Sphinx_DIR "${Sphinx-build_EXECUTABLE}" PATH)
00195   string (REGEX REPLACE "/bin/?" "" Sphinx_DIR "${Sphinx_DIR}")
00196   set (Sphinx_DIR "${Sphinx_DIR}" CACHE PATH "Installation directory of Sphinx tools." FORCE)
00197 endif ()
00198 
00199 unset (_Sphinx_VERSION)
00200 unset (_Sphinx_REQUIRED_VARS)