blob: e886737dcc0cde8ef925a36d15e4689d70c427ec [file] [log] [blame]
Takuto Ikuta3dab32e02023-01-12 18:52:001#!/usr/bin/env python3
Avi Drissman73a09d12022-09-08 20:33:382# Copyright 2015 The Chromium Authors
petrcermakaf7f4c02015-06-22 12:41:493# 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
Ben Pastenee9d888e2023-01-17 23:24:1010import shutil
brucedawsonb0e768e2016-03-19 00:30:5111import subprocess
petrcermakaf7f4c02015-06-22 12:41:4912import sys
13
14
Ben Pastenee9d888e2023-01-17 23:24:1015def 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.
petrcermakaf7f4c02015-06-22 12:41:4936
37
Junji Watanabe741c3f62023-01-19 22:00:0538def _rmtree(d):
Ben Pastenee9d888e2023-01-17 23:24:1039 # For unknown reasons (anti-virus?) rmtree of Chromium build directories
40 # often fails on Windows.
41 if sys.platform.startswith('win'):
Junji Watanabe741c3f62023-01-19 22:00:0542 subprocess.check_call(['rmdir', '/s', '/q', d], shell=True)
Ben Pastenee9d888e2023-01-17 23:24:1043 else:
Junji Watanabe741c3f62023-01-19 22:00:0544 shutil.rmtree(d)
45
46
47def _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)
brucedawsonb0e768e2016-03-19 00:30:5155
Victor Viannac0eeb7e2023-01-13 16:27:2856
Ben Pastenee9d888e2023-01-17 23:24:1057def 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 Watanabe741c3f62023-01-19 22:00:0561 _clean_dir(build_dir)
Ben Pastenee9d888e2023-01-17 23:24:1062 return
Victor Viannac0eeb7e2023-01-13 16:27:2863
Ben Pastenee9d888e2023-01-17 23:24:1064 # 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 Watanabeddcf7df2023-01-16 04:50:0870
petrcermakaf7f4c02015-06-22 12:41:4971 try:
Ben Pastenee9d888e2023-01-17 23:24:1072 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 Watanabe741c3f62023-01-19 22:00:0580 # _clean_dir() may fail, such as when chrome.exe is running,
Ben Pastenee9d888e2023-01-17 23:24:1081 # and we still want to restore args.gn/build.ninja/build.ninja.d, so catch
82 # the exception and rethrow it later.
Junji Watanabe741c3f62023-01-19 22:00:0583 # 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 Pastenee9d888e2023-01-17 23:24:1086 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
102rule gn
103 command = gn -q gen //out/%s/
104 description = Regenerating ninja files
105
106build 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 Pastene3b05a632023-01-12 22:05:30119
petrcermakaf7f4c02015-06-22 12:41:49120
121def clobber(out_dir):
Ben Pastenee9d888e2023-01-17 23:24:10122 """Clobber contents of build directory.
123
124 Don't delete the directory itself: some checkouts have the build directory
125 mounted."""
petrcermakaf7f4c02015-06-22 12:41:49126 for f in os.listdir(out_dir):
127 path = os.path.join(out_dir, f)
Ben Pastenee9d888e2023-01-17 23:24:10128 if os.path.isfile(path):
129 os.unlink(path)
130 elif os.path.isdir(path):
131 delete_build_dir(path)
petrcermakaf7f4c02015-06-22 12:41:49132
133
134def 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
142if __name__ == '__main__':
143 sys.exit(main())