FindSphinx.cmake
Go to the documentation of this file.
1 ##############################################################################
2 # @file FindSphinx.cmake
3 # @brief Find Sphinx documentation build tools.
4 #
5 # @par Input variables:
6 # <table border="0">
7 # <tr>
8 # @tp @b Sphinx_DIR @endtp
9 # <td>Installation directory of Sphinx tools. Can also be set as environment variable.</td>
10 # </tr>
11 # <tr>
12 # @tp @b SPHINX_DIR @endtp
13 # <td>Alternative environment variable for @c Sphinx_DIR.</td>
14 # </tr>
15 # <tr>
16 # @tp @b Sphinx_FIND_COMPONENTS @endtp
17 # <td>Sphinx build tools to look for, i.e., 'apidoc' and/or 'build'.</td>
18 # </tr>
19 # </table>
20 #
21 # @par Output variables:
22 # <table border="0">
23 # <tr>
24 # @tp @b Sphinx_FOUND @endtp
25 # <td>Whether all or only the requested Sphinx build tools were found.</td>
26 # </tr>
27 # <tr>
28 # @tp @b SPHINX_FOUND @endtp
29 # <td>Alias for @c Sphinx_FOUND.<td>
30 # </tr>
31 # <tr>
32 # @tp @b SPHINX_EXECUTABLE @endtp
33 # <td>Non-cached alias for @c Sphinx-build_EXECUTABLE.</td>
34 # </tr>
35 # <tr>
36 # @tp @b Sphinx_PYTHON_EXECUTABLE @endtp
37 # <td>Python executable used to run sphinx-build. This is either the
38 # by default found Python interpreter or a specific version as
39 # specified by the shebang (#!) of the sphinx-build script.</td>
40 # </tr>
41 # <tr>
42 # @tp @b Sphinx_PYTHON_OPTIONS @endtp
43 # <td>A list of Python options extracted from the shebang (#!) of the
44 # sphinx-build script. The -E option is added by this module
45 # if the Python executable is not the system default to avoid
46 # problems with a differing setting of the @c PYTHONHOME.</td>
47 # </tr>
48 # <tr>
49 # @tp @b Sphinx-build_EXECUTABLE @endtp
50 # <td>Absolute path of the found sphinx-build tool.</td>
51 # </tr>
52 # <tr>
53 # @tp @b Sphinx-apidoc_EXECUTABLE @endtp
54 # <td>Absolute path of the found sphinx-apidoc tool.</td>
55 # </tr>
56 # <tr>
57 # @tp @b Sphinx_VERSION_STRING @endtp
58 # <td>Sphinx version found e.g. 1.1.2.</td>
59 # </tr>
60 # <tr>
61 # @tp @b Sphinx_VERSION_MAJOR @endtp
62 # <td>Sphinx major version found e.g. 1.</td>
63 # </tr>
64 # <tr>
65 # @tp @b Sphinx_VERSION_MINOR @endtp
66 # <td>Sphinx minor version found e.g. 1.</td>
67 # </tr>
68 # <tr>
69 # @tp @b Sphinx_VERSION_PATCH @endtp
70 # <td>Sphinx patch version found e.g. 2.</td>
71 # </tr>
72 # </table>
73 #
74 # @ingroup CMakeFindModules
75 ##############################################################################
76 
77 #=============================================================================
78 # Copyright 2011-2012 University of Pennsylvania
79 # Copyright 2013-2016 Andreas Schuh <andreas.schuh.84@gmail.com>
80 #
81 # Distributed under the OSI-approved BSD License (the "License");
82 # see accompanying file Copyright.txt for details.
83 #
84 # This software is distributed WITHOUT ANY WARRANTY; without even the
85 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
86 # See the License for more information.
87 #=============================================================================
88 # (To distribute this file outside of CMake, substitute the full
89 # License text for the above reference.)
90 
91 set (_Sphinx_REQUIRED_VARS)
92 
93 # ----------------------------------------------------------------------------
94 # initialize search
95 if (NOT Sphinx_DIR)
96  if (NOT $ENV{Sphinx_DIR} STREQUAL "")
97  set (Sphinx_DIR "$ENV{Sphinx_DIR}")
98  else ()
99  set (Sphinx_DIR "$ENV{SPHINX_DIR}")
100  endif ()
101 endif ()
102 
103 # ----------------------------------------------------------------------------
104 # default components to look for
105 if (NOT Sphinx_FIND_COMPONENTS)
106  set (Sphinx_FIND_COMPONENTS "build" "apidoc")
107 elseif (NOT Sphinx_FIND_COMPONENTS MATCHES "^(build|apidoc)$")
108  message (FATAL_ERROR "Invalid Sphinx component in: ${Sphinx_FIND_COMPONENTS}")
109 endif ()
110 
111 # ----------------------------------------------------------------------------
112 # find components, i.e., build tools
113 foreach (_Sphinx_TOOL IN LISTS Sphinx_FIND_COMPONENTS)
114  if (Sphinx_DIR)
115  find_program (
116  Sphinx-${_Sphinx_TOOL}_EXECUTABLE
117  NAMES sphinx-${_Sphinx_TOOL} sphinx-${_Sphinx_TOOL}.py
118  HINTS "${Sphinx_DIR}"
119  PATH_SUFFIXES bin
120  DOC "The sphinx-${_Sphinx_TOOL} Python script."
121  NO_DEFAULT_PATH
122  )
123  else ()
124  find_program (
125  Sphinx-${_Sphinx_TOOL}_EXECUTABLE
126  NAMES sphinx-${_Sphinx_TOOL} sphinx-${_Sphinx_TOOL}.py
127  DOC "The sphinx-${_Sphinx_TOOL} Python script."
128  )
129  endif ()
130  mark_as_advanced (Sphinx-${_Sphinx_TOOL}_EXECUTABLE)
131  list (APPEND _Sphinx_REQUIRED_VARS Sphinx-${_Sphinx_TOOL}_EXECUTABLE)
132 endforeach ()
133 
134 # set main Sphinx_EXECUTABLE so basis_find_package can derive DEPENDS_Sphinx_DIR
135 if (Sphinx-build_EXECUTABLE)
136  set (Sphinx_EXECUTABLE ${Sphinx-build_EXECUTABLE})
137 else ()
138  set (Sphinx_EXECUTABLE ${Sphinx-apidoc_EXECUTABLE})
139 endif ()
140 
141 # ----------------------------------------------------------------------------
142 # determine Python executable used by Sphinx
143 if (Sphinx-build_EXECUTABLE)
144  # extract python executable from shebang of sphinx-build
145  find_package (PythonInterp QUIET)
146  set (Sphinx_PYTHON_EXECUTABLE "${PYTHON_EXECUTABLE}")
147  set (Sphinx_PYTHON_OPTIONS)
148  file (STRINGS "${Sphinx-build_EXECUTABLE}" FIRST_LINE LIMIT_COUNT 1)
149  if (FIRST_LINE MATCHES "^#!(.*/python.*)") # does not match "#!/usr/bin/env python" !
150  string (REGEX REPLACE "^ +| +$" "" Sphinx_PYTHON_EXECUTABLE "${CMAKE_MATCH_1}")
151  if (Sphinx_PYTHON_EXECUTABLE MATCHES "([^ ]+) (.*)")
152  set (Sphinx_PYTHON_EXECUTABLE "${CMAKE_MATCH_1}")
153  string (REGEX REPLACE " +" ";" Sphinx_PYTHON_OPTIONS "${CMAKE_MATCH_2}")
154  endif ()
155  endif ()
156  # this is done to avoid problems with multiple Python versions being installed
157  # remember: CMake command if(STR EQUAL STR) is bad and may cause many troubles !
158  string (REGEX REPLACE "([.+*?^$])" "\\\\\\1" _Sphinx_PYTHON_EXECUTABLE_RE "${PYTHON_EXECUTABLE}")
159  list (FIND Sphinx_PYTHON_OPTIONS -E IDX)
160  if (IDX EQUAL -1 AND NOT Sphinx_PYTHON_EXECUTABLE MATCHES "^${_Sphinx_PYTHON_EXECUTABLE_RE}$")
161  list (INSERT Sphinx_PYTHON_OPTIONS 0 -E)
162  endif ()
163  unset (_Sphinx_PYTHON_EXECUTABLE_RE)
164 endif ()
165 
166 # ----------------------------------------------------------------------------
167 # determine Sphinx version
168 if (Sphinx-build_EXECUTABLE)
169  # intentionally use invalid -h option here as the help that is shown then
170  # will include the Sphinx version information
171  if (Sphinx_PYTHON_EXECUTABLE)
172  execute_process (
173  COMMAND "${Sphinx_PYTHON_EXECUTABLE}" ${Sphinx_PYTHON_OPTIONS} "${Sphinx-build_EXECUTABLE}" -h
174  OUTPUT_VARIABLE _Sphinx_VERSION
175  ERROR_VARIABLE _Sphinx_VERSION
176  )
177  elseif (UNIX)
178  execute_process (
179  COMMAND "${Sphinx-build_EXECUTABLE}" -h
180  OUTPUT_VARIABLE _Sphinx_VERSION
181  ERROR_VARIABLE _Sphinx_VERSION
182  )
183  endif ()
184  if (_Sphinx_VERSION MATCHES "Sphinx v([0-9]+\\.[0-9]+\\.[0-9]+)")
185  set (Sphinx_VERSION_STRING "${CMAKE_MATCH_1}")
186  string (REPLACE "." ";" _Sphinx_VERSION "${Sphinx_VERSION_STRING}")
187  list(GET _Sphinx_VERSION 0 Sphinx_VERSION_MAJOR)
188  list(GET _Sphinx_VERSION 1 Sphinx_VERSION_MINOR)
189  list(GET _Sphinx_VERSION 2 Sphinx_VERSION_PATCH)
190  if (Sphinx_VERSION_PATCH EQUAL 0)
191  string (REGEX REPLACE "\\.0$" "" Sphinx_VERSION_STRING "${Sphinx_VERSION_STRING}")
192  endif ()
193  endif()
194 endif ()
195 
196 # ----------------------------------------------------------------------------
197 # compatibility with FindPythonInterp.cmake and FindPerl.cmake
198 set (SPHINX_EXECUTABLE "${Sphinx-build_EXECUTABLE}")
199 
200 # ----------------------------------------------------------------------------
201 # handle the QUIETLY and REQUIRED arguments and set SPHINX_FOUND to TRUE if
202 # all listed variables are TRUE
203 include (FindPackageHandleStandardArgs)
204 FIND_PACKAGE_HANDLE_STANDARD_ARGS (
205  Sphinx
206  REQUIRED_VARS
207  ${_Sphinx_REQUIRED_VARS}
208  VERSION_VAR
209  Sphinx_VERSION_STRING
210 )
211 
212 unset (_Sphinx_VERSION)
213 unset (_Sphinx_REQUIRED_VARS)
cmake Sphinx_EXECUTABLE
cmake Sphinx_PYTHON_EXECUTABLE
cmake Sphinx_DIR
cmake Sphinx_VERSION_STRING
macro find_package()
Overloaded find_package() command.
cmake Sphinx_FIND_COMPONENTS