jobserver-exec 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env python3
  2. # SPDX-License-Identifier: GPL-2.0+
  3. #
  4. # This determines how many parallel tasks "make" is expecting, as it is
  5. # not exposed via an special variables, reserves them all, runs a subprocess
  6. # with PARALLELISM environment variable set, and releases the jobs back again.
  7. #
  8. # https://www.gnu.org/software/make/manual/html_node/POSIX-Jobserver.html#POSIX-Jobserver
  9. from __future__ import print_function
  10. import os, sys, errno
  11. import subprocess
  12. # Extract and prepare jobserver file descriptors from environment.
  13. claim = 0
  14. jobs = b""
  15. try:
  16. # Fetch the make environment options.
  17. flags = os.environ['MAKEFLAGS']
  18. # Look for "--jobserver=R,W"
  19. # Note that GNU Make has used --jobserver-fds and --jobserver-auth
  20. # so this handles all of them.
  21. opts = [x for x in flags.split(" ") if x.startswith("--jobserver")]
  22. # Parse out R,W file descriptor numbers and set them nonblocking.
  23. # If the MAKEFLAGS variable contains multiple instances of the
  24. # --jobserver-auth= option, the last one is relevant.
  25. fds = opts[-1].split("=", 1)[1]
  26. # Starting with GNU Make 4.4, named pipes are used for reader and writer.
  27. # Example argument: --jobserver-auth=fifo:/tmp/GMfifo8134
  28. _, _, path = fds.partition('fifo:')
  29. if path:
  30. reader = os.open(path, os.O_RDONLY | os.O_NONBLOCK)
  31. writer = os.open(path, os.O_WRONLY)
  32. else:
  33. reader, writer = [int(x) for x in fds.split(",", 1)]
  34. # Open a private copy of reader to avoid setting nonblocking
  35. # on an unexpecting process with the same reader fd.
  36. reader = os.open("/proc/self/fd/%d" % (reader),
  37. os.O_RDONLY | os.O_NONBLOCK)
  38. # Read out as many jobserver slots as possible.
  39. while True:
  40. try:
  41. slot = os.read(reader, 8)
  42. jobs += slot
  43. except (OSError, IOError) as e:
  44. if e.errno == errno.EWOULDBLOCK:
  45. # Stop at the end of the jobserver queue.
  46. break
  47. # If something went wrong, give back the jobs.
  48. if len(jobs):
  49. os.write(writer, jobs)
  50. raise e
  51. # Add a bump for our caller's reserveration, since we're just going
  52. # to sit here blocked on our child.
  53. claim = len(jobs) + 1
  54. except (KeyError, IndexError, ValueError, OSError, IOError) as e:
  55. # Any missing environment strings or bad fds should result in just
  56. # not being parallel.
  57. pass
  58. # We can only claim parallelism if there was a jobserver (i.e. a top-level
  59. # "-jN" argument) and there were no other failures. Otherwise leave out the
  60. # environment variable and let the child figure out what is best.
  61. if claim > 0:
  62. os.environ['PARALLELISM'] = '%d' % (claim)
  63. rc = subprocess.call(sys.argv[1:])
  64. # Return all the reserved slots.
  65. if len(jobs):
  66. os.write(writer, jobs)
  67. sys.exit(rc)