makefile.sphinx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 = ./$(LXC_ENV_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 = ./$(LXC_ENV_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: $(PY_ENV)
  51. @echo "PYENV installing Sphinx$(SPHINXVERS)"
  52. $(Q)$(PY_ENV_BIN)/pip install $(PIP_VERBOSE) 'Sphinx$(SPHINXVERS)'
  53. sphinx-live: $(PY_ENV)
  54. @echo "PYENV installing Sphinx$(SPHINXVERS)"
  55. $(Q)$(PY_ENV_BIN)/pip install $(PIP_VERBOSE) 'Sphinx$(SPHINXVERS)' sphinx-autobuild
  56. PHONY += msg-texlive texlive
  57. ifeq ($(shell which xelatex >/dev/null 2>&1; echo $$?), 1)
  58. texlive: msg-TeXLive
  59. $(error The 'xelatex' command was not found)
  60. else
  61. texlive:
  62. @:
  63. endif
  64. msg-texlive:
  65. $(Q)echo "\n\
  66. The TeX/PDF output and the *math* extension require TexLive and latexmk:\n\n\
  67. Make sure you have a updated TeXLive with XeTeX engine installed, grab it\n\
  68. it from https://www.tug.org/texlive or install it from your package manager.\n\n\
  69. Install latexmk from your package manager or visit https://ctan.org/pkg/latexmk\n\n\
  70. Sphinx-doc produce (Xe)LaTeX files which might use additional TeX-packages\n\
  71. and fonts. To process these LaTeX files, a TexLive installation with the\n\
  72. additional packages is required. On debian based OS these requirements\n\
  73. are installed by::\n\n\
  74. sudo -H apt-get install\n\
  75. latexmk\n\
  76. texlive-base texlive-xetex texlive-latex-recommended\n\
  77. texlive-extra-utils dvipng ttf-dejavu\n"
  78. # ------------------------------------------------------------------------------
  79. # commands
  80. # ------------------------------------------------------------------------------
  81. # $2 sphinx builder e.g. "html"
  82. # $3 path where configuration file (conf.py) is located
  83. # $4 sourcedir
  84. # $5 dest subfolder e.g. "man" for man pages at $(DOCS_DIST)/man
  85. quiet_cmd_sphinx = SPHINX $@ --> file://$(abspath $(DOCS_DIST)/$5)
  86. cmd_sphinx = SPHINX_CONF=$(abspath $4/$(SPHINX_CONF))\
  87. $(SPHINXBUILD) $(SPHINX_VERBOSE) $(SPHINXOPTS)\
  88. -b $2 -c $3 -d $(DOCS_BUILD)/.doctrees $4 $(DOCS_DIST)/$5
  89. quiet_cmd_sphinx_autobuild = SPHINX $@ --> file://$(abspath $(DOCS_DIST)/$5)
  90. 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)\
  91. -b $2 -c $3 -d $(DOCS_BUILD)/.doctrees $4 $(DOCS_DIST)/$5
  92. quiet_cmd_sphinx_clean = CLEAN $@
  93. cmd_sphinx_clean = rm -rf $(DOCS_BUILD) $(DOCS_DIST) $(GH_PAGES)/* $(GH_PAGES)/.buildinfo
  94. # ------------------------------------------------------------------------------
  95. # targets
  96. # ------------------------------------------------------------------------------
  97. # build PDF of whole documentation in: $(DOCS_DIST)/pdf
  98. PHONY += sphinx-pdf
  99. sphinx-pdf: sphinx-latex
  100. $(Q)cd $(DOCS_BUILD)/latex/; make all-pdf
  101. $(Q)mkdir -p $(DOCS_DIST)/pdf
  102. $(Q)cp $(DOCS_BUILD)/latex/*.pdf $(DOCS_DIST)/pdf
  103. @echo "SPHINX *.pdf --> file://$(abspath $(DOCS_DIST)/pdf)"
  104. PHONY += sphinx-latex
  105. sphinx-latex: texlive sphinx-doc
  106. $(SPHINXBUILD) $(SPHINX_VERBOSE) $(SPHINXOPTS)\
  107. -b latex \
  108. -c $(DOCS_FOLDER) \
  109. -d $(DOCS_BUILD)/.doctrees \
  110. $(DOCS_FOLDER) \
  111. $(DOCS_BUILD)/latex
  112. # Sphinx projects, we call them *books* (what is more common). Books are
  113. # folders under $(BOOKS_FOLDER) containing a conf.py file. The HTML output goes
  114. # to folder $(BOOKS_DIST)/<name> while PDF is placed (BOOKS_DIST)/<name>/pdf
  115. BOOKS=$(patsubst $(BOOKS_FOLDER)/%/conf.py,books/%,$(wildcard $(BOOKS_FOLDER)/*/conf.py))
  116. # fine grained targets
  117. BOOKS_HTML = $(patsubst %,%.html,$(BOOKS))
  118. BOOKS_CLEAN = $(patsubst %,%.clean,$(BOOKS))
  119. BOOKS_LATEX = $(patsubst %,%.latex,$(BOOKS))
  120. BOOKS_PDF = $(patsubst %,%.pdf,$(BOOKS))
  121. BOOKS_LIVE = $(patsubst %,%.live,$(BOOKS))
  122. $(BOOKS_DIST):
  123. mkdir -p $(BOOKS_DIST)
  124. PHONY += $(BOOKS_HTML)
  125. $(BOOKS_HTML): sphinx-doc | $(BOOKS_DIST)
  126. SPHINX_CONF=$(patsubst books/%.html,%,$@)/conf.py \
  127. $(SPHINXBUILD) $(SPHINX_VERBOSE) $(SPHINXOPTS)\
  128. -b html \
  129. -c $(DOCS_FOLDER) \
  130. -d $(DOCS_BUILD)/books/$(patsubst books/%.html,%,$@)/.doctrees \
  131. $(BOOKS_FOLDER)/$(patsubst books/%.html,%,$@) \
  132. $(BOOKS_DIST)/$(patsubst books/%.html,%,$@)
  133. @echo "SPHINX $@ --> file://$(abspath $(BOOKS_DIST)/$(patsubst books/%.html,%,$@))"
  134. PHONY += $(BOOKS_HTML)
  135. $(BOOKS_LIVE): sphinx-live | $(BOOKS_DIST)
  136. PATH="$(PY_ENV_BIN):$(PATH)" \
  137. SPHINX_CONF=$(patsubst books/%.live,%,$@)/conf.py \
  138. $(PY_ENV_BIN)/sphinx-autobuild --poll -B --host 0.0.0.0 --port 8080 $(SPHINX_VERBOSE) $(SPHINXOPTS)\
  139. -b html \
  140. -c $(DOCS_FOLDER) \
  141. -d $(DOCS_BUILD)/books/$(patsubst books/%.live,%,$@)/.doctrees \
  142. $(BOOKS_FOLDER)/$(patsubst books/%.live,%,$@) \
  143. $(BOOKS_DIST)/$(patsubst books/%.live,%,$@)
  144. $(BOOKS_PDF): %.pdf : %.latex
  145. $(Q)cd $(DOCS_BUILD)/latex/$(patsubst books/%.pdf,%,$@); make all-pdf
  146. $(Q)mkdir -p $(BOOKS_DIST)/$(patsubst books/%.pdf,%,$@)/pdf
  147. $(Q)cp -v $(DOCS_BUILD)/latex/$(patsubst books/%.pdf,%,$@)/*.pdf $(BOOKS_DIST)/$(patsubst books/%.pdf,%,$@)/pdf
  148. @echo "SPHINX $@ --> file://$(abspath $(BOOKS_DIST)/$(patsubst books/%.pdf,%,$@))/pdf"
  149. PHONY += $(BOOKS_LATEX)
  150. $(BOOKS_LATEX): sphinx-doc | $(BOOKS_DIST)
  151. SPHINX_CONF=$(patsubst books/%.latex,%,$@)/conf.py \
  152. $(SPHINXBUILD) $(SPHINX_VERBOSE) $(SPHINXOPTS)\
  153. -b latex \
  154. -c $(DOCS_FOLDER) \
  155. -d $(DOCS_BUILD)/books/$(patsubst books/%.latex,%,$@)/.doctrees \
  156. $(BOOKS_FOLDER)/$(patsubst books/%.latex,%,$@) \
  157. $(DOCS_BUILD)/latex/$(patsubst books/%.latex,%,$@)
  158. @echo "SPHINX $@ --> file://$(abspath $(DOCS_BUILD)/latex/$(patsubst books/%.latex,%,$@))"
  159. $(BOOKS_CLEAN):
  160. $(Q)rm -rf $(BOOKS_DIST)/$(patsubst books/%.clean,%,$@) \
  161. $(DOCS_BUILD)/books/$(patsubst books/%.clean,%,$@) \
  162. $(DOCS_BUILD)/latex/$(patsubst books/%.clean,%,$@)
  163. # github pages
  164. PHONY += $(GH_PAGES)
  165. $(GH_PAGES)::
  166. $(MAKE) docs
  167. [ -d "gh-pages/.git" ] || git clone $(GIT_URL) gh-pages
  168. -cd $(GH_PAGES); git checkout gh-pages >/dev/null
  169. -cd $(GH_PAGES); git pull
  170. -cd $(GH_PAGES); ls -A | grep -v '.git$$' | xargs rm -rf
  171. cp -r $(DOCS_DIST)/* $(GH_PAGES)/
  172. touch $(GH_PAGES)/.nojekyll
  173. echo "<html><head><META http-equiv='refresh' content='0;URL=index.html'></head></html>" > $(GH_PAGES)/404.html
  174. cd $(GH_PAGES);\
  175. git add --all . ;\
  176. git commit -m "gh-pages: updated" ;\
  177. git push origin gh-pages
  178. PHONY += docs-clean
  179. docs-clean: $(BOOKS_CLEAN)
  180. $(call cmd,sphinx_clean)
  181. .PHONY: $(PHONY)