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