blob: e95a9277b2dd5cd3267d3dd985ad1b280f228563 [file] [log] [blame]
Gyorgy Szing74dae3c2018-09-27 17:00:46 +02001#-------------------------------------------------------------------------------
Minos Galanakis07689822020-03-10 15:37:20 +00002# Copyright (c) 2019-2020, Arm Limited. All rights reserved.
Gyorgy Szing74dae3c2018-09-27 17:00:46 +02003#
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")
Minos Galanakisd19a19f2020-06-03 15:38:03 +010075 set(SPHINXCFG_CONFIGURED_FILE "${TFM_ROOT_DIR}/build_docs/conf.py")
Gyorgy Szingdb9783c2019-04-17 21:08:48 +020076 set(SPHINXCFG_TEMPLATE_FILE "${TFM_ROOT_DIR}/docs/conf.py.in")
Minos Galanakisd19a19f2020-06-03 15:38:03 +010077 set(SPHINXCFG_ENVIRONMENT_FILE "${TFM_ROOT_DIR}/docs/tfm_env.py.in")
78 set(_PDF_FILE "${SPHINXCFG_OUTPUT_PATH}/latex/TF-M.pdf")
Gyorgy Szing74dae3c2018-09-27 17:00:46 +020079
Minos Galanakisd19a19f2020-06-03 15:38:03 +010080 # Set the build-tool to copy over the files to ${SPHINX_TMP_DOC_DIR~
81 set(SPHINXCFG_COPY_FILES True)
82 # Set the config to render the conf.py. If needed to build it by cmake
83 # set it to False
84 set(SPHINXCFG_RENDER_CONF True)
85
86 # TODO Reference example on how a doxygen build can be requested.
87 # Currently the logic of BuildDoxygenDoc.cmake is still used for
88 # compatibility purposes.
89 set(DOXYCFG_DOXYGEN_BUILD False)
90 if (SPHINXCFG_RENDER_CONF)
91 find_package(Doxygen 1.8.0)
92 set(DOXYCFG_DOXYGEN_CFG_DIR ${TFM_ROOT_DIR}/doxygen)
93 set(DOXYCFG_OUTPUT_PATH ${SPHINXCFG_OUTPUT_PATH}/user_manual)
94 set(DOXYCFG_ECLIPSE_DOCID "org.arm.tf-m-refman")
95 file(MAKE_DIRECTORY ${SPHINXCFG_OUTPUT_PATH}/user_manual)
96 endif()
Gyorgy Szing74dae3c2018-09-27 17:00:46 +020097
98 #Version ID of TF-M.
99 #TODO: this shall not be hard-coded here. We need a process to define the
100 # version number of the document (and TF-M).
Mate Toth-Pal955235a2020-06-15 13:48:34 +0200101 set(SPHINXCFG_TFM_VERSION "v1.1")
102 set(SPHINXCFG_TFM_VERSION_FULL "Version 1.1")
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200103
Gyorgy Szingd9c57fb2019-09-02 17:08:18 +0200104 #This command does not generates the specified output file and thus it will
105 #always be run. Any other command or target depending on the "run-allways"
106 #output will be always executed too.
Gyorgy Szing5c873232019-05-10 23:28:14 +0200107 add_custom_command(OUTPUT run-allways
108 COMMAND "${CMAKE_COMMAND}" -E echo)
109
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100110 file(REMOVE_RECURSE ${SPHINX_TMP_DOC_DIR})
111 file(MAKE_DIRECTORY ${SPHINX_TMP_DOC_DIR})
112
113 #Call configure file to fill out the message template.
114 configure_file("${SPHINXCFG_ENVIRONMENT_FILE}" "${SPHINX_TMP_DOC_DIR}/tfm_env.py" @ONLY)
115
116 file(COPY "${SPHINXCFG_CONFIGURED_FILE}" DESTINATION ${SPHINX_TMP_DOC_DIR})
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200117
Gyorgy Szingdb9783c2019-04-17 21:08:48 +0200118 add_custom_target(create_sphinx_input
119 SOURCES "${SPHINX_TMP_DOC_DIR}"
120 )
121
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200122 add_custom_command(OUTPUT "${SPHINXCFG_OUTPUT_PATH}/html"
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100123 COMMAND "${SPHINX_EXECUTABLE}" -b html "${SPHINX_TMP_DOC_DIR}" "${SPHINXCFG_OUTPUT_PATH}/html"
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200124 WORKING_DIRECTORY "${TFM_ROOT_DIR}"
Gyorgy Szing5c873232019-05-10 23:28:14 +0200125 DEPENDS create_sphinx_input run-allways
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200126 COMMENT "Running Sphinx to generate user guide (HTML)."
127 VERBATIM
128 )
129
130 #Create build target to generate HTML documentation.
131 add_custom_target(doc_userguide
132 COMMENT "Generating User Guide with Sphinx..."
133 #Copy document files from the top level dir to docs
134 SOURCES "${SPHINXCFG_OUTPUT_PATH}/html"
135 )
136
137 #Add the HTML documentation to install content
138 install(DIRECTORY ${SPHINXCFG_OUTPUT_PATH}/html DESTINATION doc/user_guide
139 EXCLUDE_FROM_ALL
140 COMPONENT user_guide
141 PATTERN .buildinfo EXCLUDE
142 )
143
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100144 if (DOXYCFG_DOXYGEN_BUILD)
145 #Add the HTML documentation to install content
146 install(DIRECTORY ${SPHINXCFG_OUTPUT_PATH}/user_manual DESTINATION doc
147 EXCLUDE_FROM_ALL
148 COMPONENT user_guide
149 PATTERN .buildinfo EXCLUDE
150 )
151 endif()
152
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200153 #If PDF documentation is being made.
Gyorgy Szingd9c57fb2019-09-02 17:08:18 +0200154 if (LATEX_PDFLATEX_FOUND)
155 if (NOT CMAKE_GENERATOR MATCHES "Makefiles")
156 message(WARNING "Generator is not make based. PDF document generation target is not created.")
157 else()
158 #This file shall not be included before cmake did finish finding the make tool and thus
159 #setting CMAKE_MAKE_PROGRAM. Currently the search is triggered by the project() command.
160 if(NOT CMAKE_MAKE_PROGRAM)
161 message(FATAL_ERROR "CMAKE_MAKE_PROGRAM is not set. This file must be included after the project command is run.")
162 endif()
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200163
Gyorgy Szingd9c57fb2019-09-02 17:08:18 +0200164 add_custom_command(OUTPUT "${SPHINXCFG_OUTPUT_PATH}/latex"
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100165 COMMAND "${SPHINX_EXECUTABLE}" -b latex "${SPHINX_TMP_DOC_DIR}" "${SPHINXCFG_OUTPUT_PATH}/latex"
Gyorgy Szingd9c57fb2019-09-02 17:08:18 +0200166 WORKING_DIRECTORY "${TFM_ROOT_DIR}"
167 DEPENDS create_sphinx_input
168 COMMENT "Running Sphinx to generate user guide (LaTeX)."
169 VERBATIM
170 )
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200171
Gyorgy Szingd9c57fb2019-09-02 17:08:18 +0200172 add_custom_command(OUTPUT "${_PDF_FILE}"
173 COMMAND "${CMAKE_MAKE_PROGRAM}" all-pdf
174 WORKING_DIRECTORY ${SPHINXCFG_OUTPUT_PATH}/latex
175 DEPENDS "${SPHINXCFG_OUTPUT_PATH}/latex"
176 COMMENT "Generating PDF version of User Guide..."
177 VERBATIM
178 )
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200179
Gyorgy Szingd9c57fb2019-09-02 17:08:18 +0200180 #We do not use the add_custom_command trick here to get proper clean
181 #command since the clean rules "added" above will remove the entire
182 #doc directory, and thus clean the PDF output too.
183 add_custom_target(doc_userguide_pdf
184 COMMENT "Generating PDF version of TF-M User Guide..."
185 SOURCES "${_PDF_FILE}"
186 VERBATIM)
187
188 #Add the pdf documentation to install content
189 install(FILES "${_PDF_FILE}" DESTINATION "doc/user_guide"
190 RENAME "tf-m_user_guide.pdf"
191 COMPONENT user_guide
192 EXCLUDE_FROM_ALL)
193 endif()
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200194 else()
195 message(WARNING "PDF generation tools are missing. PDF document generation target is not created.")
196 endif()
197
198 #Generate build target which installs the documentation.
199 if (TARGET doc_userguide_pdf)
200 add_custom_target(install_userguide
201 DEPENDS doc_userguide doc_userguide_pdf
202 COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=user_guide
203 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
204 else()
205 add_custom_target(install_userguide
206 DEPENDS doc_userguide
207 COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=user_guide
208 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
209 endif()
210
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200211endif()