Back to home page

OSCL-LXR

 
 

    


0001 #!/usr/bin/env python3
0002 # SPDX-License-Identifier: GPL-2.0+
0003 #
0004 # This determines how many parallel tasks "make" is expecting, as it is
0005 # not exposed via an special variables, reserves them all, runs a subprocess
0006 # with PARALLELISM environment variable set, and releases the jobs back again.
0007 #
0008 # https://www.gnu.org/software/make/manual/html_node/POSIX-Jobserver.html#POSIX-Jobserver
0009 from __future__ import print_function
0010 import os, sys, errno
0011 import subprocess
0012 
0013 # Extract and prepare jobserver file descriptors from environment.
0014 claim = 0
0015 jobs = b""
0016 try:
0017         # Fetch the make environment options.
0018         flags = os.environ['MAKEFLAGS']
0019 
0020         # Look for "--jobserver=R,W"
0021         # Note that GNU Make has used --jobserver-fds and --jobserver-auth
0022         # so this handles all of them.
0023         opts = [x for x in flags.split(" ") if x.startswith("--jobserver")]
0024 
0025         # Parse out R,W file descriptor numbers and set them nonblocking.
0026         fds = opts[0].split("=", 1)[1]
0027         reader, writer = [int(x) for x in fds.split(",", 1)]
0028         # Open a private copy of reader to avoid setting nonblocking
0029         # on an unexpecting process with the same reader fd.
0030         reader = os.open("/proc/self/fd/%d" % (reader),
0031                          os.O_RDONLY | os.O_NONBLOCK)
0032 
0033         # Read out as many jobserver slots as possible.
0034         while True:
0035                 try:
0036                         slot = os.read(reader, 8)
0037                         jobs += slot
0038                 except (OSError, IOError) as e:
0039                         if e.errno == errno.EWOULDBLOCK:
0040                                 # Stop at the end of the jobserver queue.
0041                                 break
0042                         # If something went wrong, give back the jobs.
0043                         if len(jobs):
0044                                 os.write(writer, jobs)
0045                         raise e
0046         # Add a bump for our caller's reserveration, since we're just going
0047         # to sit here blocked on our child.
0048         claim = len(jobs) + 1
0049 except (KeyError, IndexError, ValueError, OSError, IOError) as e:
0050         # Any missing environment strings or bad fds should result in just
0051         # not being parallel.
0052         pass
0053 
0054 # We can only claim parallelism if there was a jobserver (i.e. a top-level
0055 # "-jN" argument) and there were no other failures. Otherwise leave out the
0056 # environment variable and let the child figure out what is best.
0057 if claim > 0:
0058         os.environ['PARALLELISM'] = '%d' % (claim)
0059 
0060 rc = subprocess.call(sys.argv[1:])
0061 
0062 # Return all the reserved slots.
0063 if len(jobs):
0064         os.write(writer, jobs)
0065 
0066 sys.exit(rc)