blob: 1de321234bfd6f307a10d603ca229641a6847b9d [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:
Sylvain Defresnedfbbade32018-04-09 15:32:4825 # 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.
petrcermakaf7f4c02015-06-22 12:41:4928 num_blank_lines = 0
Sylvain Defresnedfbbade32018-04-09 15:32:4829 while num_blank_lines < 3:
petrcermakaf7f4c02015-06-22 12:41:4930 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.
Sylvain Defresne75bb49a2020-07-08 09:04:4192 f.write('''ninja_required_version = 1.7.2
93
94rule gn
95 command = gn -q gen //out/%s/
96 description = Regenerating ninja files
petrcermakaf7f4c02015-06-22 12:41:4997
98build build.ninja: gn
Sylvain Defresne75bb49a2020-07-08 09:04:4199 generator = 1
100 depfile = build.ninja.d
petrcermakaf7f4c02015-06-22 12:41:49101''' % (os.path.split(build_dir)[1]))
102
103 # Write a .d file for the build which references a nonexistant file. This
104 # will make Ninja always mark the build as dirty.
105 with open(build_ninja_d_file, 'w') as f:
106 f.write('build.ninja: nonexistant_file.gn\n')
107
brucedawson562df4f32016-05-20 22:00:46108 if e:
109 # Rethrow the exception we caught earlier.
110 raise e
petrcermakaf7f4c02015-06-22 12:41:49111
112def clobber(out_dir):
113 """Clobber contents of build directory.
114
115 Don't delete the directory itself: some checkouts have the build directory
116 mounted."""
117 for f in os.listdir(out_dir):
118 path = os.path.join(out_dir, f)
119 if os.path.isfile(path):
120 os.unlink(path)
121 elif os.path.isdir(path):
122 delete_build_dir(path)
123
124
125def main():
126 parser = argparse.ArgumentParser()
127 parser.add_argument('out_dir', help='The output directory to clobber')
128 args = parser.parse_args()
129 clobber(args.out_dir)
130 return 0
131
132
133if __name__ == '__main__':
134 sys.exit(main())