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) |
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) |
| 47 | if (LATEX_PDFLATEX_FOUND) |
| 48 | set (PDFLATEX_COMPILER "${PDFLATEX_COMPILER}" PARENT_SCOPE) |
| 49 | endif() |
| 50 | |
| 51 | if (SPHINX_FOUND AND PLANTUML_FOUND AND PY_M2R_FOUND |
Gyorgy Szing | db9783c | 2019-04-17 21:08:48 +0200 | [diff] [blame^] | 52 | AND PY_SPHINX-RTD-THEME_FOUND AND PY_SPHINXCONTRIB.PLANTUML) |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 53 | #Export executable locations to global scope. |
| 54 | set(SPHINX_EXECUTABLE "${SPHINX_EXECUTABLE}" PARENT_SCOPE) |
| 55 | set(PLANTUML_JAR_PATH "${PLANTUML_JAR_PATH}" PARENT_SCOPE) |
Gyorgy Szing | db9783c | 2019-04-17 21:08:48 +0200 | [diff] [blame^] | 56 | set(Java_JAVA_EXECUTABLE "${Java_JAVA_EXECUTABLE}" PARENT_SCOPE) |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 57 | set(SPHINX_NODOC False PARENT_SCOPE) |
| 58 | else() |
| 59 | message(WARNING "Some tools are missing for Sphinx document generation. Document generation target is not created.") |
| 60 | set(SPHINX_NODOC True PARENT_SCOPE) |
| 61 | endif() |
| 62 | endfunction() |
| 63 | |
| 64 | #Find the needed tools. |
| 65 | SphinxFindTools() |
| 66 | |
| 67 | #If mandatory tools are missing, skip creating document generation targets. |
| 68 | #This means missing documentation tools is not a critical error, and building |
| 69 | #TF-M is still possible. |
| 70 | if (NOT SPHINX_NODOC) |
| 71 | #The Sphinx configuration file needs some project specific configuration. |
| 72 | #Variables with SPHINXCFG_ prefix are setting values related to that. |
| 73 | #This is where Sphinx output will be written |
| 74 | set(SPHINXCFG_OUTPUT_PATH "${CMAKE_CURRENT_BINARY_DIR}/doc_sphinx") |
| 75 | |
| 76 | set(SPHINX_TMP_DOC_DIR "${CMAKE_CURRENT_BINARY_DIR}/doc_sphinx_in") |
| 77 | |
Gyorgy Szing | db9783c | 2019-04-17 21:08:48 +0200 | [diff] [blame^] | 78 | set(SPHINXCFG_TEMPLATE_FILE "${TFM_ROOT_DIR}/docs/conf.py.in") |
| 79 | set(SPHINXCFG_CONFIGURED_FILE "${SPHINXCFG_OUTPUT_PATH}/conf.py") |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 80 | |
| 81 | |
| 82 | #Version ID of TF-M. |
| 83 | #TODO: this shall not be hard-coded here. We need a process to define the |
| 84 | # version number of the document (and TF-M). |
| 85 | set(SPHINXCFG_TFM_VERSION "1.0.0-Beta") |
| 86 | set(SPHINXCFG_TFM_VERSION_FULL "Version 1.0.0-Beta") |
| 87 | |
| 88 | #Using add_custom_command allows CMake to generate proper clean commands |
| 89 | #for document generation. |
| 90 | add_custom_command(OUTPUT "${SPHINX_TMP_DOC_DIR}" |
| 91 | COMMAND "${CMAKE_COMMAND}" -D TFM_ROOT_DIR=${TFM_ROOT_DIR} -D DST_DIR=${SPHINX_TMP_DOC_DIR} -P "${TFM_ROOT_DIR}/cmake/SphinxCopyDoc.cmake" |
| 92 | WORKING_DIRECTORY "${TFM_ROOT_DIR}" |
| 93 | VERBATIM |
| 94 | ) |
| 95 | |
Gyorgy Szing | db9783c | 2019-04-17 21:08:48 +0200 | [diff] [blame^] | 96 | add_custom_target(create_sphinx_input |
| 97 | SOURCES "${SPHINX_TMP_DOC_DIR}" |
| 98 | ) |
| 99 | |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 100 | add_custom_command(OUTPUT "${SPHINXCFG_OUTPUT_PATH}/html" |
| 101 | COMMAND "${SPHINX_EXECUTABLE}" -c "${SPHINXCFG_OUTPUT_PATH}" -b html "${SPHINX_TMP_DOC_DIR}" "${SPHINXCFG_OUTPUT_PATH}/html" |
| 102 | WORKING_DIRECTORY "${TFM_ROOT_DIR}" |
Gyorgy Szing | db9783c | 2019-04-17 21:08:48 +0200 | [diff] [blame^] | 103 | DEPENDS create_sphinx_input |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 104 | COMMENT "Running Sphinx to generate user guide (HTML)." |
| 105 | VERBATIM |
| 106 | ) |
| 107 | |
| 108 | #Create build target to generate HTML documentation. |
| 109 | add_custom_target(doc_userguide |
| 110 | COMMENT "Generating User Guide with Sphinx..." |
| 111 | #Copy document files from the top level dir to docs |
| 112 | SOURCES "${SPHINXCFG_OUTPUT_PATH}/html" |
| 113 | ) |
| 114 | |
| 115 | #Add the HTML documentation to install content |
| 116 | install(DIRECTORY ${SPHINXCFG_OUTPUT_PATH}/html DESTINATION doc/user_guide |
| 117 | EXCLUDE_FROM_ALL |
| 118 | COMPONENT user_guide |
| 119 | PATTERN .buildinfo EXCLUDE |
| 120 | ) |
| 121 | |
| 122 | #If PDF documentation is being made. |
| 123 | if (PDFLATEX_COMPILER) |
| 124 | set(_PDF_FILE "${SPHINXCFG_OUTPUT_PATH}/latex/tf-m.pdf") |
| 125 | |
| 126 | add_custom_command(OUTPUT "${SPHINXCFG_OUTPUT_PATH}/latex" |
| 127 | COMMAND "${SPHINX_EXECUTABLE}" -c "${SPHINXCFG_OUTPUT_PATH}" -b latex "${SPHINX_TMP_DOC_DIR}" "${SPHINXCFG_OUTPUT_PATH}/latex" |
| 128 | WORKING_DIRECTORY "${TFM_ROOT_DIR}" |
Gyorgy Szing | db9783c | 2019-04-17 21:08:48 +0200 | [diff] [blame^] | 129 | DEPENDS create_sphinx_input |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 130 | COMMENT "Running Sphinx to generate user guide (LaTeX)." |
| 131 | VERBATIM |
| 132 | ) |
| 133 | |
| 134 | add_custom_command(OUTPUT "${_PDF_FILE}" |
| 135 | COMMAND "${CMAKE_MAKE_PROGRAM}" all-pdf |
| 136 | WORKING_DIRECTORY ${SPHINXCFG_OUTPUT_PATH}/latex |
| 137 | DEPENDS "${SPHINXCFG_OUTPUT_PATH}/latex" |
| 138 | COMMENT "Generating PDF version of User Guide..." |
| 139 | VERBATIM |
| 140 | ) |
| 141 | |
| 142 | #We do not use the add_custom_command trick here to get proper clean |
| 143 | #command since the clean rules "added" above will remove the entire |
| 144 | #doc directory, and thus clean the PDF output too. |
| 145 | add_custom_target(doc_userguide_pdf |
| 146 | COMMENT "Generating PDF version of TF-M User Guide..." |
| 147 | SOURCES "${_PDF_FILE}" |
| 148 | VERBATIM) |
| 149 | |
| 150 | #Add the pdf documentation to install content |
| 151 | install(FILES "${_PDF_FILE}" DESTINATION "doc/user_guide" |
| 152 | RENAME "tf-m_user_guide.pdf" |
| 153 | COMPONENT user_guide |
| 154 | EXCLUDE_FROM_ALL) |
| 155 | else() |
| 156 | message(WARNING "PDF generation tools are missing. PDF document generation target is not created.") |
| 157 | endif() |
| 158 | |
| 159 | #Generate build target which installs the documentation. |
| 160 | if (TARGET doc_userguide_pdf) |
| 161 | add_custom_target(install_userguide |
| 162 | DEPENDS doc_userguide doc_userguide_pdf |
| 163 | COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=user_guide |
| 164 | -P "${CMAKE_BINARY_DIR}/cmake_install.cmake") |
| 165 | else() |
| 166 | add_custom_target(install_userguide |
| 167 | DEPENDS doc_userguide |
| 168 | COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=user_guide |
| 169 | -P "${CMAKE_BINARY_DIR}/cmake_install.cmake") |
| 170 | endif() |
| 171 | |
| 172 | #Now instantiate a Sphinx configuration file from the template. |
| 173 | message(STATUS "Writing Sphinx configuration...") |
| 174 | configure_file(${SPHINXCFG_TEMPLATE_FILE} ${SPHINXCFG_CONFIGURED_FILE} @ONLY) |
| 175 | endif() |