2 # Copyright 2015 The Chromium Authors
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 # Reads until the first empty line after the "build build.ninja:" target.
26 # We assume everything before it necessary as well (eg the
27 # "ninja_required_version" line).
28 found_build_dot_ninja_target = False
29 for line in f.readlines():
31 if line.startswith('build build.ninja:'):
32 found_build_dot_ninja_target = True
33 if found_build_dot_ninja_target and line[0] == '\n':
35 return '' # We got to EOF and didn't find what we were looking for.
39 # For unknown reasons (anti-virus?) rmtree of Chromium build directories
40 # often fails on Windows.
41 if sys.platform.startswith('win'):
42 subprocess.check_call(['rmdir', '/s', '/q', d], shell=True)
47 def _clean_dir(build_dir):
48 # Remove files/sub directories individually instead of recreating the build
49 # dir because it fails when the build dir is symlinked or mounted.
50 for e in os.scandir(build_dir):
57 def delete_build_dir(build_dir):
58 # GN writes a build.ninja.d file. Note that not all GN builds have args.gn.
59 build_ninja_d_file = os.path.join(build_dir, 'build.ninja.d')
60 if not os.path.exists(build_ninja_d_file):
64 # GN builds aren't automatically regenerated when you sync. To avoid
65 # messing with the GN workflow, erase everything but the args file, and
66 # write a dummy build.ninja file that will automatically rerun GN the next
68 build_ninja_file = os.path.join(build_dir, 'build.ninja')
69 build_commands = extract_gn_build_commands(build_ninja_file)
72 gn_args_file = os.path.join(build_dir, 'args.gn')
73 with open(gn_args_file, 'r') as f:
74 args_contents = f.read()
78 exception_during_rm = None
80 # _clean_dir() may fail, such as when chrome.exe is running,
81 # and we still want to restore args.gn/build.ninja/build.ninja.d, so catch
82 # the exception and rethrow it later.
83 # We manually rm files inside the build dir rather than using "gn clean/gen"
84 # since we may not have run all necessary DEPS hooks yet at this point.
86 except Exception as e:
87 exception_during_rm = e
89 # Put back the args file (if any).
90 if args_contents != '':
91 with open(gn_args_file, 'w') as f:
92 f.write(args_contents)
94 # Write the build.ninja file sufficiently to regenerate itself.
95 with open(os.path.join(build_dir, 'build.ninja'), 'w') as f:
96 if build_commands != '':
97 f.write(build_commands)
99 # Couldn't parse the build.ninja file, write a default thing.
100 f.write('''ninja_required_version = 1.7.2
103 command = gn -q gen //out/%s/
104 description = Regenerating ninja files
106 build build.ninja: gn
108 depfile = build.ninja.d
109 ''' % (os.path.split(build_dir)[1]))
111 # Write a .d file for the build which references a nonexistant file. This
112 # will make Ninja always mark the build as dirty.
113 with open(build_ninja_d_file, 'w') as f:
114 f.write('build.ninja: nonexistant_file.gn\n')
116 if exception_during_rm:
117 # Rethrow the exception we caught earlier.
118 raise exception_during_rm
121 def clobber(out_dir):
122 """Clobber contents of build directory.
124 Don't delete the directory itself: some checkouts have the build directory
126 for f in os.listdir(out_dir):
127 path = os.path.join(out_dir, f)
128 if os.path.isfile(path):
130 elif os.path.isdir(path):
131 delete_build_dir(path)
135 parser = argparse.ArgumentParser()
136 parser.add_argument('out_dir', help='The output directory to clobber')
137 args = parser.parse_args()
138 clobber(args.out_dir)
142 if __name__ == '__main__':