Masahiro Yamada | d8d2d38 | 2021-02-01 10:00:24 +0900 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Kees Cook | 51e46c7 | 2019-11-21 12:59:29 -0800 | [diff] [blame] | 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 | |
Jonathan Neuschäfer | 98a499a | 2021-05-13 18:24:02 +0200 | [diff] [blame] | 13 | # Extract and prepare jobserver file descriptors from environment. |
Kees Cook | 51e46c7 | 2019-11-21 12:59:29 -0800 | [diff] [blame] | 14 | claim = 0 |
| 15 | jobs = b"" |
| 16 | try: |
| 17 | # Fetch the make environment options. |
| 18 | flags = os.environ['MAKEFLAGS'] |
| 19 | |
| 20 | # Look for "--jobserver=R,W" |
| 21 | # Note that GNU Make has used --jobserver-fds and --jobserver-auth |
| 22 | # so this handles all of them. |
| 23 | opts = [x for x in flags.split(" ") if x.startswith("--jobserver")] |
| 24 | |
| 25 | # Parse out R,W file descriptor numbers and set them nonblocking. |
Masahiro Yamada | f8f4dc7 | 2022-11-15 03:10:55 +0900 | [diff] [blame] | 26 | # If the MAKEFLAGS variable contains multiple instances of the |
| 27 | # --jobserver-auth= option, the last one is relevant. |
| 28 | fds = opts[-1].split("=", 1)[1] |
Martin Liska | 8c00898 | 2023-01-16 11:45:33 +0100 | [diff] [blame] | 29 | |
| 30 | # Starting with GNU Make 4.4, named pipes are used for reader and writer. |
| 31 | # Example argument: --jobserver-auth=fifo:/tmp/GMfifo8134 |
| 32 | _, _, path = fds.partition('fifo:') |
| 33 | |
| 34 | if path: |
| 35 | reader = os.open(path, os.O_RDONLY | os.O_NONBLOCK) |
| 36 | writer = os.open(path, os.O_WRONLY) |
| 37 | else: |
| 38 | reader, writer = [int(x) for x in fds.split(",", 1)] |
| 39 | # Open a private copy of reader to avoid setting nonblocking |
| 40 | # on an unexpecting process with the same reader fd. |
| 41 | reader = os.open("/proc/self/fd/%d" % (reader), |
| 42 | os.O_RDONLY | os.O_NONBLOCK) |
Kees Cook | 51e46c7 | 2019-11-21 12:59:29 -0800 | [diff] [blame] | 43 | |
| 44 | # Read out as many jobserver slots as possible. |
| 45 | while True: |
| 46 | try: |
| 47 | slot = os.read(reader, 8) |
| 48 | jobs += slot |
| 49 | except (OSError, IOError) as e: |
| 50 | if e.errno == errno.EWOULDBLOCK: |
| 51 | # Stop at the end of the jobserver queue. |
| 52 | break |
| 53 | # If something went wrong, give back the jobs. |
| 54 | if len(jobs): |
| 55 | os.write(writer, jobs) |
| 56 | raise e |
| 57 | # Add a bump for our caller's reserveration, since we're just going |
| 58 | # to sit here blocked on our child. |
| 59 | claim = len(jobs) + 1 |
| 60 | except (KeyError, IndexError, ValueError, OSError, IOError) as e: |
| 61 | # Any missing environment strings or bad fds should result in just |
| 62 | # not being parallel. |
| 63 | pass |
| 64 | |
| 65 | # We can only claim parallelism if there was a jobserver (i.e. a top-level |
| 66 | # "-jN" argument) and there were no other failures. Otherwise leave out the |
| 67 | # environment variable and let the child figure out what is best. |
| 68 | if claim > 0: |
| 69 | os.environ['PARALLELISM'] = '%d' % (claim) |
| 70 | |
| 71 | rc = subprocess.call(sys.argv[1:]) |
| 72 | |
| 73 | # Return all the reserved slots. |
| 74 | if len(jobs): |
| 75 | os.write(writer, jobs) |
| 76 | |
| 77 | sys.exit(rc) |