Fix invalidation bugs.

1) Delete the hash-file before building a target, so that if
   the build aborts and leaves the output dir in a bad state
   and then we revert the change, we force a rebuild.

2) Ensure hashes are computed consistently.