Debian Codegraph @

mesonbuild/scripts/gtkdochelper.py

raw
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2015-2016 The Meson development team
3
4 from __future__ import annotations
5
6 import sys, os
7 import subprocess
8 import shutil
9 import argparse
10 from ..mesonlib import (
11 MesonException, Popen_safe, is_windows, is_cygwin, is_parent_path,
12 split_args,
13 )
14 from . import destdir_join
15 import typing as T
16
17 parser = argparse.ArgumentParser()
18
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 for tool in ['scan', 'scangobj', 'mkdb', 'mkhtml', 'fixxref']:
45 program_name = 'gtkdoc-' + tool
46 parser.add_argument('--' + program_name, dest=program_name.replace('-', '_'))
47
48 def gtkdoc_run_check(cmd: T.List[str], cwd: str, library_paths: T.Optional[T.List[str]] = None) -> None:
49 if library_paths is None:
50 library_paths = []
51
52 env = dict(os.environ)
53 if is_windows() or is_cygwin():
54 if 'PATH' in env:
55 library_paths.extend(env['PATH'].split(os.pathsep))
56 env['PATH'] = os.pathsep.join(library_paths)
57 else:
58 if 'LD_LIBRARY_PATH' in env:
59 library_paths.extend(env['LD_LIBRARY_PATH'].split(os.pathsep))
60 env['LD_LIBRARY_PATH'] = os.pathsep.join(library_paths)
61
62 if is_windows():
63 cmd.insert(0, sys.executable)
64
65 # Put stderr into stdout since we want to print it out anyway.
66 # This preserves the order of messages.
67 p, out = Popen_safe(cmd, cwd=cwd, env=env, stderr=subprocess.STDOUT)[0:2]
68 if p.returncode != 0:
69 err_msg = [f"{cmd!r} failed with status {p.returncode:d}"]
70 if out:
71 err_msg.append(out)
72 raise MesonException('\n'.join(err_msg))
73 elif out:
74 # Unfortunately Windows cmd.exe consoles may be using a codepage
75 # that might choke print() with a UnicodeEncodeError, so let's
76 # ignore such errors for now, as a compromise as we are outputting
77 # console output here...
78 try:
79 print(out)
80 except UnicodeEncodeError:
81 pass
82
83 def build_gtkdoc(source_root: str, build_root: str, doc_subdir: str, src_subdirs: T.List[str],
84 main_file: str, module: str, module_version: str,
85 html_args: T.List[str], scan_args: T.List[str], fixxref_args: T.List[str], mkdb_args: T.List[str],
86 gobject_typesfile: str, scanobjs_args: T.List[str], run: str, ld: str, cc: str, ldflags: str, cflags: str,
87 html_assets: T.List[str], content_files: T.List[str], ignore_headers: T.List[str], namespace: str,
88 expand_content_files: T.List[str], mode: str, options: argparse.Namespace) -> None:
89 print("Building documentation for %s" % module)
90
91 src_dir_args = []
92 for src_dir in src_subdirs:
93 if not os.path.isabs(src_dir):
94 dirs = [os.path.join(source_root, src_dir),
95 os.path.join(build_root, src_dir)]
96 else:
97 dirs = [src_dir]
98 src_dir_args += ['--source-dir=' + d for d in dirs]
99
100 doc_src = os.path.join(source_root, doc_subdir)
101 abs_out = os.path.join(build_root, doc_subdir)
102 htmldir = os.path.join(abs_out, 'html')
103
104 content_files += [main_file]
105 sections = os.path.join(doc_src, module + "-sections.txt")
106 if os.path.exists(sections):
107 content_files.append(sections)
108
109 overrides = os.path.join(doc_src, module + "-overrides.txt")
110 if os.path.exists(overrides):
111 content_files.append(overrides)
112
113 # Copy files to build directory
114 for f in content_files:
115 # FIXME: Use mesonlib.File objects so we don't need to do this
116 if not os.path.isabs(f):
117 f = os.path.join(doc_src, f)
118 elif is_parent_path(build_root, f):
119 continue
120 shutil.copyfile(f, os.path.join(abs_out, os.path.basename(f)))
121
122 shutil.rmtree(htmldir, ignore_errors=True)
123 try:
124 os.mkdir(htmldir)
125 except Exception:
126 pass
127
128 for f in html_assets:
129 f_abs = os.path.join(doc_src, f)
130 shutil.copyfile(f_abs, os.path.join(htmldir, os.path.basename(f_abs)))
131
132 scan_cmd = [options.gtkdoc_scan, '--module=' + module] + src_dir_args
133 if ignore_headers:
134 scan_cmd.append('--ignore-headers=' + ' '.join(ignore_headers))
135 # Add user-specified arguments
136 scan_cmd += scan_args
137 gtkdoc_run_check(scan_cmd, abs_out)
138
139 # Use the generated types file when available, otherwise gobject_typesfile
140 # would often be a path to source dir instead of build dir.
141 if '--rebuild-types' in scan_args:
142 gobject_typesfile = os.path.join(abs_out, module + '.types')
143
144 if gobject_typesfile:
145 scanobjs_cmd = [options.gtkdoc_scangobj] + scanobjs_args
146 scanobjs_cmd += ['--types=' + gobject_typesfile,
147 '--module=' + module,
148 '--run=' + run,
149 '--cflags=' + cflags,
150 '--ldflags=' + ldflags,
151 '--cc=' + cc,
152 '--ld=' + ld,
153 '--output-dir=' + abs_out]
154
155 library_paths = []
156 for ldflag in split_args(ldflags):
157 if ldflag.startswith('-Wl,-rpath,'):
158 library_paths.append(ldflag[11:])
159
160 gtkdoc_run_check(scanobjs_cmd, build_root, library_paths)
161
162 # Make docbook files
163 if mode == 'auto':
164 # Guessing is probably a poor idea but these keeps compat
165 # with previous behavior
166 if main_file.endswith('sgml'):
167 modeflag = '--sgml-mode'
168 else:
169 modeflag = '--xml-mode'
170 elif mode == 'xml':
171 modeflag = '--xml-mode'
172 elif mode == 'sgml':
173 modeflag = '--sgml-mode'
174 else: # none
175 modeflag = None
176
177 mkdb_cmd = [options.gtkdoc_mkdb,
178 '--module=' + module,
179 '--output-format=xml',
180 '--expand-content-files=' + ' '.join(expand_content_files),
181 ] + src_dir_args
182 if namespace:
183 mkdb_cmd.append('--name-space=' + namespace)
184 if modeflag:
185 mkdb_cmd.append(modeflag)
186 if main_file:
187 # Yes, this is the flag even if the file is in xml.
188 mkdb_cmd.append('--main-sgml-file=' + main_file)
189 # Add user-specified arguments
190 mkdb_cmd += mkdb_args
191 gtkdoc_run_check(mkdb_cmd, abs_out)
192
193 # Make HTML documentation
194 mkhtml_cmd = [options.gtkdoc_mkhtml,
195 '--path=' + os.pathsep.join((doc_src, abs_out)),
196 module,
197 ] + html_args
198 if main_file:
199 mkhtml_cmd.append('../' + main_file)
200 else:
201 mkhtml_cmd.append('%s-docs.xml' % module)
202 # html gen must be run in the HTML dir
203 gtkdoc_run_check(mkhtml_cmd, htmldir)
204
205 # Fix cross-references in HTML files
206 fixref_cmd = [options.gtkdoc_fixxref,
207 '--module=' + module,
208 '--module-dir=html'] + fixxref_args
209 gtkdoc_run_check(fixref_cmd, abs_out)
210
211 if module_version:
212 shutil.move(os.path.join(htmldir, f'{module}.devhelp2'),
213 os.path.join(htmldir, f'{module}-{module_version}.devhelp2'))
214
215 def install_gtkdoc(build_root: str, doc_subdir: str, install_prefix: str, datadir: str, module: str) -> None:
216 source = os.path.join(build_root, doc_subdir, 'html')
217 final_destination = os.path.join(install_prefix, datadir, module)
218 shutil.rmtree(final_destination, ignore_errors=True)
219 shutil.copytree(source, final_destination)
220
221 def run(args: T.List[str]) -> int:
222 options = parser.parse_args(args)
223 if options.htmlargs:
224 htmlargs = options.htmlargs.split('@@')
225 else:
226 htmlargs = []
227 if options.scanargs:
228 scanargs = options.scanargs.split('@@')
229 else:
230 scanargs = []
231 if options.scanobjsargs:
232 scanobjsargs = options.scanobjsargs.split('@@')
233 else:
234 scanobjsargs = []
235 if options.fixxrefargs:
236 fixxrefargs = options.fixxrefargs.split('@@')
237 else:
238 fixxrefargs = []
239 if options.mkdbargs:
240 mkdbargs = options.mkdbargs.split('@@')
241 else:
242 mkdbargs = []
243 build_gtkdoc(
244 options.sourcedir,
245 options.builddir,
246 options.subdir,
247 options.headerdirs.split('@@'),
248 options.mainfile,
249 options.modulename,
250 options.moduleversion,
251 htmlargs,
252 scanargs,
253 fixxrefargs,
254 mkdbargs,
255 options.gobject_typesfile,
256 scanobjsargs,
257 options.run,
258 options.ld,
259 options.cc,
260 options.ldflags,
261 options.cflags,
262 options.html_assets.split('@@') if options.html_assets else [],
263 options.content_files.split('@@') if options.content_files else [],
264 options.ignore_headers.split('@@') if options.ignore_headers else [],
265 options.namespace,
266 options.expand_content_files.split('@@') if options.expand_content_files else [],
267 options.mode,
268 options)
269
270 if 'MESON_INSTALL_PREFIX' in os.environ:
271 destdir = os.environ.get('DESTDIR', '')
272 install_prefix = destdir_join(destdir, os.environ['MESON_INSTALL_PREFIX'])
273 if options.install_dir:
274 install_dir = options.install_dir
275 else:
276 install_dir = options.modulename
277 if options.moduleversion:
278 install_dir += '-' + options.moduleversion
279 if os.path.isabs(install_dir):
280 install_dir = destdir_join(destdir, install_dir)
281 install_gtkdoc(options.builddir,
282 options.subdir,
283 install_prefix,
284 'share/gtk-doc/html',
285 install_dir)
286 return 0
287
288 if __name__ == '__main__':
289 sys.exit(run(sys.argv[1:]))