blob: 1b2f1cdbb08965adbdf0be2ad7e1bbb16b83b99a [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
scherkusbb04e1052014-09-03 21:28:238the build should be clobbered, it will delete the contents of the build
9directory.
[email protected]abab8d72012-11-14 04:59:4810
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
[email protected]d99e6bf2014-04-23 04:19:2317import errno
[email protected]9372bec2014-08-14 14:03:3018import gyp_environment
[email protected]c45227b2012-11-15 02:53:0319import logging
20import optparse
[email protected]abab8d72012-11-14 04:59:4821import os
[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
petrcermakaf7f4c02015-06-22 12:41:4926import clobber
[email protected]ec069f72013-08-21 02:44:5827import landmine_utils
28
29
thakis08e98782016-07-30 00:35:2430def get_build_dir(src_dir):
[email protected]abab8d72012-11-14 04:59:4831 """
32 Returns output directory absolute path dependent on build and targets.
33 Examples:
[email protected]9372bec2014-08-14 14:03:3034 r'c:\b\build\slave\win\build\src\out'
35 '/mnt/data/b/build/slave/linux/build/src/out'
thakis08e98782016-07-30 00:35:2436 '/b/build/slave/ios_rel_device/build/src/out'
[email protected]abab8d72012-11-14 04:59:4837
38 Keep this function in sync with tools/build/scripts/slave/compile.py
39 """
thakis08e98782016-07-30 00:35:2440 if 'CHROMIUM_OUT_DIR' in os.environ:
41 output_dir = os.environ.get('CHROMIUM_OUT_DIR').strip()
42 if not output_dir:
43 raise Error('CHROMIUM_OUT_DIR environment variable is set but blank!')
[email protected]abab8d72012-11-14 04:59:4844 else:
thakis08e98782016-07-30 00:35:2445 output_dir = landmine_utils.gyp_generator_flags().get('output_dir', 'out')
46 return os.path.abspath(os.path.join(src_dir, output_dir))
[email protected]abab8d72012-11-14 04:59:4847
48
kjellander20b29152015-10-14 19:28:0649def clobber_if_necessary(new_landmines, src_dir):
[email protected]c45227b2012-11-15 02:53:0350 """Does the work of setting, planting, and triggering landmines."""
thakis08e98782016-07-30 00:35:2451 out_dir = get_build_dir(src_dir)
kjellander20b29152015-10-14 19:28:0652 landmines_path = os.path.normpath(os.path.join(src_dir, '.landmines'))
[email protected]d99e6bf2014-04-23 04:19:2353 try:
[email protected]c45227b2012-11-15 02:53:0354 os.makedirs(out_dir)
[email protected]d99e6bf2014-04-23 04:19:2355 except OSError as e:
56 if e.errno == errno.EEXIST:
57 pass
[email protected]c45227b2012-11-15 02:53:0358
[email protected]f3d0d1c2014-05-22 22:12:4859 if os.path.exists(landmines_path):
[email protected]c45227b2012-11-15 02:53:0360 with open(landmines_path, 'r') as f:
61 old_landmines = f.readlines()
62 if old_landmines != new_landmines:
63 old_date = time.ctime(os.stat(landmines_path).st_ctime)
64 diff = difflib.unified_diff(old_landmines, new_landmines,
65 fromfile='old_landmines', tofile='new_landmines',
66 fromfiledate=old_date, tofiledate=time.ctime(), n=0)
[email protected]9372bec2014-08-14 14:03:3067 sys.stdout.write('Clobbering due to:\n')
68 sys.stdout.writelines(diff)
thakisd7d6455c2016-05-03 18:00:0569 sys.stdout.flush()
[email protected]c45227b2012-11-15 02:53:0370
petrcermakaf7f4c02015-06-22 12:41:4971 clobber.clobber(out_dir)
[email protected]9372bec2014-08-14 14:03:3072
73 # Save current set of landmines for next time.
[email protected]f3d0d1c2014-05-22 22:12:4874 with open(landmines_path, 'w') as f:
75 f.writelines(new_landmines)
[email protected]c45227b2012-11-15 02:53:0376
77
[email protected]c36d62932013-09-02 21:51:1878def process_options():
kjellander20b29152015-10-14 19:28:0679 """Returns an options object containing the configuration for this script."""
[email protected]c45227b2012-11-15 02:53:0380 parser = optparse.OptionParser()
[email protected]ec069f72013-08-21 02:44:5881 parser.add_option(
82 '-s', '--landmine-scripts', action='append',
[email protected]ec069f72013-08-21 02:44:5883 help='Path to the script which emits landmines to stdout. The target '
[email protected]c36d62932013-09-02 21:51:1884 'is passed to this script via option -t. Note that an extra '
85 'script can be specified via an env var EXTRA_LANDMINES_SCRIPT.')
kjellander20b29152015-10-14 19:28:0686 parser.add_option('-d', '--src-dir',
87 help='Path of the source root dir. Overrides the default location of the '
88 'source root dir when calculating the build directory.')
[email protected]c45227b2012-11-15 02:53:0389 parser.add_option('-v', '--verbose', action='store_true',
90 default=('LANDMINES_VERBOSE' in os.environ),
91 help=('Emit some extra debugging information (default off). This option '
92 'is also enabled by the presence of a LANDMINES_VERBOSE environment '
93 'variable.'))
[email protected]ec069f72013-08-21 02:44:5894
[email protected]c45227b2012-11-15 02:53:0395 options, args = parser.parse_args()
96
97 if args:
98 parser.error('Unknown arguments %s' % args)
99
100 logging.basicConfig(
101 level=logging.DEBUG if options.verbose else logging.ERROR)
[email protected]abab8d72012-11-14 04:59:48102
kjellander20b29152015-10-14 19:28:06103 if options.src_dir:
104 if not os.path.isdir(options.src_dir):
105 parser.error('Cannot find source root dir at %s' % options.src_dir)
106 logging.debug('Overriding source root dir. Using: %s', options.src_dir)
107 else:
108 options.src_dir = \
109 os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
110
111 if not options.landmine_scripts:
112 options.landmine_scripts = [os.path.join(options.src_dir, 'build',
113 'get_landmines.py')]
114
[email protected]c36d62932013-09-02 21:51:18115 extra_script = os.environ.get('EXTRA_LANDMINES_SCRIPT')
116 if extra_script:
kjellander20b29152015-10-14 19:28:06117 options.landmine_scripts += [extra_script]
118
119 return options
[email protected]c36d62932013-09-02 21:51:18120
121
122def main():
kjellander20b29152015-10-14 19:28:06123 options = process_options()
[email protected]07e55632014-02-15 05:23:29124
[email protected]9372bec2014-08-14 14:03:30125 gyp_environment.SetEnvironment()
126
127 landmines = []
kjellander20b29152015-10-14 19:28:06128 for s in options.landmine_scripts:
[email protected]9372bec2014-08-14 14:03:30129 proc = subprocess.Popen([sys.executable, s], stdout=subprocess.PIPE)
130 output, _ = proc.communicate()
131 landmines.extend([('%s\n' % l.strip()) for l in output.splitlines()])
kjellander20b29152015-10-14 19:28:06132 clobber_if_necessary(landmines, options.src_dir)
[email protected]abab8d72012-11-14 04:59:48133
134 return 0
135
136
137if __name__ == '__main__':
[email protected]c45227b2012-11-15 02:53:03138 sys.exit(main())