1# Handy function for creating the different Sphinx targets.
2#
3# ``builder`` should be one of the supported builders used by
4# the sphinx-build command.
5#
6# ``project`` should be the project name
7function (add_sphinx_target builder project)
8  set(SPHINX_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/${builder}")
9  set(SPHINX_DOC_TREE_DIR "${CMAKE_CURRENT_BINARY_DIR}/_doctrees")
10  set(SPHINX_TARGET_NAME docs-${project}-${builder})
11
12  if (SPHINX_WARNINGS_AS_ERRORS)
13    set(SPHINX_WARNINGS_AS_ERRORS_FLAG "-W")
14  else()
15    set(SPHINX_WARNINGS_AS_ERRORS_FLAG "")
16  endif()
17
18  add_custom_target(${SPHINX_TARGET_NAME}
19                    COMMAND ${SPHINX_EXECUTABLE}
20                            -b ${builder}
21                            -d "${SPHINX_DOC_TREE_DIR}"
22                            -q # Quiet: no output other than errors and warnings.
23                            ${SPHINX_WARNINGS_AS_ERRORS_FLAG} # Treat warnings as errors if requested
24                            "${CMAKE_CURRENT_SOURCE_DIR}" # Source
25                            "${SPHINX_BUILD_DIR}" # Output
26                    COMMENT
27                    "Generating ${builder} Sphinx documentation for ${project} into \"${SPHINX_BUILD_DIR}\"")
28
29  # When "clean" target is run, remove the Sphinx build directory
30  set_property(DIRECTORY APPEND PROPERTY
31               ADDITIONAL_MAKE_CLEAN_FILES
32               "${SPHINX_BUILD_DIR}")
33
34  # We need to remove ${SPHINX_DOC_TREE_DIR} when make clean is run
35  # but we should only add this path once
36  get_property(_CURRENT_MAKE_CLEAN_FILES
37               DIRECTORY PROPERTY ADDITIONAL_MAKE_CLEAN_FILES)
38  list(FIND _CURRENT_MAKE_CLEAN_FILES "${SPHINX_DOC_TREE_DIR}" _INDEX)
39  if (_INDEX EQUAL -1)
40    set_property(DIRECTORY APPEND PROPERTY
41                 ADDITIONAL_MAKE_CLEAN_FILES
42                 "${SPHINX_DOC_TREE_DIR}")
43  endif()
44
45  if (LLVM_BUILD_DOCS)
46    add_dependencies(sphinx ${SPHINX_TARGET_NAME})
47
48    # Handle installation
49    if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
50      if (builder STREQUAL man)
51        # FIXME: We might not ship all the tools that these man pages describe
52        install(DIRECTORY "${SPHINX_BUILD_DIR}/" # Slash indicates contents of
53                DESTINATION share/man/man1)
54
55      elseif (builder STREQUAL html)
56        install(DIRECTORY "${SPHINX_BUILD_DIR}"
57                DESTINATION "share/doc/${project}")
58      else()
59        message(WARNING Installation of ${builder} not supported)
60      endif()
61    endif()
62  endif()
63endfunction()
64