xref: /openbmc/qemu/docs/sphinx/depfile.py (revision cd0a9e983c984e3a6a08397dc2ca4500886ec48b)
1# coding=utf-8
2#
3# QEMU depfile generation extension
4#
5# Copyright (c) 2020 Red Hat, Inc.
6#
7# This work is licensed under the terms of the GNU GPLv2 or later.
8# See the COPYING file in the top-level directory.
9
10"""depfile is a Sphinx extension that writes a dependency file for
11   an external build system"""
12
13import os
14import sphinx
15import sys
16
17__version__ = '1.0'
18
19def get_infiles(env):
20    for x in env.found_docs:
21        yield env.doc2path(x)
22        yield from ((os.path.join(env.srcdir, dep)
23                    for dep in env.dependencies[x]))
24    for mod in sys.modules.values():
25        if hasattr(mod, '__file__'):
26            if mod.__file__:
27                yield mod.__file__
28
29
30def write_depfile(app, exception):
31    if exception:
32        return
33
34    env = app.env
35    if not env.config.depfile:
36        return
37
38    # Using a directory as the output file does not work great because
39    # its timestamp does not necessarily change when the contents change.
40    # So create a timestamp file.
41    if env.config.depfile_stamp:
42        with open(env.config.depfile_stamp, 'w') as f:
43            pass
44
45    with open(env.config.depfile, 'w') as f:
46        print((env.config.depfile_stamp or app.outdir) + ": \\", file=f)
47        print(*get_infiles(env), file=f)
48        for x in get_infiles(env):
49            print(x + ":", file=f)
50
51
52def setup(app):
53    app.add_config_value('depfile', None, 'env')
54    app.add_config_value('depfile_stamp', None, 'env')
55    app.connect('build-finished', write_depfile)
56
57    return dict(
58        version = __version__,
59        parallel_read_safe = True,
60        parallel_write_safe = True
61    )
62