blob: c31bac7901c362a320d735596dba887aa2d34ba3 [file] [log] [blame]
[email protected]abab8d72012-11-14 04:59:481#!/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"""
[email protected]9372bec2014-08-14 14:03:307This script runs every build as the first hook (See DEPS). If it detects that
8the build should be clobbered, it will remove the build directory.
[email protected]abab8d72012-11-14 04:59:489
10A landmine is tripped when a builder checks out a different revision, and the
11diff between the new landmines and the old ones is non-null. At this point, the
12build is clobbered.
13"""
14
15import difflib
[email protected]d99e6bf2014-04-23 04:19:2316import errno
[email protected]9372bec2014-08-14 14:03:3017import gyp_environment
[email protected]c45227b2012-11-15 02:53:0318import logging
19import optparse
[email protected]abab8d72012-11-14 04:59:4820import os
[email protected]9372bec2014-08-14 14:03:3021import shutil
[email protected]abab8d72012-11-14 04:59:4822import sys
[email protected]ec069f72013-08-21 02:44:5823import subprocess
[email protected]abab8d72012-11-14 04:59:4824import time
25
[email protected]ec069f72013-08-21 02:44:5826import landmine_utils
27
28
[email protected]abab8d72012-11-14 04:59:4829SRC_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
30
[email protected]abab8d72012-11-14 04:59:4831
[email protected]9372bec2014-08-14 14:03:3032def get_build_dir(build_tool, is_iphone=False):
[email protected]abab8d72012-11-14 04:59:4833 """
34 Returns output directory absolute path dependent on build and targets.
35 Examples:
[email protected]9372bec2014-08-14 14:03:3036 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'
[email protected]abab8d72012-11-14 04:59:4839
40 Keep this function in sync with tools/build/scripts/slave/compile.py
41 """
42 ret = None
43 if build_tool == 'xcode':
[email protected]9372bec2014-08-14 14:03:3044 ret = os.path.join(SRC_DIR, 'xcodebuild')
[email protected]ddca2f72013-05-11 19:13:2145 elif build_tool in ['make', 'ninja', 'ninja-ios']: # TODO: Remove ninja-ios.
[email protected]9372bec2014-08-14 14:03:3046 ret = os.path.join(SRC_DIR, 'out')
[email protected]d074d472012-11-15 01:52:0747 elif build_tool in ['msvs', 'vs', 'ib']:
[email protected]9372bec2014-08-14 14:03:3048 ret = os.path.join(SRC_DIR, 'build')
[email protected]abab8d72012-11-14 04:59:4849 else:
[email protected]68d80692013-01-17 23:50:0250 raise NotImplementedError('Unexpected GYP_GENERATORS (%s)' % build_tool)
[email protected]abab8d72012-11-14 04:59:4851 return os.path.abspath(ret)
52
53
[email protected]9372bec2014-08-14 14:03:3054def clobber_if_necessary(new_landmines):
[email protected]c45227b2012-11-15 02:53:0355 """Does the work of setting, planting, and triggering landmines."""
[email protected]9372bec2014-08-14 14:03:3056 out_dir = get_build_dir(landmine_utils.builder())
57 landmines_path = os.path.normpath(os.path.join(out_dir, '..', '.landmines'))
[email protected]d99e6bf2014-04-23 04:19:2358 try:
[email protected]c45227b2012-11-15 02:53:0359 os.makedirs(out_dir)
[email protected]d99e6bf2014-04-23 04:19:2360 except OSError as e:
61 if e.errno == errno.EEXIST:
62 pass
[email protected]c45227b2012-11-15 02:53:0363
[email protected]f3d0d1c32014-05-22 22:12:4864 if os.path.exists(landmines_path):
[email protected]c45227b2012-11-15 02:53:0365 with open(landmines_path, 'r') as f:
66 old_landmines = f.readlines()
67 if old_landmines != new_landmines:
68 old_date = time.ctime(os.stat(landmines_path).st_ctime)
69 diff = difflib.unified_diff(old_landmines, new_landmines,
70 fromfile='old_landmines', tofile='new_landmines',
71 fromfiledate=old_date, tofiledate=time.ctime(), n=0)
[email protected]9372bec2014-08-14 14:03:3072 sys.stdout.write('Clobbering due to:\n')
73 sys.stdout.writelines(diff)
[email protected]c45227b2012-11-15 02:53:0374
[email protected]9372bec2014-08-14 14:03:3075 # Clobber.
76 shutil.rmtree(out_dir)
77
78 # Save current set of landmines for next time.
[email protected]f3d0d1c32014-05-22 22:12:4879 with open(landmines_path, 'w') as f:
80 f.writelines(new_landmines)
[email protected]c45227b2012-11-15 02:53:0381
82
[email protected]c36d62932013-09-02 21:51:1883def process_options():
84 """Returns a list of landmine emitting scripts."""
[email protected]c45227b2012-11-15 02:53:0385 parser = optparse.OptionParser()
[email protected]ec069f72013-08-21 02:44:5886 parser.add_option(
87 '-s', '--landmine-scripts', action='append',
88 default=[os.path.join(SRC_DIR, 'build', 'get_landmines.py')],
89 help='Path to the script which emits landmines to stdout. The target '
[email protected]c36d62932013-09-02 21:51:1890 'is passed to this script via option -t. Note that an extra '
91 'script can be specified via an env var EXTRA_LANDMINES_SCRIPT.')
[email protected]c45227b2012-11-15 02:53:0392 parser.add_option('-v', '--verbose', action='store_true',
93 default=('LANDMINES_VERBOSE' in os.environ),
94 help=('Emit some extra debugging information (default off). This option '
95 'is also enabled by the presence of a LANDMINES_VERBOSE environment '
96 'variable.'))
[email protected]ec069f72013-08-21 02:44:5897
[email protected]c45227b2012-11-15 02:53:0398 options, args = parser.parse_args()
99
100 if args:
101 parser.error('Unknown arguments %s' % args)
102
103 logging.basicConfig(
104 level=logging.DEBUG if options.verbose else logging.ERROR)
[email protected]abab8d72012-11-14 04:59:48105
[email protected]c36d62932013-09-02 21:51:18106 extra_script = os.environ.get('EXTRA_LANDMINES_SCRIPT')
107 if extra_script:
108 return options.landmine_scripts + [extra_script]
109 else:
110 return options.landmine_scripts
111
112
113def main():
114 landmine_scripts = process_options()
[email protected]07e55632014-02-15 05:23:29115
[email protected]a403a3d2014-04-12 01:13:21116 if landmine_utils.builder() in ('dump_dependency_json', 'eclipse'):
[email protected]07e55632014-02-15 05:23:29117 return 0
[email protected]abab8d72012-11-14 04:59:48118
[email protected]9372bec2014-08-14 14:03:30119 gyp_environment.SetEnvironment()
120
121 landmines = []
122 for s in landmine_scripts:
123 proc = subprocess.Popen([sys.executable, s], stdout=subprocess.PIPE)
124 output, _ = proc.communicate()
125 landmines.extend([('%s\n' % l.strip()) for l in output.splitlines()])
126 clobber_if_necessary(landmines)
[email protected]abab8d72012-11-14 04:59:48127
128 return 0
129
130
131if __name__ == '__main__':
[email protected]c45227b2012-11-15 02:53:03132 sys.exit(main())