Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
Minos Galanakis | 0768982 | 2020-03-10 15:37:20 +0000 | [diff] [blame^] | 2 | # Copyright (c) 2019-2020, Arm Limited. All rights reserved. |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 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. |
Gyorgy Szing | d9c57fb | 2019-09-02 17:08:18 +0200 | [diff] [blame] | 28 | # LATEX_PDFLATEX_FOUND - true if pdflatex executable found |
| 29 | # SPHINX_EXECUTABLE - path to sphinx-build |
| 30 | # PLANTUML_JAR_PATH - path to PlantUML |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 31 | |
| 32 | #Examples |
| 33 | # SphinxFindTools() |
| 34 | # |
| 35 | function(SphinxFindTools) |
Gyorgy Szing | db9783c | 2019-04-17 21:08:48 +0200 | [diff] [blame] | 36 | #Find Sphinx |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 37 | find_package(Sphinx) |
| 38 | |
Gyorgy Szing | db9783c | 2019-04-17 21:08:48 +0200 | [diff] [blame] | 39 | #Find additional needed Sphinx dependencies. |
| 40 | find_package(PythonModules COMPONENTS m2r sphinx-rtd-theme sphinxcontrib.plantuml) |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 41 | |
| 42 | #Find plantUML |
| 43 | find_package(PlantUML) |
| 44 | |
| 45 | #Find tools needed for PDF generation. |
| 46 | find_package(LATEX COMPONENTS PDFLATEX) |
Gyorgy Szing | d9c57fb | 2019-09-02 17:08:18 +0200 | [diff] [blame] | 47 | set (LATEX_PDFLATEX_FOUND "${LATEX_PDFLATEX_FOUND}" PARENT_SCOPE) |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 48 | |
| 49 | if (SPHINX_FOUND AND PLANTUML_FOUND AND PY_M2R_FOUND |
Gyorgy Szing | db9783c | 2019-04-17 21:08:48 +0200 | [diff] [blame] | 50 | AND PY_SPHINX-RTD-THEME_FOUND AND PY_SPHINXCONTRIB.PLANTUML) |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 51 | #Export executable locations to global scope. |
| 52 | set(SPHINX_EXECUTABLE "${SPHINX_EXECUTABLE}" PARENT_SCOPE) |
| 53 | set(PLANTUML_JAR_PATH "${PLANTUML_JAR_PATH}" PARENT_SCOPE) |
Gyorgy Szing | db9783c | 2019-04-17 21:08:48 +0200 | [diff] [blame] | 54 | set(Java_JAVA_EXECUTABLE "${Java_JAVA_EXECUTABLE}" PARENT_SCOPE) |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 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 | |
Gyorgy Szing | db9783c | 2019-04-17 21:08:48 +0200 | [diff] [blame] | 76 | set(SPHINXCFG_TEMPLATE_FILE "${TFM_ROOT_DIR}/docs/conf.py.in") |
| 77 | set(SPHINXCFG_CONFIGURED_FILE "${SPHINXCFG_OUTPUT_PATH}/conf.py") |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 78 | |
Gyorgy Szing | 5c87323 | 2019-05-10 23:28:14 +0200 | [diff] [blame] | 79 | set(SPHINX_DESIGN_DOC_ROOT "${TFM_ROOT_DIR}/docs/design_documents") |
Galanakis, Minos | 0c1ad78 | 2019-11-08 11:28:40 +0000 | [diff] [blame] | 80 | set(SPHINX_TEMPLATE_INDEX_FILE "${SPHINX_DESIGN_DOC_ROOT}/index.rst.in") |
| 81 | set(SPHINX_CONFIGURED_INDEX_FILE "${SPHINX_TMP_DOC_DIR}/docs/design_documents/index.rst") |
| 82 | set(SPHINX_MAIN_INDEX_FILE "docs/index.rst") |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 83 | |
| 84 | #Version ID of TF-M. |
| 85 | #TODO: this shall not be hard-coded here. We need a process to define the |
| 86 | # version number of the document (and TF-M). |
Minos Galanakis | 0768982 | 2020-03-10 15:37:20 +0000 | [diff] [blame^] | 87 | set(SPHINXCFG_TFM_VERSION "v1.0") |
| 88 | set(SPHINXCFG_TFM_VERSION_FULL "Version 1.0") |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 89 | |
Gyorgy Szing | 5c87323 | 2019-05-10 23:28:14 +0200 | [diff] [blame] | 90 | get_filename_component(_NDX_FILE_DIR ${SPHINX_CONFIGURED_INDEX_FILE} DIRECTORY ) |
| 91 | |
Gyorgy Szing | d9c57fb | 2019-09-02 17:08:18 +0200 | [diff] [blame] | 92 | #This command does not generates the specified output file and thus it will |
| 93 | #always be run. Any other command or target depending on the "run-allways" |
| 94 | #output will be always executed too. |
Gyorgy Szing | 5c87323 | 2019-05-10 23:28:14 +0200 | [diff] [blame] | 95 | add_custom_command(OUTPUT run-allways |
| 96 | COMMAND "${CMAKE_COMMAND}" -E echo) |
| 97 | |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 98 | #Using add_custom_command allows CMake to generate proper clean commands |
| 99 | #for document generation. |
| 100 | add_custom_command(OUTPUT "${SPHINX_TMP_DOC_DIR}" |
Gyorgy Szing | d9c57fb | 2019-09-02 17:08:18 +0200 | [diff] [blame] | 101 | "${SPHINX_CONFIGURED_INDEX_FILE}" |
Gyorgy Szing | 5c87323 | 2019-05-10 23:28:14 +0200 | [diff] [blame] | 102 | #Create target directory for SPHINX_CONFIGURED_INDEX_FILE. Needed |
| 103 | #by the next command. |
| 104 | COMMAND "${CMAKE_COMMAND}" -E make_directory "${_NDX_FILE_DIR}" |
| 105 | #Fill out index.rst template |
| 106 | COMMAND "${CMAKE_COMMAND}" -D TFM_ROOT_DIR=${TFM_ROOT_DIR} |
| 107 | -D SPHINX_TEMPLATE_INDEX_FILE=${SPHINX_TEMPLATE_INDEX_FILE} |
| 108 | -D SPHINX_CONFIGURED_INDEX_FILE=${SPHINX_CONFIGURED_INDEX_FILE} |
| 109 | -D SPHINX_DESIGN_DOC_ROOT=${SPHINX_DESIGN_DOC_ROOT} |
| 110 | -P "${TFM_ROOT_DIR}/cmake/SphinxDesignDocStatus.cmake" |
| 111 | #Copy document files to temp direcotry |
| 112 | COMMAND "${CMAKE_COMMAND}" -D TFM_ROOT_DIR=${TFM_ROOT_DIR} |
| 113 | -D DST_DIR=${SPHINX_TMP_DOC_DIR} |
| 114 | -D BINARY_DIR=${CMAKE_BINARY_DIR} |
Galanakis, Minos | 0c1ad78 | 2019-11-08 11:28:40 +0000 | [diff] [blame] | 115 | -D MASTER_IDX=${SPHINX_MAIN_INDEX_FILE} |
Gyorgy Szing | 5c87323 | 2019-05-10 23:28:14 +0200 | [diff] [blame] | 116 | -P "${TFM_ROOT_DIR}/cmake/SphinxCopyDoc.cmake" |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 117 | WORKING_DIRECTORY "${TFM_ROOT_DIR}" |
Gyorgy Szing | 5c87323 | 2019-05-10 23:28:14 +0200 | [diff] [blame] | 118 | DEPENDS run-allways |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 119 | VERBATIM |
| 120 | ) |
| 121 | |
Gyorgy Szing | db9783c | 2019-04-17 21:08:48 +0200 | [diff] [blame] | 122 | add_custom_target(create_sphinx_input |
| 123 | SOURCES "${SPHINX_TMP_DOC_DIR}" |
| 124 | ) |
| 125 | |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 126 | add_custom_command(OUTPUT "${SPHINXCFG_OUTPUT_PATH}/html" |
| 127 | COMMAND "${SPHINX_EXECUTABLE}" -c "${SPHINXCFG_OUTPUT_PATH}" -b html "${SPHINX_TMP_DOC_DIR}" "${SPHINXCFG_OUTPUT_PATH}/html" |
| 128 | WORKING_DIRECTORY "${TFM_ROOT_DIR}" |
Gyorgy Szing | 5c87323 | 2019-05-10 23:28:14 +0200 | [diff] [blame] | 129 | DEPENDS create_sphinx_input run-allways |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 130 | COMMENT "Running Sphinx to generate user guide (HTML)." |
| 131 | VERBATIM |
| 132 | ) |
| 133 | |
| 134 | #Create build target to generate HTML documentation. |
| 135 | add_custom_target(doc_userguide |
| 136 | COMMENT "Generating User Guide with Sphinx..." |
| 137 | #Copy document files from the top level dir to docs |
| 138 | SOURCES "${SPHINXCFG_OUTPUT_PATH}/html" |
| 139 | ) |
| 140 | |
| 141 | #Add the HTML documentation to install content |
| 142 | install(DIRECTORY ${SPHINXCFG_OUTPUT_PATH}/html DESTINATION doc/user_guide |
| 143 | EXCLUDE_FROM_ALL |
| 144 | COMPONENT user_guide |
| 145 | PATTERN .buildinfo EXCLUDE |
| 146 | ) |
| 147 | |
| 148 | #If PDF documentation is being made. |
Gyorgy Szing | d9c57fb | 2019-09-02 17:08:18 +0200 | [diff] [blame] | 149 | if (LATEX_PDFLATEX_FOUND) |
| 150 | if (NOT CMAKE_GENERATOR MATCHES "Makefiles") |
| 151 | message(WARNING "Generator is not make based. PDF document generation target is not created.") |
| 152 | else() |
| 153 | #This file shall not be included before cmake did finish finding the make tool and thus |
| 154 | #setting CMAKE_MAKE_PROGRAM. Currently the search is triggered by the project() command. |
| 155 | if(NOT CMAKE_MAKE_PROGRAM) |
| 156 | message(FATAL_ERROR "CMAKE_MAKE_PROGRAM is not set. This file must be included after the project command is run.") |
| 157 | endif() |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 158 | |
Gyorgy Szing | d9c57fb | 2019-09-02 17:08:18 +0200 | [diff] [blame] | 159 | set(_PDF_FILE "${SPHINXCFG_OUTPUT_PATH}/latex/TF-M.pdf") |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 160 | |
Gyorgy Szing | d9c57fb | 2019-09-02 17:08:18 +0200 | [diff] [blame] | 161 | add_custom_command(OUTPUT "${SPHINXCFG_OUTPUT_PATH}/latex" |
| 162 | COMMAND "${SPHINX_EXECUTABLE}" -c "${SPHINXCFG_OUTPUT_PATH}" -b latex "${SPHINX_TMP_DOC_DIR}" "${SPHINXCFG_OUTPUT_PATH}/latex" |
| 163 | WORKING_DIRECTORY "${TFM_ROOT_DIR}" |
| 164 | DEPENDS create_sphinx_input |
| 165 | COMMENT "Running Sphinx to generate user guide (LaTeX)." |
| 166 | VERBATIM |
| 167 | ) |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 168 | |
Gyorgy Szing | d9c57fb | 2019-09-02 17:08:18 +0200 | [diff] [blame] | 169 | add_custom_command(OUTPUT "${_PDF_FILE}" |
| 170 | COMMAND "${CMAKE_MAKE_PROGRAM}" all-pdf |
| 171 | WORKING_DIRECTORY ${SPHINXCFG_OUTPUT_PATH}/latex |
| 172 | DEPENDS "${SPHINXCFG_OUTPUT_PATH}/latex" |
| 173 | COMMENT "Generating PDF version of User Guide..." |
| 174 | VERBATIM |
| 175 | ) |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 176 | |
Gyorgy Szing | d9c57fb | 2019-09-02 17:08:18 +0200 | [diff] [blame] | 177 | #We do not use the add_custom_command trick here to get proper clean |
| 178 | #command since the clean rules "added" above will remove the entire |
| 179 | #doc directory, and thus clean the PDF output too. |
| 180 | add_custom_target(doc_userguide_pdf |
| 181 | COMMENT "Generating PDF version of TF-M User Guide..." |
| 182 | SOURCES "${_PDF_FILE}" |
| 183 | VERBATIM) |
| 184 | |
| 185 | #Add the pdf documentation to install content |
| 186 | install(FILES "${_PDF_FILE}" DESTINATION "doc/user_guide" |
| 187 | RENAME "tf-m_user_guide.pdf" |
| 188 | COMPONENT user_guide |
| 189 | EXCLUDE_FROM_ALL) |
| 190 | endif() |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 191 | else() |
| 192 | message(WARNING "PDF generation tools are missing. PDF document generation target is not created.") |
| 193 | endif() |
| 194 | |
| 195 | #Generate build target which installs the documentation. |
| 196 | if (TARGET doc_userguide_pdf) |
| 197 | add_custom_target(install_userguide |
| 198 | DEPENDS doc_userguide doc_userguide_pdf |
| 199 | COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=user_guide |
| 200 | -P "${CMAKE_BINARY_DIR}/cmake_install.cmake") |
| 201 | else() |
| 202 | add_custom_target(install_userguide |
| 203 | DEPENDS doc_userguide |
| 204 | COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=user_guide |
| 205 | -P "${CMAKE_BINARY_DIR}/cmake_install.cmake") |
| 206 | endif() |
| 207 | |
| 208 | #Now instantiate a Sphinx configuration file from the template. |
| 209 | message(STATUS "Writing Sphinx configuration...") |
| 210 | configure_file(${SPHINXCFG_TEMPLATE_FILE} ${SPHINXCFG_CONFIGURED_FILE} @ONLY) |
| 211 | endif() |