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