Takuto Ikuta | 3dab32e0 | 2023-01-12 18:52:00 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Avi Drissman | 73a09d1 | 2022-09-08 20:33:38 | [diff] [blame] | 2 | # Copyright 2015 The Chromium Authors |
petrcermak | af7f4c0 | 2015-06-22 12:41:49 | [diff] [blame] | 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 |
Ben Pastene | e9d888e | 2023-01-17 23:24:10 | [diff] [blame] | 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 | |
Ben Pastene | e9d888e | 2023-01-17 23:24:10 | [diff] [blame] | 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: |
| 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(): |
| 30 | result += line |
| 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': |
| 34 | return result |
| 35 | return '' # We got to EOF and didn't find what we were looking for. |
petrcermak | af7f4c0 | 2015-06-22 12:41:49 | [diff] [blame] | 36 | |
| 37 | |
Junji Watanabe | 741c3f6 | 2023-01-19 22:00:05 | [diff] [blame] | 38 | def _rmtree(d): |
Ben Pastene | e9d888e | 2023-01-17 23:24:10 | [diff] [blame] | 39 | # For unknown reasons (anti-virus?) rmtree of Chromium build directories |
| 40 | # often fails on Windows. |
| 41 | if sys.platform.startswith('win'): |
Junji Watanabe | 741c3f6 | 2023-01-19 22:00:05 | [diff] [blame] | 42 | subprocess.check_call(['rmdir', '/s', '/q', d], shell=True) |
Ben Pastene | e9d888e | 2023-01-17 23:24:10 | [diff] [blame] | 43 | else: |
Junji Watanabe | 741c3f6 | 2023-01-19 22:00:05 | [diff] [blame] | 44 | shutil.rmtree(d) |
| 45 | |
| 46 | |
| 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): |
| 51 | if e.is_dir(): |
| 52 | _rmtree(e.path) |
| 53 | else: |
| 54 | os.remove(e.path) |
brucedawson | b0e768e | 2016-03-19 00:30:51 | [diff] [blame] | 55 | |
Victor Vianna | c0eeb7e | 2023-01-13 16:27:28 | [diff] [blame] | 56 | |
Ben Pastene | e9d888e | 2023-01-17 23:24:10 | [diff] [blame] | 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): |
Junji Watanabe | 741c3f6 | 2023-01-19 22:00:05 | [diff] [blame] | 61 | _clean_dir(build_dir) |
Ben Pastene | e9d888e | 2023-01-17 23:24:10 | [diff] [blame] | 62 | return |
Victor Vianna | c0eeb7e | 2023-01-13 16:27:28 | [diff] [blame] | 63 | |
Ben Pastene | e9d888e | 2023-01-17 23:24:10 | [diff] [blame] | 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 |
| 67 | # time Ninja is run. |
| 68 | build_ninja_file = os.path.join(build_dir, 'build.ninja') |
| 69 | build_commands = extract_gn_build_commands(build_ninja_file) |
Junji Watanabe | ddcf7df | 2023-01-16 04:50:08 | [diff] [blame] | 70 | |
petrcermak | af7f4c0 | 2015-06-22 12:41:49 | [diff] [blame] | 71 | try: |
Ben Pastene | e9d888e | 2023-01-17 23:24:10 | [diff] [blame] | 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() |
| 75 | except IOError: |
| 76 | args_contents = '' |
| 77 | |
| 78 | exception_during_rm = None |
| 79 | try: |
Junji Watanabe | 741c3f6 | 2023-01-19 22:00:05 | [diff] [blame] | 80 | # _clean_dir() may fail, such as when chrome.exe is running, |
Ben Pastene | e9d888e | 2023-01-17 23:24:10 | [diff] [blame] | 81 | # and we still want to restore args.gn/build.ninja/build.ninja.d, so catch |
| 82 | # the exception and rethrow it later. |
Junji Watanabe | 741c3f6 | 2023-01-19 22:00:05 | [diff] [blame] | 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. |
| 85 | _clean_dir(build_dir) |
Ben Pastene | e9d888e | 2023-01-17 23:24:10 | [diff] [blame] | 86 | except Exception as e: |
| 87 | exception_during_rm = e |
| 88 | |
| 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) |
| 93 | |
| 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) |
| 98 | else: |
| 99 | # Couldn't parse the build.ninja file, write a default thing. |
| 100 | f.write('''ninja_required_version = 1.7.2 |
| 101 | |
| 102 | rule gn |
| 103 | command = gn -q gen //out/%s/ |
| 104 | description = Regenerating ninja files |
| 105 | |
| 106 | build build.ninja: gn |
| 107 | generator = 1 |
| 108 | depfile = build.ninja.d |
| 109 | ''' % (os.path.split(build_dir)[1])) |
| 110 | |
| 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') |
| 115 | |
| 116 | if exception_during_rm: |
| 117 | # Rethrow the exception we caught earlier. |
| 118 | raise exception_during_rm |
Ben Pastene | 3b05a63 | 2023-01-12 22:05:30 | [diff] [blame] | 119 | |
petrcermak | af7f4c0 | 2015-06-22 12:41:49 | [diff] [blame] | 120 | |
| 121 | def clobber(out_dir): |
Ben Pastene | e9d888e | 2023-01-17 23:24:10 | [diff] [blame] | 122 | """Clobber contents of build directory. |
| 123 | |
| 124 | Don't delete the directory itself: some checkouts have the build directory |
| 125 | mounted.""" |
petrcermak | af7f4c0 | 2015-06-22 12:41:49 | [diff] [blame] | 126 | for f in os.listdir(out_dir): |
| 127 | path = os.path.join(out_dir, f) |
Ben Pastene | e9d888e | 2023-01-17 23:24:10 | [diff] [blame] | 128 | if os.path.isfile(path): |
| 129 | os.unlink(path) |
| 130 | elif os.path.isdir(path): |
| 131 | delete_build_dir(path) |
petrcermak | af7f4c0 | 2015-06-22 12:41:49 | [diff] [blame] | 132 | |
| 133 | |
| 134 | def main(): |
| 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) |
| 139 | return 0 |
| 140 | |
| 141 | |
| 142 | if __name__ == '__main__': |
| 143 | sys.exit(main()) |