build_docs.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python
  2. #
  3. # Top-level docs builder
  4. #
  5. # This is just a front-end to sphinx-build that can call it multiple times for different language/target combinations
  6. #
  7. # Will build out to _build/LANG/TARGET by default
  8. #
  9. # Specific custom docs functionality should be added in conf_common.py or in a Sphinx extension, not here.
  10. #
  11. import argparse
  12. import os
  13. import os.path
  14. import subprocess
  15. import sys
  16. LANGUAGES = ["en", "zh_CN"]
  17. TARGETS = ["esp32", "esp32s2"]
  18. def main():
  19. # check Python dependencies for docs
  20. try:
  21. subprocess.check_call([sys.executable,
  22. os.path.join(os.environ["IDF_PATH"],
  23. "tools",
  24. "check_python_dependencies.py"),
  25. "-r",
  26. "{}/docs/requirements.txt".format(os.environ["IDF_PATH"])
  27. ])
  28. except subprocess.CalledProcessError:
  29. raise SystemExit(2) # stdout will already have these errors
  30. parser = argparse.ArgumentParser(description='build_docs.py: Build IDF docs',
  31. prog='build_docs.py')
  32. parser.add_argument("--language", "-l", choices=LANGUAGES,
  33. required=False)
  34. parser.add_argument("--target", "-t", choices=TARGETS, required=False)
  35. parser.add_argument("--build-dir", "-b", type=str, default="_build")
  36. args = parser.parse_args()
  37. if args.language is None:
  38. print("Building all languages")
  39. languages = LANGUAGES
  40. else:
  41. languages = [args.language]
  42. if args.target is None:
  43. print("Building all targets")
  44. targets = TARGETS
  45. else:
  46. targets = [args.target]
  47. for language in languages:
  48. for target in targets:
  49. build_dir = os.path.realpath(os.path.join(args.build_dir, language, target))
  50. build_docs(language, target, build_dir)
  51. def build_docs(language, target, build_dir):
  52. print("Building language:%s target:%s build_dir:%s" % (language, target, build_dir))
  53. try:
  54. os.makedirs(build_dir)
  55. except OSError:
  56. pass
  57. try:
  58. environ = {}
  59. environ.update(os.environ)
  60. environ['BUILDDIR'] = build_dir
  61. args = [sys.executable, "-m", "sphinx",
  62. "-j", "auto", # use all the cores! (where possible)
  63. "-b", "html", # TODO: PDFs
  64. "-d", os.path.join(build_dir, "doctrees"),
  65. # TODO: support multiple sphinx-warning.log files, somehow
  66. "-w", "sphinx-warning.log",
  67. "-t", target,
  68. "-D", "idf_target={}".format(target),
  69. os.path.join(os.path.abspath(os.path.dirname(__file__)), language), # srcdir for this language
  70. os.path.join(build_dir, "html") # build directory
  71. ]
  72. cwd = build_dir # also run sphinx in the build directory
  73. print("Running '{}'".format(" ".join(args)))
  74. subprocess.check_call(args, cwd=cwd, env=environ)
  75. except subprocess.CalledProcessError:
  76. print("Sphinx failed for language:%s target:%s" % (language, target))
  77. raise SystemExit(1) # rest of the details should be in stdout
  78. if __name__ == "__main__":
  79. main()