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