1 2# Create sphinx target 3if (LLVM_ENABLE_SPHINX) 4 message(STATUS "Sphinx enabled.") 5 find_package(Sphinx REQUIRED) 6 if (LLVM_BUILD_DOCS AND NOT TARGET sphinx) 7 add_custom_target(sphinx ALL) 8 endif() 9else() 10 message(STATUS "Sphinx disabled.") 11endif() 12 13 14# Handy function for creating the different Sphinx targets. 15# 16# ``builder`` should be one of the supported builders used by 17# the sphinx-build command. 18# 19# ``project`` should be the project name 20function (add_sphinx_target builder project) 21 cmake_parse_arguments(ARG "" "SOURCE_DIR" "" ${ARGN}) 22 set(SPHINX_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/${builder}") 23 set(SPHINX_DOC_TREE_DIR "${CMAKE_CURRENT_BINARY_DIR}/_doctrees-${project}-${builder}") 24 set(SPHINX_TARGET_NAME docs-${project}-${builder}) 25 26 if (SPHINX_WARNINGS_AS_ERRORS) 27 set(SPHINX_WARNINGS_AS_ERRORS_FLAG "-W") 28 else() 29 set(SPHINX_WARNINGS_AS_ERRORS_FLAG "") 30 endif() 31 32 if (NOT ARG_SOURCE_DIR) 33 set(ARG_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}") 34 endif() 35 36 add_custom_target(${SPHINX_TARGET_NAME} 37 COMMAND ${SPHINX_EXECUTABLE} 38 -b ${builder} 39 -d "${SPHINX_DOC_TREE_DIR}" 40 -q # Quiet: no output other than errors and warnings. 41 -t builder-${builder} # tag for builder 42 ${SPHINX_WARNINGS_AS_ERRORS_FLAG} # Treat warnings as errors if requested 43 "${ARG_SOURCE_DIR}" # Source 44 "${SPHINX_BUILD_DIR}" # Output 45 COMMENT 46 "Generating ${builder} Sphinx documentation for ${project} into \"${SPHINX_BUILD_DIR}\"") 47 48 # When "clean" target is run, remove the Sphinx build directory 49 set_property(DIRECTORY APPEND PROPERTY 50 ADDITIONAL_MAKE_CLEAN_FILES 51 "${SPHINX_BUILD_DIR}") 52 53 # We need to remove ${SPHINX_DOC_TREE_DIR} when make clean is run 54 # but we should only add this path once 55 get_property(_CURRENT_MAKE_CLEAN_FILES 56 DIRECTORY PROPERTY ADDITIONAL_MAKE_CLEAN_FILES) 57 list(FIND _CURRENT_MAKE_CLEAN_FILES "${SPHINX_DOC_TREE_DIR}" _INDEX) 58 if (_INDEX EQUAL -1) 59 set_property(DIRECTORY APPEND PROPERTY 60 ADDITIONAL_MAKE_CLEAN_FILES 61 "${SPHINX_DOC_TREE_DIR}") 62 endif() 63 64 if (LLVM_BUILD_DOCS) 65 add_dependencies(sphinx ${SPHINX_TARGET_NAME}) 66 67 # Handle installation 68 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY) 69 if (builder STREQUAL man) 70 if (CMAKE_INSTALL_MANDIR) 71 set(INSTALL_MANDIR ${CMAKE_INSTALL_MANDIR}/) 72 else() 73 set(INSTALL_MANDIR share/man/) 74 endif() 75 # FIXME: We might not ship all the tools that these man pages describe 76 install(DIRECTORY "${SPHINX_BUILD_DIR}/" # Slash indicates contents of 77 COMPONENT "${project}-sphinx-man" 78 DESTINATION ${INSTALL_MANDIR}man1) 79 80 if(NOT LLVM_ENABLE_IDE) 81 add_llvm_install_targets("install-${SPHINX_TARGET_NAME}" 82 DEPENDS ${SPHINX_TARGET_NAME} 83 COMPONENT "${project}-sphinx-man") 84 endif() 85 elseif (builder STREQUAL html) 86 string(TOUPPER "${project}" project_upper) 87 set(${project_upper}_INSTALL_SPHINX_HTML_DIR "share/doc/${project}/html" 88 CACHE STRING "HTML documentation install directory for ${project}") 89 90 # '/.' indicates: copy the contents of the directory directly into 91 # the specified destination, without recreating the last component 92 # of ${SPHINX_BUILD_DIR} implicitly. 93 install(DIRECTORY "${SPHINX_BUILD_DIR}/." 94 COMPONENT "${project}-sphinx-html" 95 DESTINATION "${${project_upper}_INSTALL_SPHINX_HTML_DIR}") 96 97 if(NOT LLVM_ENABLE_IDE) 98 add_llvm_install_targets("install-${SPHINX_TARGET_NAME}" 99 DEPENDS ${SPHINX_TARGET_NAME} 100 COMPONENT "${project}-sphinx-html") 101 endif() 102 else() 103 message(WARNING Installation of ${builder} not supported) 104 endif() 105 endif() 106 endif() 107endfunction() 108