xref: /OK3568_Linux_fs/kernel/scripts/jobserver-exec (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#!/usr/bin/env python
2*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0+
3*4882a593Smuzhiyun#
4*4882a593Smuzhiyun# This determines how many parallel tasks "make" is expecting, as it is
5*4882a593Smuzhiyun# not exposed via an special variables, reserves them all, runs a subprocess
6*4882a593Smuzhiyun# with PARALLELISM environment variable set, and releases the jobs back again.
7*4882a593Smuzhiyun#
8*4882a593Smuzhiyun# https://www.gnu.org/software/make/manual/html_node/POSIX-Jobserver.html#POSIX-Jobserver
9*4882a593Smuzhiyunfrom __future__ import print_function
10*4882a593Smuzhiyunimport os, sys, errno
11*4882a593Smuzhiyunimport subprocess
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun# Extract and prepare jobserver file descriptors from envirnoment.
14*4882a593Smuzhiyunclaim = 0
15*4882a593Smuzhiyunjobs = b""
16*4882a593Smuzhiyuntry:
17*4882a593Smuzhiyun	# Fetch the make environment options.
18*4882a593Smuzhiyun	flags = os.environ['MAKEFLAGS']
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun	# Look for "--jobserver=R,W"
21*4882a593Smuzhiyun	# Note that GNU Make has used --jobserver-fds and --jobserver-auth
22*4882a593Smuzhiyun	# so this handles all of them.
23*4882a593Smuzhiyun	opts = [x for x in flags.split(" ") if x.startswith("--jobserver")]
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun	# Parse out R,W file descriptor numbers and set them nonblocking.
26*4882a593Smuzhiyun	fds = opts[0].split("=", 1)[1]
27*4882a593Smuzhiyun	reader, writer = [int(x) for x in fds.split(",", 1)]
28*4882a593Smuzhiyun	# Open a private copy of reader to avoid setting nonblocking
29*4882a593Smuzhiyun	# on an unexpecting process with the same reader fd.
30*4882a593Smuzhiyun	reader = os.open("/proc/self/fd/%d" % (reader),
31*4882a593Smuzhiyun			 os.O_RDONLY | os.O_NONBLOCK)
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun	# Read out as many jobserver slots as possible.
34*4882a593Smuzhiyun	while True:
35*4882a593Smuzhiyun		try:
36*4882a593Smuzhiyun			slot = os.read(reader, 8)
37*4882a593Smuzhiyun			jobs += slot
38*4882a593Smuzhiyun		except (OSError, IOError) as e:
39*4882a593Smuzhiyun			if e.errno == errno.EWOULDBLOCK:
40*4882a593Smuzhiyun				# Stop at the end of the jobserver queue.
41*4882a593Smuzhiyun				break
42*4882a593Smuzhiyun			# If something went wrong, give back the jobs.
43*4882a593Smuzhiyun			if len(jobs):
44*4882a593Smuzhiyun				os.write(writer, jobs)
45*4882a593Smuzhiyun			raise e
46*4882a593Smuzhiyun	# Add a bump for our caller's reserveration, since we're just going
47*4882a593Smuzhiyun	# to sit here blocked on our child.
48*4882a593Smuzhiyun	claim = len(jobs) + 1
49*4882a593Smuzhiyunexcept (KeyError, IndexError, ValueError, OSError, IOError) as e:
50*4882a593Smuzhiyun	# Any missing environment strings or bad fds should result in just
51*4882a593Smuzhiyun	# not being parallel.
52*4882a593Smuzhiyun	pass
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun# We can only claim parallelism if there was a jobserver (i.e. a top-level
55*4882a593Smuzhiyun# "-jN" argument) and there were no other failures. Otherwise leave out the
56*4882a593Smuzhiyun# environment variable and let the child figure out what is best.
57*4882a593Smuzhiyunif claim > 0:
58*4882a593Smuzhiyun	os.environ['PARALLELISM'] = '%d' % (claim)
59*4882a593Smuzhiyun
60*4882a593Smuzhiyunrc = subprocess.call(sys.argv[1:])
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun# Return all the reserved slots.
63*4882a593Smuzhiyunif len(jobs):
64*4882a593Smuzhiyun	os.write(writer, jobs)
65*4882a593Smuzhiyun
66*4882a593Smuzhiyunsys.exit(rc)
67