gtkdochelper.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 shlex
  14. import shutil
  15. import argparse
  16. from ..mesonlib import MesonException, Popen_safe, is_windows
  17. from . import destdir_join
  18. parser = argparse.ArgumentParser()
  19. parser.add_argument('--sourcedir', dest='sourcedir')
  20. parser.add_argument('--builddir', dest='builddir')
  21. parser.add_argument('--subdir', dest='subdir')
  22. parser.add_argument('--headerdirs', dest='headerdirs')
  23. parser.add_argument('--mainfile', dest='mainfile')
  24. parser.add_argument('--modulename', dest='modulename')
  25. parser.add_argument('--moduleversion', dest='moduleversion')
  26. parser.add_argument('--htmlargs', dest='htmlargs', default='')
  27. parser.add_argument('--scanargs', dest='scanargs', default='')
  28. parser.add_argument('--scanobjsargs', dest='scanobjsargs', default='')
  29. parser.add_argument('--gobjects-types-file', dest='gobject_typesfile', default='')
  30. parser.add_argument('--fixxrefargs', dest='fixxrefargs', default='')
  31. parser.add_argument('--mkdbargs', dest='mkdbargs', default='')
  32. parser.add_argument('--ld', dest='ld', default='')
  33. parser.add_argument('--cc', dest='cc', default='')
  34. parser.add_argument('--ldflags', dest='ldflags', default='')
  35. parser.add_argument('--cflags', dest='cflags', default='')
  36. parser.add_argument('--content-files', dest='content_files', default='')
  37. parser.add_argument('--expand-content-files', dest='expand_content_files', default='')
  38. parser.add_argument('--html-assets', dest='html_assets', default='')
  39. parser.add_argument('--ignore-headers', dest='ignore_headers', default='')
  40. parser.add_argument('--namespace', dest='namespace', default='')
  41. parser.add_argument('--mode', dest='mode', default='')
  42. parser.add_argument('--installdir', dest='install_dir')
  43. parser.add_argument('--run', dest='run', default='')
  44. def gtkdoc_run_check(cmd, cwd, library_paths=None):
  45. if library_paths is None:
  46. library_paths = []
  47. env = dict(os.environ)
  48. if is_windows():
  49. if 'PATH' in env:
  50. library_paths.extend(env['PATH'].split(os.pathsep))
  51. env['PATH'] = os.pathsep.join(library_paths)
  52. else:
  53. if 'LD_LIBRARY_PATH' in env:
  54. library_paths.extend(env['LD_LIBRARY_PATH'].split(os.pathsep))
  55. env['LD_LIBRARY_PATH'] = os.pathsep.join(library_paths)
  56. # Put stderr into stdout since we want to print it out anyway.
  57. # This preserves the order of messages.
  58. p, out = Popen_safe(cmd, cwd=cwd, env=env, stderr=subprocess.STDOUT)[0:2]
  59. if p.returncode != 0:
  60. err_msg = ["{!r} failed with status {:d}".format(cmd, p.returncode)]
  61. if out:
  62. err_msg.append(out)
  63. raise MesonException('\n'.join(err_msg))
  64. elif out:
  65. print(out)
  66. def build_gtkdoc(source_root, build_root, doc_subdir, src_subdirs,
  67. main_file, module, module_version,
  68. html_args, scan_args, fixxref_args, mkdb_args,
  69. gobject_typesfile, scanobjs_args, run, ld, cc, ldflags, cflags,
  70. html_assets, content_files, ignore_headers, namespace,
  71. expand_content_files, mode):
  72. print("Building documentation for %s" % module)
  73. src_dir_args = []
  74. for src_dir in src_subdirs:
  75. if not os.path.isabs(src_dir):
  76. dirs = [os.path.join(source_root, src_dir),
  77. os.path.join(build_root, src_dir)]
  78. else:
  79. dirs = [src_dir]
  80. src_dir_args += ['--source-dir=' + d for d in dirs]
  81. doc_src = os.path.join(source_root, doc_subdir)
  82. abs_out = os.path.join(build_root, doc_subdir)
  83. htmldir = os.path.join(abs_out, 'html')
  84. content_files += [main_file]
  85. sections = os.path.join(doc_src, module + "-sections.txt")
  86. if os.path.exists(sections):
  87. content_files.append(sections)
  88. overrides = os.path.join(doc_src, module + "-overrides.txt")
  89. if os.path.exists(overrides):
  90. content_files.append(overrides)
  91. # Copy files to build directory
  92. for f in content_files:
  93. # FIXME: Use mesonlib.File objects so we don't need to do this
  94. if not os.path.isabs(f):
  95. f = os.path.join(doc_src, f)
  96. elif os.path.commonpath([f, build_root]) == build_root:
  97. continue
  98. shutil.copyfile(f, os.path.join(abs_out, os.path.basename(f)))
  99. shutil.rmtree(htmldir, ignore_errors=True)
  100. try:
  101. os.mkdir(htmldir)
  102. except Exception:
  103. pass
  104. for f in html_assets:
  105. f_abs = os.path.join(doc_src, f)
  106. shutil.copyfile(f_abs, os.path.join(htmldir, os.path.basename(f_abs)))
  107. scan_cmd = ['gtkdoc-scan', '--module=' + module] + src_dir_args
  108. if ignore_headers:
  109. scan_cmd.append('--ignore-headers=' + ' '.join(ignore_headers))
  110. # Add user-specified arguments
  111. scan_cmd += scan_args
  112. gtkdoc_run_check(scan_cmd, abs_out)
  113. # Use the generated types file when available, otherwise gobject_typesfile
  114. # would often be a path to source dir instead of build dir.
  115. if '--rebuild-types' in scan_args:
  116. gobject_typesfile = os.path.join(abs_out, module + '.types')
  117. if gobject_typesfile:
  118. scanobjs_cmd = ['gtkdoc-scangobj'] + scanobjs_args + ['--types=' + gobject_typesfile,
  119. '--module=' + module,
  120. '--run=' + run,
  121. '--cflags=' + cflags,
  122. '--ldflags=' + ldflags,
  123. '--cc=' + cc,
  124. '--ld=' + ld,
  125. '--output-dir=' + abs_out]
  126. library_paths = []
  127. for ldflag in shlex.split(ldflags):
  128. if ldflag.startswith('-Wl,-rpath,'):
  129. library_paths.append(ldflag[11:])
  130. gtkdoc_run_check(scanobjs_cmd, build_root, library_paths)
  131. # Make docbook files
  132. if mode == 'auto':
  133. # Guessing is probably a poor idea but these keeps compat
  134. # with previous behavior
  135. if main_file.endswith('sgml'):
  136. modeflag = '--sgml-mode'
  137. else:
  138. modeflag = '--xml-mode'
  139. elif mode == 'xml':
  140. modeflag = '--xml-mode'
  141. elif mode == 'sgml':
  142. modeflag = '--sgml-mode'
  143. else: # none
  144. modeflag = None
  145. mkdb_cmd = ['gtkdoc-mkdb',
  146. '--module=' + module,
  147. '--output-format=xml',
  148. '--expand-content-files=' + ' '.join(expand_content_files),
  149. ] + src_dir_args
  150. if namespace:
  151. mkdb_cmd.append('--name-space=' + namespace)
  152. if modeflag:
  153. mkdb_cmd.append(modeflag)
  154. if len(main_file) > 0:
  155. # Yes, this is the flag even if the file is in xml.
  156. mkdb_cmd.append('--main-sgml-file=' + main_file)
  157. # Add user-specified arguments
  158. mkdb_cmd += mkdb_args
  159. gtkdoc_run_check(mkdb_cmd, abs_out)
  160. # Make HTML documentation
  161. mkhtml_cmd = ['gtkdoc-mkhtml',
  162. '--path=' + ':'.join((doc_src, abs_out)),
  163. module,
  164. ] + html_args
  165. if len(main_file) > 0:
  166. mkhtml_cmd.append('../' + main_file)
  167. else:
  168. mkhtml_cmd.append('%s-docs.xml' % module)
  169. # html gen must be run in the HTML dir
  170. gtkdoc_run_check(mkhtml_cmd, htmldir)
  171. # Fix cross-references in HTML files
  172. fixref_cmd = ['gtkdoc-fixxref',
  173. '--module=' + module,
  174. '--module-dir=html'] + fixxref_args
  175. gtkdoc_run_check(fixref_cmd, abs_out)
  176. if module_version:
  177. shutil.move(os.path.join(htmldir, '{}.devhelp2'.format(module)),
  178. os.path.join(htmldir, '{}-{}.devhelp2'.format(module, module_version)))
  179. def install_gtkdoc(build_root, doc_subdir, install_prefix, datadir, module):
  180. source = os.path.join(build_root, doc_subdir, 'html')
  181. final_destination = os.path.join(install_prefix, datadir, module)
  182. shutil.rmtree(final_destination, ignore_errors=True)
  183. shutil.copytree(source, final_destination)
  184. def run(args):
  185. options = parser.parse_args(args)
  186. if len(options.htmlargs) > 0:
  187. htmlargs = options.htmlargs.split('@@')
  188. else:
  189. htmlargs = []
  190. if len(options.scanargs) > 0:
  191. scanargs = options.scanargs.split('@@')
  192. else:
  193. scanargs = []
  194. if len(options.scanobjsargs) > 0:
  195. scanobjsargs = options.scanobjsargs.split('@@')
  196. else:
  197. scanobjsargs = []
  198. if len(options.fixxrefargs) > 0:
  199. fixxrefargs = options.fixxrefargs.split('@@')
  200. else:
  201. fixxrefargs = []
  202. if len(options.mkdbargs) > 0:
  203. mkdbargs = options.mkdbargs.split('@@')
  204. else:
  205. mkdbargs = []
  206. build_gtkdoc(
  207. options.sourcedir,
  208. options.builddir,
  209. options.subdir,
  210. options.headerdirs.split('@@'),
  211. options.mainfile,
  212. options.modulename,
  213. options.moduleversion,
  214. htmlargs,
  215. scanargs,
  216. fixxrefargs,
  217. mkdbargs,
  218. options.gobject_typesfile,
  219. scanobjsargs,
  220. options.run,
  221. options.ld,
  222. options.cc,
  223. options.ldflags,
  224. options.cflags,
  225. options.html_assets.split('@@') if options.html_assets else [],
  226. options.content_files.split('@@') if options.content_files else [],
  227. options.ignore_headers.split('@@') if options.ignore_headers else [],
  228. options.namespace,
  229. options.expand_content_files.split('@@') if options.expand_content_files else [],
  230. options.mode)
  231. if 'MESON_INSTALL_PREFIX' in os.environ:
  232. destdir = os.environ.get('DESTDIR', '')
  233. install_prefix = destdir_join(destdir, os.environ['MESON_INSTALL_PREFIX'])
  234. if options.install_dir:
  235. install_dir = options.install_dir
  236. else:
  237. install_dir = options.modulename
  238. if options.moduleversion:
  239. install_dir += '-' + options.moduleversion
  240. if os.path.isabs(install_dir):
  241. install_dir = destdir_join(destdir, install_dir)
  242. install_gtkdoc(options.builddir,
  243. options.subdir,
  244. install_prefix,
  245. 'share/gtk-doc/html',
  246. install_dir)
  247. return 0
  248. if __name__ == '__main__':
  249. sys.exit(run(sys.argv[1:]))