blob: 4db23807cca32ff7c0dd030c434dbcdd7fe44d60 [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
scherkusda2b5522014-09-04 06:28:23 +09008the build should be clobbered, it will delete the contents of the build
9directory.
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +090010
11A landmine is tripped when a builder checks out a different revision, and the
12diff between the new landmines and the old ones is non-null. At this point, the
13build is clobbered.
14"""
15
16import difflib
smut@google.come121fc22014-04-23 13:19:23 +090017import errno
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +090018import gyp_environment
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090019import logging
20import optparse
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +090021import os
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
petrcermak51dbc392015-06-22 21:41:49 +090026import clobber
sivachandra@chromium.org54da9682013-08-21 11:44:58 +090027import landmine_utils
28
29
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +090030SRC_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
31
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +090032
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +090033def get_build_dir(build_tool, is_iphone=False):
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +090034 """
35 Returns output directory absolute path dependent on build and targets.
36 Examples:
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +090037 r'c:\b\build\slave\win\build\src\out'
38 '/mnt/data/b/build/slave/linux/build/src/out'
39 '/b/build/slave/ios_rel_device/build/src/xcodebuild'
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +090040
41 Keep this function in sync with tools/build/scripts/slave/compile.py
42 """
43 ret = None
44 if build_tool == 'xcode':
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +090045 ret = os.path.join(SRC_DIR, 'xcodebuild')
thakis@chromium.orgcf894a72013-05-12 04:13:21 +090046 elif build_tool in ['make', 'ninja', 'ninja-ios']: # TODO: Remove ninja-ios.
johnme387807d2015-01-14 06:28:41 +090047 if 'CHROMIUM_OUT_DIR' in os.environ:
48 output_dir = os.environ.get('CHROMIUM_OUT_DIR').strip()
49 if not output_dir:
50 raise Error('CHROMIUM_OUT_DIR environment variable is set but blank!')
oetuaho0c7f69f2014-11-03 18:09:53 +090051 else:
johnme387807d2015-01-14 06:28:41 +090052 output_dir = landmine_utils.gyp_generator_flags().get('output_dir', 'out')
oetuaho0c7f69f2014-11-03 18:09:53 +090053 ret = os.path.join(SRC_DIR, output_dir)
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +090054 else:
scottmg@chromium.org39465182013-01-18 08:50:02 +090055 raise NotImplementedError('Unexpected GYP_GENERATORS (%s)' % build_tool)
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +090056 return os.path.abspath(ret)
57
58
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +090059def clobber_if_necessary(new_landmines):
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090060 """Does the work of setting, planting, and triggering landmines."""
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +090061 out_dir = get_build_dir(landmine_utils.builder())
62 landmines_path = os.path.normpath(os.path.join(out_dir, '..', '.landmines'))
smut@google.come121fc22014-04-23 13:19:23 +090063 try:
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090064 os.makedirs(out_dir)
smut@google.come121fc22014-04-23 13:19:23 +090065 except OSError as e:
66 if e.errno == errno.EEXIST:
67 pass
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090068
scottmg@chromium.org4bf07be2014-05-23 07:12:48 +090069 if os.path.exists(landmines_path):
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090070 with open(landmines_path, 'r') as f:
71 old_landmines = f.readlines()
72 if old_landmines != new_landmines:
73 old_date = time.ctime(os.stat(landmines_path).st_ctime)
74 diff = difflib.unified_diff(old_landmines, new_landmines,
75 fromfile='old_landmines', tofile='new_landmines',
76 fromfiledate=old_date, tofiledate=time.ctime(), n=0)
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +090077 sys.stdout.write('Clobbering due to:\n')
78 sys.stdout.writelines(diff)
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090079
petrcermak51dbc392015-06-22 21:41:49 +090080 clobber.clobber(out_dir)
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +090081
82 # Save current set of landmines for next time.
scottmg@chromium.org4bf07be2014-05-23 07:12:48 +090083 with open(landmines_path, 'w') as f:
84 f.writelines(new_landmines)
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090085
86
tkent@chromium.org05c93a72013-09-03 06:51:18 +090087def process_options():
88 """Returns a list of landmine emitting scripts."""
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090089 parser = optparse.OptionParser()
sivachandra@chromium.org54da9682013-08-21 11:44:58 +090090 parser.add_option(
91 '-s', '--landmine-scripts', action='append',
92 default=[os.path.join(SRC_DIR, 'build', 'get_landmines.py')],
93 help='Path to the script which emits landmines to stdout. The target '
tkent@chromium.org05c93a72013-09-03 06:51:18 +090094 'is passed to this script via option -t. Note that an extra '
95 'script can be specified via an env var EXTRA_LANDMINES_SCRIPT.')
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090096 parser.add_option('-v', '--verbose', action='store_true',
97 default=('LANDMINES_VERBOSE' in os.environ),
98 help=('Emit some extra debugging information (default off). This option '
99 'is also enabled by the presence of a LANDMINES_VERBOSE environment '
100 'variable.'))
sivachandra@chromium.org54da9682013-08-21 11:44:58 +0900101
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +0900102 options, args = parser.parse_args()
103
104 if args:
105 parser.error('Unknown arguments %s' % args)
106
107 logging.basicConfig(
108 level=logging.DEBUG if options.verbose else logging.ERROR)
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +0900109
tkent@chromium.org05c93a72013-09-03 06:51:18 +0900110 extra_script = os.environ.get('EXTRA_LANDMINES_SCRIPT')
111 if extra_script:
112 return options.landmine_scripts + [extra_script]
113 else:
114 return options.landmine_scripts
115
116
117def main():
118 landmine_scripts = process_options()
scottmg@chromium.org87ca2792014-02-15 14:23:29 +0900119
newt@chromium.org776db5e2014-04-12 10:13:21 +0900120 if landmine_utils.builder() in ('dump_dependency_json', 'eclipse'):
scottmg@chromium.org87ca2792014-02-15 14:23:29 +0900121 return 0
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +0900122
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +0900123 gyp_environment.SetEnvironment()
124
125 landmines = []
126 for s in landmine_scripts:
127 proc = subprocess.Popen([sys.executable, s], stdout=subprocess.PIPE)
128 output, _ = proc.communicate()
129 landmines.extend([('%s\n' % l.strip()) for l in output.splitlines()])
130 clobber_if_necessary(landmines)
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +0900131
132 return 0
133
134
135if __name__ == '__main__':
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +0900136 sys.exit(main())