1include(CMakeParseArguments) 2include(ProcessorCount) 3 4find_program(SPHINX_EXECUTABLE NAMES sphinx-build 5 HINTS 6 $ENV{SPHINX_DIR} 7 PATH_SUFFIXES bin 8 DOC "Sphinx documentation generator" 9) 10 11mark_as_advanced(SPHINX_EXECUTABLE) 12 13function(clean_doc_output DIR) 14 set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${DIR}) 15endfunction() 16 17set(BINARY_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/sphinx/_build") 18 19# Sphinx cache with pickled ReST documents 20set(SPHINX_CACHE_DIR "${CMAKE_CURRENT_BINARY_DIR}/sphinx/_doctrees") 21 22# HTML output directory 23set(SPHINX_DEFAULT_HTML_DIR "${CMAKE_CURRENT_BINARY_DIR}/sphinx/html") 24function(add_sphinx_doc SRC_DIR) 25 set(options) 26 set(oneValueArgs HTML_DIR) 27 set(multiValueArgs VARS TEMPLATE_VARS) 28 29 cmake_parse_arguments(PARSE "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) 30 ProcessorCount(N) 31 32 set(ADDITIONAL_ARGS) 33 foreach(VAR ${PARSE_VARS}) 34 list(APPEND ADDITIONAL_ARGS "-D ${VAR}") 35 endforeach() 36 foreach(VAR ${PARSE_TEMPLATE_VARS}) 37 list(APPEND ADDITIONAL_ARGS "-A ${VAR}") 38 endforeach() 39 40 if(PARSE_HTML_DIR) 41 set(SPHINX_HTML_DIR ${PARSE_HTML_DIR} CACHE PATH "Path to html output") 42 else() 43 set(SPHINX_HTML_DIR ${SPHINX_DEFAULT_HTML_DIR} CACHE PATH "Path to html output") 44 endif() 45 46 clean_doc_output(${SPHINX_HTML_DIR}) 47 clean_doc_output(${SPHINX_CACHE_DIR}) 48 clean_doc_output(${BINARY_BUILD_DIR}) 49 50 add_custom_target(doc 51 ${SPHINX_EXECUTABLE} 52 -j ${N} 53 -n 54 -b html 55 -d "${SPHINX_CACHE_DIR}" 56 ${ADDITIONAL_ARGS} 57 "${SRC_DIR}" 58 "${SPHINX_HTML_DIR}" 59 COMMENT "Building HTML documentation with Sphinx") 60endfunction() 61 62