makefile.sphinx 7.8 KB

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