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