• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2
3import argparse
4import datetime
5import errno
6import os
7import subprocess
8import sys
9
10# Custom configuration for each documentation format
11doxygen_templates = {
12    'xml': [
13        'GENERATE_XML=YES\n',
14        'XML_OUTPUT={format}/{section}\n',
15        'INPUT= {files}\n',
16    ],
17    'html': [
18        'GENERATE_HTML=YES\n',
19        'HTML_OUTPUT={format}/{section}\n',
20        'PROJECT_NAME=\"Wayland {section} API\"\n',
21        'INPUT= {files}\n',
22    ],
23    'man': [
24        'GENERATE_MAN=YES\n',
25        'MAN_OUTPUT={format}\n',
26        'MAN_SUBDIR=.\n',
27        'JAVADOC_AUTOBRIEF=NO\n',
28        'INPUT= {files}\n',
29    ],
30}
31
32def load_doxygen_file(doxyfile):
33    with open(doxyfile, 'r') as f:
34        res = f.readlines()
35    return res
36
37def get_template(outformat):
38    for (k,v) in doxygen_templates.items():
39        if outformat.startswith(k):
40            return v
41
42def gen_doxygen_file(data, outformat, section, files):
43    for l in get_template(outformat):
44        data.append(l.format(format=outformat, section=section, files=' '.join(files)))
45    return data
46
47parser = argparse.ArgumentParser(description='Generate docs with Doxygen')
48parser.add_argument('doxygen_file',
49                    help='The doxygen file to use')
50parser.add_argument('files',
51                    help='The list of files to parse',
52                    metavar='FILES',
53                    nargs='+')
54parser.add_argument('--builddir',
55                    help='The build directory',
56                    metavar='DIR',
57                    default='.')
58parser.add_argument('--section',
59                    help='The section to build',
60                    metavar='NAME',
61                    default='Client')
62parser.add_argument('--output-format',
63                    help='The output format: xml, html, man',
64                    metavar='FORMAT',
65                    default='xml')
66parser.add_argument('--stamp',
67                    help='Stamp file to output',
68                    metavar='STAMP_FILE',
69                    nargs='?',
70                    type=argparse.FileType('w'))
71
72args = parser.parse_args()
73
74# Merge the doxyfile with our custom templates
75conf = load_doxygen_file(args.doxygen_file)
76conf = gen_doxygen_file(conf, args.output_format, args.section, args.files)
77
78# Doxygen is not clever enough to create the directories it
79# needs beforehand
80try:
81    os.makedirs(os.path.join(args.builddir, args.output_format))
82except OSError as e:
83    if e.errno != errno.EEXIST:
84        raise e
85
86# Run Doxygen with the generated doxyfile
87cmd = subprocess.Popen(['doxygen', '-'], stdin=subprocess.PIPE)
88cmd.stdin.write(''.join(conf).encode('utf-8'))
89cmd.stdin.close()
90if cmd.wait() != 0:
91    sys.exit(1)
92
93# This is a bit of a hack; Doxygen will generate way more files than we
94# want to install, but there's no way to know how many at configuration
95# time. Since we want to install only the wl_* man pages anyway, we can
96# delete the other files and let Meson install the whole man3 subdirectory
97if args.output_format.startswith('man'):
98    manpath = os.path.join(args.builddir, args.output_format)
99    for filename in os.listdir(manpath):
100        full_path = os.path.join(manpath, filename)
101        if not filename.startswith('wl_'):
102            os.remove(full_path)
103
104if args.stamp:
105   args.stamp.write(str(datetime.datetime.now()))
106