blob: b6c69982a783d45f3635eebe9ce8ca4197ffef42 [file] [log] [blame]
petrcermakaf7f4c02015-06-22 12:41:491#!/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
brucedawsonb0e768e2016-03-19 00:30:5111import subprocess
petrcermakaf7f4c02015-06-22 12:41:4912import 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
brucedawsonb0e768e2016-03-19 00:30:5139def delete_dir(build_dir):
sfiera29acddf2017-01-19 17:42:5840 if os.path.islink(build_dir):
41 return
brucedawsonb0e768e2016-03-19 00:30:5142 # 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)
46 else:
47 shutil.rmtree(build_dir)
48
49
petrcermakaf7f4c02015-06-22 12:41:4950def 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):
brucedawsonb0e768e2016-03-19 00:30:5154 delete_dir(build_dir)
petrcermakaf7f4c02015-06-22 12:41:4955 return
56
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
60 # time Ninja is run.
61 build_ninja_file = os.path.join(build_dir, 'build.ninja')
62 build_commands = extract_gn_build_commands(build_ninja_file)
63
64 try:
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()
68 except IOError:
69 args_contents = ''
70
brucedawson562df4f32016-05-20 22:00:4671 e = None
72 try:
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.
76 delete_dir(build_dir)
77 os.mkdir(build_dir)
78 except Exception as e:
79 pass
petrcermakaf7f4c02015-06-22 12:41:4980
81 # Put back the args file (if any).
petrcermakaf7f4c02015-06-22 12:41:4982 if args_contents != '':
83 with open(gn_args_file, 'w') as f:
84 f.write(args_contents)
85
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)
90 else:
91 # Couldn't parse the build.ninja file, write a default thing.
92 f.write('''rule gn
93command = gn -q gen //out/%s/
94description = Regenerating ninja files
95
96build build.ninja: gn
97generator = 1
98depfile = build.ninja.d
99''' % (os.path.split(build_dir)[1]))
100
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')
105
brucedawson562df4f32016-05-20 22:00:46106 if e:
107 # Rethrow the exception we caught earlier.
108 raise e
petrcermakaf7f4c02015-06-22 12:41:49109
110def clobber(out_dir):
111 """Clobber contents of build directory.
112
113 Don't delete the directory itself: some checkouts have the build directory
114 mounted."""
115 for f in os.listdir(out_dir):
116 path = os.path.join(out_dir, f)
117 if os.path.isfile(path):
118 os.unlink(path)
119 elif os.path.isdir(path):
120 delete_build_dir(path)
121
122
123def main():
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)
128 return 0
129
130
131if __name__ == '__main__':
132 sys.exit(main())