makefile.sphinx 7.2 KB

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