apenwarr-redo/redo/cmd_ifchange.py
Avery Pennarun 19049d52fc jobserver: allow overriding the parent jobserver in a subprocess.
Previously, if you passed a -j option to a redo process in a redo or
make process hierarchy with MAKEFLAGS already set, it would ignore the
-j option and continue using the jobserver provided by the parent.

With this change, we instead initialize a new jobserver with the
desired number of tokens, which is what GNU make does in the same
situation.  A typical use case for this is to force serialization of
build steps in a subtree (by using -j1).  In make, this is often useful
for "fixing" makefiles that haven't been written correctly for parallel
builds.  In redo, that happens much less often, but it's useful at
least in unit tests.

Passing -j1 is relatively harmless (the redo you are starting inherits
a token anyway, so it doesn't create any new tokens).  Passing -j > 1
is more risky, because it creates new tokens, thus increasing the level
of parallelism in the system.  Because this may not be what you wanted,
we print a warning when you pass -j > 1 to a sub-redo.  GNU make gives
a similar warning in this situation.
2018-12-31 19:24:27 -05:00

68 lines
2.2 KiB
Python

"""redo-ifchange: build the given targets if they have changed."""
import os, sys, traceback
from . import env, builder, deps, jobserver, logs, state
from .logs import debug2, err
def should_build(t):
f = state.File(name=t)
if f.is_failed():
raise builder.ImmediateReturn(32)
dirty = deps.isdirty(f, depth='', max_changed=env.v.RUNID,
already_checked=[])
return f.is_generated, dirty == [f] and deps.DIRTY or dirty
def main():
rv = 202
try:
targets = sys.argv[1:]
state.init(targets)
if env.is_toplevel and not targets:
targets = ['all']
if env.is_toplevel and env.v.LOG:
builder.close_stdin()
builder.start_stdin_log_reader(
status=True, details=True,
pretty=True, color=True, debug_locks=False, debug_pids=False)
else:
logs.setup(tty=sys.stderr, pretty=env.v.PRETTY, color=env.v.COLOR)
if env.v.TARGET and not env.v.UNLOCKED:
me = os.path.join(env.v.STARTDIR,
os.path.join(env.v.PWD, env.v.TARGET))
f = state.File(name=me)
debug2('TARGET: %r %r %r\n'
% (env.v.STARTDIR, env.v.PWD, env.v.TARGET))
else:
f = me = None
debug2('redo-ifchange: not adding depends.\n')
jobserver.setup(0)
try:
if f:
for t in targets:
f.add_dep('m', t)
f.save()
state.commit()
rv = builder.run(targets, should_build)
finally:
try:
state.rollback()
finally:
try:
jobserver.force_return_tokens()
except Exception, e: # pylint: disable=broad-except
traceback.print_exc(100, sys.stderr)
err('unexpected error: %r\n' % e)
rv = 1
except KeyboardInterrupt:
if env.is_toplevel:
builder.await_log_reader()
sys.exit(200)
state.commit()
if env.is_toplevel:
builder.await_log_reader()
sys.exit(rv)
if __name__ == '__main__':
main()