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.
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
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
15 Before adding or changing a landmine consider the consequences of doing so.
16 Doing so will wipe out every output directory on every Chrome developer's
17 machine. This can be particularly problematic on Windows where the directory
18 deletion may well fail (locked files, command prompt in the directory, etc.),
19 and generated .sln and .vcxproj files will be deleted.
21 This output directory deletion will be repated when going back and forth across
22 the change that added the landmine, adding to the cost. There are usually less
23 troublesome alternatives.
39 def get_build_dir(src_dir):
41 Returns output directory absolute path dependent on build and targets.
43 r'c:\b\build\slave\win\build\src\out'
44 '/mnt/data/b/build/slave/linux/build/src/out'
45 '/b/build/slave/ios_rel_device/build/src/out'
47 Keep this function in sync with tools/build/scripts/slave/compile.py
49 if 'CHROMIUM_OUT_DIR' in os.environ:
50 output_dir = os.environ.get('CHROMIUM_OUT_DIR').strip()
52 raise Error('CHROMIUM_OUT_DIR environment variable is set but blank!')
55 return os.path.abspath(os.path.join(src_dir, output_dir))
58 def clobber_if_necessary(new_landmines, src_dir):
59 """Does the work of setting, planting, and triggering landmines."""
60 out_dir = get_build_dir(src_dir)
61 landmines_path = os.path.normpath(os.path.join(src_dir, '.landmines'))
65 if e.errno == errno.EEXIST:
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)
80 clobber.clobber(out_dir)
82 # Save current set of landmines for next time.
83 with open(landmines_path, 'w') as f:
84 f.writelines(new_landmines)
87 def process_options():
88 """Returns an options object containing the configuration for this script."""
89 parser = optparse.OptionParser()
91 '-s', '--landmine-scripts', action='append',
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('-d', '--src-dir',
96 help='Path of the source root dir. Overrides the default location of the '
97 'source root dir when calculating the build directory.')
98 parser.add_option('-v', '--verbose', action='store_true',
99 default=('LANDMINES_VERBOSE' in os.environ),
100 help=('Emit some extra debugging information (default off). This option '
101 'is also enabled by the presence of a LANDMINES_VERBOSE environment '
104 options, args = parser.parse_args()
107 parser.error('Unknown arguments %s' % args)
110 level=logging.DEBUG if options.verbose else logging.ERROR)
113 if not os.path.isdir(options.src_dir):
114 parser.error('Cannot find source root dir at %s' % options.src_dir)
115 logging.debug('Overriding source root dir. Using: %s', options.src_dir)
118 os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
120 if not options.landmine_scripts:
121 options.landmine_scripts = [os.path.join(options.src_dir, 'build',
124 extra_script = os.environ.get('EXTRA_LANDMINES_SCRIPT')
126 options.landmine_scripts += [extra_script]
132 options = process_options()
135 for s in options.landmine_scripts:
136 proc = subprocess.Popen([sys.executable, s], stdout=subprocess.PIPE)
137 output, _ = proc.communicate()
138 landmines.extend([('%s\n' % l.strip()) for l in output.splitlines()])
139 clobber_if_necessary(landmines, options.src_dir)
144 if __name__ == '__main__':