1#!/usr/bin/env python3
2#
3# Copyright © 2021 Intel Corporation
4#
5# Permission is hereby granted, free of charge, to any person obtaining a
6# copy of this software and associated documentation files (the
7# "Software"), to deal in the Software without restriction, including
8# without limitation the rights to use, copy, modify, merge, publish,
9# distribute, sub license, and/or sell copies of the Software, and to
10# permit persons to whom the Software is furnished to do so, subject to
11# the following conditions:
12#
13# The above copyright notice and this permission notice (including the
14# next paragraph) shall be included in all copies or substantial portions
15# of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20# IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25import argparse
26from mako.template import Template
27import os
28import subprocess
29import tempfile
30
31INPUT_PATHS = [
32    'src/compiler/nir/nir.h',
33    'src/intel/isl',
34    'src/vulkan/runtime',
35]
36
37TEMPLATE_DOXYFILE = Template("""
38# Doxyfile 1.9.1
39DOXYFILE_ENCODING = UTF-8
40PROJECT_NAME = "Mesa"
41
42INPUT = ${' '.join(input_files)}
43XML_OUTPUT = ${output_xml}
44
45# Only generate XML
46GENERATE_HTML = NO
47GENERATE_LATEX = NO
48GENERATE_XML = YES
49
50# Add aliases for easily writing reStructuredText in comments
51ALIASES  = "rst=\\verbatim embed:rst:leading-asterisk"
52ALIASES += "endrst=\endverbatim"
53
54ENABLE_PREPROCESSING = YES
55MACRO_EXPANSION = YES
56EXPAND_ONLY_PREDEF = YES
57
58# Defines required to keep doxygen from tripping on our attribute macros
59PREDEFINED  = PACKED=
60PREDEFINED += ATTRIBUTE_CONST=
61""")
62
63def run_doxygen(output_path, input_paths=[]):
64    doxyfile = tempfile.NamedTemporaryFile(mode='w', delete=False)
65    try:
66        doxyfile.write(TEMPLATE_DOXYFILE.render(
67            input_files=[ os.path.abspath(i) for i in input_paths ],
68            output_xml=os.path.abspath(output_path),
69        ))
70        doxyfile.close()
71
72        subprocess.run(['doxygen', doxyfile.name])
73
74    finally:
75        doxyfile.close()
76        os.unlink(doxyfile.name)
77
78if __name__ == '__main__':
79    parser = argparse.ArgumentParser()
80    parser.add_argument('--out-dir',
81                        help='Output XML directory.',
82                        required=True)
83    args = parser.parse_args()
84
85    this_dir = os.path.dirname(os.path.abspath(__file__))
86    mesa_dir = os.path.join(this_dir, '..')
87    def fixpath(p):
88        if os.path.isabs(p):
89            return p
90        return os.path.join(mesa_dir, p)
91
92    input_paths = [ fixpath(p) for p in INPUT_PATHS ]
93
94    run_doxygen(args.out_dir, input_paths)
95