162306a36Sopenharmony_ci#!/usr/bin/env python3
262306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0+
362306a36Sopenharmony_ci#
462306a36Sopenharmony_ci# This determines how many parallel tasks "make" is expecting, as it is
562306a36Sopenharmony_ci# not exposed via an special variables, reserves them all, runs a subprocess
662306a36Sopenharmony_ci# with PARALLELISM environment variable set, and releases the jobs back again.
762306a36Sopenharmony_ci#
862306a36Sopenharmony_ci# https://www.gnu.org/software/make/manual/html_node/POSIX-Jobserver.html#POSIX-Jobserver
962306a36Sopenharmony_cifrom __future__ import print_function
1062306a36Sopenharmony_ciimport os, sys, errno
1162306a36Sopenharmony_ciimport subprocess
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci# Extract and prepare jobserver file descriptors from environment.
1462306a36Sopenharmony_ciclaim = 0
1562306a36Sopenharmony_cijobs = b""
1662306a36Sopenharmony_citry:
1762306a36Sopenharmony_ci	# Fetch the make environment options.
1862306a36Sopenharmony_ci	flags = os.environ['MAKEFLAGS']
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci	# Look for "--jobserver=R,W"
2162306a36Sopenharmony_ci	# Note that GNU Make has used --jobserver-fds and --jobserver-auth
2262306a36Sopenharmony_ci	# so this handles all of them.
2362306a36Sopenharmony_ci	opts = [x for x in flags.split(" ") if x.startswith("--jobserver")]
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci	# Parse out R,W file descriptor numbers and set them nonblocking.
2662306a36Sopenharmony_ci	# If the MAKEFLAGS variable contains multiple instances of the
2762306a36Sopenharmony_ci	# --jobserver-auth= option, the last one is relevant.
2862306a36Sopenharmony_ci	fds = opts[-1].split("=", 1)[1]
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci	# Starting with GNU Make 4.4, named pipes are used for reader and writer.
3162306a36Sopenharmony_ci	# Example argument: --jobserver-auth=fifo:/tmp/GMfifo8134
3262306a36Sopenharmony_ci	_, _, path = fds.partition('fifo:')
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci	if path:
3562306a36Sopenharmony_ci		reader = os.open(path, os.O_RDONLY | os.O_NONBLOCK)
3662306a36Sopenharmony_ci		writer = os.open(path, os.O_WRONLY)
3762306a36Sopenharmony_ci	else:
3862306a36Sopenharmony_ci		reader, writer = [int(x) for x in fds.split(",", 1)]
3962306a36Sopenharmony_ci		# Open a private copy of reader to avoid setting nonblocking
4062306a36Sopenharmony_ci		# on an unexpecting process with the same reader fd.
4162306a36Sopenharmony_ci		reader = os.open("/proc/self/fd/%d" % (reader),
4262306a36Sopenharmony_ci				 os.O_RDONLY | os.O_NONBLOCK)
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	# Read out as many jobserver slots as possible.
4562306a36Sopenharmony_ci	while True:
4662306a36Sopenharmony_ci		try:
4762306a36Sopenharmony_ci			slot = os.read(reader, 8)
4862306a36Sopenharmony_ci			jobs += slot
4962306a36Sopenharmony_ci		except (OSError, IOError) as e:
5062306a36Sopenharmony_ci			if e.errno == errno.EWOULDBLOCK:
5162306a36Sopenharmony_ci				# Stop at the end of the jobserver queue.
5262306a36Sopenharmony_ci				break
5362306a36Sopenharmony_ci			# If something went wrong, give back the jobs.
5462306a36Sopenharmony_ci			if len(jobs):
5562306a36Sopenharmony_ci				os.write(writer, jobs)
5662306a36Sopenharmony_ci			raise e
5762306a36Sopenharmony_ci	# Add a bump for our caller's reserveration, since we're just going
5862306a36Sopenharmony_ci	# to sit here blocked on our child.
5962306a36Sopenharmony_ci	claim = len(jobs) + 1
6062306a36Sopenharmony_ciexcept (KeyError, IndexError, ValueError, OSError, IOError) as e:
6162306a36Sopenharmony_ci	# Any missing environment strings or bad fds should result in just
6262306a36Sopenharmony_ci	# not being parallel.
6362306a36Sopenharmony_ci	pass
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci# We can only claim parallelism if there was a jobserver (i.e. a top-level
6662306a36Sopenharmony_ci# "-jN" argument) and there were no other failures. Otherwise leave out the
6762306a36Sopenharmony_ci# environment variable and let the child figure out what is best.
6862306a36Sopenharmony_ciif claim > 0:
6962306a36Sopenharmony_ci	os.environ['PARALLELISM'] = '%d' % (claim)
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_circ = subprocess.call(sys.argv[1:])
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci# Return all the reserved slots.
7462306a36Sopenharmony_ciif len(jobs):
7562306a36Sopenharmony_ci	os.write(writer, jobs)
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_cisys.exit(rc)
78