blob: 18791c28f13c45f8e457c8cc6ae3dcd7881e1fac [file] [log] [blame]
#!/usr/bin/env python
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""This script provides methods for clobbering build directories."""
import argparse
import os
import shutil
import subprocess
import sys
def extract_gn_build_commands(build_ninja_file):
"""Extracts from a the commands to run GN.
The commands to run GN are the gn rule and build step at the
top of the file. We want to keep these when deleting GN builds
since we want to preserve the command-line flags to GN.
On error, returns the empty string."""
result = ""
with open(build_ninja_file, 'r') as f:
# Read until the third blank line. The first thing GN writes to the file
# is "ninja_required_version = x.y.z", then the "rule gn" and the third
# is the section for "build", separated by blank lines.
num_blank_lines = 0
while num_blank_lines < 3:
line = f.readline()
if len(line) == 0:
return '' # Unexpected EOF.
result += line
if line[0] == '\n':
num_blank_lines = num_blank_lines + 1
return result
def delete_dir(build_dir):
if os.path.islink(build_dir):
# For unknown reasons (anti-virus?) rmtree of Chromium build directories
# often fails on Windows.
if sys.platform.startswith('win'):
subprocess.check_call(['rmdir', '/s', '/q', build_dir], shell=True)
def delete_build_dir(build_dir):
# GN writes a file. Note that not all GN builds have
build_ninja_d_file = os.path.join(build_dir, '')
if not os.path.exists(build_ninja_d_file):
# GN builds aren't automatically regenerated when you sync. To avoid
# messing with the GN workflow, erase everything but the args file, and
# write a dummy file that will automatically rerun GN the next
# time Ninja is run.
build_ninja_file = os.path.join(build_dir, '')
build_commands = extract_gn_build_commands(build_ninja_file)
gn_args_file = os.path.join(build_dir, '')
with open(gn_args_file, 'r') as f:
args_contents =
except IOError:
args_contents = ''
e = None
# delete_dir and os.mkdir() may fail, such as when chrome.exe is running,
# and we still want to restore, so catch
# the exception and rethrow it later.
except Exception as e:
# Put back the args file (if any).
if args_contents != '':
with open(gn_args_file, 'w') as f:
# Write the file sufficiently to regenerate itself.
with open(os.path.join(build_dir, ''), 'w') as f:
if build_commands != '':
# Couldn't parse the file, write a default thing.
f.write('''rule gn
command = gn -q gen //out/%s/
description = Regenerating ninja files
build gn
generator = 1
depfile =
''' % (os.path.split(build_dir)[1]))
# Write a .d file for the build which references a nonexistant file. This
# will make Ninja always mark the build as dirty.
with open(build_ninja_d_file, 'w') as f:
if e:
# Rethrow the exception we caught earlier.
raise e
def clobber(out_dir):
"""Clobber contents of build directory.
Don't delete the directory itself: some checkouts have the build directory
for f in os.listdir(out_dir):
path = os.path.join(out_dir, f)
if os.path.isfile(path):
elif os.path.isdir(path):
def main():
parser = argparse.ArgumentParser()
parser.add_argument('out_dir', help='The output directory to clobber')
args = parser.parse_args()
return 0
if __name__ == '__main__':