petrcermak | af7f4c0 | 2015-06-22 12:41:49 | [diff] [blame] | 1 | #!/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 | |
| 8 | import argparse |
| 9 | import os |
| 10 | import shutil |
brucedawson | b0e768e | 2016-03-19 00:30:51 | [diff] [blame] | 11 | import subprocess |
petrcermak | af7f4c0 | 2015-06-22 12:41:49 | [diff] [blame] | 12 | import sys |
| 13 | |
| 14 | |
| 15 | def 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 Defresne | dfbbade3 | 2018-04-09 15:32:48 | [diff] [blame] | 25 | # 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. |
petrcermak | af7f4c0 | 2015-06-22 12:41:49 | [diff] [blame] | 28 | num_blank_lines = 0 |
Sylvain Defresne | dfbbade3 | 2018-04-09 15:32:48 | [diff] [blame] | 29 | while num_blank_lines < 3: |
petrcermak | af7f4c0 | 2015-06-22 12:41:49 | [diff] [blame] | 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 | |
brucedawson | b0e768e | 2016-03-19 00:30:51 | [diff] [blame] | 39 | def delete_dir(build_dir): |
sfiera | 29acddf | 2017-01-19 17:42:58 | [diff] [blame] | 40 | if os.path.islink(build_dir): |
| 41 | return |
brucedawson | b0e768e | 2016-03-19 00:30:51 | [diff] [blame] | 42 | # 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 | |
petrcermak | af7f4c0 | 2015-06-22 12:41:49 | [diff] [blame] | 50 | def 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): |
brucedawson | b0e768e | 2016-03-19 00:30:51 | [diff] [blame] | 54 | delete_dir(build_dir) |
petrcermak | af7f4c0 | 2015-06-22 12:41:49 | [diff] [blame] | 55 | 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 | |
brucedawson | 562df4f3 | 2016-05-20 22:00:46 | [diff] [blame] | 71 | 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 |
petrcermak | af7f4c0 | 2015-06-22 12:41:49 | [diff] [blame] | 80 | |
| 81 | # Put back the args file (if any). |
petrcermak | af7f4c0 | 2015-06-22 12:41:49 | [diff] [blame] | 82 | 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 Defresne | 75bb49a | 2020-07-08 09:04:41 | [diff] [blame] | 92 | f.write('''ninja_required_version = 1.7.2 |
| 93 | |
| 94 | rule gn |
| 95 | command = gn -q gen //out/%s/ |
| 96 | description = Regenerating ninja files |
petrcermak | af7f4c0 | 2015-06-22 12:41:49 | [diff] [blame] | 97 | |
| 98 | build build.ninja: gn |
Sylvain Defresne | 75bb49a | 2020-07-08 09:04:41 | [diff] [blame] | 99 | generator = 1 |
| 100 | depfile = build.ninja.d |
petrcermak | af7f4c0 | 2015-06-22 12:41:49 | [diff] [blame] | 101 | ''' % (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 | |
brucedawson | 562df4f3 | 2016-05-20 22:00:46 | [diff] [blame] | 108 | if e: |
| 109 | # Rethrow the exception we caught earlier. |
| 110 | raise e |
petrcermak | af7f4c0 | 2015-06-22 12:41:49 | [diff] [blame] | 111 | |
| 112 | def 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 | |
| 125 | def 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 | |
| 133 | if __name__ == '__main__': |
| 134 | sys.exit(main()) |