1# SPDX-License-Identifier: Apache-2.0
2
3cmake_minimum_required(VERSION 3.20.0)
4project(Zephyr-Kernel-Doc LANGUAGES)
5
6set(MIN_WEST_VERSION 1.0.0)
7find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE} .. COMPONENTS doc)
8
9file(TO_CMAKE_PATH "${ZEPHYR_BASE}" ZEPHYR_BASE)
10message(STATUS "Zephyr base: ${ZEPHYR_BASE}")
11
12#-------------------------------------------------------------------------------
13# Options
14
15set(SPHINXOPTS "-j auto -W --keep-going -T" CACHE STRING "Default Sphinx Options")
16set(SPHINXOPTS_EXTRA "" CACHE STRING "Extra Sphinx Options (added to defaults)")
17set(LATEXMKOPTS "-halt-on-error -no-shell-escape" CACHE STRING "Default latexmk options")
18set(DT_TURBO_MODE OFF CACHE BOOL "Enable DT turbo mode")
19set(DOC_TAG "development" CACHE STRING "Documentation tag")
20set(DTS_ROOTS "${ZEPHYR_BASE}" CACHE STRING "DT bindings root folders")
21
22separate_arguments(SPHINXOPTS)
23separate_arguments(SPHINXOPTS_EXTRA)
24separate_arguments(LATEXMKOPTS)
25
26#-------------------------------------------------------------------------------
27# Dependencies
28
29find_package(Doxygen REQUIRED dot)
30
31find_program(SPHINXBUILD sphinx-build)
32if(NOT SPHINXBUILD)
33  message(FATAL_ERROR "The 'sphinx-build' command was not found")
34endif()
35
36find_package(LATEX COMPONENTS PDFLATEX)
37find_program(LATEXMK latexmk)
38if(NOT LATEX_PDFLATEX_FOUND OR NOT LATEXMK)
39  message(WARNING "LaTeX components not found. PDF build will not be available.")
40endif()
41
42#-------------------------------------------------------------------------------
43# Environment & Paths
44
45set(SPHINX_ENV
46  DOXYGEN_EXECUTABLE=${DOXYGEN_EXECUTABLE}
47  DOT_EXECUTABLE=${DOXYGEN_DOT_EXECUTABLE}
48)
49
50set(DOCS_CFG_DIR ${CMAKE_CURRENT_LIST_DIR})
51set(DOCS_DOCTREE_DIR ${CMAKE_CURRENT_BINARY_DIR}/doctrees)
52set(DOCS_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR})
53set(DOCS_SRC_DIR ${CMAKE_CURRENT_BINARY_DIR}/src)
54set(DOCS_HTML_DIR ${CMAKE_CURRENT_BINARY_DIR}/html)
55set(DOCS_LINKCHECK_DIR ${CMAKE_CURRENT_BINARY_DIR}/linkcheck)
56set(DOCS_LATEX_DIR ${CMAKE_CURRENT_BINARY_DIR}/latex)
57
58if(WIN32)
59  set(SEP $<SEMICOLON>)
60else()
61  set(SEP :)
62endif()
63
64#-------------------------------------------------------------------------------
65# Functions
66
67# Create a custom doc target.
68#
69# This function has the same signature as `add_custom_target()`
70#
71# The function will create two targets for the doc build system.
72# - Target 1 named: `<name>`
73# - Target 2 named: `<name>-nodeps`
74#
75# Both targets will produce same result, but target 2 must have no dependencies.
76# This is useful to, e.g. re-run the Sphinx build without dependencies such as
77# devicetree generator.
78#
79function(add_doc_target name)
80  add_custom_target(${name} ${ARGN})
81  add_custom_target(${name}-nodeps ${ARGN})
82endfunction()
83
84#-------------------------------------------------------------------------------
85# Doxygen (standalone)
86
87set(DOXY_OUT ${CMAKE_CURRENT_BINARY_DIR}/doxygen)
88set(DOXYFILE_IN ${CMAKE_CURRENT_LIST_DIR}/zephyr.doxyfile.in)
89set(DOXYFILE_OUT ${CMAKE_CURRENT_BINARY_DIR}/zephyr.doxyfile)
90set(ZEPHYR_VERSION "${Zephyr_VERSION}")
91
92configure_file(${DOXYFILE_IN} ${DOXYFILE_OUT} @ONLY)
93
94add_custom_target(
95  doxygen
96  COMMAND
97    ${DOXYGEN_EXECUTABLE} ${DOXYFILE_OUT}
98  COMMENT "Running Doxygen..."
99)
100
101set_target_properties(
102  doxygen
103  PROPERTIES
104    ADDITIONAL_CLEAN_FILES "${DOXY_OUT}"
105)
106
107#-------------------------------------------------------------------------------
108# devicetree
109
110set(GEN_DEVICETREE_REST_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/_scripts/gen_devicetree_rest.py)
111
112set(DTS_ARGS)
113foreach(root ${DTS_ROOTS})
114  list(APPEND DTS_ARGS --dts-root ${root})
115endforeach()
116
117if(DT_TURBO_MODE)
118  list(APPEND DTS_ARGS --turbo-mode)
119endif()
120
121add_custom_target(
122  devicetree
123  COMMAND ${CMAKE_COMMAND} -E env
124  PYTHONPATH=${ZEPHYR_BASE}/scripts/dts/python-devicetree/src${SEP}$ENV{PYTHONPATH}
125  ZEPHYR_BASE=${ZEPHYR_BASE}
126  ${PYTHON_EXECUTABLE} ${GEN_DEVICETREE_REST_SCRIPT}
127    --vendor-prefixes ${ZEPHYR_BASE}/dts/bindings/vendor-prefixes.txt
128    ${DTS_ARGS}
129    ${DOCS_SRC_DIR}/build/dts/api
130  VERBATIM
131  USES_TERMINAL
132  COMMENT "Generating Devicetree bindings documentation..."
133)
134
135set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${GEN_DEVICETREE_REST_SCRIPT})
136
137#-------------------------------------------------------------------------------
138# html
139
140add_doc_target(
141  html
142  COMMAND ${CMAKE_COMMAND} -E env ${SPHINX_ENV}
143  ${SPHINXBUILD}
144    -b html
145    -c ${DOCS_CFG_DIR}
146    -d ${DOCS_DOCTREE_DIR}
147    -w ${DOCS_BUILD_DIR}/html.log
148    -t ${DOC_TAG}
149    ${SPHINXOPTS}
150    ${SPHINXOPTS_EXTRA}
151    ${DOCS_SRC_DIR}
152    ${DOCS_HTML_DIR}
153  USES_TERMINAL
154  COMMENT "Running Sphinx HTML build..."
155)
156
157set_target_properties(
158  html html-nodeps
159  PROPERTIES
160    ADDITIONAL_CLEAN_FILES "${DOCS_SRC_DIR};${DOCS_HTML_DIR};${DOCS_DOCTREE_DIR}"
161)
162
163add_dependencies(html devicetree)
164
165#-------------------------------------------------------------------------------
166# pdf
167
168add_doc_target(
169  latex
170  COMMAND ${CMAKE_COMMAND} -E env ${SPHINX_ENV}
171  ${SPHINXBUILD}
172    -b latex
173    -c ${DOCS_CFG_DIR}
174    -d ${DOCS_DOCTREE_DIR}
175    -w ${DOCS_BUILD_DIR}/latex.log
176    -t ${DOC_TAG}
177    -t svgconvert
178    ${SPHINXOPTS}
179    ${SPHINXOPTS_EXTRA}
180    ${DOCS_SRC_DIR}
181    ${DOCS_LATEX_DIR}
182  USES_TERMINAL
183  COMMENT "Running Sphinx LaTeX build..."
184)
185
186set_target_properties(
187  latex latex-nodeps
188  PROPERTIES
189    ADDITIONAL_CLEAN_FILES "${DOCS_SRC_DIR};${DOCS_LATEX_DIR};${DOCS_DOCTREE_DIR}"
190)
191
192add_dependencies(latex devicetree)
193
194if(LATEX_PDFLATEX_FOUND AND LATEXMK)
195  if(WIN32)
196    set(PDF_BUILD_COMMAND "make.bat")
197  else()
198    find_program(MAKE make)
199    if(NOT MAKE)
200      message(FATAL_ERROR "The 'make' command was not found")
201    endif()
202    set(PDF_BUILD_COMMAND ${MAKE})
203  endif()
204
205  add_custom_target(
206    pdf
207    COMMAND ${CMAKE_COMMAND} -E env LATEXMKOPTS="${LATEXMKOPTS}"
208    ${PDF_BUILD_COMMAND}
209    WORKING_DIRECTORY ${DOCS_LATEX_DIR}
210    COMMENT "Building PDF file..."
211    USES_TERMINAL
212  )
213
214  add_dependencies(pdf latex)
215endif()
216
217#-------------------------------------------------------------------------------
218# linkcheck
219
220add_doc_target(
221  linkcheck
222  COMMAND ${CMAKE_COMMAND} -E env ${SPHINX_ENV}
223  ${SPHINXBUILD}
224    -b linkcheck
225    -c ${DOCS_CFG_DIR}
226    -d ${DOCS_DOCTREE_DIR}
227    -w ${DOCS_BUILD_DIR}/linkcheck.log
228    -t ${DOC_TAG}
229    ${SPHINXOPTS}
230    ${SPHINXOPTS_EXTRA}
231    ${DOCS_SRC_DIR}
232    ${DOCS_LINKCHECK_DIR}
233  USES_TERMINAL
234  COMMENT "Running Sphinx link check..."
235)
236
237set_target_properties(
238  linkcheck linkcheck-nodeps
239  PROPERTIES
240    ADDITIONAL_CLEAN_FILES "${DOCS_SRC_DIR};${DOCS_LINKCHECK_DIR};${DOCS_DOCTREE_DIR}"
241)
242
243add_dependencies(linkcheck devicetree)
244
245#-------------------------------------------------------------------------------
246# others
247
248add_custom_target(
249  pristine
250  COMMAND ${CMAKE_COMMAND} -P ${ZEPHYR_BASE}/cmake/pristine.cmake
251)
252