gtkdochelper.py 9.3 KB

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