OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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 """ | 6 """ |
7 This script runs every build as the first hook (See DEPS). If it detects that | 7 This script runs every build as the first hook (See DEPS). If it detects that |
8 the build should be clobbered, it will delete the contents of the build | 8 the build should be clobbered, it will delete the contents of the build |
9 directory. | 9 directory. |
10 | 10 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 'output_dir' in landmine_utils.gyp_generator_flags()): | 47 'output_dir' in landmine_utils.gyp_generator_flags()): |
48 output_dir = landmine_utils.gyp_generator_flags()['output_dir'] | 48 output_dir = landmine_utils.gyp_generator_flags()['output_dir'] |
49 else: | 49 else: |
50 output_dir = os.environ.get('CHROMIUM_OUT_DIR', 'out') | 50 output_dir = os.environ.get('CHROMIUM_OUT_DIR', 'out') |
51 ret = os.path.join(SRC_DIR, output_dir) | 51 ret = os.path.join(SRC_DIR, output_dir) |
52 else: | 52 else: |
53 raise NotImplementedError('Unexpected GYP_GENERATORS (%s)' % build_tool) | 53 raise NotImplementedError('Unexpected GYP_GENERATORS (%s)' % build_tool) |
54 return os.path.abspath(ret) | 54 return os.path.abspath(ret) |
55 | 55 |
56 | 56 |
| 57 def extract_gn_build_commands(build_ninja_file): |
| 58 """Extracts from a build.ninja the commands to run GN. |
| 59 |
| 60 The commands to run GN are the gn rule and build.ninja build step at the |
| 61 top of the build.ninja file. We want to keep these when deleting GN builds |
| 62 since we want to preserve the command-line flags to GN. |
| 63 |
| 64 On error, returns the empty string.""" |
| 65 result = "" |
| 66 with open(build_ninja_file, 'r') as f: |
| 67 # Read until the second blank line. The first thing GN writes to the file |
| 68 # is the "rule gn" and the second is the section for "build build.ninja", |
| 69 # separated by blank lines. |
| 70 num_blank_lines = 0 |
| 71 while num_blank_lines < 2: |
| 72 line = f.readline() |
| 73 if len(line) == 0: |
| 74 return '' # Unexpected EOF. |
| 75 result += line |
| 76 if line[0] == '\n': |
| 77 num_blank_lines = num_blank_lines + 1 |
| 78 return result |
| 79 |
| 80 def delete_build_dir(build_dir): |
| 81 # GN writes a build.ninja.d file. Note that not all GN builds have args.gn. |
| 82 build_ninja_d_file = os.path.join(build_dir, 'build.ninja.d') |
| 83 if not os.path.exists(build_ninja_d_file): |
| 84 shutil.rmtree(build_dir) |
| 85 return |
| 86 |
| 87 # GN builds aren't automatically regenerated when you sync. To avoid |
| 88 # messing with the GN workflow, erase everything but the args file, and |
| 89 # write a dummy build.ninja file that will automatically rerun GN the next |
| 90 # time Ninja is run. |
| 91 build_ninja_file = os.path.join(build_dir, 'build.ninja') |
| 92 build_commands = extract_gn_build_commands(build_ninja_file) |
| 93 |
| 94 try: |
| 95 gn_args_file = os.path.join(build_dir, 'args.gn') |
| 96 with open(gn_args_file, 'r') as f: |
| 97 args_contents = f.read() |
| 98 except IOError: |
| 99 args_contents = '' |
| 100 |
| 101 shutil.rmtree(build_dir) |
| 102 |
| 103 # Put back the args file (if any). |
| 104 os.mkdir(build_dir) |
| 105 if args_contents != '': |
| 106 with open(gn_args_file, 'w') as f: |
| 107 f.write(args_contents) |
| 108 |
| 109 # Write the build.ninja file sufficiently to regenerate itself. |
| 110 with open(os.path.join(build_dir, 'build.ninja'), 'w') as f: |
| 111 if build_commands != '': |
| 112 f.write(build_commands) |
| 113 else: |
| 114 # Couldn't parse the build.ninja file, write a default thing. |
| 115 f.write('''rule gn |
| 116 command = gn -q gen //out/%s/ |
| 117 description = Regenerating ninja files |
| 118 |
| 119 build build.ninja: gn |
| 120 generator = 1 |
| 121 depfile = build.ninja.d |
| 122 ''' % (os.path.split(build_dir)[1])) |
| 123 |
| 124 # Write a .d file for the build which references a nonexistant file. This |
| 125 # will make Ninja always mark the build as dirty. |
| 126 with open(build_ninja_d_file, 'w') as f: |
| 127 f.write('build.ninja: nonexistant_file.gn\n') |
| 128 |
| 129 |
57 def clobber_if_necessary(new_landmines): | 130 def clobber_if_necessary(new_landmines): |
58 """Does the work of setting, planting, and triggering landmines.""" | 131 """Does the work of setting, planting, and triggering landmines.""" |
59 out_dir = get_build_dir(landmine_utils.builder()) | 132 out_dir = get_build_dir(landmine_utils.builder()) |
60 landmines_path = os.path.normpath(os.path.join(out_dir, '..', '.landmines')) | 133 landmines_path = os.path.normpath(os.path.join(out_dir, '..', '.landmines')) |
61 try: | 134 try: |
62 os.makedirs(out_dir) | 135 os.makedirs(out_dir) |
63 except OSError as e: | 136 except OSError as e: |
64 if e.errno == errno.EEXIST: | 137 if e.errno == errno.EEXIST: |
65 pass | 138 pass |
66 | 139 |
67 if os.path.exists(landmines_path): | 140 if os.path.exists(landmines_path): |
68 with open(landmines_path, 'r') as f: | 141 with open(landmines_path, 'r') as f: |
69 old_landmines = f.readlines() | 142 old_landmines = f.readlines() |
70 if old_landmines != new_landmines: | 143 if old_landmines != new_landmines: |
71 old_date = time.ctime(os.stat(landmines_path).st_ctime) | 144 old_date = time.ctime(os.stat(landmines_path).st_ctime) |
72 diff = difflib.unified_diff(old_landmines, new_landmines, | 145 diff = difflib.unified_diff(old_landmines, new_landmines, |
73 fromfile='old_landmines', tofile='new_landmines', | 146 fromfile='old_landmines', tofile='new_landmines', |
74 fromfiledate=old_date, tofiledate=time.ctime(), n=0) | 147 fromfiledate=old_date, tofiledate=time.ctime(), n=0) |
75 sys.stdout.write('Clobbering due to:\n') | 148 sys.stdout.write('Clobbering due to:\n') |
76 sys.stdout.writelines(diff) | 149 sys.stdout.writelines(diff) |
77 | 150 |
78 # Clobber contents of build directory but not directory itself: some | 151 # Clobber contents of build directory but not directory itself: some |
79 # checkouts have the build directory mounted. | 152 # checkouts have the build directory mounted. |
80 for f in os.listdir(out_dir): | 153 for f in os.listdir(out_dir): |
81 path = os.path.join(out_dir, f) | 154 path = os.path.join(out_dir, f) |
82 if os.path.isfile(path): | 155 if os.path.isfile(path): |
83 os.unlink(path) | 156 os.unlink(path) |
84 elif os.path.isdir(path): | 157 elif os.path.isdir(path): |
85 shutil.rmtree(path) | 158 delete_build_dir(path) |
86 | 159 |
87 # Save current set of landmines for next time. | 160 # Save current set of landmines for next time. |
88 with open(landmines_path, 'w') as f: | 161 with open(landmines_path, 'w') as f: |
89 f.writelines(new_landmines) | 162 f.writelines(new_landmines) |
90 | 163 |
91 | 164 |
92 def process_options(): | 165 def process_options(): |
93 """Returns a list of landmine emitting scripts.""" | 166 """Returns a list of landmine emitting scripts.""" |
94 parser = optparse.OptionParser() | 167 parser = optparse.OptionParser() |
95 parser.add_option( | 168 parser.add_option( |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 proc = subprocess.Popen([sys.executable, s], stdout=subprocess.PIPE) | 204 proc = subprocess.Popen([sys.executable, s], stdout=subprocess.PIPE) |
132 output, _ = proc.communicate() | 205 output, _ = proc.communicate() |
133 landmines.extend([('%s\n' % l.strip()) for l in output.splitlines()]) | 206 landmines.extend([('%s\n' % l.strip()) for l in output.splitlines()]) |
134 clobber_if_necessary(landmines) | 207 clobber_if_necessary(landmines) |
135 | 208 |
136 return 0 | 209 return 0 |
137 | 210 |
138 | 211 |
139 if __name__ == '__main__': | 212 if __name__ == '__main__': |
140 sys.exit(main()) | 213 sys.exit(main()) |
OLD | NEW |