blob: 997de3b9c7dcce9d04f17534d663f55c45ccdd0d [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.
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#
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)
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 Szingdb9783c2019-04-17 21:08:48 +020052 AND PY_SPHINX-RTD-THEME_FOUND AND PY_SPHINXCONTRIB.PLANTUML)
Gyorgy Szing74dae3c2018-09-27 17:00:46 +020053 #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 Szingdb9783c2019-04-17 21:08:48 +020056 set(Java_JAVA_EXECUTABLE "${Java_JAVA_EXECUTABLE}" PARENT_SCOPE)
Gyorgy Szing74dae3c2018-09-27 17:00:46 +020057 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()
62endfunction()
63
64#Find the needed tools.
65SphinxFindTools()
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.
70if (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 Szingdb9783c2019-04-17 21:08:48 +020078 set(SPHINXCFG_TEMPLATE_FILE "${TFM_ROOT_DIR}/docs/conf.py.in")
79 set(SPHINXCFG_CONFIGURED_FILE "${SPHINXCFG_OUTPUT_PATH}/conf.py")
Gyorgy Szing74dae3c2018-09-27 17:00:46 +020080
Gyorgy Szing5c873232019-05-10 23:28:14 +020081 set(SPHINX_TEMPLATE_INDEX_FILE "${TFM_ROOT_DIR}/docs/index.rst.in")
82 set(SPHINX_CONFIGURED_INDEX_FILE "${SPHINX_TMP_DOC_DIR}/index.rst")
83 set(SPHINX_DESIGN_DOC_ROOT "${TFM_ROOT_DIR}/docs/design_documents")
Gyorgy Szing74dae3c2018-09-27 17:00:46 +020084
85 #Version ID of TF-M.
86 #TODO: this shall not be hard-coded here. We need a process to define the
87 # version number of the document (and TF-M).
88 set(SPHINXCFG_TFM_VERSION "1.0.0-Beta")
89 set(SPHINXCFG_TFM_VERSION_FULL "Version 1.0.0-Beta")
90
Gyorgy Szing5c873232019-05-10 23:28:14 +020091 get_filename_component(_NDX_FILE_DIR ${SPHINX_CONFIGURED_INDEX_FILE} DIRECTORY )
92
93 #This command does not generates the specifyed output file and thus it will
94 #allways be run. Any other command or target depending on the "run-allways"
95 #output will be alwways executed too.
96 add_custom_command(OUTPUT run-allways
97 COMMAND "${CMAKE_COMMAND}" -E echo)
98
Gyorgy Szing74dae3c2018-09-27 17:00:46 +020099 #Using add_custom_command allows CMake to generate proper clean commands
100 #for document generation.
101 add_custom_command(OUTPUT "${SPHINX_TMP_DOC_DIR}"
Gyorgy Szing5c873232019-05-10 23:28:14 +0200102 "${SPHINX_CONFIGURED_INDEX_FILE}"
103 #Create target directory for SPHINX_CONFIGURED_INDEX_FILE. Needed
104 #by the next command.
105 COMMAND "${CMAKE_COMMAND}" -E make_directory "${_NDX_FILE_DIR}"
106 #Fill out index.rst template
107 COMMAND "${CMAKE_COMMAND}" -D TFM_ROOT_DIR=${TFM_ROOT_DIR}
108 -D SPHINX_TEMPLATE_INDEX_FILE=${SPHINX_TEMPLATE_INDEX_FILE}
109 -D SPHINX_CONFIGURED_INDEX_FILE=${SPHINX_CONFIGURED_INDEX_FILE}
110 -D SPHINX_DESIGN_DOC_ROOT=${SPHINX_DESIGN_DOC_ROOT}
111 -P "${TFM_ROOT_DIR}/cmake/SphinxDesignDocStatus.cmake"
112 #Copy document files to temp direcotry
113 COMMAND "${CMAKE_COMMAND}" -D TFM_ROOT_DIR=${TFM_ROOT_DIR}
114 -D DST_DIR=${SPHINX_TMP_DOC_DIR}
115 -D BINARY_DIR=${CMAKE_BINARY_DIR}
116 -P "${TFM_ROOT_DIR}/cmake/SphinxCopyDoc.cmake"
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200117 WORKING_DIRECTORY "${TFM_ROOT_DIR}"
Gyorgy Szing5c873232019-05-10 23:28:14 +0200118 DEPENDS run-allways
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200119 VERBATIM
120 )
121
Gyorgy Szingdb9783c2019-04-17 21:08:48 +0200122 add_custom_target(create_sphinx_input
123 SOURCES "${SPHINX_TMP_DOC_DIR}"
124 )
125
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200126 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 Szing5c873232019-05-10 23:28:14 +0200129 DEPENDS create_sphinx_input run-allways
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200130 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.
149 if (PDFLATEX_COMPILER)
Antonio de Angelisb9bba1a2019-05-08 10:28:22 +0100150 set(_PDF_FILE "${SPHINXCFG_OUTPUT_PATH}/latex/TF-M.pdf")
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200151
152 add_custom_command(OUTPUT "${SPHINXCFG_OUTPUT_PATH}/latex"
153 COMMAND "${SPHINX_EXECUTABLE}" -c "${SPHINXCFG_OUTPUT_PATH}" -b latex "${SPHINX_TMP_DOC_DIR}" "${SPHINXCFG_OUTPUT_PATH}/latex"
154 WORKING_DIRECTORY "${TFM_ROOT_DIR}"
Gyorgy Szingdb9783c2019-04-17 21:08:48 +0200155 DEPENDS create_sphinx_input
Gyorgy Szing74dae3c2018-09-27 17:00:46 +0200156 COMMENT "Running Sphinx to generate user guide (LaTeX)."
157 VERBATIM
158 )
159
160 add_custom_command(OUTPUT "${_PDF_FILE}"
161 COMMAND "${CMAKE_MAKE_PROGRAM}" all-pdf
162 WORKING_DIRECTORY ${SPHINXCFG_OUTPUT_PATH}/latex
163 DEPENDS "${SPHINXCFG_OUTPUT_PATH}/latex"
164 COMMENT "Generating PDF version of User Guide..."
165 VERBATIM
166 )
167
168 #We do not use the add_custom_command trick here to get proper clean
169 #command since the clean rules "added" above will remove the entire
170 #doc directory, and thus clean the PDF output too.
171 add_custom_target(doc_userguide_pdf
172 COMMENT "Generating PDF version of TF-M User Guide..."
173 SOURCES "${_PDF_FILE}"
174 VERBATIM)
175
176 #Add the pdf documentation to install content
177 install(FILES "${_PDF_FILE}" DESTINATION "doc/user_guide"
178 RENAME "tf-m_user_guide.pdf"
179 COMPONENT user_guide
180 EXCLUDE_FROM_ALL)
181 else()
182 message(WARNING "PDF generation tools are missing. PDF document generation target is not created.")
183 endif()
184
185 #Generate build target which installs the documentation.
186 if (TARGET doc_userguide_pdf)
187 add_custom_target(install_userguide
188 DEPENDS doc_userguide doc_userguide_pdf
189 COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=user_guide
190 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
191 else()
192 add_custom_target(install_userguide
193 DEPENDS doc_userguide
194 COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=user_guide
195 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
196 endif()
197
198 #Now instantiate a Sphinx configuration file from the template.
199 message(STATUS "Writing Sphinx configuration...")
200 configure_file(${SPHINXCFG_TEMPLATE_FILE} ${SPHINXCFG_CONFIGURED_FILE} @ONLY)
201endif()