Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / cmake / modules / AddSphinxTarget.cmake
blobb90639fbbf073bb94a8744d12f56a92c23ca143b
1 include(GNUInstallDirs)
3 # Create sphinx target
4 if (LLVM_ENABLE_SPHINX)
5   message(STATUS "Sphinx enabled.")
6   find_package(Sphinx REQUIRED)
7   if (LLVM_BUILD_DOCS AND NOT TARGET sphinx)
8     add_custom_target(sphinx ALL)
9   endif()
10 else()
11   message(STATUS "Sphinx disabled.")
12 endif()
15 # Handy function for creating the different Sphinx targets.
17 # ``builder`` should be one of the supported builders used by
18 # the sphinx-build command.
20 # ``project`` should be the project name
22 # Named arguments:
23 # ``ENV_VARS`` should be a list of environment variables that should be set when
24 #              running Sphinx. Each environment variable should be a string with
25 #              the form KEY=VALUE.
26 function (add_sphinx_target builder project)
27   cmake_parse_arguments(ARG "" "SOURCE_DIR" "ENV_VARS" ${ARGN})
28   set(SPHINX_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/${builder}")
29   set(SPHINX_DOC_TREE_DIR "${CMAKE_CURRENT_BINARY_DIR}/_doctrees-${project}-${builder}")
30   set(SPHINX_TARGET_NAME docs-${project}-${builder})
32   if (SPHINX_WARNINGS_AS_ERRORS)
33     set(SPHINX_WARNINGS_AS_ERRORS_FLAG "-W")
34   else()
35     set(SPHINX_WARNINGS_AS_ERRORS_FLAG "")
36   endif()
38   if (NOT ARG_SOURCE_DIR)
39     set(ARG_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
40   endif()
42   if ("${LLVM_VERSION_SUFFIX}" STREQUAL "git")
43     set(PreReleaseTag "-tPreRelease")
44   endif()
46   add_custom_target(${SPHINX_TARGET_NAME}
47                     COMMAND ${CMAKE_COMMAND} -E env ${ARG_ENV_VARS}
48                             ${SPHINX_EXECUTABLE}
49                             -b ${builder}
50                             -d "${SPHINX_DOC_TREE_DIR}"
51                             -q # Quiet: no output other than errors and warnings.
52                             -t builder-${builder} # tag for builder
53                             -D version=${LLVM_VERSION_MAJOR}
54                             -D release=${PACKAGE_VERSION}
55                             ${PreReleaseTag}
56                             ${SPHINX_WARNINGS_AS_ERRORS_FLAG} # Treat warnings as errors if requested
57                             "${ARG_SOURCE_DIR}" # Source
58                             "${SPHINX_BUILD_DIR}" # Output
59                     COMMENT
60                     "Generating ${builder} Sphinx documentation for ${project} into \"${SPHINX_BUILD_DIR}\"")
62   # When "clean" target is run, remove the Sphinx build directory
63   set_property(DIRECTORY APPEND PROPERTY
64                ADDITIONAL_MAKE_CLEAN_FILES
65                "${SPHINX_BUILD_DIR}")
67   # We need to remove ${SPHINX_DOC_TREE_DIR} when make clean is run
68   # but we should only add this path once
69   get_property(_CURRENT_MAKE_CLEAN_FILES
70                DIRECTORY PROPERTY ADDITIONAL_MAKE_CLEAN_FILES)
71   if (NOT "${SPHINX_DOC_TREE_DIR}" IN_LIST _CURRENT_MAKE_CLEAN_FILES)
72     set_property(DIRECTORY APPEND PROPERTY
73                  ADDITIONAL_MAKE_CLEAN_FILES
74                  "${SPHINX_DOC_TREE_DIR}")
75   endif()
77   if (LLVM_BUILD_DOCS)
78     add_dependencies(sphinx ${SPHINX_TARGET_NAME})
80     # Handle installation
81     if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
82       if (builder STREQUAL man)
83         # FIXME: We might not ship all the tools that these man pages describe
84         install(DIRECTORY "${SPHINX_BUILD_DIR}/" # Slash indicates contents of
85                 COMPONENT "${project}-sphinx-man"
86                 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1")
88         if(NOT LLVM_ENABLE_IDE)
89           add_llvm_install_targets("install-${SPHINX_TARGET_NAME}"
90                                    DEPENDS ${SPHINX_TARGET_NAME}
91                                    COMPONENT "${project}-sphinx-man")
92         endif()
93       elseif (builder STREQUAL html)
94         string(TOUPPER "${project}" project_upper)
95         set(${project_upper}_INSTALL_SPHINX_HTML_DIR "${CMAKE_INSTALL_DOCDIR}/${project}/html"
96             CACHE STRING "HTML documentation install directory for ${project}")
98         # '/.' indicates: copy the contents of the directory directly into
99         # the specified destination, without recreating the last component
100         # of ${SPHINX_BUILD_DIR} implicitly.
101         install(DIRECTORY "${SPHINX_BUILD_DIR}/."
102                 COMPONENT "${project}-sphinx-html"
103                 DESTINATION "${${project_upper}_INSTALL_SPHINX_HTML_DIR}")
105         if(NOT LLVM_ENABLE_IDE)
106           add_llvm_install_targets("install-${SPHINX_TARGET_NAME}"
107                                    DEPENDS ${SPHINX_TARGET_NAME}
108                                    COMPONENT "${project}-sphinx-html")
109         endif()
110       else()
111         message(WARNING Installation of ${builder} not supported)
112       endif()
113     endif()
114   endif()
115 endfunction()