OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
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 | |
4 # found in the LICENSE file. | |
5 | |
6 """ | |
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 | |
9 directory. | |
10 | |
11 A landmine is tripped when a builder checks out a different revision, and the | |
12 diff between the new landmines and the old ones is non-null. At this point, the | |
13 build is clobbered. | |
14 """ | |
15 | |
16 import difflib | |
17 import errno | |
18 import logging | |
19 import optparse | |
20 import os | |
21 import sys | |
22 import subprocess | |
23 import time | |
24 | |
25 import clobber | |
26 import landmine_utils | |
27 | |
28 | |
29 SRC_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) | |
30 | |
31 | |
32 def get_build_dir(build_tool, is_iphone=False): | |
33 """ | |
34 Returns output directory absolute path dependent on build and targets. | |
35 Examples: | |
36 r'c:\b\build\slave\win\build\src\out' | |
37 '/mnt/data/b/build/slave/linux/build/src/out' | |
38 '/b/build/slave/ios_rel_device/build/src/xcodebuild' | |
39 | |
40 Keep this function in sync with tools/build/scripts/slave/compile.py | |
41 """ | |
42 ret = None | |
43 if build_tool == 'xcode': | |
44 ret = os.path.join(SRC_DIR, 'xcodebuild') | |
45 elif build_tool in ['make', 'ninja', 'ninja-ios']: # TODO: Remove ninja-ios. | |
46 if 'CHROMIUM_OUT_DIR' in os.environ: | |
47 output_dir = os.environ.get('CHROMIUM_OUT_DIR').strip() | |
48 if not output_dir: | |
49 raise Error('CHROMIUM_OUT_DIR environment variable is set but blank!') | |
50 else: | |
51 output_dir = landmine_utils.gyp_generator_flags().get('output_dir', 'out') | |
52 ret = os.path.join(SRC_DIR, output_dir) | |
53 else: | |
54 raise NotImplementedError('Unexpected GYP_GENERATORS (%s)' % build_tool) | |
55 return os.path.abspath(ret) | |
56 | |
57 | |
58 def clobber_if_necessary(new_landmines): | |
59 """Does the work of setting, planting, and triggering landmines.""" | |
60 out_dir = get_build_dir(landmine_utils.builder()) | |
61 landmines_path = os.path.normpath(os.path.join(out_dir, '..', '.landmines')) | |
62 try: | |
63 os.makedirs(out_dir) | |
64 except OSError as e: | |
65 if e.errno == errno.EEXIST: | |
66 pass | |
67 | |
68 if os.path.exists(landmines_path): | |
69 with open(landmines_path, 'r') as f: | |
70 old_landmines = f.readlines() | |
71 if old_landmines != new_landmines: | |
72 old_date = time.ctime(os.stat(landmines_path).st_ctime) | |
73 diff = difflib.unified_diff(old_landmines, new_landmines, | |
74 fromfile='old_landmines', tofile='new_landmines', | |
75 fromfiledate=old_date, tofiledate=time.ctime(), n=0) | |
76 sys.stdout.write('Clobbering due to:\n') | |
77 sys.stdout.writelines(diff) | |
78 | |
79 clobber.clobber(out_dir) | |
80 | |
81 # Save current set of landmines for next time. | |
82 with open(landmines_path, 'w') as f: | |
83 f.writelines(new_landmines) | |
84 | |
85 | |
86 def process_options(): | |
87 """Returns a list of landmine emitting scripts.""" | |
88 parser = optparse.OptionParser() | |
89 parser.add_option( | |
90 '-s', '--landmine-scripts', action='append', | |
91 default=[os.path.join(SRC_DIR, 'build', 'get_landmines.py')], | |
92 help='Path to the script which emits landmines to stdout. The target ' | |
93 'is passed to this script via option -t. Note that an extra ' | |
94 'script can be specified via an env var EXTRA_LANDMINES_SCRIPT.') | |
95 parser.add_option('-v', '--verbose', action='store_true', | |
96 default=('LANDMINES_VERBOSE' in os.environ), | |
97 help=('Emit some extra debugging information (default off). This option ' | |
98 'is also enabled by the presence of a LANDMINES_VERBOSE environment ' | |
99 'variable.')) | |
100 | |
101 options, args = parser.parse_args() | |
102 | |
103 if args: | |
104 parser.error('Unknown arguments %s' % args) | |
105 | |
106 logging.basicConfig( | |
107 level=logging.DEBUG if options.verbose else logging.ERROR) | |
108 | |
109 extra_script = os.environ.get('EXTRA_LANDMINES_SCRIPT') | |
110 if extra_script: | |
111 return options.landmine_scripts + [extra_script] | |
112 else: | |
113 return options.landmine_scripts | |
114 | |
115 | |
116 def main(): | |
117 landmine_scripts = process_options() | |
118 | |
119 if landmine_utils.builder() in ('dump_dependency_json', 'eclipse'): | |
120 return 0 | |
121 | |
122 | |
123 landmines = [] | |
124 for s in landmine_scripts: | |
125 proc = subprocess.Popen([sys.executable, s], stdout=subprocess.PIPE) | |
126 output, _ = proc.communicate() | |
127 landmines.extend([('%s\n' % l.strip()) for l in output.splitlines()]) | |
128 clobber_if_necessary(landmines) | |
129 | |
130 return 0 | |
131 | |
132 | |
133 if __name__ == '__main__': | |
134 sys.exit(main()) | |
OLD | NEW |