xref: /qemu/docs/sphinx/depfile.py (revision e3a6e0da)
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
15
16__version__ = '1.0'
17
18def get_infiles(env):
19    for x in env.found_docs:
20        yield env.doc2path(x)
21        yield from ((os.path.join(env.srcdir, dep)
22                    for dep in env.dependencies[x]))
23
24def write_depfile(app, env):
25    if not env.config.depfile:
26        return
27
28    # Using a directory as the output file does not work great because
29    # its timestamp does not necessarily change when the contents change.
30    # So create a timestamp file.
31    if env.config.depfile_stamp:
32        with open(env.config.depfile_stamp, 'w') as f:
33            pass
34
35    with open(env.config.depfile, 'w') as f:
36        print((env.config.depfile_stamp or app.outdir) + ": \\", file=f)
37        print(*get_infiles(env), file=f)
38        for x in get_infiles(env):
39            print(x + ":", file=f)
40
41
42def setup(app):
43    app.add_config_value('depfile', None, 'env')
44    app.add_config_value('depfile_stamp', None, 'env')
45    app.connect('env-updated', write_depfile)
46
47    return dict(
48        version = __version__,
49        parallel_read_safe = True,
50        parallel_write_safe = True
51    )
52