OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """This script provides methods for clobbering build directories.""" | 6 """This script provides methods for clobbering build directories.""" |
7 | 7 |
8 import argparse | 8 import argparse |
9 import os | 9 import os |
10 import platform | |
10 import shutil | 11 import shutil |
12 import subprocess | |
11 import sys | 13 import sys |
12 | 14 |
13 | 15 |
14 def extract_gn_build_commands(build_ninja_file): | 16 def extract_gn_build_commands(build_ninja_file): |
15 """Extracts from a build.ninja the commands to run GN. | 17 """Extracts from a build.ninja the commands to run GN. |
16 | 18 |
17 The commands to run GN are the gn rule and build.ninja build step at the | 19 The commands to run GN are the gn rule and build.ninja build step at the |
18 top of the build.ninja file. We want to keep these when deleting GN builds | 20 top of the build.ninja file. We want to keep these when deleting GN builds |
19 since we want to preserve the command-line flags to GN. | 21 since we want to preserve the command-line flags to GN. |
20 | 22 |
21 On error, returns the empty string.""" | 23 On error, returns the empty string.""" |
22 result = "" | 24 result = "" |
23 with open(build_ninja_file, 'r') as f: | 25 with open(build_ninja_file, 'r') as f: |
24 # Read until the second blank line. The first thing GN writes to the file | 26 # Read until the second blank line. The first thing GN writes to the file |
25 # is the "rule gn" and the second is the section for "build build.ninja", | 27 # is the "rule gn" and the second is the section for "build build.ninja", |
26 # separated by blank lines. | 28 # separated by blank lines. |
27 num_blank_lines = 0 | 29 num_blank_lines = 0 |
28 while num_blank_lines < 2: | 30 while num_blank_lines < 2: |
29 line = f.readline() | 31 line = f.readline() |
30 if len(line) == 0: | 32 if len(line) == 0: |
31 return '' # Unexpected EOF. | 33 return '' # Unexpected EOF. |
32 result += line | 34 result += line |
33 if line[0] == '\n': | 35 if line[0] == '\n': |
34 num_blank_lines = num_blank_lines + 1 | 36 num_blank_lines = num_blank_lines + 1 |
35 return result | 37 return result |
36 | 38 |
37 | 39 |
40 def delete_dir(build_dir): | |
41 # For unknown reasons (anti-virus?) rmtree of Chromium build directories | |
42 # often fails on Windows. | |
43 if platform.system() == 'Windows': | |
scottmg
2016/03/18 00:40:37
I think "if sys.platform.startswith('win')" is mor
brucedawson
2016/03/18 20:55:50
Ah - yes. That's what I was looking for. Done (inc
| |
44 subprocess.call(['rmdir', '/s', '/q', build_dir], shell=True) | |
scottmg
2016/03/18 00:40:37
.call -> .check_call
brucedawson
2016/03/18 20:55:50
Done.
| |
45 else: | |
46 shutil.rmtree(build_dir) | |
47 | |
48 | |
38 def delete_build_dir(build_dir): | 49 def delete_build_dir(build_dir): |
39 # GN writes a build.ninja.d file. Note that not all GN builds have args.gn. | 50 # GN writes a build.ninja.d file. Note that not all GN builds have args.gn. |
40 build_ninja_d_file = os.path.join(build_dir, 'build.ninja.d') | 51 build_ninja_d_file = os.path.join(build_dir, 'build.ninja.d') |
41 if not os.path.exists(build_ninja_d_file): | 52 if not os.path.exists(build_ninja_d_file): |
42 shutil.rmtree(build_dir) | 53 delete_dir(build_dir) |
43 return | 54 return |
44 | 55 |
45 # GN builds aren't automatically regenerated when you sync. To avoid | 56 # GN builds aren't automatically regenerated when you sync. To avoid |
46 # messing with the GN workflow, erase everything but the args file, and | 57 # messing with the GN workflow, erase everything but the args file, and |
47 # write a dummy build.ninja file that will automatically rerun GN the next | 58 # write a dummy build.ninja file that will automatically rerun GN the next |
48 # time Ninja is run. | 59 # time Ninja is run. |
49 build_ninja_file = os.path.join(build_dir, 'build.ninja') | 60 build_ninja_file = os.path.join(build_dir, 'build.ninja') |
50 build_commands = extract_gn_build_commands(build_ninja_file) | 61 build_commands = extract_gn_build_commands(build_ninja_file) |
51 | 62 |
52 try: | 63 try: |
53 gn_args_file = os.path.join(build_dir, 'args.gn') | 64 gn_args_file = os.path.join(build_dir, 'args.gn') |
54 with open(gn_args_file, 'r') as f: | 65 with open(gn_args_file, 'r') as f: |
55 args_contents = f.read() | 66 args_contents = f.read() |
56 except IOError: | 67 except IOError: |
57 args_contents = '' | 68 args_contents = '' |
58 | 69 |
59 shutil.rmtree(build_dir) | 70 delete_dir(build_dir) |
60 | 71 |
61 # Put back the args file (if any). | 72 # Put back the args file (if any). |
62 os.mkdir(build_dir) | 73 os.mkdir(build_dir) |
63 if args_contents != '': | 74 if args_contents != '': |
64 with open(gn_args_file, 'w') as f: | 75 with open(gn_args_file, 'w') as f: |
65 f.write(args_contents) | 76 f.write(args_contents) |
66 | 77 |
67 # Write the build.ninja file sufficiently to regenerate itself. | 78 # Write the build.ninja file sufficiently to regenerate itself. |
68 with open(os.path.join(build_dir, 'build.ninja'), 'w') as f: | 79 with open(os.path.join(build_dir, 'build.ninja'), 'w') as f: |
69 if build_commands != '': | 80 if build_commands != '': |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
101 def main(): | 112 def main(): |
102 parser = argparse.ArgumentParser() | 113 parser = argparse.ArgumentParser() |
103 parser.add_argument('out_dir', help='The output directory to clobber') | 114 parser.add_argument('out_dir', help='The output directory to clobber') |
104 args = parser.parse_args() | 115 args = parser.parse_args() |
105 clobber(args.out_dir) | 116 clobber(args.out_dir) |
106 return 0 | 117 return 0 |
107 | 118 |
108 | 119 |
109 if __name__ == '__main__': | 120 if __name__ == '__main__': |
110 sys.exit(main()) | 121 sys.exit(main()) |
OLD | NEW |