1# -*- makefile -*-
2# Makefile for Sphinx documentation
3#
4
5# for cleaning
6subdir- := devicetree/bindings
7
8# Check for broken documentation file references
9ifeq ($(CONFIG_WARN_MISSING_DOCUMENTS),y)
10$(shell $(srctree)/scripts/documentation-file-ref-check --warn)
11endif
12
13# Check for broken ABI files
14ifeq ($(CONFIG_WARN_ABI_ERRORS),y)
15$(shell $(srctree)/scripts/get_abi.pl validate --dir $(srctree)/Documentation/ABI)
16endif
17
18# You can set these variables from the command line.
19SPHINXBUILD   = sphinx-build
20SPHINXOPTS    =
21SPHINXDIRS    = .
22DOCS_THEME    =
23DOCS_CSS      =
24_SPHINXDIRS   = $(sort $(patsubst $(srctree)/Documentation/%/index.rst,%,$(wildcard $(srctree)/Documentation/*/index.rst)))
25SPHINX_CONF   = conf.py
26PAPER         =
27BUILDDIR      = $(obj)/output
28PDFLATEX      = xelatex
29LATEXOPTS     = -interaction=batchmode -no-shell-escape
30
31ifeq ($(findstring 1, $(KBUILD_VERBOSE)),)
32SPHINXOPTS    += "-q"
33endif
34
35# User-friendly check for sphinx-build
36HAVE_SPHINX := $(shell if which $(SPHINXBUILD) >/dev/null 2>&1; then echo 1; else echo 0; fi)
37
38ifeq ($(HAVE_SPHINX),0)
39
40.DEFAULT:
41	$(warning The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed and in PATH, or set the SPHINXBUILD make variable to point to the full path of the '$(SPHINXBUILD)' executable.)
42	@echo
43	@$(srctree)/scripts/sphinx-pre-install
44	@echo "  SKIP    Sphinx $@ target."
45
46else # HAVE_SPHINX
47
48# User-friendly check for pdflatex and latexmk
49HAVE_PDFLATEX := $(shell if which $(PDFLATEX) >/dev/null 2>&1; then echo 1; else echo 0; fi)
50HAVE_LATEXMK := $(shell if which latexmk >/dev/null 2>&1; then echo 1; else echo 0; fi)
51
52ifeq ($(HAVE_LATEXMK),1)
53	PDFLATEX := latexmk -$(PDFLATEX)
54endif #HAVE_LATEXMK
55
56# Internal variables.
57PAPEROPT_a4     = -D latex_paper_size=a4
58PAPEROPT_letter = -D latex_paper_size=letter
59KERNELDOC       = $(srctree)/scripts/kernel-doc
60KERNELDOC_CONF  = -D kerneldoc_srctree=$(srctree) -D kerneldoc_bin=$(KERNELDOC)
61ALLSPHINXOPTS   =  $(KERNELDOC_CONF) $(PAPEROPT_$(PAPER)) $(SPHINXOPTS)
62ifneq ($(wildcard $(srctree)/.config),)
63ifeq ($(CONFIG_RUST),y)
64	# Let Sphinx know we will include rustdoc
65	ALLSPHINXOPTS   +=  -t rustdoc
66endif
67endif
68# the i18n builder cannot share the environment and doctrees with the others
69I18NSPHINXOPTS  = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
70
71# commands; the 'cmd' from scripts/Kbuild.include is not *loopable*
72loop_cmd = $(echo-cmd) $(cmd_$(1)) || exit;
73
74# $2 sphinx builder e.g. "html"
75# $3 name of the build subfolder / e.g. "userspace-api/media", used as:
76#    * dest folder relative to $(BUILDDIR) and
77#    * cache folder relative to $(BUILDDIR)/.doctrees
78# $4 dest subfolder e.g. "man" for man pages at userspace-api/media/man
79# $5 reST source folder relative to $(srctree)/$(src),
80#    e.g. "userspace-api/media" for the linux-tv book-set at ./Documentation/userspace-api/media
81
82quiet_cmd_sphinx = SPHINX  $@ --> file://$(abspath $(BUILDDIR)/$3/$4)
83      cmd_sphinx = $(MAKE) BUILDDIR=$(abspath $(BUILDDIR)) $(build)=Documentation/userspace-api/media $2 && \
84	PYTHONDONTWRITEBYTECODE=1 \
85	BUILDDIR=$(abspath $(BUILDDIR)) SPHINX_CONF=$(abspath $(srctree)/$(src)/$5/$(SPHINX_CONF)) \
86	$(PYTHON3) $(srctree)/scripts/jobserver-exec \
87	$(CONFIG_SHELL) $(srctree)/Documentation/sphinx/parallel-wrapper.sh \
88	$(SPHINXBUILD) \
89	-b $2 \
90	-c $(abspath $(srctree)/$(src)) \
91	-d $(abspath $(BUILDDIR)/.doctrees/$3) \
92	-D version=$(KERNELVERSION) -D release=$(KERNELRELEASE) \
93	$(ALLSPHINXOPTS) \
94	$(abspath $(srctree)/$(src)/$5) \
95	$(abspath $(BUILDDIR)/$3/$4) && \
96	if [ "x$(DOCS_CSS)" != "x" ]; then \
97		cp $(if $(patsubst /%,,$(DOCS_CSS)),$(abspath $(srctree)/$(DOCS_CSS)),$(DOCS_CSS)) $(BUILDDIR)/$3/_static/; \
98	fi
99
100YNL_INDEX:=$(srctree)/Documentation/networking/netlink_spec/index.rst
101YNL_RST_DIR:=$(srctree)/Documentation/networking/netlink_spec
102YNL_YAML_DIR:=$(srctree)/Documentation/netlink/specs
103YNL_TOOL:=$(srctree)/tools/net/ynl/ynl-gen-rst.py
104
105YNL_RST_FILES_TMP := $(patsubst %.yaml,%.rst,$(wildcard $(YNL_YAML_DIR)/*.yaml))
106YNL_RST_FILES := $(patsubst $(YNL_YAML_DIR)%,$(YNL_RST_DIR)%, $(YNL_RST_FILES_TMP))
107
108$(YNL_INDEX): $(YNL_RST_FILES)
109	$(Q)$(YNL_TOOL) -o $@ -x
110
111$(YNL_RST_DIR)/%.rst: $(YNL_YAML_DIR)/%.yaml $(YNL_TOOL)
112	$(Q)$(YNL_TOOL) -i $< -o $@
113
114htmldocs texinfodocs latexdocs epubdocs xmldocs: $(YNL_INDEX)
115
116htmldocs:
117	@$(srctree)/scripts/sphinx-pre-install --version-check
118	@+$(foreach var,$(SPHINXDIRS),$(call loop_cmd,sphinx,html,$(var),,$(var)))
119
120# If Rust support is available and .config exists, add rustdoc generated contents.
121# If there are any, the errors from this make rustdoc will be displayed but
122# won't stop the execution of htmldocs
123
124ifneq ($(wildcard $(srctree)/.config),)
125ifeq ($(CONFIG_RUST),y)
126	$(Q)$(MAKE) rustdoc || true
127endif
128endif
129
130texinfodocs:
131	@$(srctree)/scripts/sphinx-pre-install --version-check
132	@+$(foreach var,$(SPHINXDIRS),$(call loop_cmd,sphinx,texinfo,$(var),texinfo,$(var)))
133
134# Note: the 'info' Make target is generated by sphinx itself when
135# running the texinfodocs target define above.
136infodocs: texinfodocs
137	$(MAKE) -C $(BUILDDIR)/texinfo info
138
139linkcheckdocs:
140	@$(foreach var,$(SPHINXDIRS),$(call loop_cmd,sphinx,linkcheck,$(var),,$(var)))
141
142latexdocs:
143	@$(srctree)/scripts/sphinx-pre-install --version-check
144	@+$(foreach var,$(SPHINXDIRS),$(call loop_cmd,sphinx,latex,$(var),latex,$(var)))
145
146ifeq ($(HAVE_PDFLATEX),0)
147
148pdfdocs:
149	$(warning The '$(PDFLATEX)' command was not found. Make sure you have it installed and in PATH to produce PDF output.)
150	@echo "  SKIP    Sphinx $@ target."
151
152else # HAVE_PDFLATEX
153
154pdfdocs: latexdocs
155	@$(srctree)/scripts/sphinx-pre-install --version-check
156	$(foreach var,$(SPHINXDIRS), \
157	   $(MAKE) PDFLATEX="$(PDFLATEX)" LATEXOPTS="$(LATEXOPTS)" -C $(BUILDDIR)/$(var)/latex || exit; \
158	   mkdir -p $(BUILDDIR)/$(var)/pdf; \
159	   mv $(subst .tex,.pdf,$(wildcard $(BUILDDIR)/$(var)/latex/*.tex)) $(BUILDDIR)/$(var)/pdf/; \
160	)
161
162endif # HAVE_PDFLATEX
163
164epubdocs:
165	@$(srctree)/scripts/sphinx-pre-install --version-check
166	@+$(foreach var,$(SPHINXDIRS),$(call loop_cmd,sphinx,epub,$(var),epub,$(var)))
167
168xmldocs:
169	@$(srctree)/scripts/sphinx-pre-install --version-check
170	@+$(foreach var,$(SPHINXDIRS),$(call loop_cmd,sphinx,xml,$(var),xml,$(var)))
171
172endif # HAVE_SPHINX
173
174# The following targets are independent of HAVE_SPHINX, and the rules should
175# work or silently pass without Sphinx.
176
177refcheckdocs:
178	$(Q)cd $(srctree);scripts/documentation-file-ref-check
179
180cleandocs:
181	$(Q)rm -f $(YNL_INDEX) $(YNL_RST_FILES)
182	$(Q)rm -rf $(BUILDDIR)
183	$(Q)$(MAKE) BUILDDIR=$(abspath $(BUILDDIR)) $(build)=Documentation/userspace-api/media clean
184
185dochelp:
186	@echo  ' Linux kernel internal documentation in different formats from ReST:'
187	@echo  '  htmldocs        - HTML'
188	@echo  '  texinfodocs     - Texinfo'
189	@echo  '  infodocs        - Info'
190	@echo  '  latexdocs       - LaTeX'
191	@echo  '  pdfdocs         - PDF'
192	@echo  '  epubdocs        - EPUB'
193	@echo  '  xmldocs         - XML'
194	@echo  '  linkcheckdocs   - check for broken external links'
195	@echo  '                    (will connect to external hosts)'
196	@echo  '  refcheckdocs    - check for references to non-existing files under'
197	@echo  '                    Documentation'
198	@echo  '  cleandocs       - clean all generated files'
199	@echo
200	@echo  '  make SPHINXDIRS="s1 s2" [target] Generate only docs of folder s1, s2'
201	@echo  '  valid values for SPHINXDIRS are: $(_SPHINXDIRS)'
202	@echo
203	@echo  '  make SPHINX_CONF={conf-file} [target] use *additional* sphinx-build'
204	@echo  '  configuration. This is e.g. useful to build with nit-picking config.'
205	@echo
206	@echo  '  make DOCS_THEME={sphinx-theme} selects a different Sphinx theme.'
207	@echo
208	@echo  '  make DOCS_CSS={a .css file} adds a DOCS_CSS override file for html/epub output.'
209	@echo
210	@echo  '  Default location for the generated documents is Documentation/output'
211