makefile.sphinx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. # -*- coding: utf-8; mode: makefile-gmake -*-
  2. # You can set these variables from the command line.
  3. SPHINXOPTS ?=
  4. SPHINXBUILD ?= $(PY_ENV_BIN)/sphinx-build
  5. SPHINX_CONF ?= conf.py
  6. DOCS_FOLDER ?= docs
  7. DOCS_BUILD ?= build/docs
  8. DOCS_DIST ?= dist/docs
  9. GH_PAGES ?= gh-pages
  10. BOOKS_FOLDER ?= docs
  11. BOOKS_DIST ?= dist/books
  12. ifeq ($(KBUILD_VERBOSE),1)
  13. SPHINX_VERBOSE = "-v"
  14. else
  15. SPHINX_VERBOSE =
  16. endif
  17. ## SPHINXVERS variable
  18. ## ===================
  19. ##
  20. ## .. _requirement-specifiers: https://pip.pypa.io/en/stable/reference/pip_install/#requirement-specifiers
  21. ##
  22. ## Sphinx version to use, when building documentation. Set this when calling
  23. ## build target. The default value is empty (install latest), to select a
  24. ## specific version use a requirement-specifiers_. E.g. to build your target
  25. ## 'doc' with a select sphinx-doc_ version 1.7.9::
  26. ##
  27. ## make SPHINXVERS='==1.7.9' docs
  28. ##
  29. ## To build with latest 1.7::
  30. ##
  31. ## make SPHINXVERS='>=1.7,<1.8' docs
  32. ##
  33. SPHINXVERS ?=
  34. docs-help:
  35. @echo 'makefile.sphinx:'
  36. @echo ' docs-clean - clean intermediate doc objects'
  37. @echo ' $(GH_PAGES) - create & upload github pages'
  38. @echo ' sphinx-pdf - run sphinx latex & pdf targets'
  39. echo ''
  40. @echo ' books/{name}.html : build only the HTML of document {name}'
  41. @echo ' valid values for books/{name}.html are:'
  42. @echo ' $(BOOKS_HTML)' | $(FMT)
  43. @echo ' books/{name}.pdf : build only the PDF of document {name}'
  44. @echo ' valid values for books/{name}.pdf are:'
  45. @echo ' $(BOOKS_PDF) ' | $(FMT)
  46. # ------------------------------------------------------------------------------
  47. # requirements
  48. # ------------------------------------------------------------------------------
  49. sphinx-doc: $(PY_ENV)
  50. @echo "PYENV installing Sphinx$(SPHINXVERS)"
  51. $(Q)$(PY_ENV_BIN)/pip install $(PIP_VERBOSE) 'Sphinx$(SPHINXVERS)'
  52. sphinx-live: $(PY_ENV)
  53. @echo "PYENV installing Sphinx$(SPHINXVERS)"
  54. $(Q)$(PY_ENV_BIN)/pip install $(PIP_VERBOSE) 'Sphinx$(SPHINXVERS)' sphinx-autobuild
  55. PHONY += msg-texlive texlive
  56. ifeq ($(shell which xelatex >/dev/null 2>&1; echo $$?), 1)
  57. texlive: msg-TeXLive
  58. $(error The 'xelatex' command was not found)
  59. else
  60. texlive:
  61. @:
  62. endif
  63. msg-texlive:
  64. $(Q)echo "\n\
  65. The TeX/PDF output and the *math* extension require TexLive and latexmk:\n\n\
  66. Make sure you have a updated TeXLive with XeTeX engine installed, grab it\n\
  67. it from https://www.tug.org/texlive or install it from your package manager.\n\n\
  68. Install latexmk from your package manager or visit https://ctan.org/pkg/latexmk\n\n\
  69. Sphinx-doc produce (Xe)LaTeX files which might use additional TeX-packages\n\
  70. and fonts. To process these LaTeX files, a TexLive installation with the\n\
  71. additional packages is required. On debian based OS these requirements\n\
  72. are installed by::\n\n\
  73. sudo -H apt-get install\n\
  74. latexmk\n\
  75. texlive-base texlive-xetex texlive-latex-recommended\n\
  76. texlive-extra-utils dvipng ttf-dejavu\n"
  77. # ------------------------------------------------------------------------------
  78. # commands
  79. # ------------------------------------------------------------------------------
  80. # $2 sphinx builder e.g. "html"
  81. # $3 path where configuration file (conf.py) is located
  82. # $4 sourcedir
  83. # $5 dest subfolder e.g. "man" for man pages at $(DOCS_DIST)/man
  84. quiet_cmd_sphinx = SPHINX $@ --> file://$(abspath $(DOCS_DIST)/$5)
  85. cmd_sphinx = SPHINX_CONF=$(abspath $4/$(SPHINX_CONF))\
  86. $(SPHINXBUILD) $(SPHINX_VERBOSE) $(SPHINXOPTS)\
  87. -b $2 -c $3 -d $(DOCS_BUILD)/.doctrees $4 $(DOCS_DIST)/$5
  88. quiet_cmd_sphinx_autobuild = SPHINX $@ --> file://$(abspath $(DOCS_DIST)/$5)
  89. 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)\
  90. -b $2 -c $3 -d $(DOCS_BUILD)/.doctrees $4 $(DOCS_DIST)/$5
  91. quiet_cmd_sphinx_clean = CLEAN $@
  92. cmd_sphinx_clean = rm -rf $(DOCS_BUILD) $(DOCS_DIST) $(GH_PAGES)/* $(GH_PAGES)/.buildinfo
  93. # ------------------------------------------------------------------------------
  94. # targets
  95. # ------------------------------------------------------------------------------
  96. # build PDF of whole documentation in: $(DOCS_DIST)/pdf
  97. PHONY += sphinx-pdf
  98. sphinx-pdf: sphinx-latex
  99. $(Q)cd $(DOCS_BUILD)/latex/; make all-pdf
  100. $(Q)mkdir -p $(DOCS_DIST)/pdf
  101. $(Q)cp $(DOCS_BUILD)/latex/*.pdf $(DOCS_DIST)/pdf
  102. @echo "SPHINX *.pdf --> file://$(abspath $(DOCS_DIST)/pdf)"
  103. PHONY += sphinx-latex
  104. sphinx-latex: texlive sphinx-doc
  105. $(SPHINXBUILD) $(SPHINX_VERBOSE) $(SPHINXOPTS)\
  106. -b latex \
  107. -c $(DOCS_FOLDER) \
  108. -d $(DOCS_BUILD)/.doctrees \
  109. $(DOCS_FOLDER) \
  110. $(DOCS_BUILD)/latex
  111. # Sphinx projects, we call them *books* (what is more common). Books are
  112. # folders under $(BOOKS_FOLDER) containing a conf.py file. The HTML output goes
  113. # to folder $(BOOKS_DIST)/<name> while PDF is placed (BOOKS_DIST)/<name>/pdf
  114. BOOKS=$(patsubst $(BOOKS_FOLDER)/%/conf.py,books/%,$(wildcard $(BOOKS_FOLDER)/*/conf.py))
  115. # fine grained targets
  116. BOOKS_HTML = $(patsubst %,%.html,$(BOOKS))
  117. BOOKS_CLEAN = $(patsubst %,%.clean,$(BOOKS))
  118. BOOKS_LATEX = $(patsubst %,%.latex,$(BOOKS))
  119. BOOKS_PDF = $(patsubst %,%.pdf,$(BOOKS))
  120. BOOKS_LIVE = $(patsubst %,%.live,$(BOOKS))
  121. $(BOOKS_DIST):
  122. mkdir -p $(BOOKS_DIST)
  123. PHONY += $(BOOKS_HTML)
  124. $(BOOKS_HTML): sphinx-doc | $(BOOKS_DIST)
  125. SPHINX_CONF=$(patsubst books/%.html,%,$@)/conf.py \
  126. $(SPHINXBUILD) $(SPHINX_VERBOSE) $(SPHINXOPTS)\
  127. -b html \
  128. -c $(DOCS_FOLDER) \
  129. -d $(DOCS_BUILD)/books/$(patsubst books/%.html,%,$@)/.doctrees \
  130. $(patsubst books/%.html,%,$@) \
  131. $(BOOKS_DIST)/$(patsubst books/%.html,%,$@)
  132. @echo "SPHINX $@ --> file://$(abspath $(BOOKS_DIST)/$(patsubst books/%.html,%,$@))"
  133. PHONY += $(BOOKS_HTML)
  134. $(BOOKS_LIVE): sphinx-live | $(BOOKS_DIST)
  135. PATH="$(PY_ENV_BIN):$(PATH)" \
  136. SPHINX_CONF=$(patsubst books/%.live,%,$@)/conf.py \
  137. $(PY_ENV_BIN)/sphinx-autobuild --poll -B --host 0.0.0.0 --port 8080 $(SPHINX_VERBOSE) $(SPHINXOPTS)\
  138. -b html \
  139. -c $(DOCS_FOLDER) \
  140. -d $(DOCS_BUILD)/books/$(patsubst books/%.live,%,$@)/.doctrees \
  141. $(patsubst books/%.live,%,$@) \
  142. $(BOOKS_DIST)/$(patsubst books/%.live,%,$@)
  143. $(BOOKS_PDF): %.pdf : %.latex
  144. $(Q)cd $(DOCS_BUILD)/latex/$(patsubst books/%.pdf,%,$@); make all-pdf
  145. $(Q)mkdir -p $(BOOKS_DIST)/$(patsubst books/%.pdf,%,$@)/pdf
  146. $(Q)cp -v $(DOCS_BUILD)/latex/$(patsubst books/%.pdf,%,$@)/*.pdf $(BOOKS_DIST)/$(patsubst books/%.pdf,%,$@)/pdf
  147. @echo "SPHINX $@ --> file://$(abspath $(BOOKS_DIST)/$(patsubst books/%.pdf,%,$@))/pdf"
  148. PHONY += $(BOOKS_LATEX)
  149. $(BOOKS_LATEX): sphinx-doc | $(BOOKS_DIST)
  150. SPHINX_CONF=$(patsubst books/%.latex,%,$@)/conf.py \
  151. $(SPHINXBUILD) $(SPHINX_VERBOSE) $(SPHINXOPTS)\
  152. -b latex \
  153. -c $(DOCS_FOLDER) \
  154. -d $(DOCS_BUILD)/books/$(patsubst books/%.latex,%,$@)/.doctrees \
  155. $(patsubst books/%.latex,%,$@) \
  156. $(DOCS_BUILD)/latex/$(patsubst books/%.latex,%,$@)
  157. @echo "SPHINX $@ --> file://$(abspath $(DOCS_BUILD)/latex/$(patsubst books/%.latex,%,$@))"
  158. $(BOOKS_CLEAN):
  159. $(Q)rm -rf $(BOOKS_DIST)/$(patsubst books/%.clean,%,$@) \
  160. $(DOCS_BUILD)/books/$(patsubst books/%.clean,%,$@) \
  161. $(DOCS_BUILD)/latex/$(patsubst books/%.clean,%,$@)
  162. # github pages
  163. PHONY += $(GH_PAGES)
  164. $(GH_PAGES):
  165. $(MAKE) docs
  166. [ -d "gh-pages/.git" ] || git clone $(GIT_URL) gh-pages
  167. -cd $(GH_PAGES); git checkout --orphan gh-pages || exit 0
  168. rm -rf $(GH_PAGES)/* $(GH_PAGES)/.gitignore
  169. touch $(GH_PAGES)/.nojekyll ;\
  170. cp -r $(DOCS_DIST)/* $(GH_PAGES)/
  171. echo "<html><head><META http-equiv='refresh' content='0;URL=index.html'></head></html>" > $(GH_PAGES)/404.html
  172. -cd $(GH_PAGES); git push origin :gh-pages
  173. cd $(GH_PAGES);\
  174. git add --all . ;\
  175. git commit -m "gh-pages: updated" ;\
  176. git push origin gh-pages
  177. PHONY += docs-clean
  178. docs-clean: $(BOOKS_CLEAN)
  179. $(call cmd,sphinx_clean)
  180. .PHONY: $(PHONY)