Re-create build directory if using existing one fails
authorPyry Haulos <phaulos@google.com>
Mon, 13 Jul 2015 19:44:53 +0000 (12:44 -0700)
committerPyry Haulos <phaulos@google.com>
Mon, 13 Jul 2015 20:02:21 +0000 (13:02 -0700)
When having multiple branches simultaneously checked out, it is common
to run into a situation where build_caselists.py or
build_android_mustpass.py fails since the existing build directory
points to different source tree.

As a fix remove and re-create build directory if using existing one
fails.

Change-Id: I3345482fbd3807dc153ac57aee6e893d059fc963

scripts/build/build.py

index 7732e12..1f030fa 100644 (file)
@@ -22,6 +22,7 @@
 
 import os
 import sys
+import shutil
 
 from common import *
 from config import *
@@ -39,12 +40,23 @@ def initBuildDir (config, generator):
                os.makedirs(config.buildDir)
 
        pushWorkingDir(config.getBuildDir())
-       execute(["cmake", config.getSrcPath()] + cfgArgs)
-       popWorkingDir()
+
+       try:
+               execute(["cmake", config.getSrcPath()] + cfgArgs)
+       finally:
+               popWorkingDir()
 
 def build (config, generator, targets = None):
-       # Initialize or update build dir.
-       initBuildDir(config, generator)
+       if os.path.exists(config.buildDir):
+               try:
+                       initBuildDir(config, generator)
+               except Exception as e:
+                       print e
+                       print "WARNING: Using existing build directory failed; re-creating build directory"
+                       shutil.rmtree(config.buildDir)
+                       initBuildDir(config, generator)
+       else:
+               initBuildDir(config, generator)
 
        baseCmd         = ['cmake', '--build', '.']
        buildArgs       = generator.getBuildArgs(config.getBuildType())