Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame^] | 1 | #------------------------------------------------------------------------------- |
| 2 | # Copyright (c) 2019, Arm Limited. All rights reserved. |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
| 8 | #This file adds build and install targets to build the Sphinx documentation |
| 9 | #for TF-M. This currently means the "User Guide". Further documentation |
| 10 | #builds may be added in the future. |
| 11 | |
| 12 | Include(CMakeParseArguments) |
| 13 | |
| 14 | #This function will find the location of tools needed to build the |
| 15 | #documentation. These are: |
| 16 | # - Mandatory: |
| 17 | # - Sphinx 1.9.0 or higher |
| 18 | # - PlantUML and it's dependencies |
| 19 | # - Optional |
| 20 | # - LateX/PDFLateX |
| 21 | # |
| 22 | #Inputs: |
| 23 | # none (some global variables might be used by FindXXX modules used. For |
| 24 | # details please check the modules used.) |
| 25 | #Outputs: |
| 26 | # SPHINX_NODOC - will be defined and set to true is any mandatory tool is |
| 27 | # missing. |
| 28 | # PDFLATEX_COMPILER - path to pdflatex executable |
| 29 | # SPHINX_EXECUTABLE - path to sphinx-build |
| 30 | # PLANTUML_JAR_PATH - path to PlantUML |
| 31 | |
| 32 | #Examples |
| 33 | # SphinxFindTools() |
| 34 | # |
| 35 | function(SphinxFindTools) |
| 36 | find_package(Sphinx) |
| 37 | |
| 38 | #Find additional needed python dependencies. |
| 39 | find_package(PythonModules COMPONENTS m2r sphinx-rtd-theme) |
| 40 | |
| 41 | #Find plantUML |
| 42 | find_package(PlantUML) |
| 43 | |
| 44 | #Find tools needed for PDF generation. |
| 45 | find_package(LATEX COMPONENTS PDFLATEX) |
| 46 | if (LATEX_PDFLATEX_FOUND) |
| 47 | set (PDFLATEX_COMPILER "${PDFLATEX_COMPILER}" PARENT_SCOPE) |
| 48 | endif() |
| 49 | |
| 50 | if (SPHINX_FOUND AND PLANTUML_FOUND AND PY_M2R_FOUND |
| 51 | AND PY_SPHINX-RTD-THEME_FOUND) |
| 52 | #Export executable locations to global scope. |
| 53 | set(SPHINX_EXECUTABLE "${SPHINX_EXECUTABLE}" PARENT_SCOPE) |
| 54 | set(PLANTUML_JAR_PATH "${PLANTUML_JAR_PATH}" PARENT_SCOPE) |
| 55 | set(SPHINX_NODOC False PARENT_SCOPE) |
| 56 | else() |
| 57 | message(WARNING "Some tools are missing for Sphinx document generation. Document generation target is not created.") |
| 58 | set(SPHINX_NODOC True PARENT_SCOPE) |
| 59 | endif() |
| 60 | endfunction() |
| 61 | |
| 62 | #Find the needed tools. |
| 63 | SphinxFindTools() |
| 64 | |
| 65 | #If mandatory tools are missing, skip creating document generation targets. |
| 66 | #This means missing documentation tools is not a critical error, and building |
| 67 | #TF-M is still possible. |
| 68 | if (NOT SPHINX_NODOC) |
| 69 | #The Sphinx configuration file needs some project specific configuration. |
| 70 | #Variables with SPHINXCFG_ prefix are setting values related to that. |
| 71 | #This is where Sphinx output will be written |
| 72 | set(SPHINXCFG_OUTPUT_PATH "${CMAKE_CURRENT_BINARY_DIR}/doc_sphinx") |
| 73 | |
| 74 | set(SPHINX_TMP_DOC_DIR "${CMAKE_CURRENT_BINARY_DIR}/doc_sphinx_in") |
| 75 | |
| 76 | set(SPHINXCFG_TEMPLATE_FILE "${TFM_ROOT_DIR}/docs/Conf.py.in") |
| 77 | set(SPHINXCFG_CONFIGURED_FILE "${SPHINXCFG_OUTPUT_PATH}/Conf.py") |
| 78 | |
| 79 | |
| 80 | #Version ID of TF-M. |
| 81 | #TODO: this shall not be hard-coded here. We need a process to define the |
| 82 | # version number of the document (and TF-M). |
| 83 | set(SPHINXCFG_TFM_VERSION "1.0.0-Beta") |
| 84 | set(SPHINXCFG_TFM_VERSION_FULL "Version 1.0.0-Beta") |
| 85 | |
| 86 | #Using add_custom_command allows CMake to generate proper clean commands |
| 87 | #for document generation. |
| 88 | add_custom_command(OUTPUT "${SPHINX_TMP_DOC_DIR}" |
| 89 | COMMAND "${CMAKE_COMMAND}" -D TFM_ROOT_DIR=${TFM_ROOT_DIR} -D DST_DIR=${SPHINX_TMP_DOC_DIR} -P "${TFM_ROOT_DIR}/cmake/SphinxCopyDoc.cmake" |
| 90 | WORKING_DIRECTORY "${TFM_ROOT_DIR}" |
| 91 | VERBATIM |
| 92 | ) |
| 93 | |
| 94 | add_custom_command(OUTPUT "${SPHINXCFG_OUTPUT_PATH}/html" |
| 95 | COMMAND "${SPHINX_EXECUTABLE}" -c "${SPHINXCFG_OUTPUT_PATH}" -b html "${SPHINX_TMP_DOC_DIR}" "${SPHINXCFG_OUTPUT_PATH}/html" |
| 96 | WORKING_DIRECTORY "${TFM_ROOT_DIR}" |
| 97 | DEPENDS "${SPHINX_TMP_DOC_DIR}" |
| 98 | COMMENT "Running Sphinx to generate user guide (HTML)." |
| 99 | VERBATIM |
| 100 | ) |
| 101 | |
| 102 | #Create build target to generate HTML documentation. |
| 103 | add_custom_target(doc_userguide |
| 104 | COMMENT "Generating User Guide with Sphinx..." |
| 105 | #Copy document files from the top level dir to docs |
| 106 | SOURCES "${SPHINXCFG_OUTPUT_PATH}/html" |
| 107 | ) |
| 108 | |
| 109 | #Add the HTML documentation to install content |
| 110 | install(DIRECTORY ${SPHINXCFG_OUTPUT_PATH}/html DESTINATION doc/user_guide |
| 111 | EXCLUDE_FROM_ALL |
| 112 | COMPONENT user_guide |
| 113 | PATTERN .buildinfo EXCLUDE |
| 114 | ) |
| 115 | |
| 116 | #If PDF documentation is being made. |
| 117 | if (PDFLATEX_COMPILER) |
| 118 | set(_PDF_FILE "${SPHINXCFG_OUTPUT_PATH}/latex/tf-m.pdf") |
| 119 | |
| 120 | add_custom_command(OUTPUT "${SPHINXCFG_OUTPUT_PATH}/latex" |
| 121 | COMMAND "${SPHINX_EXECUTABLE}" -c "${SPHINXCFG_OUTPUT_PATH}" -b latex "${SPHINX_TMP_DOC_DIR}" "${SPHINXCFG_OUTPUT_PATH}/latex" |
| 122 | WORKING_DIRECTORY "${TFM_ROOT_DIR}" |
| 123 | DEPENDS "${SPHINX_TMP_DOC_DIR}" |
| 124 | COMMENT "Running Sphinx to generate user guide (LaTeX)." |
| 125 | VERBATIM |
| 126 | ) |
| 127 | |
| 128 | add_custom_command(OUTPUT "${_PDF_FILE}" |
| 129 | COMMAND "${CMAKE_MAKE_PROGRAM}" all-pdf |
| 130 | WORKING_DIRECTORY ${SPHINXCFG_OUTPUT_PATH}/latex |
| 131 | DEPENDS "${SPHINXCFG_OUTPUT_PATH}/latex" |
| 132 | COMMENT "Generating PDF version of User Guide..." |
| 133 | VERBATIM |
| 134 | ) |
| 135 | |
| 136 | #We do not use the add_custom_command trick here to get proper clean |
| 137 | #command since the clean rules "added" above will remove the entire |
| 138 | #doc directory, and thus clean the PDF output too. |
| 139 | add_custom_target(doc_userguide_pdf |
| 140 | COMMENT "Generating PDF version of TF-M User Guide..." |
| 141 | SOURCES "${_PDF_FILE}" |
| 142 | VERBATIM) |
| 143 | |
| 144 | #Add the pdf documentation to install content |
| 145 | install(FILES "${_PDF_FILE}" DESTINATION "doc/user_guide" |
| 146 | RENAME "tf-m_user_guide.pdf" |
| 147 | COMPONENT user_guide |
| 148 | EXCLUDE_FROM_ALL) |
| 149 | else() |
| 150 | message(WARNING "PDF generation tools are missing. PDF document generation target is not created.") |
| 151 | endif() |
| 152 | |
| 153 | #Generate build target which installs the documentation. |
| 154 | if (TARGET doc_userguide_pdf) |
| 155 | add_custom_target(install_userguide |
| 156 | DEPENDS doc_userguide doc_userguide_pdf |
| 157 | COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=user_guide |
| 158 | -P "${CMAKE_BINARY_DIR}/cmake_install.cmake") |
| 159 | else() |
| 160 | add_custom_target(install_userguide |
| 161 | DEPENDS doc_userguide |
| 162 | COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=user_guide |
| 163 | -P "${CMAKE_BINARY_DIR}/cmake_install.cmake") |
| 164 | endif() |
| 165 | |
| 166 | #Now instantiate a Sphinx configuration file from the template. |
| 167 | message(STATUS "Writing Sphinx configuration...") |
| 168 | configure_file(${SPHINXCFG_TEMPLATE_FILE} ${SPHINXCFG_CONFIGURED_FILE} @ONLY) |
| 169 | endif() |