2
0

depfile.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 str(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. # also include kdoc script
  30. yield str(env.config.kerneldoc_bin[1])
  31. def write_depfile(app, exception):
  32. if exception:
  33. return
  34. env = app.env
  35. if not env.config.depfile:
  36. return
  37. # Using a directory as the output file does not work great because
  38. # its timestamp does not necessarily change when the contents change.
  39. # So create a timestamp file.
  40. if env.config.depfile_stamp:
  41. with open(env.config.depfile_stamp, 'w') as f:
  42. pass
  43. with open(env.config.depfile, 'w') as f:
  44. print((env.config.depfile_stamp or app.outdir) + ": \\", file=f)
  45. print(*get_infiles(env), file=f)
  46. for x in get_infiles(env):
  47. print(x + ":", file=f)
  48. def setup(app):
  49. app.add_config_value('depfile', None, 'env')
  50. app.add_config_value('depfile_stamp', None, 'env')
  51. app.connect('build-finished', write_depfile)
  52. return dict(
  53. version = __version__,
  54. parallel_read_safe = True,
  55. parallel_write_safe = True
  56. )