gtkdochelper.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. # Copyright 2015-2016 The Meson development team
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. # Unless required by applicable law or agreed to in writing, software
  7. # distributed under the License is distributed on an "AS IS" BASIS,
  8. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. # See the License for the specific language governing permissions and
  10. # limitations under the License.
  11. import sys, os
  12. import subprocess
  13. import shutil
  14. import argparse
  15. from ..mesonlib import MesonException, Popen_safe
  16. from . import destdir_join
  17. parser = argparse.ArgumentParser()
  18. parser.add_argument('--sourcedir', dest='sourcedir')
  19. parser.add_argument('--builddir', dest='builddir')
  20. parser.add_argument('--subdir', dest='subdir')
  21. parser.add_argument('--headerdirs', dest='headerdirs')
  22. parser.add_argument('--mainfile', dest='mainfile')
  23. parser.add_argument('--modulename', dest='modulename')
  24. parser.add_argument('--htmlargs', dest='htmlargs', default='')
  25. parser.add_argument('--scanargs', dest='scanargs', default='')
  26. parser.add_argument('--scanobjsargs', dest='scanobjsargs', default='')
  27. parser.add_argument('--gobjects-types-file', dest='gobject_typesfile', default='')
  28. parser.add_argument('--fixxrefargs', dest='fixxrefargs', default='')
  29. parser.add_argument('--mkdbargs', dest='mkdbargs', default='')
  30. parser.add_argument('--ld', dest='ld', default='')
  31. parser.add_argument('--cc', dest='cc', default='')
  32. parser.add_argument('--ldflags', dest='ldflags', default='')
  33. parser.add_argument('--cflags', dest='cflags', default='')
  34. parser.add_argument('--content-files', dest='content_files', default='')
  35. parser.add_argument('--expand-content-files', dest='expand_content_files', default='')
  36. parser.add_argument('--html-assets', dest='html_assets', default='')
  37. parser.add_argument('--ignore-headers', dest='ignore_headers', default='')
  38. parser.add_argument('--namespace', dest='namespace', default='')
  39. parser.add_argument('--mode', dest='mode', default='')
  40. parser.add_argument('--installdir', dest='install_dir')
  41. def gtkdoc_run_check(cmd, cwd):
  42. # Put stderr into stdout since we want to print it out anyway.
  43. # This preserves the order of messages.
  44. p, out = Popen_safe(cmd, cwd=cwd, stderr=subprocess.STDOUT)[0:2]
  45. if p.returncode != 0:
  46. err_msg = ["{!r} failed with status {:d}".format(cmd[0], p.returncode)]
  47. if out:
  48. err_msg.append(out)
  49. raise MesonException('\n'.join(err_msg))
  50. def build_gtkdoc(source_root, build_root, doc_subdir, src_subdirs,
  51. main_file, module,
  52. html_args, scan_args, fixxref_args, mkdb_args,
  53. gobject_typesfile, scanobjs_args, ld, cc, ldflags, cflags,
  54. html_assets, content_files, ignore_headers, namespace,
  55. expand_content_files, mode):
  56. print("Building documentation for %s" % module)
  57. src_dir_args = []
  58. for src_dir in src_subdirs:
  59. if not os.path.isabs(src_dir):
  60. dirs = [os.path.join(source_root, src_dir),
  61. os.path.join(build_root, src_dir)]
  62. else:
  63. dirs = [src_dir]
  64. src_dir_args += ['--source-dir=' + d for d in dirs]
  65. doc_src = os.path.join(source_root, doc_subdir)
  66. abs_out = os.path.join(build_root, doc_subdir)
  67. htmldir = os.path.join(abs_out, 'html')
  68. content_files += [main_file]
  69. sections = os.path.join(doc_src, module + "-sections.txt")
  70. if os.path.exists(sections):
  71. content_files.append(sections)
  72. overrides = os.path.join(doc_src, module + "-overrides.txt")
  73. if os.path.exists(overrides):
  74. content_files.append(overrides)
  75. # Copy files to build directory
  76. for f in content_files:
  77. f_abs = os.path.join(doc_src, f)
  78. shutil.copyfile(f_abs, os.path.join(
  79. abs_out, os.path.basename(f_abs)))
  80. shutil.rmtree(htmldir, ignore_errors=True)
  81. try:
  82. os.mkdir(htmldir)
  83. except Exception:
  84. pass
  85. for f in html_assets:
  86. f_abs = os.path.join(doc_src, f)
  87. shutil.copyfile(f_abs, os.path.join(htmldir, os.path.basename(f_abs)))
  88. scan_cmd = ['gtkdoc-scan', '--module=' + module] + src_dir_args
  89. if ignore_headers:
  90. scan_cmd.append('--ignore-headers=' + ' '.join(ignore_headers))
  91. # Add user-specified arguments
  92. scan_cmd += scan_args
  93. gtkdoc_run_check(scan_cmd, abs_out)
  94. if gobject_typesfile:
  95. scanobjs_cmd = ['gtkdoc-scangobj'] + scanobjs_args + ['--types=' + gobject_typesfile,
  96. '--module=' + module,
  97. '--cflags=' + cflags,
  98. '--ldflags=' + ldflags]
  99. gtkdoc_run_check(scanobjs_cmd, abs_out)
  100. # Make docbook files
  101. if mode == 'auto':
  102. # Guessing is probably a poor idea but these keeps compat
  103. # with previous behavior
  104. if main_file.endswith('sgml'):
  105. modeflag = '--sgml-mode'
  106. else:
  107. modeflag = '--xml-mode'
  108. elif mode == 'xml':
  109. modeflag = '--xml-mode'
  110. elif mode == 'sgml':
  111. modeflag = '--sgml-mode'
  112. else: # none
  113. modeflag = None
  114. mkdb_cmd = ['gtkdoc-mkdb',
  115. '--module=' + module,
  116. '--output-format=xml',
  117. '--expand-content-files=' + ' '.join(expand_content_files),
  118. ] + src_dir_args
  119. if namespace:
  120. mkdb_cmd.append('--name-space=' + namespace)
  121. if modeflag:
  122. mkdb_cmd.append(modeflag)
  123. if len(main_file) > 0:
  124. # Yes, this is the flag even if the file is in xml.
  125. mkdb_cmd.append('--main-sgml-file=' + main_file)
  126. # Add user-specified arguments
  127. mkdb_cmd += mkdb_args
  128. gtkdoc_run_check(mkdb_cmd, abs_out)
  129. # Make HTML documentation
  130. mkhtml_cmd = ['gtkdoc-mkhtml',
  131. '--path=' + ':'.join((doc_src, abs_out)),
  132. module,
  133. ] + html_args
  134. if len(main_file) > 0:
  135. mkhtml_cmd.append('../' + main_file)
  136. else:
  137. mkhtml_cmd.append('%s-docs.xml' % module)
  138. # html gen must be run in the HTML dir
  139. gtkdoc_run_check(mkhtml_cmd, os.path.join(abs_out, 'html'))
  140. # Fix cross-references in HTML files
  141. fixref_cmd = ['gtkdoc-fixxref',
  142. '--module=' + module,
  143. '--module-dir=html'] + fixxref_args
  144. gtkdoc_run_check(fixref_cmd, abs_out)
  145. def install_gtkdoc(build_root, doc_subdir, install_prefix, datadir, module):
  146. source = os.path.join(build_root, doc_subdir, 'html')
  147. final_destination = os.path.join(install_prefix, datadir, module)
  148. shutil.rmtree(final_destination, ignore_errors=True)
  149. shutil.copytree(source, final_destination)
  150. def run(args):
  151. options = parser.parse_args(args)
  152. if len(options.htmlargs) > 0:
  153. htmlargs = options.htmlargs.split('@@')
  154. else:
  155. htmlargs = []
  156. if len(options.scanargs) > 0:
  157. scanargs = options.scanargs.split('@@')
  158. else:
  159. scanargs = []
  160. if len(options.scanobjsargs) > 0:
  161. scanobjsargs = options.scanobjsargs.split('@@')
  162. else:
  163. scanobjsargs = []
  164. if len(options.fixxrefargs) > 0:
  165. fixxrefargs = options.fixxrefargs.split('@@')
  166. else:
  167. fixxrefargs = []
  168. if len(options.mkdbargs) > 0:
  169. mkdbargs = options.mkdbargs.split('@@')
  170. else:
  171. mkdbargs = []
  172. build_gtkdoc(
  173. options.sourcedir,
  174. options.builddir,
  175. options.subdir,
  176. options.headerdirs.split('@@'),
  177. options.mainfile,
  178. options.modulename,
  179. htmlargs,
  180. scanargs,
  181. fixxrefargs,
  182. mkdbargs,
  183. options.gobject_typesfile,
  184. scanobjsargs,
  185. options.ld,
  186. options.cc,
  187. options.ldflags,
  188. options.cflags,
  189. options.html_assets.split('@@') if options.html_assets else [],
  190. options.content_files.split('@@') if options.content_files else [],
  191. options.ignore_headers.split('@@') if options.ignore_headers else [],
  192. options.namespace,
  193. options.expand_content_files.split('@@') if options.expand_content_files else [],
  194. options.mode)
  195. if 'MESON_INSTALL_PREFIX' in os.environ:
  196. destdir = os.environ.get('DESTDIR', '')
  197. install_prefix = destdir_join(destdir, os.environ['MESON_INSTALL_PREFIX'])
  198. install_dir = options.install_dir if options.install_dir else options.modulename
  199. if os.path.isabs(install_dir):
  200. install_dir = destdir_join(destdir, install_dir)
  201. install_gtkdoc(options.builddir,
  202. options.subdir,
  203. install_prefix,
  204. 'share/gtk-doc/html',
  205. install_dir)
  206. return 0
  207. if __name__ == '__main__':
  208. sys.exit(run(sys.argv[1:]))