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.
6 """This script provides methods for clobbering build directories."""
15 def extract_gn_build_commands(build_ninja_file):
16 """Extracts from a build.ninja the commands to run GN.
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.
22 On error, returns the empty string."""
24 with open(build_ninja_file, 'r') as f:
25 # Read until the third blank line. The first thing GN writes to the file
26 # is "ninja_required_version = x.y.z", then the "rule gn" and the third
27 # is the section for "build build.ninja", separated by blank lines.
29 while num_blank_lines < 3:
32 return '' # Unexpected EOF.
35 num_blank_lines = num_blank_lines + 1
39 def delete_dir(build_dir):
40 if os.path.islink(build_dir):
42 # For unknown reasons (anti-virus?) rmtree of Chromium build directories
43 # often fails on Windows.
44 if sys.platform.startswith('win'):
45 subprocess.check_call(['rmdir', '/s', '/q', build_dir], shell=True)
47 shutil.rmtree(build_dir)
50 def delete_build_dir(build_dir):
51 # GN writes a build.ninja.d file. Note that not all GN builds have args.gn.
52 build_ninja_d_file = os.path.join(build_dir, 'build.ninja.d')
53 if not os.path.exists(build_ninja_d_file):
57 # GN builds aren't automatically regenerated when you sync. To avoid
58 # messing with the GN workflow, erase everything but the args file, and
59 # write a dummy build.ninja file that will automatically rerun GN the next
61 build_ninja_file = os.path.join(build_dir, 'build.ninja')
62 build_commands = extract_gn_build_commands(build_ninja_file)
65 gn_args_file = os.path.join(build_dir, 'args.gn')
66 with open(gn_args_file, 'r') as f:
67 args_contents = f.read()
73 # delete_dir and os.mkdir() may fail, such as when chrome.exe is running,
74 # and we still want to restore args.gn/build.ninja/build.ninja.d, so catch
75 # the exception and rethrow it later.
78 except Exception as e:
81 # Put back the args file (if any).
82 if args_contents != '':
83 with open(gn_args_file, 'w') as f:
84 f.write(args_contents)
86 # Write the build.ninja file sufficiently to regenerate itself.
87 with open(os.path.join(build_dir, 'build.ninja'), 'w') as f:
88 if build_commands != '':
89 f.write(build_commands)
91 # Couldn't parse the build.ninja file, write a default thing.
93 command = gn -q gen //out/%s/
94 description = Regenerating ninja files
98 depfile = build.ninja.d
99 ''' % (os.path.split(build_dir)[1]))
101 # Write a .d file for the build which references a nonexistant file. This
102 # will make Ninja always mark the build as dirty.
103 with open(build_ninja_d_file, 'w') as f:
104 f.write('build.ninja: nonexistant_file.gn\n')
107 # Rethrow the exception we caught earlier.
110 def clobber(out_dir):
111 """Clobber contents of build directory.
113 Don't delete the directory itself: some checkouts have the build directory
115 for f in os.listdir(out_dir):
116 path = os.path.join(out_dir, f)
117 if os.path.isfile(path):
119 elif os.path.isdir(path):
120 delete_build_dir(path)
124 parser = argparse.ArgumentParser()
125 parser.add_argument('out_dir', help='The output directory to clobber')
126 args = parser.parse_args()
127 clobber(args.out_dir)
131 if __name__ == '__main__':