1#!/usr/bin/env python 2# Copyright 2015 The Chromium Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6"""This script provides methods for clobbering build directories.""" 7 8import argparse 9import os 10import shutil 11import subprocess 12import sys 13 14 15def extract_gn_build_commands(build_ninja_file): 16 """Extracts from a build.ninja the commands to run GN. 17 18 The commands to run GN are the gn rule and build.ninja build step at the 19 top of the build.ninja file. We want to keep these when deleting GN builds 20 since we want to preserve the command-line flags to GN. 21 22 On error, returns the empty string.""" 23 result = "" 24 with open(build_ninja_file, 'r') as f: 25 # Read until the second blank line. The first thing GN writes to the file 26 # is the "rule gn" and the second is the section for "build build.ninja", 27 # separated by blank lines. 28 num_blank_lines = 0 29 while num_blank_lines < 2: 30 line = f.readline() 31 if len(line) == 0: 32 return '' # Unexpected EOF. 33 result += line 34 if line[0] == '\n': 35 num_blank_lines = num_blank_lines + 1 36 return result 37 38 39def delete_dir(build_dir): 40 # For unknown reasons (anti-virus?) rmtree of Chromium build directories 41 # often fails on Windows. 42 if sys.platform.startswith('win'): 43 subprocess.check_call(['rmdir', '/s', '/q', build_dir], shell=True) 44 else: 45 shutil.rmtree(build_dir) 46 47 48def delete_build_dir(build_dir): 49 # GN writes a build.ninja.d file. Note that not all GN builds have args.gn. 50 build_ninja_d_file = os.path.join(build_dir, 'build.ninja.d') 51 if not os.path.exists(build_ninja_d_file): 52 delete_dir(build_dir) 53 return 54 55 # GN builds aren't automatically regenerated when you sync. To avoid 56 # messing with the GN workflow, erase everything but the args file, and 57 # write a dummy build.ninja file that will automatically rerun GN the next 58 # time Ninja is run. 59 build_ninja_file = os.path.join(build_dir, 'build.ninja') 60 build_commands = extract_gn_build_commands(build_ninja_file) 61 62 try: 63 gn_args_file = os.path.join(build_dir, 'args.gn') 64 with open(gn_args_file, 'r') as f: 65 args_contents = f.read() 66 except IOError: 67 args_contents = '' 68 69 delete_dir(build_dir) 70 71 # Put back the args file (if any). 72 os.mkdir(build_dir) 73 if args_contents != '': 74 with open(gn_args_file, 'w') as f: 75 f.write(args_contents) 76 77 # Write the build.ninja file sufficiently to regenerate itself. 78 with open(os.path.join(build_dir, 'build.ninja'), 'w') as f: 79 if build_commands != '': 80 f.write(build_commands) 81 else: 82 # Couldn't parse the build.ninja file, write a default thing. 83 f.write('''rule gn 84command = gn -q gen //out/%s/ 85description = Regenerating ninja files 86 87build build.ninja: gn 88generator = 1 89depfile = build.ninja.d 90''' % (os.path.split(build_dir)[1])) 91 92 # Write a .d file for the build which references a nonexistant file. This 93 # will make Ninja always mark the build as dirty. 94 with open(build_ninja_d_file, 'w') as f: 95 f.write('build.ninja: nonexistant_file.gn\n') 96 97 98def clobber(out_dir): 99 """Clobber contents of build directory. 100 101 Don't delete the directory itself: some checkouts have the build directory 102 mounted.""" 103 for f in os.listdir(out_dir): 104 path = os.path.join(out_dir, f) 105 if os.path.isfile(path): 106 os.unlink(path) 107 elif os.path.isdir(path): 108 delete_build_dir(path) 109 110 111def main(): 112 parser = argparse.ArgumentParser() 113 parser.add_argument('out_dir', help='The output directory to clobber') 114 args = parser.parse_args() 115 clobber(args.out_dir) 116 return 0 117 118 119if __name__ == '__main__': 120 sys.exit(main()) 121