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 16from pathlib import Path 17 18__version__ = '1.0' 19 20def get_infiles(env): 21 for x in env.found_docs: 22 yield str(env.doc2path(x)) 23 yield from ((os.path.join(env.srcdir, dep) 24 for dep in env.dependencies[x])) 25 for mod in sys.modules.values(): 26 if hasattr(mod, '__file__'): 27 if mod.__file__: 28 yield mod.__file__ 29 # this is perhaps going to include unused files: 30 for static_path in env.config.html_static_path + env.config.templates_path: 31 for path in Path(static_path).rglob('*'): 32 yield str(path) 33 34 # also include kdoc script 35 yield str(env.config.kerneldoc_bin[1]) 36 37 38def write_depfile(app, exception): 39 if exception: 40 return 41 42 env = app.env 43 if not env.config.depfile: 44 return 45 46 # Using a directory as the output file does not work great because 47 # its timestamp does not necessarily change when the contents change. 48 # So create a timestamp file. 49 if env.config.depfile_stamp: 50 with open(env.config.depfile_stamp, 'w') as f: 51 pass 52 53 with open(env.config.depfile, 'w') as f: 54 print((env.config.depfile_stamp or app.outdir) + ": \\", file=f) 55 print(*get_infiles(env), file=f) 56 for x in get_infiles(env): 57 print(x + ":", file=f) 58 59 60def setup(app): 61 app.add_config_value('depfile', None, 'env') 62 app.add_config_value('depfile_stamp', None, 'env') 63 app.connect('build-finished', write_depfile) 64 65 return dict( 66 version = __version__, 67 parallel_read_safe = True, 68 parallel_write_safe = True 69 ) 70