blob: e9da6ba83c5f337316c0157b455dd106501e9829 [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
kjellander96497fd2015-10-15 04:28:06 +090030def get_build_dir(build_tool, src_dir, is_iphone=False):
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +090031 """
32 Returns output directory absolute path dependent on build and targets.
33 Examples:
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +090034 r'c:\b\build\slave\win\build\src\out'
35 '/mnt/data/b/build/slave/linux/build/src/out'
36 '/b/build/slave/ios_rel_device/build/src/xcodebuild'
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +090037
38 Keep this function in sync with tools/build/scripts/slave/compile.py
39 """
40 ret = None
41 if build_tool == 'xcode':
kjellander96497fd2015-10-15 04:28:06 +090042 ret = os.path.join(src_dir, 'xcodebuild')
thakis@chromium.orgcf894a72013-05-12 04:13:21 +090043 elif build_tool in ['make', 'ninja', 'ninja-ios']: # TODO: Remove ninja-ios.
johnme387807d2015-01-14 06:28:41 +090044 if 'CHROMIUM_OUT_DIR' in os.environ:
45 output_dir = os.environ.get('CHROMIUM_OUT_DIR').strip()
46 if not output_dir:
47 raise Error('CHROMIUM_OUT_DIR environment variable is set but blank!')
oetuaho0c7f69f2014-11-03 18:09:53 +090048 else:
johnme387807d2015-01-14 06:28:41 +090049 output_dir = landmine_utils.gyp_generator_flags().get('output_dir', 'out')
kjellander96497fd2015-10-15 04:28:06 +090050 ret = os.path.join(src_dir, output_dir)
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +090051 else:
scottmg@chromium.org39465182013-01-18 08:50:02 +090052 raise NotImplementedError('Unexpected GYP_GENERATORS (%s)' % build_tool)
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +090053 return os.path.abspath(ret)
54
55
kjellander96497fd2015-10-15 04:28:06 +090056def clobber_if_necessary(new_landmines, src_dir):
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090057 """Does the work of setting, planting, and triggering landmines."""
kjellander96497fd2015-10-15 04:28:06 +090058 out_dir = get_build_dir(landmine_utils.builder(), src_dir)
59 landmines_path = os.path.normpath(os.path.join(src_dir, '.landmines'))
smut@google.come121fc22014-04-23 13:19:23 +090060 try:
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090061 os.makedirs(out_dir)
smut@google.come121fc22014-04-23 13:19:23 +090062 except OSError as e:
63 if e.errno == errno.EEXIST:
64 pass
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090065
scottmg@chromium.org4bf07be2014-05-23 07:12:48 +090066 if os.path.exists(landmines_path):
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090067 with open(landmines_path, 'r') as f:
68 old_landmines = f.readlines()
69 if old_landmines != new_landmines:
70 old_date = time.ctime(os.stat(landmines_path).st_ctime)
71 diff = difflib.unified_diff(old_landmines, new_landmines,
72 fromfile='old_landmines', tofile='new_landmines',
73 fromfiledate=old_date, tofiledate=time.ctime(), n=0)
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +090074 sys.stdout.write('Clobbering due to:\n')
75 sys.stdout.writelines(diff)
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090076
petrcermak51dbc392015-06-22 21:41:49 +090077 clobber.clobber(out_dir)
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +090078
79 # Save current set of landmines for next time.
scottmg@chromium.org4bf07be2014-05-23 07:12:48 +090080 with open(landmines_path, 'w') as f:
81 f.writelines(new_landmines)
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090082
83
tkent@chromium.org05c93a72013-09-03 06:51:18 +090084def process_options():
kjellander96497fd2015-10-15 04:28:06 +090085 """Returns an options object containing the configuration for this script."""
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090086 parser = optparse.OptionParser()
sivachandra@chromium.org54da9682013-08-21 11:44:58 +090087 parser.add_option(
88 '-s', '--landmine-scripts', action='append',
sivachandra@chromium.org54da9682013-08-21 11:44:58 +090089 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.')
kjellander96497fd2015-10-15 04:28:06 +090092 parser.add_option('-d', '--src-dir',
93 help='Path of the source root dir. Overrides the default location of the '
94 'source root dir when calculating the build directory.')
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +090095 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.'))
sivachandra@chromium.org54da9682013-08-21 11:44:58 +0900100
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +0900101 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)
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +0900108
kjellander96497fd2015-10-15 04:28:06 +0900109 if options.src_dir:
110 if not os.path.isdir(options.src_dir):
111 parser.error('Cannot find source root dir at %s' % options.src_dir)
112 logging.debug('Overriding source root dir. Using: %s', options.src_dir)
113 else:
114 options.src_dir = \
115 os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
116
117 if not options.landmine_scripts:
118 options.landmine_scripts = [os.path.join(options.src_dir, 'build',
119 'get_landmines.py')]
120
tkent@chromium.org05c93a72013-09-03 06:51:18 +0900121 extra_script = os.environ.get('EXTRA_LANDMINES_SCRIPT')
122 if extra_script:
kjellander96497fd2015-10-15 04:28:06 +0900123 options.landmine_scripts += [extra_script]
124
125 return options
tkent@chromium.org05c93a72013-09-03 06:51:18 +0900126
127
128def main():
kjellander96497fd2015-10-15 04:28:06 +0900129 options = process_options()
scottmg@chromium.org87ca2792014-02-15 14:23:29 +0900130
newt@chromium.org776db5e2014-04-12 10:13:21 +0900131 if landmine_utils.builder() in ('dump_dependency_json', 'eclipse'):
scottmg@chromium.org87ca2792014-02-15 14:23:29 +0900132 return 0
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +0900133
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +0900134 gyp_environment.SetEnvironment()
135
136 landmines = []
kjellander96497fd2015-10-15 04:28:06 +0900137 for s in options.landmine_scripts:
scottmg@chromium.org2f8c83b2014-08-14 23:03:30 +0900138 proc = subprocess.Popen([sys.executable, s], stdout=subprocess.PIPE)
139 output, _ = proc.communicate()
140 landmines.extend([('%s\n' % l.strip()) for l in output.splitlines()])
kjellander96497fd2015-10-15 04:28:06 +0900141 clobber_if_necessary(landmines, options.src_dir)
iannucci@chromium.orgdccbcf12012-11-14 13:59:48 +0900142
143 return 0
144
145
146if __name__ == '__main__':
iannucci@chromium.orgb48a6382012-11-15 11:53:03 +0900147 sys.exit(main())