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
9from __future__ import print_function
10import os, sys, errno
11import subprocess
12
13# Extract and prepare jobserver file descriptors from environment.
14claim = 0
15jobs = b""
16try:
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.
26	fds = opts[0].split("=", 1)[1]
27	reader, writer = [int(x) for x in fds.split(",", 1)]
28	# Open a private copy of reader to avoid setting nonblocking
29	# on an unexpecting process with the same reader fd.
30	reader = os.open("/proc/self/fd/%d" % (reader),
31			 os.O_RDONLY | os.O_NONBLOCK)
32
33	# Read out as many jobserver slots as possible.
34	while True:
35		try:
36			slot = os.read(reader, 8)
37			jobs += slot
38		except (OSError, IOError) as e:
39			if e.errno == errno.EWOULDBLOCK:
40				# Stop at the end of the jobserver queue.
41				break
42			# If something went wrong, give back the jobs.
43			if len(jobs):
44				os.write(writer, jobs)
45			raise e
46	# Add a bump for our caller's reserveration, since we're just going
47	# to sit here blocked on our child.
48	claim = len(jobs) + 1
49except (KeyError, IndexError, ValueError, OSError, IOError) as e:
50	# Any missing environment strings or bad fds should result in just
51	# not being parallel.
52	pass
53
54# We can only claim parallelism if there was a jobserver (i.e. a top-level
55# "-jN" argument) and there were no other failures. Otherwise leave out the
56# environment variable and let the child figure out what is best.
57if claim > 0:
58	os.environ['PARALLELISM'] = '%d' % (claim)
59
60rc = subprocess.call(sys.argv[1:])
61
62# Return all the reserved slots.
63if len(jobs):
64	os.write(writer, jobs)
65
66sys.exit(rc)
67