Debian Codegraph @

src/cmake/modules/SphinxDocument.cmake

raw
1 # Define add_sphinx_document() and helpers.
2
3 # Is a helper macro for add_sphinx_document() to add the arguments passed to
4 # the macro to all the commands that will be passed to add_custom_target().
5 MACRO(sphinx_helper_add_to_commands)
6 IF(Sphinx_FOUND)
7 LIST(APPEND _COMMAND ${ARGV})
8 FOREACH(_X ${_EXTRA_COMMANDS})
9 LIST(APPEND ${_X} ${ARGV})
10 ENDFOREACH()
11 ENDIF()
12 ENDMACRO()
13
14
15 # Is a helper macro for add_sphinx_document() to set the first argument to
16 # the path in the second argument with the third argument used as the base
17 # for relative paths. If the fourth argument evaluates to be true, the
18 # path will be converted to the native format.
19 MACRO(sphinx_helper_convert_path _OUT _IN _BASE _NATIVE)
20 IF("${_IN}" MATCHES "^[/~]")
21 IF(${_NATIVE})
22 FILE(TO_NATIVE_PATH "${_IN}" ${_OUT})
23 ELSE()
24 SET(${_OUT} "${_IN}")
25 ENDIF()
26 ELSEIF("${_BASE}" MATCHES "/$")
27 IF(${_NATIVE})
28 FILE(TO_NATIVE_PATH "${_BASE}${_IN}" ${_OUT})
29 ELSE()
30 SET(${_OUT} "${_BASE}${_IN}")
31 ENDIF()
32 ELSE()
33 IF(${_NATIVE})
34 FILE(TO_NATIVE_PATH "${_BASE}/${_IN}" ${_OUT})
35 ELSE()
36 SET(${_OUT} "${_BASE}/${_IN}")
37 ENDIF()
38 ENDIF()
39 ENDMACRO()
40
41
42 # add_sphinx_document(
43 # <target_name>
44 # SOURCE_DIR <source_dir> |
45 # SOURCE_TARGET <source_target> [SOURCE_TARGET_DIR <source_dir>]
46 # [[NOCONF] [[CONF_DIR <conf_dir] |
47 # [CONF_TARGET <conf_target> [CONF_TARGET_DIR <conf_dir>]]]]
48 # [OUTPUT_FORMATS <format1> ...]
49 # [OUTPUT_DIR <output_dir>]
50 # [CONF_OPTIONS <option1> ...]
51 # [ALL]
52 # [BUILD_FAIL_NO_SPHINX] [BUILD_MESSAGE_NO_SPHINX message_part ...]
53 # )
54 # <target_name> is the name of the target that will build a set of documention
55 # with sphinx-build. The target will be created with add_custom_target().
56 # If sphinx-build was not found with find_package(Sphinx), the target will
57 # generate a message, customizable with BUILD_MESSAGE_NO_SPHINX, and, if
58 # configured with BUILD_FAIL_NO_SPHINX, cause that build step to fail.
59 # SOURCE_DIR specifies that <source_dir> is the source directory to be used
60 # with sphinx-build for the target. If <source_dir> is a relative path,
61 # it is evaluated with respect to CMAKE_CURRENT_SOURCE_DIR.
62 # SOURCE_TARGET specifies that the source directory for the target will be
63 # generated by another target, <source_target>, created by
64 # add_custom_target(). SOURCE_TARGET_DIR specifies the directory,
65 # <source_dir>, that is created by <source_target> and will be passed
66 # to sphinx-build as the source directory for the documentation. If
67 # <source_dir> is a relative path, it is interpreted relative to the
68 # value of the RUNTIME_OUTPUT_DIRECTORY property of <source_target>.
69 # If SOURCE_TARGET_DIR is not present, the source directory will be the
70 # value of the RUNTIME_OUTPUT_DIRECTORY property of <source_target>.
71 # <source_target> will be added as a prerequisite for <target_name> with
72 # add_dependencies().
73 # NOCONF specifies that the target will not have a configuration file and
74 # all configuration will have to be done via the options set with
75 # CONF_OPTIONS.
76 # CONF_DIR specifies that <conf_dir> is the directory with the conf.py file
77 # that sphinx-build should use for the target. If <conf_dir> is a
78 # relative path, it is evaluated with respect to CMAKE_CURRENT_SOURCE_DIR.
79 # If NOCONF is not set and neither CONF_DIR nor CONF_TARGET are used, the
80 # path to conf.py in the directory set by SOURCE_DIR or SOURCE_TARGET.
81 # CONF_TARGET specifies that the configuration file for the target will be
82 # generated by another target, <conf_target>, created by
83 # add_custom_target(). CONF_TARGET_DIR specifies the directory,
84 # <conf_dir>, that is created by <conf_target> and will be passed as the
85 # location of the conf.py file to sphinx-build. If <conf_dir> is a
86 # relative path, it will be interpreted relative to the value of the
87 # RUNTIME_OUTPUT_DIRECTORY property of <conf_target>. If CONF_TARGET_DIR
88 # is not present, the path to conf.py will be the value of the
89 # RUNTIME_OUTPUT_DIRECTORY property of <conf_target>. <conf_target>
90 # will be added as a prerequisite for <target_name> with
91 # add_dependencies(). If NOCONF is not set and neither CONF_DIR nor
92 # CONF_TARGET are used, the path to conf.py will be the directory set by
93 # SOURCE_DIR or SOURCE_TARGET.
94 # OUTPUT_FORMATS specifies the output formats that the target will generate.
95 # Each output format must be an allowed value for the "-b" option to
96 # sphinx-build. One a target is created, use
97 # sphinx_document_get_output_formats() on it to get the formts it will
98 # generate.
99 # OUTPUT_DIR specifies that <output_dir> is the base directory for where
100 # results and intermediates will be written. If <output_dir> is a relative
101 # path, it will be interpreted relative to CMAKE_CURRENT_BINARY_DIR.
102 # If not set, the base output directory will be
103 # CMAKE_RUNTIME_OUTPUT_DIRECTORY. Once a target is created, use
104 # sphinx_document_get_output_directory() on it to get the subdirectory of
105 # the base directory where the generated documentation will be written.
106 # Use sphinx_document_get_output_formats() to get the list of the names
107 # of the output formats generated. The result for each format will be
108 # stored in a subdirectory, with the same name as the name of the format,
109 # of the directory returned by sphinx_document_get_output_directory().
110 # CONF_OPTIONS specifies one or more options to override what is in the
111 # configuration file: each option will be passed to sphinx-build with
112 # a "-D" option.
113 # ALL causes the generated target to be added to the default build target.
114 # BUILD_FAIL_NO_SPHINX causes the target to fail if triggered during a build
115 # and sphinx-build was not available.
116 # BUILD_MESSAGE_NO_SPHINX customizes the message shown when the target is
117 # triggered during a build and sphinx-build was not available. Set the
118 # message to an empty string to not have any message printed.
119 #
120 # Usage notes:
121 # If you are using CONF_TARGET to pick up a conf.py generated in the build
122 # directories, the things that conf.py pulls in, like the _static or
123 # _templates directories, will have to be copied to the build directory
124 # (or generated some other way there) so they can be found by sphinx-build
125 # at the same locations relative to conf.py.
126 #
127 # Limitations:
128 # Currently provides no way to allow the caller to pass "-a", "-E", "-t",
129 # "-j", "-A", "-n", "-N", "-v", "-q", "-Q", "-w", "-W", "--keep-going",
130 # "-T", or "-P" options to sphinx-build.
131 #
132 FUNCTION(add_sphinx_document _TARGET_NAME)
133 FIND_PACKAGE(Sphinx)
134
135 # Parse the arguments.
136 SET(
137 _OPTIONS
138 NOCONF
139 ALL
140 BUILD_FAIL_NO_SPHINX
141 )
142 SET(
143 _ONE_VALUE_ARGS
144 SOURCE_DIR
145 SOURCE_TARGET
146 SOURCE_TARGET_DIR
147 CONF_DIR
148 CONF_TARGET
149 CONF_TARGET_DIR
150 OUTPUT_DIR
151 )
152 SET(
153 _MULT_VALUE_ARGS
154 OUTPUT_FORMATS
155 CONF_OPTIONS
156 BUILD_MESSAGE_NO_SPHINX
157 )
158 CMAKE_PARSE_ARGUMENTS(_IN "${_OPTIONS}" "${_ONE_VALUE_ARGS}" "${_MULT_VALUE_ARGS}" ${ARGN})
159 IF(DEFINED _IN_UNPARSED_ARGUMENTS)
160 MESSAGE(FATAL_ERROR "add_sphinx_document() was called with one or more unrecognized arguments: ${_IN_UNPARSED_ARGUMENTS}")
161 ENDIF()
162
163 # Initialize a list of the target dependencies.
164 UNSET(_TARGET_DEPENDS)
165 # Initialize the list, _COMMAND, that will be the first command passed to
166 # add_custom_target(). Also initialize the list of the other commands that
167 # will be passed.
168 UNSET(_EXTRA_COMMANDS)
169 SET(_EXTRA_INDEX -1)
170 IF(Sphinx_FOUND)
171 SET(_COMMAND "${SPHINX_EXECUTABLE}")
172 ELSE()
173 IF(DEFINED _IN_BUILD_MESSAGE_NO_SPHINX)
174 IF(NOT _IN_BUILD_MESSAGE_NO_SPHINX STREQUAL "")
175 SET(
176 _COMMAND
177 "${CMAKE_COMMAND}" "-E" "echo"
178 ${_IN_BUILD_MESSAGE_NO_SPHINX}
179 )
180 ELSE()
181 UNSET(_COMMAND)
182 ENDIF()
183 ELSE()
184 SET(
185 _COMMAND
186 "${CMAKE_COMMAND}" "-E" "echo"
187 "sphinx-build not found; cannot build ${_TARGET_NAME}"
188 )
189 ENDIF()
190 IF(_IN_BUILD_FAIL_NO_SPHINX)
191 IF(DEFINED _COMMAND)
192 MATH(EXPR _EXTRA_INDEX "${_EXTRA_INDEX} + 1")
193 SET(_Y "_EXTRA_COMMAND_${_EXTRA_INDEX}")
194 LIST(APPEND _EXTRA_COMMANDS ${_Y})
195 SET(${_Y} "${CMAKE_COMMAND}" "-E" "false")
196 ELSE()
197 SET(_COMMAND "${CMAKE_COMMAND}" "-E" "false")
198 ENDIF()
199 ENDIF()
200 ENDIF()
201
202 IF((DEFINED _IN_OUTPUT_FORMATS) AND Sphinx_FOUND)
203 FOREACH(_X ${_IN_OUTPUT_FORMATS})
204 IF(_EXTRA_INDEX EQUAL -1)
205 LIST(APPEND _COMMAND "-b" "${_X}")
206 ELSE()
207 SET(_Y "_EXTRA_COMMAND_${_EXTRA_INDEX}")
208 LIST(APPEND _EXTRA_COMMANDS "${_Y}")
209 SET(${_Y} "${SPHINX_EXECUTABLE}" "-b" "${_X}")
210 ENDIF()
211 MATH(EXPR _EXTRA_INDEX "${_EXTRA_INDEX} + 1")
212 ENDFOREACH()
213 ENDIF()
214
215 # Handle the source directory.
216 IF(DEFINED _IN_SOURCE_DIR)
217 IF((DEFINED _IN_SOURCE_TARGET) OR (DEFINED _IN_SOURCE_TARGET_DIR))
218 MESSAGE(FATAL_ERROR "add_sphinx_document() was called with SOURCE_DIR and one or more of SOURCE_TARGET and SOURCE_TARGET_DIR")
219 ENDIF()
220 SPHINX_HELPER_CONVERT_PATH(
221 _NATIVE_SOURCE_DIR
222 "${_IN_SOURCE_DIR}"
223 "${CMAKE_CURRENT_SOURCE_DIR}"
224 YES
225 )
226 ELSEIF(DEFINED _IN_SOURCE_TARGET)
227 IF(NOT DEFINED _IN_SOURCE_TARGET_DIR)
228 SET(_IN_SOURCE_TARGET_DIR "")
229 ENDIF()
230 GET_PROPERTY(
231 _X
232 TARGET ${_IN_SOURCE_TARGET}
233 PROPERTY "RUNTIME_OUTPUT_DIRECTORY"
234 )
235 SPHINX_HELPER_CONVERT_PATH(
236 _NATIVE_SOURCE_TARGET_DIR
237 "${_IN_SOURCE_TARGET_DIR}"
238 "${_X}"
239 YES
240 )
241 LIST(APPEND _TARGET_DEPENDS "${_IN_SOURCE_TARGET}")
242 ELSE()
243 MESSAGE(FATAL_ERROR "add_sphinx_document() was called without either SOURCE_DIR or SOURCE_TARGET")
244 ENDIF()
245
246 # Handle the configuration file.
247 IF(_IN_NOCONF)
248 IF((DEFINED _IN_CONF_DIR) OR (DEFINED _IN_CONF_TARGET)
249 OR (DEFINED _IN_CONF_TARGET_DIR))
250 MESSAGE(FATAL_ERROR "add_sphinx_document() was called with NOCONF and one or more of CONF_DIR, CONF_TARGET, or CONF_TARGET_DIR")
251 ENDIF()
252 SPHINX_HELPER_ADD_TO_COMMANDS("-C")
253 ELSEIF(DEFINED _IN_CONF_DIR)
254 IF(DEFINED _IN_CONF_TARGET)
255 MESSAGE(FATAL_ERROR "add_sphinx_document() was called with both CONF_DIR and CONF_TARGET")
256 ENDIF()
257 SPHINX_HELPER_CONVERT_PATH(
258 _NATIVE_CONF_DIR
259 "${_IN_CONF_DIR}"
260 "${CMAKE_CURRENT_SOURCE_DIR}"
261 YES
262 )
263 ELSEIF(DEFINED _IN_CONF_TARGET)
264 IF(NOT DEFINED _IN_CONF_TARGET_DIR)
265 SET(_IN_CONF_TARGET_DIR "")
266 ENDIF()
267 GET_PROPERTY(
268 _X
269 TARGET ${_IN_CONF_TARGET}
270 PROPERTY "RUNTIME_OUTPUT_DIRECTORY"
271 )
272 SPHINX_HELPER_CONVERT_PATH(
273 _NATIVE_CONF_TARGET_DIR
274 "${_IN_CONF_TARGET_DIR}"
275 "${_X}"
276 YES
277 )
278 LIST(APPEND _TARGET_DEPENDS "${_IN_CONF_TARGET}")
279 SPHINX_HELPER_ADD_TO_COMMANDS("-c" "${_NATIVE_CONF_TARGET_DIR}")
280 ELSE()
281 IF(DEFINED _IN_SOURCE_DIR)
282 SET(_IN_CONF_DIR "${_IN_SOURCE_DIR}")
283 SET(_X "${CMAKE_CURRENT_SOURCE_DIR}")
284 ELSE()
285 SET(_IN_CONF_DIR "${_IN_SOURCE_TARGET_DIR}")
286 GET_PROPERTY(
287 _X
288 TARGET ${_IN_CONF_TARGET}
289 PROPERTY "RUNTIME_OUTPUT_DIRECTORY"
290 )
291 ENDIF()
292 SPHINX_HELPER_CONVERT_PATH(
293 _NATIVE_CONF_DIR
294 "${_IN_CONF_DIR}"
295 "${_X}"
296 YES
297 )
298 ENDIF()
299
300 # Handle the output directory.
301 IF(NOT (DEFINED _IN_OUTPUT_DIR))
302 SET(_IN_OUTPUT_DIR "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
303 ENDIF()
304 SPHINX_HELPER_CONVERT_PATH(
305 _OUT_OUTPUT_BASE_DIR
306 "${_IN_OUTPUT_DIR}"
307 "${CMAKE_CURRENT_BINARY_DIR}"
308 NO
309 )
310 SPHINX_HELPER_CONVERT_PATH(
311 _NATIVE_OUTPUT_CACHE_DIR
312 "_doctree"
313 "${_OUT_OUTPUT_BASE_DIR}"
314 YES
315 )
316 SPHINX_HELPER_ADD_TO_COMMANDS("-d" "${_NATIVE_OUTPUT_CACHE_DIR}")
317
318 # Handle the overrides for values set in the configuration file.
319 IF(DEFINED _IN_CONF_OPTIONS)
320 FOREACH(_X ${_IN_CONF_OPTIONS})
321 SPHINX_HELPER_ADD_TO_COMMANDS("-D" "${_X}")
322 ENDFOREACH()
323 ENDIF()
324
325 # Add the source directory as the first non-option to the command.
326 IF(DEFINED _IN_SOURCE_DIR)
327 SPHINX_HELPER_ADD_TO_COMMANDS("${_NATIVE_SOURCE_DIR}")
328 ELSE()
329 SPHINX_HELPER_ADD_TO_COMMANDS("${_NATIVE_SOURCE_TARGET_DIR}")
330 ENDIF()
331
332 # Add the output directory for documents as the second non-option to the
333 # the command. Put each format in a separate directory, like the make
334 # mode for sphinx-build does.
335 IF((DEFINED _IN_OUTPUT_FORMATS) AND Sphinx_FOUND)
336 SET(_EXTRA_INDEX -1)
337 FOREACH(_X ${_IN_OUTPUT_FORMATS})
338 SPHINX_HELPER_CONVERT_PATH(
339 _NATIVE_OUTPUT_DOC_DIR
340 "${_X}"
341 "${_OUT_OUTPUT_BASE_DIR}"
342 YES
343 )
344 IF(_EXTRA_INDEX EQUAL -1)
345 LIST(APPEND _COMMAND "${_NATIVE_OUTPUT_DOC_DIR}")
346 ELSE()
347 SET(_Y "_EXTRA_COMMAND_${_EXTRA_INDEX}")
348 LIST(APPEND ${_Y} "${_NATIVE_OUTPUT_DOC_DIR}")
349 ENDIF()
350 MATH(EXPR _EXTRA_INDEX "${_EXTRA_INDEX} + 1")
351 ENDFOREACH()
352 ELSEIF(Sphinx_FOUND)
353 # There were no formats specified so use the bare output directory.
354 SPHINX_HELPER_CONVERT_PATH(
355 _NATIVE_OUTPUT_DOC_DIR
356 "${_OUT_OUTPUT_BASE_DIR}"
357 ""
358 YES
359 )
360 SPHINX_HELPER_ADD_TO_COMMANDS("${_NATIVE_OUTPUT_DOC_DIR}")
361 ENDIF()
362
363 # Convert some variables for convenient expansion in create_custom_target().
364 IF(_IN_ALL)
365 SET(_IN_ALL "ALL")
366 ELSE()
367 SET(_IN_ALL "")
368 ENDIF()
369 SET(_EXTRA "")
370 IF (NOT (_EXTRA_COMMANDS STREQUAL ""))
371 FOREACH(_X ${_EXTRA_COMMANDS})
372 SET(_EXTRA ${_EXTRA} "COMMAND" ${${_X}})
373 ENDFOREACH()
374 ENDIF()
375
376 # Create the target. Modify and restore CMAKE_RUNTIME_OUTPUT_DIRECTORY
377 # so that the target's RUNTIME_OUTPUT_DIRECTORY property reflects what
378 # was set by the OUTPUT_DIR argument.
379 IF(DEFINED CMAKE_RUNTIME_OUTPUT_DIRECTORY)
380 SET(_OLD "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
381 ELSE()
382 UNSET(_OLD)
383 ENDIF()
384 SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${_OUT_OUTPUT_BASE_DIR}")
385 ADD_CUSTOM_TARGET(${_TARGET_NAME} ${_IN_ALL} ${_COMMAND} ${_EXTRA} VERBATIM)
386 IF(DEFINED _OLD)
387 SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${_OLD}")
388 ELSE()
389 UNSET(CMAKE_RUNTIME_OUTPUT_DIRECTORY)
390 ENDIF()
391
392 # Add the target dependencies.
393 IF(NOT ("${_TARGET_DEPENDS}" STREQUAL ""))
394 ADD_DEPENDENCIES(${_TARGET_NAME} ${_TARGET_DEPENDS})
395 ENDIF()
396
397 # Define properties on the target to describe the placement of the output.
398 # That's to allow downstream targets (installation or packaging, for
399 # instance) to query it with sphinx_document_get_output_directory() and
400 # sphinx_document_get_output_formats().
401 GET_PROPERTY(
402 _PROP_DEFINED
403 TARGET ${_TARGET_NAME}
404 PROPERTY "SPHINX_OUTPUT_DIRECTORY"
405 DEFINED
406 )
407 IF(NOT _PROP_DEFINED)
408 DEFINE_PROPERTY(
409 TARGET
410 PROPERTY "SPHINX_OUTPUT_DIRECTORY"
411 BRIEF_DOCS "output directory for documention generated by sphinx-build"
412 FULL_DOCS "Is the output directory associated with a target for documentation generated by sphinx-build. The path is a full one and is in CMake's format. The property reflects the directory configure into the target; changing the property won't affect the behavior of the target."
413 )
414 ENDIF()
415 SET_PROPERTY(
416 TARGET ${_TARGET_NAME}
417 PROPERTY "SPHINX_OUTPUT_DIRECTORY"
418 "${_OUT_OUTPUT_BASE_DIR}"
419 )
420
421 GET_PROPERTY(
422 _PROP_DEFINED
423 TARGET ${_TARGET_NAME}
424 PROPERTY "SPHINX_OUTPUT_FORMATS"
425 DEFINED
426 )
427 IF(NOT _PROP_DEFINED)
428 DEFINE_PROPERTY(
429 TARGET
430 PROPERTY "SPHINX_OUTPUT_FORMATS"
431 BRIEF_DOCS "list of output formats generated by sphinx-build"
432 FULL_DOCS "Is the list of output formats generate by sphinx-build for a target. The output for each format will be in its own subdirectory of the directory named by the target's SPHINX_OUTPUT_DIRECTORY property. The name of the subdirectory will be the same as the name of the format. The property reflects which formats were configured into the target; changing the property won't affect the behavior of the target."
433 )
434 ENDIF()
435 IF(DEFINED _IN_OUTPUT_FORMATS)
436 SET_PROPERTY(
437 TARGET ${_TARGET_NAME}
438 PROPERTY "SPHINX_OUTPUT_FORMATS"
439 ${_IN_OUTPUT_FORMATS}
440 )
441 ENDIF()
442 ENDFUNCTION()
443
444
445 # sphinx_document_get_output_directory(<VAR> target)
446 # Queries the given target for its output directory with the documentation
447 # generated by sphinx-build. If that has not been set, it will set <VAR>
448 # to <VAR>-NOTFOUND. Otherwise it will set <VAR> to the path to the
449 # directory.
450 FUNCTION(sphinx_document_get_output_directory _OUT _TARGET)
451 GET_TARGET_PROPERTY(_RESULT ${_TARGET} "SPHINX_OUTPUT_DIRECTORY")
452 SET(${_OUT} "${_RESULT}" PARENT_SCOPE)
453 ENDFUNCTION()
454
455
456 # sphinx_document_get_output_formats(<VAR> target)
457 # Queries the given target for its list of output formats generated by
458 # sphinx-build. If that has not been set, it will set <VAR> to
459 # <VAR>-NOTFOUND. Otherwise it will set <VAR> to the list of the formats.
460 # The location of the documentation generated for a particular format will
461 # be a subdirectory, with the same name as the name of the format, of the
462 # directory returned by sphinx_document_get_output_directory().
463 FUNCTION(sphinx_document_get_output_formats _OUT _TARGET)
464 GET_TARGET_PROPERTY(_RESULT ${_TARGET} "SPHINX_OUTPUT_FORMATS")
465 SET(${_OUT} "${_RESULT}" PARENT_SCOPE)
466 ENDFUNCTION()