blob: d3dd70f6b58f6739ddbd2314428040aed3381431 [file] [log] [blame]
Gyorgy Szing74dae3c2018-09-27 17:00:46 +02001#-------------------------------------------------------------------------------
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
12Include(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 Szingd9c57fb2019-09-02 17:08:18 +020028# LATEX_PDFLATEX_FOUND - true if pdflatex executable found
29# SPHINX_EXECUTABLE - path to sphinx-build
30# PLANTUML_JAR_PATH - path to PlantUML
Gyorgy Szing74dae3c2018-09-27 17:00:46 +020031
32#Examples
33# SphinxFindTools()
34#
35function(SphinxFindTools)
Gyorgy Szingdb9783c2019-04-17 21:08:48 +020036 #Find Sphinx
Gyorgy Szing74dae3c2018-09-27 17:00:46 +020037 find_package(Sphinx)
38
Gyorgy Szingdb9783c2019-04-17 21:08:48 +020039 #Find additional needed Sphinx dependencies.
40 find_package(PythonModules COMPONENTS m2r sphinx-rtd-theme sphinxcontrib.plantuml)
Gyorgy Szing74dae3c2018-09-27 17:00:46 +020041
42 #Find plantUML
43 find_package(PlantUML)
44
45 #Find tools needed for PDF generation.
46 find_package(LATEX COMPONENTS PDFLATEX)
Gyorgy Szingd9c57fb2019-09-02 17:08:18 +020047 set (LATEX_PDFLATEX_FOUND "${LATEX_PDFLATEX_FOUND}" PARENT_SCOPE)
Gyorgy Szing74dae3c2018-09-27 17:00:46 +020048
49 if (SPHINX_FOUND AND PLANTUML_FOUND AND PY_M2R_FOUND
Gyorgy Szingdb9783c2019-04-17 21:08:48 +020050 AND PY_SPHINX-RTD-THEME_FOUND AND PY_SPHINXCONTRIB.PLANTUML)
Gyorgy Szing74dae3c2018-09-27 17:00:46 +020051 #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 Szingdb9783c2019-04-17 21:08:48 +020054 set(Java_JAVA_EXECUTABLE "${Java_JAVA_EXECUTABLE}" PARENT_SCOPE)
Gyorgy Szing74dae3c2018-09-27 17:00:46 +020055 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()
60endfunction()
61
62#Find the needed tools.
63SphinxFindTools()
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.
68if (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 Szingdb9783c2019-04-17 21:08:48 +020076 set(SPHINXCFG_TEMPLATE_FILE "${TFM_ROOT_DIR}/docs/conf.py.in")
77 set(SPHINXCFG_CONFIGURED_FILE "${SPHINXCFG_OUTPUT_PATH}/conf.py")
Gyorgy Szing74dae3c2018-09-27 17:00:46 +020078
Gyorgy Szing5c873232019-05-10 23:28:14 +020079 set(SPHINX_TEMPLATE_INDEX_FILE "${TFM_ROOT_DIR}/docs/index.rst.in")
80 set(SPHINX_CONFIGURED_INDEX_FILE "${SPHINX_TMP_DOC_DIR}/index.rst")
81 set(SPHINX_DESIGN_DOC_ROOT "${TFM_ROOT_DIR}/docs/design_documents")
Gyorgy Szing74dae3c2018-09-27 17:00:46 +020082
83 #Version ID of TF-M.
84 #TODO: this shall not be hard-coded here. We need a process to define the
85 # version number of the document (and TF-M).
86 set(SPHINXCFG_TFM_VERSION "1.0.0-Beta")
87 set(SPHINXCFG_TFM_VERSION_FULL "Version 1.0.0-Beta")
88
Gyorgy Szing5c873232019-05-10 23:28:14 +020089 get_filename_component(_NDX_FILE_DIR ${SPHINX_CONFIGURED_INDEX_FILE} DIRECTORY )
90
Gyorgy Szingd9c57fb2019-09-02 17:08:18 +020091 #This command does not generates the specified output file and thus it will
92 #always be run. Any other command or target depending on the "run-allways"
93 #output will be always executed too.
Gyorgy Szing5c873232019-05-10 23:28:14 +020094 add_custom_command(OUTPUT run-allways
95 COMMAND "${CMAKE_COMMAND}" -E echo)
96
Gyorgy Szing74dae3c2018-09-27 17:00:46 +020097 #Using add_custom_command allows CMake to generate proper clean commands
98 #for document generation.
99 add_custom_command(OUTPUT "${SPHINX_TMP_DOC_DIR}"
Gyorgy Szingd9c57fb2019-09-02 17:08:18 +0200100 "${SPHINX_CONFIGURED_INDEX_FILE}"
Gyorgy Szing5c873232019-05-10 23:28:14 +0200101 #Create target directory for SPHINX_CONFIGURED_INDEX_FILE. Needed
102 #by the next command.
103 COMMAND "${CMAKE_COMMAND}" -E make_directory "${_NDX_FILE_DIR}"
104 #Fill out index.rst template
105 COMMAND "${CMAKE_COMMAND}" -D TFM_ROOT_DIR=${TFM_ROOT_DIR}
106 -D SPHINX_TEMPLATE_INDEX_FILE=${SPHINX_TEMPLATE_INDEX_FILE}
107 -D SPHINX_CONFIGURED_INDEX_FILE=${SPHINX_CONFIGURED_INDEX_FILE}
108 -D SPHINX_DESIGN_DOC_ROOT=${SPHINX_DESIGN_DOC_ROOT}
109 -P "${TFM_ROOT_DIR}/cmake/SphinxDesignDocStatus.cmake"
110 #Copy document files to temp direcotry
111 COMMAND "${CMAKE_COMMAND}" -D TFM_ROOT_DIR=${TFM_ROOT_DIR}
112 -D DST_DIR=${SPHINX_TMP_DOC_DIR}
113 -D BINARY_DIR=${CMAKE_BINARY_DIR}
114 -P "${TFM_ROOT_DIR}/cmake/SphinxCopyDoc.cmake"
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200115 WORKING_DIRECTORY "${TFM_ROOT_DIR}"
Gyorgy Szing5c873232019-05-10 23:28:14 +0200116 DEPENDS run-allways
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200117 VERBATIM
118 )
119
Gyorgy Szingdb9783c2019-04-17 21:08:48 +0200120 add_custom_target(create_sphinx_input
121 SOURCES "${SPHINX_TMP_DOC_DIR}"
122 )
123
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200124 add_custom_command(OUTPUT "${SPHINXCFG_OUTPUT_PATH}/html"
125 COMMAND "${SPHINX_EXECUTABLE}" -c "${SPHINXCFG_OUTPUT_PATH}" -b html "${SPHINX_TMP_DOC_DIR}" "${SPHINXCFG_OUTPUT_PATH}/html"
126 WORKING_DIRECTORY "${TFM_ROOT_DIR}"
Gyorgy Szing5c873232019-05-10 23:28:14 +0200127 DEPENDS create_sphinx_input run-allways
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200128 COMMENT "Running Sphinx to generate user guide (HTML)."
129 VERBATIM
130 )
131
132 #Create build target to generate HTML documentation.
133 add_custom_target(doc_userguide
134 COMMENT "Generating User Guide with Sphinx..."
135 #Copy document files from the top level dir to docs
136 SOURCES "${SPHINXCFG_OUTPUT_PATH}/html"
137 )
138
139 #Add the HTML documentation to install content
140 install(DIRECTORY ${SPHINXCFG_OUTPUT_PATH}/html DESTINATION doc/user_guide
141 EXCLUDE_FROM_ALL
142 COMPONENT user_guide
143 PATTERN .buildinfo EXCLUDE
144 )
145
146 #If PDF documentation is being made.
Gyorgy Szingd9c57fb2019-09-02 17:08:18 +0200147 if (LATEX_PDFLATEX_FOUND)
148 if (NOT CMAKE_GENERATOR MATCHES "Makefiles")
149 message(WARNING "Generator is not make based. PDF document generation target is not created.")
150 else()
151 #This file shall not be included before cmake did finish finding the make tool and thus
152 #setting CMAKE_MAKE_PROGRAM. Currently the search is triggered by the project() command.
153 if(NOT CMAKE_MAKE_PROGRAM)
154 message(FATAL_ERROR "CMAKE_MAKE_PROGRAM is not set. This file must be included after the project command is run.")
155 endif()
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200156
Gyorgy Szingd9c57fb2019-09-02 17:08:18 +0200157 set(_PDF_FILE "${SPHINXCFG_OUTPUT_PATH}/latex/TF-M.pdf")
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200158
Gyorgy Szingd9c57fb2019-09-02 17:08:18 +0200159 add_custom_command(OUTPUT "${SPHINXCFG_OUTPUT_PATH}/latex"
160 COMMAND "${SPHINX_EXECUTABLE}" -c "${SPHINXCFG_OUTPUT_PATH}" -b latex "${SPHINX_TMP_DOC_DIR}" "${SPHINXCFG_OUTPUT_PATH}/latex"
161 WORKING_DIRECTORY "${TFM_ROOT_DIR}"
162 DEPENDS create_sphinx_input
163 COMMENT "Running Sphinx to generate user guide (LaTeX)."
164 VERBATIM
165 )
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200166
Gyorgy Szingd9c57fb2019-09-02 17:08:18 +0200167 add_custom_command(OUTPUT "${_PDF_FILE}"
168 COMMAND "${CMAKE_MAKE_PROGRAM}" all-pdf
169 WORKING_DIRECTORY ${SPHINXCFG_OUTPUT_PATH}/latex
170 DEPENDS "${SPHINXCFG_OUTPUT_PATH}/latex"
171 COMMENT "Generating PDF version of User Guide..."
172 VERBATIM
173 )
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200174
Gyorgy Szingd9c57fb2019-09-02 17:08:18 +0200175 #We do not use the add_custom_command trick here to get proper clean
176 #command since the clean rules "added" above will remove the entire
177 #doc directory, and thus clean the PDF output too.
178 add_custom_target(doc_userguide_pdf
179 COMMENT "Generating PDF version of TF-M User Guide..."
180 SOURCES "${_PDF_FILE}"
181 VERBATIM)
182
183 #Add the pdf documentation to install content
184 install(FILES "${_PDF_FILE}" DESTINATION "doc/user_guide"
185 RENAME "tf-m_user_guide.pdf"
186 COMPONENT user_guide
187 EXCLUDE_FROM_ALL)
188 endif()
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200189 else()
190 message(WARNING "PDF generation tools are missing. PDF document generation target is not created.")
191 endif()
192
193 #Generate build target which installs the documentation.
194 if (TARGET doc_userguide_pdf)
195 add_custom_target(install_userguide
196 DEPENDS doc_userguide doc_userguide_pdf
197 COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=user_guide
198 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
199 else()
200 add_custom_target(install_userguide
201 DEPENDS doc_userguide
202 COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=user_guide
203 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
204 endif()
205
206 #Now instantiate a Sphinx configuration file from the template.
207 message(STATUS "Writing Sphinx configuration...")
208 configure_file(${SPHINXCFG_TEMPLATE_FILE} ${SPHINXCFG_CONFIGURED_FILE} @ONLY)
209endif()