depfile.py 1.8 KB

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