18c2ecf20Sopenharmony_ci#!/usr/bin/env python
28c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0+
38c2ecf20Sopenharmony_ci#
48c2ecf20Sopenharmony_ci# This determines how many parallel tasks "make" is expecting, as it is
58c2ecf20Sopenharmony_ci# not exposed via an special variables, reserves them all, runs a subprocess
68c2ecf20Sopenharmony_ci# with PARALLELISM environment variable set, and releases the jobs back again.
78c2ecf20Sopenharmony_ci#
88c2ecf20Sopenharmony_ci# https://www.gnu.org/software/make/manual/html_node/POSIX-Jobserver.html#POSIX-Jobserver
98c2ecf20Sopenharmony_cifrom __future__ import print_function
108c2ecf20Sopenharmony_ciimport os, sys, errno
118c2ecf20Sopenharmony_ciimport subprocess
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci# Extract and prepare jobserver file descriptors from envirnoment.
148c2ecf20Sopenharmony_ciclaim = 0
158c2ecf20Sopenharmony_cijobs = b""
168c2ecf20Sopenharmony_citry:
178c2ecf20Sopenharmony_ci	# Fetch the make environment options.
188c2ecf20Sopenharmony_ci	flags = os.environ['MAKEFLAGS']
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci	# Look for "--jobserver=R,W"
218c2ecf20Sopenharmony_ci	# Note that GNU Make has used --jobserver-fds and --jobserver-auth
228c2ecf20Sopenharmony_ci	# so this handles all of them.
238c2ecf20Sopenharmony_ci	opts = [x for x in flags.split(" ") if x.startswith("--jobserver")]
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	# Parse out R,W file descriptor numbers and set them nonblocking.
268c2ecf20Sopenharmony_ci	fds = opts[0].split("=", 1)[1]
278c2ecf20Sopenharmony_ci	reader, writer = [int(x) for x in fds.split(",", 1)]
288c2ecf20Sopenharmony_ci	# Open a private copy of reader to avoid setting nonblocking
298c2ecf20Sopenharmony_ci	# on an unexpecting process with the same reader fd.
308c2ecf20Sopenharmony_ci	reader = os.open("/proc/self/fd/%d" % (reader),
318c2ecf20Sopenharmony_ci			 os.O_RDONLY | os.O_NONBLOCK)
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	# Read out as many jobserver slots as possible.
348c2ecf20Sopenharmony_ci	while True:
358c2ecf20Sopenharmony_ci		try:
368c2ecf20Sopenharmony_ci			slot = os.read(reader, 8)
378c2ecf20Sopenharmony_ci			jobs += slot
388c2ecf20Sopenharmony_ci		except (OSError, IOError) as e:
398c2ecf20Sopenharmony_ci			if e.errno == errno.EWOULDBLOCK:
408c2ecf20Sopenharmony_ci				# Stop at the end of the jobserver queue.
418c2ecf20Sopenharmony_ci				break
428c2ecf20Sopenharmony_ci			# If something went wrong, give back the jobs.
438c2ecf20Sopenharmony_ci			if len(jobs):
448c2ecf20Sopenharmony_ci				os.write(writer, jobs)
458c2ecf20Sopenharmony_ci			raise e
468c2ecf20Sopenharmony_ci	# Add a bump for our caller's reserveration, since we're just going
478c2ecf20Sopenharmony_ci	# to sit here blocked on our child.
488c2ecf20Sopenharmony_ci	claim = len(jobs) + 1
498c2ecf20Sopenharmony_ciexcept (KeyError, IndexError, ValueError, OSError, IOError) as e:
508c2ecf20Sopenharmony_ci	# Any missing environment strings or bad fds should result in just
518c2ecf20Sopenharmony_ci	# not being parallel.
528c2ecf20Sopenharmony_ci	pass
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci# We can only claim parallelism if there was a jobserver (i.e. a top-level
558c2ecf20Sopenharmony_ci# "-jN" argument) and there were no other failures. Otherwise leave out the
568c2ecf20Sopenharmony_ci# environment variable and let the child figure out what is best.
578c2ecf20Sopenharmony_ciif claim > 0:
588c2ecf20Sopenharmony_ci	os.environ['PARALLELISM'] = '%d' % (claim)
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_circ = subprocess.call(sys.argv[1:])
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci# Return all the reserved slots.
638c2ecf20Sopenharmony_ciif len(jobs):
648c2ecf20Sopenharmony_ci	os.write(writer, jobs)
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cisys.exit(rc)
67