blob: b62e75851cb4e74581440943a36dd9631fc772f1 [file] [log] [blame]
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +09001#!/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"""
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +09007This 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.
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +09009
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
smut@google.come121fc22014-04-23 13:19:23 +090016import errno
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +090017import gyp_environment
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090018import logging
19import optparse
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +090020import os
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +090021import shutil
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +090022import sys
sivachandra@chromium.org54da9682013-08-21 11:44:58 +090023import subprocess
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +090024import time
25
sivachandra@chromium.org54da9682013-08-21 11:44:58 +090026import landmine_utils
27
28
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +090029SRC_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
30
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +090031
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +090032def get_build_dir(build_tool, is_iphone=False):
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +090033 """
34 Returns output directory absolute path dependent on build and targets.
35 Examples:
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +090036 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'
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +090039
40 Keep this function in sync with tools/build/scripts/slave/compile.py
41 """
42 ret = None
43 if build_tool == 'xcode':
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +090044 ret = os.path.join(SRC_DIR, 'xcodebuild')
thakis@chromium.orgcf894a72013-05-12 04:13:21 +090045 elif build_tool in ['make', 'ninja', 'ninja-ios']: # TODO: Remove ninja-ios.
scottmg@chromium.org83040822014-08-19 05:37:35 +090046 ret = os.path.join(SRC_DIR, os.environ.get('CHROMIUM_OUT_DIR', 'out'))
iannucci@chromium.orgf2710aa2012-11-15 10:52:07 +090047 elif build_tool in ['msvs', 'vs', 'ib']:
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +090048 ret = os.path.join(SRC_DIR, 'build')
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +090049 else:
scottmg@chromium.org39465182013-01-18 08:50:02 +090050 raise NotImplementedError('Unexpected GYP_GENERATORS (%s)' % build_tool)
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +090051 return os.path.abspath(ret)
52
53
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +090054def clobber_if_necessary(new_landmines):
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090055 """Does the work of setting, planting, and triggering landmines."""
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +090056 out_dir = get_build_dir(landmine_utils.builder())
57 landmines_path = os.path.normpath(os.path.join(out_dir, '..', '.landmines'))
smut@google.come121fc22014-04-23 13:19:23 +090058 try:
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090059 os.makedirs(out_dir)
smut@google.come121fc22014-04-23 13:19:23 +090060 except OSError as e:
61 if e.errno == errno.EEXIST:
62 pass
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090063
scottmg@chromium.org4bf07be2014-05-23 07:12:48 +090064 if os.path.exists(landmines_path):
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090065 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)
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +090072 sys.stdout.write('Clobbering due to:\n')
73 sys.stdout.writelines(diff)
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090074
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +090075 # Clobber.
76 shutil.rmtree(out_dir)
77
78 # Save current set of landmines for next time.
scottmg@chromium.org4bf07be2014-05-23 07:12:48 +090079 with open(landmines_path, 'w') as f:
80 f.writelines(new_landmines)
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090081
82
tkent@chromium.org05c93a72013-09-03 06:51:18 +090083def process_options():
84 """Returns a list of landmine emitting scripts."""
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090085 parser = optparse.OptionParser()
sivachandra@chromium.org54da9682013-08-21 11:44:58 +090086 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 '
tkent@chromium.org05c93a72013-09-03 06:51:18 +090090 'is passed to this script via option -t. Note that an extra '
91 'script can be specified via an env var EXTRA_LANDMINES_SCRIPT.')
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090092 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.'))
sivachandra@chromium.org54da9682013-08-21 11:44:58 +090097
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090098 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)
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +0900105
tkent@chromium.org05c93a72013-09-03 06:51:18 +0900106 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()
scottmg@chromium.org87ca2792014-02-15 14:23:29 +0900115
newt@chromium.org776db5e2014-04-12 10:13:21 +0900116 if landmine_utils.builder() in ('dump_dependency_json', 'eclipse'):
scottmg@chromium.org87ca2792014-02-15 14:23:29 +0900117 return 0
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +0900118
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +0900119 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)
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +0900127
128 return 0
129
130
131if __name__ == '__main__':
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +0900132 sys.exit(main())