Commit graph

56 commits

Author SHA1 Message Date
Avery Pennarun
c1a1f32445 MacOS: "-e filename/." returns true even for non-directories.
This has something to do with resource forks.  So use "-d filename/."
instead, which returns false if filename is not a directory.
2011-03-05 19:03:30 -08:00
Avery Pennarun
ea7057d9b6 redo-ifchange: remove special case for zero arguments.
Not sure why I put there, but special cases aren't worth the hassle.
2011-02-21 03:55:18 -08:00
Avery Pennarun
c077d77285 builder.py: correctly set $3 to include the subdir path.
If we're using a .do file from a parent directory, we should set $3 using
the same path prefix as $1.  We were previously using just the basename,
which mostly works (since we would rename it to $1$2 eventually anyway) but
is not quite right, and you can't safely rename files across filesystems, so
it could theoretically cause problems.

Also improved t/defaults-nested to test for this behaviour.

Reported by Eric Kow.
2011-01-18 00:48:52 -08:00
Avery Pennarun
4c06332ea1 builder.py: we weren't stamping .do files correctly if dodir!='.'.
The result was that t/deps/dirtest was actually failing in some cases.  But
it wasn't failing quite reliably enough, because the failing test was
dirtest/dir1/all, which has the same name as some other 'all' files,
confusing the issue.  Renamed dirtest/dir1/all.do to dirtest/dir1/go.do instead.

Reported by Prakhar Goel and Berke Durak.
2011-01-18 00:48:51 -08:00
Avery Pennarun
e98696caef Merge branch 'master' into search-parent-dirs
* master:
  Fixed markdown errors in README - code samples now correctly formatted.
  Fix use of config.sh in example
  log.py, minimal/do: don't use ansi colour codes if $TERM is blank or 'dumb'
  Use named constants for terminal control codes.
  redo-sh: keep testing even after finding a 'good' shell.
  redo-sh.do: hide warning output from 'which' in some shells.
  redo-sh.do: wrap long lines.
  Handle .do files that start with "#!/" to specify an explicit interpreter.
  minimal/do: don't print an error on exit if we don't build anything.
  bash completions: also mark 'do' as a completable command.
  bash completions: work correctly when $cur is an empty string.
  bash completions: call redo-targets for a more complete list.
  bash completions: work correctly with subdirs, ie. 'redo t/<tab>'
  Sample bash completion rules for redo targets.
  minimal/do: faster deletion of stamp files.
  minimal/do: delete .tmp files if a build fails.
  minimal/do: use ".did" stamp files instead of empty target files.
  minimal/do: use posix shell features instead of dirname/basename.
  Automatically select a good shell instead of relying on /bin/sh.

Conflicts:
	t/clean.do
2011-01-15 16:00:12 -08:00
Avery Pennarun
f641e52e3b Handle .do files that start with "#!/" to specify an explicit interpreter.
Now you can have your .do files interpreted by whatever interpreter you
want.
2011-01-01 22:10:14 -08:00
Avery Pennarun
fb388b3dde Automatically select a good shell instead of relying on /bin/sh.
This includes a fairly detailed test of various known shell bugs from the
autoconf docs.

The idea here is that if redo works on your system, you should be able to
rely on a *good* shell to run your .do files; you shouldn't have to work
around zillions of bugs like autoconf does.
2010-12-21 04:44:39 -08:00
Avery Pennarun
0dcc3f61b6 Search parent directories for default*.do.
Previously, we would only search for default*.do in the same directory in
the target; now we search parent directories as well.

Let's say we're in a/b/ and trying to build foo.o.  If we find
../../default.o.do, then we'll run

	cd ../..; sh default.o.do a/b/foo .o $TMPNAME

In other words, we still always chdir to the same directory as the .do file.
But now $1 might have a path in it, not just a basename.
2010-12-19 05:58:49 -08:00
Avery Pennarun
df85b3d163 Move dependency checking from redo-ifchange into deps.py.
In preparation for sharing between multiple commands.
2010-12-19 03:50:38 -08:00
Avery Pennarun
db4c4fc17a Rename redo-oob to redo-unlocked, to more accurately represent its use.
It's still undocumented.  Because you shouldn't run it by hand.  So don't!
It's dangerous!
2010-12-19 01:20:13 -08:00
Avery Pennarun
294945bd0f Assert that one instance never holds multiple locks on the same file at once.
This could happen if you did 'redo foo foo'.  Which nobody ever did, I
think, but let's make sure we catch it if they do.

One problem with having multiple locks on the same file is then you have to
remember not to *unlock* it until they're all done.  But there are other
problems, such as: why the heck did we think it was a good idea to lock the
same file more than once?  So just prevent it from happening for now,
unless/until we somehow come up with a reason it might be a good idea.
2010-12-14 02:19:08 -08:00
Avery Pennarun
c64b8a3eb1 Fix a race condition caused by zap_deps().
We can't just delete all the dependencies at the beginning and re-add them:
other people might be checking the same dependencies in parallel.  Instead,
mark them as delete_me up front, and then after the build completes, remove
only the delete_me entries.
2010-12-11 22:59:55 -08:00
Avery Pennarun
49f0a041b2 clean.do: cleanup *.tmp files that might have been left lying around.
...and fix a bug where builder.py can't handle it if its temp file is
deleted out from under it.
2010-12-11 21:10:57 -08:00
Avery Pennarun
e18fa85d58 The only thing in helpers.py that needed vars.py was the log stuff.
So put it in its own file.  Now it's safer to import and use helpers even if
you can't safely touch vars.
2010-12-11 18:34:02 -08:00
Avery Pennarun
91630a892a Whoops, redo-oob was slightly wrong when used with -j.
We called 'redo' instead of 'redo-ifchange' on our indeterminate objects.
Since other instances of redo-oob might be running at the same time, this
could cause the same object to get rebuilt more than once unnecessarily.
The unit tests caught this, I just didn't notice earlier.
2010-12-11 05:54:39 -08:00
Avery Pennarun
f702417ef3 The second half of redo-stamp: out-of-order building.
If a depends on b depends on c, and c is dirty but b uses redo-stamp
checksums, then 'redo-ifchange a' is indeterminate: we won't know if we need
to run a.do unless we first build b, but the script that *normally* runs
'redo-ifchange b' is a.do, and we don't want to run that yet, because we
don't know for sure if b is dirty, and we shouldn't build a unless one of
its dependencies is dirty.  Eek!

Luckily, there's a safe solution.  If we *know* a is dirty - eg. because
a.do or one of its children has definitely changed - then we can just run
a.do immediately and there's no problem, even if b is indeterminate, because
we were going to run a.do anyhow.

If a's dependencies are *not* definitely dirty, and all we have is
indeterminate ones like b, then that means a's build process *hasn't
changed*, which means its tree of dependencies still includes b, which means
we can deduce that if we *did* run a.do, it would end up running b.do.

Since we know that anyhow, we can safely just run b.do, which will either
b.set_checked() or b.set_changed().  Once that's done, we can re-parse a's
dependencies and this time conclusively tell if it needs to be redone or
not.  Even if it does, b is already up-to-date, so the 'redo-ifchange b'
line in a.do will be fast.

...now take all the above and do it recursively to handle nested
dependencies, etc, and you're done.
2010-12-11 05:54:39 -08:00
Avery Pennarun
22617d335c Half-support for using file checksums instead of stamps.
A new redo-stamp program takes whatever you give it as stdin and uses it to
calculate a checksum for the current target.  If that checksum is the same
as last time, then we consider the target to be unchanged, and we set
checked_runid and stamp, but leave changed_runid alone.  That will make
future callers of redo-ifchange see this target as unmodified.

However, this is only "half" support because by the time we run the .do
script that calls redo-stamp, it's too late; the caller is a dependant of
the stamped program, which is already being rebuilt, even if redo-stamp
turns out to say that this target is unchanged.

The other half is coming up.
2010-12-11 05:54:37 -08:00
Avery Pennarun
59201dd7a0 $3 and stdout no longer refer to the same file.
This is slightly inelegant, as the old style
	echo foo
	echo blah
	chmod a+x $3

doesn't work anymore; the stuff you wrote to stdout didn't end up in $3.
You can rewrite it as:
	exec >$3
	echo foo
	echo blah
	chmod a+x $3

Anyway, it's better this way, because now we can tell the difference between
a zero-length $3 and a nonexistent one.  A .do script can thus produce
either one and we'll either delete the target or move the empty $3 to
replace it, whichever is right.

As a bonus, this simplifies our detection of whether you did something weird
with overlapping changes to stdout and $3.
2010-12-11 00:29:04 -08:00
Avery Pennarun
c4be0050f7 Release the jwack token when doing a synchronous lock wait.
Although we were deadlock-free before, under some circumstances we'd end up
holding a perfectly good token while in sync wait; that would reduce our
parallelism for no good reason.  So give back our tokens before waiting for
anybody else.
2010-12-10 23:04:46 -08:00
Avery Pennarun
f6d11d5411 If a user manually changes a generated file, don't ever overwrite it.
That way the user can modify an auto-generated 'compile' script, for
example, and it'll stay modified.

If they delete the file, we can then generate it for them again.

Also, we have to warn whenever we're doing this, or people might think it's
a bug.
2010-12-10 22:43:11 -08:00
Avery Pennarun
0126f6be1e Don't wipe the timestamp when a target fails to redo.
It's really a separate condition.  And since we're not removing the target
*file* in case of error - we update it atomically, and keeping it is better
than losing it - there's no reason to wipe the timestamp in that case
either.

However, we do need to know that the build failed, so that anybody else
(especially in a parallel build) who looks at that target knows that it
died.  So add a separate flag just for that.
2010-12-10 22:41:11 -08:00
Avery Pennarun
16bebd21b5 builder: the (WAITING) message from --debug-locks didn't print every time.
This was misleading; we end up waiting synchronously for a lock more often
than I thought, and it really does slow down builds.
2010-12-10 22:39:25 -08:00
Avery Pennarun
f70c028a8a With --debug-locks, print a message when we stop to wait on a lock.
Helps in seeing why a particular process might be stopped, and in detecting
potential reasons that parallelism might be reduced.
2010-12-10 04:31:22 -08:00
Avery Pennarun
84169c5d27 Change locking stuff from fifos to fcntl.lockf().
This should reduce filesystem grinding a bit, and makes the code simpler.
It's also theoretically a bit more portable, since I'm guessing fifo
semantics aren't the same on win32 if we ever get there.

Also, a major problem with the old fifo-based system is that if a redo
process died without cleaning up after itself, it wouldn't delete its
lockfiles, so we had to wipe them all at the beginning of each build.  Now
we don't; in theory, you can now have multiple copies of redo poking at the
same tree at the same time and not stepping on each other.
2010-12-10 03:55:51 -08:00
Avery Pennarun
6e6e453908 Some speedups for doing redo-ifchange on a large number of static files.
Fix some wastage revealed by the (almost useless, sigh) python profiler.
2010-12-10 00:50:53 -08:00
Avery Pennarun
e446d4dd04 builder.py: don't import the 'random' module unless we need it.
Initializing the random number generator involves some pointless reading
from /dev/urandom.
2010-12-10 00:50:53 -08:00
Avery Pennarun
3ef2bd7300 Don't check as often whether the .redo directory exists.
Just check it once after running a subprocess: that's the only way it ought
to be able to disappear (ie. in a 'make clean' setup).
2010-12-10 00:50:52 -08:00
Avery Pennarun
29d6c9a746 Don't db.commit() so frequently.
Just commit when we're about to do something blocking.  sqlite goes a lot
faster with bigger transactions.  This change does show a small percentage
speedup in tests, but not as much as I'd like.
2010-12-10 00:50:52 -08:00
Avery Pennarun
a62bd50d44 Switch state.py to use sqlite3 instead of filesystem-based stamps.
It passes all tests when run serialized, but still gives weird errors
(OperationalError: database is locked) when run with -j5.  sqlite3 shouldn't
be barfing just because the database is locked, since the default timeout is
5 seconds, and it's dying *way* faster than that.
2010-12-10 00:50:52 -08:00
Avery Pennarun
43b74f3220 builder._nice(): show the right filename in the case of chdir().
This only affects cosmetics, not actual behaviour, which is why the unit
tests didn't catch it.
2010-12-10 00:49:30 -08:00
Avery Pennarun
51bbdc6c5a If we can't find a .do file for a target, mark it as not is_generated.
This allows files to transition from generated to not-generated if the .do
file is ever removed (ie. the user is changing things and the file is now a
source file, not a target).
2010-12-06 03:12:53 -08:00
Avery Pennarun
b3a14a28c4 When -x or -v is given, print the sh command we're executing. 2010-12-06 02:47:24 -08:00
Avery Pennarun
66187e879e Slightly improve the "if target already existed" rule to ignore directories.
So if you have a default.do, it might be used to build mydir, even if mydir
already existed when we started.

This might be useful if you have a mydir.setup.do and a mydir.do; mydir.do
depends on mydir.setup.do, but mydir.setup.do creates mydir, it just doesn't
*finish* with mydir.  Still, my the time mydir.do runs, mydir already
exists, and redo would get confused.

I think directories are fundamentally special in this way, because it makes
sense to "create" a directory even if that directory isn't "done" at this
phase.
2010-12-04 05:42:07 -08:00
Avery Pennarun
c29de89051 Fix more trouble with .do scripts that cd to other directories.
The interaction of REDO_STARTDIR, REDO_PWD, and getcwd() are pretty
complicated.  In this case, we accidentally assumed that the current
instance of redo was running with getcwd() == REDO_STARTDIR+REDO_PWD, and so
the new target was REDO_STARTDIR+REDO_PWD+t, but this isn't the case if the
current .do script did chdir().

The correct answer is REDO_STARTDIR+getcwd()+t.
2010-11-25 06:37:24 -08:00
Avery Pennarun
f3413c0f7c doublestatic: fix dependencies if two files depend on one non-generated file.
If a and b both depend on c, and c is a static (non-generated) file that has
changed since the last successful build of a and b, we would try to redo
a, but would forget to redo b.  Now it does both.
2010-11-24 04:52:30 -08:00
Avery Pennarun
0ec15eeb09 If a target's .do file disappears, don't forget to stamp it.
If a file previously was generated but now isn't (ie. its .do file
disappears), we would never re-stamp that target, and so all its
dependencies would rebuild continually.
2010-11-24 03:44:37 -08:00
Avery Pennarun
984ad747f8 Remove special case for "dirname" -> "dirname/all"
It actually decreases readability of the .do files - by not making it
explicit when you're going into a subdir.

Plus it adds ambiguity: what if there's a dirname.do *and* a dirname/all?
We could resolve the ambiguity if we wanted, but that adds more code, while
taking out this special case makes *less* code and improves readability.
I think it's the right way to go.
2010-11-24 02:48:27 -08:00
Avery Pennarun
282bb0488e If the created target is a directory, it's okay for the .do to create it.
Normally, creating the target $1 yourself is bad; create $3 instead.  But if
$1 is a directory, we'll allow it.  That way 'redo subdir' can call
subdir.do, and subdir.do can both create the directory *and* run a bunch of
sub-.do files on it.
2010-11-24 02:30:54 -08:00
Avery Pennarun
83dd52c209 Targets created from stdout should be rw-, not rwx.
I had forgotten to pass the create mode to open().  Oops!
2010-11-24 02:26:15 -08:00
Avery Pennarun
190dd657d8 It's okay if a file is marked as generated and doesn't have a .do.
If we don't know how to rebuild the file, but it already exists, that's
pretty harmless.  Just consider it a successful rebuild.
2010-11-24 02:18:19 -08:00
Avery Pennarun
f337df463d state.stamp() can't imply state.built().
...because we deliberately stamp non-generated files as well, and that
doesn't need to imply that we rebuilt them just now.  In fact, we know for a
fact that we *didn't* rebuild them just now, but we still need to record the
timestamp for later.
2010-11-22 22:53:40 -08:00
Avery Pennarun
6d767e2a65 user-friendliness sanity checks: catch common mistakes regarding $1/$2/$3.
.do files should never modify $1, and should write to *either* $3 or stdout,
but not both.  If they write to both, it's probably because they forgot to
redirect stdout to stderr, a very easy mistake to make but a hard one to
detect.

Now redo detects it for you and prints an informative message.
2010-11-22 04:43:33 -08:00
Avery Pennarun
dce0076554 Print a useful message and exit when the .redo directory disappears. 2010-11-22 04:04:45 -08:00
Avery Pennarun
2dbd47100d state.py: reduce race condition between Lock.trylock() and unlock().
If 'redo clean' deletes the lockfile after trylock() succeeds but before
unlock(), then unlock() won't be able to open the pipe in order to release
readers, and any waiters might end up waiting forever.

We can't open the fifo for write until there's at least one reader, so let's
open a reader *just* to let us open a writer.  Then we'll leave them open
until the later unlock(), which can just close them both.
2010-11-22 04:04:45 -08:00
Avery Pennarun
135d1c161a builder.py: set FD_CLOEXEC flag on $3 when running a .do file.
Otherwise it could be inherited by other jwack jobs started from the same
parent process, resulting in some very-hard-to-debug race conditions, let me
tell you.
2010-11-22 01:48:02 -08:00
Avery Pennarun
8d0eba9c44 builder.py: use os.exec() instead of subprocess.call().
This avoids a second fork altogether, which apparently doesn't matter all
that much (5.3s -> 5.15s).  However, simply not importing the subprocess
module reduces us further to 4.6s.

And 'ps axf' now looks prettier because we have fewer stupid intermediate
processes.
2010-11-22 00:15:29 -08:00
Avery Pennarun
dcc2edba0c builder.py: further refactoring to run more stuff in the parent process
instead of inside the fork.

Still doesn't seem to affect runtime.  Good.

One nice side effect is jwack.py no longer needs to know anything about our
locks.
2010-11-22 00:04:15 -08:00
Avery Pennarun
3209316856 builder.py: now the only exported function is main().
We can also avoid forking altogether if should_build() returns false.  This
doesn't seem to result in any noticeable speedup, but it's cleaner at least.
2010-11-21 23:36:29 -08:00
Avery Pennarun
547dbe550f builder.py: remove the build() wrapper function and BuildError.
The only thing we want to do on a build error is return a nonzero exit code,
so let's just do that, simplifying the code a bit.
2010-11-21 23:06:31 -08:00
Avery Pennarun
7aa7c41e38 builder,jwack: slight cleanup to token passing.
In rare cases, one process could end up holding onto more than one token.
2010-11-21 22:46:20 -08:00