Skip to content

Commit

Permalink
Merge pull request #107 from CyberShadow/rdmd-20140109
Browse files Browse the repository at this point in the history
rdmd: Clean up the right executable file if the build fails
  • Loading branch information
MartinNowak committed Jan 11, 2014
2 parents a36e702 + c640d97 commit ef5dc0c
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions rdmd.d
Expand Up @@ -257,11 +257,7 @@ int main(string[] args)
immutable result = rebuild(root, exe, workDir, objDir,
myDeps, compilerFlags, addStubMain);
if (result)
{
if (exists(exe))
remove(exe);
return result;
}

// Touch the build witness to track the build time
if (buildWitness != exe)
Expand Down Expand Up @@ -399,7 +395,26 @@ private int rebuild(string root, string fullExe,
string workDir, string objDir, in string[string] myDeps,
string[] compilerFlags, bool addStubMain)
{
// Delete the old executable before we start building.
yap("stat ", fullExe);
if (!dryRun && exists(fullExe))
{
yap("rm ", fullExe);
try
remove(fullExe);
catch (FileException e)
{
// This can occur on Windows if the executable is locked.
// Although we can't delete the file, we can still rename it.
auto oldExe = "%s.%s-%s.old".format(fullExe,
Clock.currTime.stdTime, thisProcessID);
yap("mv ", fullExe, " ", oldExe);
rename(fullExe, oldExe);
}
}

auto fullExeTemp = fullExe ~ ".tmp";

string[] buildTodo()
{
auto todo = compilerFlags
Expand Down Expand Up @@ -457,19 +472,7 @@ private int rebuild(string root, string fullExe,
collectException(rmdirRecurse(objDir));
}
yap("mv ", fullExeTemp, " ", fullExe);
try
rename(fullExeTemp, fullExe);
catch (FileException e)
{
// This can occur on Windows if the executable is locked.
// Although we can't delete the file, we can still rename it.
auto oldExe = "%s.%s-%s".format(fullExe, Clock.currTime.stdTime,
thisProcessID);
yap("mv ", fullExe, " ", oldExe);
rename(fullExe, oldExe);
yap("mv ", fullExeTemp, " ", fullExe);
rename(fullExeTemp, fullExe);
}
rename(fullExeTemp, fullExe);
}
return 0;
}
Expand Down

0 comments on commit ef5dc0c

Please sign in to comment.