build.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env python3
  2. # Build the documentation.
  3. import errno, os, re, sys
  4. from subprocess import check_call, CalledProcessError, Popen, PIPE, STDOUT
  5. versions = ['1.0.0', '1.1.0', '2.0.0', '3.0.2', '4.0.0', '4.1.0', '5.0.0', '5.1.0', '5.2.0', '5.2.1', '5.3.0', '6.0.0', '6.1.0', '6.1.1', '6.1.2', '6.2.0', '6.2.1', '7.0.0', '7.0.1', '7.0.2', '7.0.3', '7.1.0', '7.1.1', '7.1.2', '7.1.3', '8.0.0', '8.0.1', '8.1.0', '8.1.1', '9.0.0', '9.1.0', '10.0.0', '10.1.0', '10.1.1', '10.1.1']
  6. class Pip:
  7. def __init__(self, venv_dir):
  8. self.path = os.path.join(venv_dir, 'bin', 'pip')
  9. def install(self, package, commit=None):
  10. "Install package using pip."
  11. if commit:
  12. package = 'git+https://github.com/{0}.git@{1}'.format(package, commit)
  13. print('Installing {0}'.format(package))
  14. check_call([self.path, 'install', package])
  15. def create_build_env(venv_dir='virtualenv'):
  16. # Create virtualenv.
  17. if not os.path.exists(venv_dir):
  18. check_call(['python3', '-m', 'venv', venv_dir])
  19. # Install Sphinx and Breathe. Require the exact version of Sphinx which is
  20. # compatible with Breathe.
  21. pip = Pip(venv_dir)
  22. pip.install('wheel')
  23. pip.install('six')
  24. # See: https://github.com/sphinx-doc/sphinx/issues/9777
  25. pip.install('docutils==0.17.1')
  26. # Jinja2 >= 3.1 incompatible with sphinx 3.3.0
  27. # See: https://github.com/sphinx-doc/sphinx/issues/10291
  28. pip.install('Jinja2<3.1')
  29. pip.install('sphinx-doc/sphinx', 'v3.3.0')
  30. pip.install('michaeljones/breathe', 'v4.25.0')
  31. def build_docs(version='dev', **kwargs):
  32. doc_dir = kwargs.get('doc_dir', os.path.dirname(os.path.realpath(__file__)))
  33. work_dir = kwargs.get('work_dir', '.')
  34. include_dir = kwargs.get(
  35. 'include_dir', os.path.join(os.path.dirname(doc_dir), 'include', 'fmt'))
  36. # Build docs.
  37. cmd = ['doxygen', '-']
  38. p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
  39. doxyxml_dir = os.path.join(work_dir, 'doxyxml')
  40. out, _ = p.communicate(input=r'''
  41. PROJECT_NAME = fmt
  42. GENERATE_LATEX = NO
  43. GENERATE_MAN = NO
  44. GENERATE_RTF = NO
  45. CASE_SENSE_NAMES = NO
  46. INPUT = {0}/args.h {0}/chrono.h {0}/color.h {0}/core.h \
  47. {0}/compile.h {0}/format.h {0}/os.h {0}/ostream.h \
  48. {0}/printf.h {0}/xchar.h
  49. QUIET = YES
  50. JAVADOC_AUTOBRIEF = YES
  51. AUTOLINK_SUPPORT = NO
  52. GENERATE_HTML = NO
  53. GENERATE_XML = YES
  54. XML_OUTPUT = {1}
  55. ALIASES = "rst=\verbatim embed:rst"
  56. ALIASES += "endrst=\endverbatim"
  57. MACRO_EXPANSION = YES
  58. PREDEFINED = _WIN32=1 \
  59. __linux__=1 \
  60. FMT_ENABLE_IF(...)= \
  61. FMT_USE_VARIADIC_TEMPLATES=1 \
  62. FMT_USE_RVALUE_REFERENCES=1 \
  63. FMT_USE_USER_DEFINED_LITERALS=1 \
  64. FMT_USE_ALIAS_TEMPLATES=1 \
  65. FMT_USE_NONTYPE_TEMPLATE_ARGS=1 \
  66. FMT_API= \
  67. "FMT_BEGIN_NAMESPACE=namespace fmt {{" \
  68. "FMT_END_NAMESPACE=}}" \
  69. "FMT_STRING_ALIAS=1" \
  70. "FMT_VARIADIC(...)=" \
  71. "FMT_VARIADIC_W(...)=" \
  72. "FMT_DOC=1"
  73. EXCLUDE_SYMBOLS = fmt::formatter fmt::printf_formatter fmt::arg_join \
  74. fmt::basic_format_arg::handle
  75. '''.format(include_dir, doxyxml_dir).encode('UTF-8'))
  76. out = out.decode('utf-8')
  77. internal_symbols = [
  78. 'fmt::detail::.*',
  79. 'basic_data<>',
  80. 'fmt::type_identity'
  81. ]
  82. noisy_warnings = [
  83. 'warning: (Compound|Member .* of class) (' + '|'.join(internal_symbols) + \
  84. ') is not documented.',
  85. 'warning: Internal inconsistency: .* does not belong to any container!'
  86. ]
  87. for w in noisy_warnings:
  88. out = re.sub('.*' + w + '\n', '', out)
  89. print(out)
  90. if p.returncode != 0:
  91. raise CalledProcessError(p.returncode, cmd)
  92. html_dir = os.path.join(work_dir, 'html')
  93. main_versions = reversed(versions[-3:])
  94. check_call([os.path.join(work_dir, 'virtualenv', 'bin', 'sphinx-build'),
  95. '-Dbreathe_projects.format=' + os.path.abspath(doxyxml_dir),
  96. '-Dversion=' + version, '-Drelease=' + version,
  97. '-Aversion=' + version, '-Aversions=' + ','.join(main_versions),
  98. '-b', 'html', doc_dir, html_dir])
  99. try:
  100. check_call(['lessc', '--verbose', '--clean-css',
  101. '--include-path=' + os.path.join(doc_dir, 'bootstrap'),
  102. os.path.join(doc_dir, 'fmt.less'),
  103. os.path.join(html_dir, '_static', 'fmt.css')])
  104. except OSError as e:
  105. if e.errno != errno.ENOENT:
  106. raise
  107. print('lessc not found; make sure that Less (http://lesscss.org/) ' +
  108. 'is installed')
  109. sys.exit(1)
  110. return html_dir
  111. if __name__ == '__main__':
  112. create_build_env()
  113. build_docs(sys.argv[1])