summaryrefslogtreecommitdiff
path: root/utils/makefile.sphinx
blob: 1b0f42ccb5ca3b1d717745b3a045fbefac77e733 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# -*- coding: utf-8; mode: makefile-gmake -*-

export DOCS_FOLDER DOCS_BUILD DOCS_DIST BOOKS_FOLDER BOOKS_DIST

# You can set these variables from the command line.
SPHINXOPTS  ?=
SPHINXBUILD ?= $(PY_ENV_BIN)/sphinx-build
SPHINX_CONF ?= conf.py

DOCS_FOLDER = ./docs
DOCS_BUILD  = ./$(LXC_ENV_FOLDER)build/docs
DOCS_DIST   = ./$(LXC_ENV_FOLDER)dist/docs
GH_PAGES    ?= gh-pages

BOOKS_FOLDER = ./docs
BOOKS_DIST   = ./$(LXC_ENV_FOLDER)dist/books

ifeq ($(KBUILD_VERBOSE),1)
  SPHINX_VERBOSE = "-v"
else
  SPHINX_VERBOSE =
endif

## SPHINXVERS variable
## ===================
##
## .. _requirement-specifiers: https://pip.pypa.io/en/stable/reference/pip_install/#requirement-specifiers
##
## Sphinx version to use, when building documentation.  Set this when calling
## build target.  The default value is empty (install latest), to select a
## specific version use a requirement-specifiers_.  E.g. to build your target
## 'doc' with a select sphinx-doc_ version 1.7.9::
##
##     make SPHINXVERS='==1.7.9' docs
##
## To build with latest 1.7::
##
##     make SPHINXVERS='>=1.7,<1.8' docs
##
SPHINXVERS  ?=

docs-help:
	@echo  'makefile.sphinx:'
	@echo  '  docs-clean	- clean intermediate doc objects'
	@echo  '  $(GH_PAGES)	- create & upload github pages'
	@echo  '  sphinx-pdf    - run sphinx latex & pdf targets'
	echo   ''
	@echo  '  books/{name}.html : build only the HTML of document {name}'
	@echo  '  valid values for books/{name}.html are:'
	@echo  '    $(BOOKS_HTML)' | $(FMT)
	@echo  '  books/{name}.pdf : build only the PDF of document {name}'
	@echo  '  valid values for books/{name}.pdf are:'
	@echo  '    $(BOOKS_PDF) ' | $(FMT)

# ------------------------------------------------------------------------------
# requirements
# ------------------------------------------------------------------------------

sphinx-doc-prebuilds:: $(PY_ENV)

sphinx-doc: sphinx-doc-prebuilds
	@echo "PYENV     installing Sphinx$(SPHINXVERS)"
	$(Q)$(PY_ENV_BIN)/pip install $(PIP_VERBOSE) 'Sphinx$(SPHINXVERS)'

sphinx-live: sphinx-doc-prebuilds
	@echo "PYENV     installing Sphinx$(SPHINXVERS)"
	$(Q)$(PY_ENV_BIN)/pip install $(PIP_VERBOSE) 'Sphinx$(SPHINXVERS)' sphinx-autobuild


PHONY += msg-texlive texlive

ifeq ($(shell which xelatex >/dev/null 2>&1; echo $$?), 1)
texlive: msg-TeXLive
	$(error The 'xelatex' command was not found)
else
texlive:
	@:
endif

msg-texlive:
	$(Q)echo "\n\
The TeX/PDF output and the *math* extension require TexLive and latexmk:\n\n\
  Make sure you have a updated TeXLive with XeTeX engine installed, grab it\n\
  it from https://www.tug.org/texlive or install it from your package manager.\n\n\
  Install latexmk from your package manager or visit https://ctan.org/pkg/latexmk\n\n\
  Sphinx-doc produce (Xe)LaTeX files which might use additional TeX-packages\n\
  and fonts. To process these LaTeX files, a TexLive installation with the\n\
  additional packages is required. On debian based OS these requirements\n\
  are installed by::\n\n\
    sudo -H apt-get install\n\
         latexmk\n\
         texlive-base texlive-xetex texlive-latex-recommended\n\
         texlive-extra-utils dvipng ttf-dejavu\n"

# ------------------------------------------------------------------------------
# commands
# ------------------------------------------------------------------------------

# $2 sphinx builder e.g. "html"
# $3 path where configuration file (conf.py) is located
# $4 sourcedir
# $5 dest subfolder e.g. "man" for man pages at $(DOCS_DIST)/man

quiet_cmd_sphinx = SPHINX    $@ --> file://$(abspath $(DOCS_DIST)/$5)
      cmd_sphinx = SPHINX_CONF=$(abspath $4/$(SPHINX_CONF))\
	$(SPHINXBUILD) $(SPHINX_VERBOSE) $(SPHINXOPTS)\
	-b $2 -c $3 -d $(DOCS_BUILD)/.doctrees $4 $(DOCS_DIST)/$5

quiet_cmd_sphinx_autobuild = SPHINX    $@ --> file://$(abspath $(DOCS_DIST)/$5)
      cmd_sphinx_autobuild = PATH="$(PY_ENV_BIN):$(PATH)" $(PY_ENV_BIN)/sphinx-autobuild  $(SPHINX_VERBOSE) --poll -B --host 0.0.0.0 --port 8080 $(SPHINXOPTS)\
	-b $2 -c $3 -d $(DOCS_BUILD)/.doctrees $4 $(DOCS_DIST)/$5

quiet_cmd_sphinx_clean = CLEAN     $@
      cmd_sphinx_clean = rm -rf $(DOCS_BUILD) $(DOCS_DIST) $(GH_PAGES)/* $(GH_PAGES)/.buildinfo

# ------------------------------------------------------------------------------
# targets
# ------------------------------------------------------------------------------

# build PDF of whole documentation in: $(DOCS_DIST)/pdf

PHONY += sphinx-pdf
sphinx-pdf: sphinx-latex
	$(Q)cd $(DOCS_BUILD)/latex/; make all-pdf
	$(Q)mkdir -p $(DOCS_DIST)/pdf
	$(Q)cp $(DOCS_BUILD)/latex/*.pdf $(DOCS_DIST)/pdf
	@echo "SPHINX    *.pdf --> file://$(abspath $(DOCS_DIST)/pdf)"

PHONY += sphinx-latex
sphinx-latex: texlive sphinx-doc
	$(SPHINXBUILD) $(SPHINX_VERBOSE) $(SPHINXOPTS)\
	  -b latex \
	  -c $(DOCS_FOLDER) \
	  -d $(DOCS_BUILD)/.doctrees \
	  $(DOCS_FOLDER) \
	  $(DOCS_BUILD)/latex

# Sphinx projects, we call them *books* (what is more common).  Books are
# folders under $(BOOKS_FOLDER) containing a conf.py file. The HTML output goes
# to folder $(BOOKS_DIST)/<name> while PDF is placed (BOOKS_DIST)/<name>/pdf

BOOKS=$(patsubst $(BOOKS_FOLDER)/%/conf.py,books/%,$(wildcard $(BOOKS_FOLDER)/*/conf.py))

# fine grained targets
BOOKS_HTML  = $(patsubst %,%.html,$(BOOKS))
BOOKS_CLEAN = $(patsubst %,%.clean,$(BOOKS))
BOOKS_LATEX = $(patsubst %,%.latex,$(BOOKS))
BOOKS_PDF   = $(patsubst %,%.pdf,$(BOOKS))
BOOKS_LIVE  = $(patsubst %,%.live,$(BOOKS))

$(BOOKS_DIST):
	mkdir -p $(BOOKS_DIST)

PHONY += $(BOOKS_HTML)
$(BOOKS_HTML): sphinx-doc | $(BOOKS_DIST)
	SPHINX_CONF=$(patsubst books/%.html,%,$@)/conf.py \
	$(SPHINXBUILD) $(SPHINX_VERBOSE) $(SPHINXOPTS)\
	  -b html \
	  -c $(DOCS_FOLDER) \
	  -d $(DOCS_BUILD)/books/$(patsubst books/%.html,%,$@)/.doctrees \
	  $(BOOKS_FOLDER)/$(patsubst books/%.html,%,$@) \
	  $(BOOKS_DIST)/$(patsubst books/%.html,%,$@)
	@echo "SPHINX    $@ --> file://$(abspath $(BOOKS_DIST)/$(patsubst books/%.html,%,$@))"

PHONY += $(BOOKS_HTML)
$(BOOKS_LIVE): sphinx-live | $(BOOKS_DIST)
	PATH="$(PY_ENV_BIN):$(PATH)" \
	SPHINX_CONF=$(patsubst books/%.live,%,$@)/conf.py \
	$(PY_ENV_BIN)/sphinx-autobuild --poll -B --host 0.0.0.0 --port 8080 $(SPHINX_VERBOSE) $(SPHINXOPTS)\
	  -b html \
	  -c $(DOCS_FOLDER) \
	  -d $(DOCS_BUILD)/books/$(patsubst books/%.live,%,$@)/.doctrees \
	  $(BOOKS_FOLDER)/$(patsubst books/%.live,%,$@) \
	  $(BOOKS_DIST)/$(patsubst books/%.live,%,$@)

$(BOOKS_PDF): %.pdf : %.latex
	$(Q)cd $(DOCS_BUILD)/latex/$(patsubst books/%.pdf,%,$@); make all-pdf
	$(Q)mkdir -p $(BOOKS_DIST)/$(patsubst books/%.pdf,%,$@)/pdf
	$(Q)cp -v $(DOCS_BUILD)/latex/$(patsubst books/%.pdf,%,$@)/*.pdf $(BOOKS_DIST)/$(patsubst books/%.pdf,%,$@)/pdf
	@echo "SPHINX    $@ --> file://$(abspath $(BOOKS_DIST)/$(patsubst books/%.pdf,%,$@))/pdf"

PHONY += $(BOOKS_LATEX)
$(BOOKS_LATEX): sphinx-doc | $(BOOKS_DIST)
	SPHINX_CONF=$(patsubst books/%.latex,%,$@)/conf.py \
	$(SPHINXBUILD) $(SPHINX_VERBOSE) $(SPHINXOPTS)\
	  -b latex \
	  -c $(DOCS_FOLDER) \
	  -d $(DOCS_BUILD)/books/$(patsubst books/%.latex,%,$@)/.doctrees \
	  $(BOOKS_FOLDER)/$(patsubst books/%.latex,%,$@) \
	  $(DOCS_BUILD)/latex/$(patsubst books/%.latex,%,$@)
	@echo "SPHINX    $@ --> file://$(abspath $(DOCS_BUILD)/latex/$(patsubst books/%.latex,%,$@))"

$(BOOKS_CLEAN):
	$(Q)rm -rf $(BOOKS_DIST)/$(patsubst books/%.clean,%,$@) \
	       $(DOCS_BUILD)/books/$(patsubst books/%.clean,%,$@) \
	       $(DOCS_BUILD)/latex/$(patsubst books/%.clean,%,$@)

# github pages

PHONY += $(GH_PAGES)
$(GH_PAGES)::
	$(MAKE) docs
	[ -d "gh-pages/.git" ] || git clone $(GIT_URL) gh-pages
	-cd $(GH_PAGES); git checkout gh-pages >/dev/null
	-cd $(GH_PAGES); git pull
	-cd $(GH_PAGES); ls -A | grep -v '.git$$' | xargs rm -rf
	cp -r $(DOCS_DIST)/* $(GH_PAGES)/
	touch $(GH_PAGES)/.nojekyll
	echo "<html><head><META http-equiv='refresh' content='0;URL=index.html'></head></html>" > $(GH_PAGES)/404.html
	cd $(GH_PAGES);\
		git add --all . ;\
		git commit -m "gh-pages: updated" ;\
		git push origin gh-pages


PHONY += docs-clean
docs-clean: $(BOOKS_CLEAN)
	$(call cmd,sphinx_clean)

.PHONY: $(PHONY)