[email protected] | abab8d7 | 2012-11-14 04:59:48 | [diff] [blame] | 1 | #!/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] | 9372bec | 2014-08-14 14:03:30 | [diff] [blame] | 7 | This script runs every build as the first hook (See DEPS). If it detects that |
scherkus | bb04e105 | 2014-09-03 21:28:23 | [diff] [blame] | 8 | the build should be clobbered, it will delete the contents of the build |
| 9 | directory. |
[email protected] | abab8d7 | 2012-11-14 04:59:48 | [diff] [blame] | 10 | |
| 11 | A landmine is tripped when a builder checks out a different revision, and the |
| 12 | diff between the new landmines and the old ones is non-null. At this point, the |
| 13 | build is clobbered. |
| 14 | """ |
| 15 | |
| 16 | import difflib |
[email protected] | d99e6bf | 2014-04-23 04:19:23 | [diff] [blame] | 17 | import errno |
[email protected] | 9372bec | 2014-08-14 14:03:30 | [diff] [blame] | 18 | import gyp_environment |
[email protected] | c45227b | 2012-11-15 02:53:03 | [diff] [blame] | 19 | import logging |
| 20 | import optparse |
[email protected] | abab8d7 | 2012-11-14 04:59:48 | [diff] [blame] | 21 | import os |
[email protected] | abab8d7 | 2012-11-14 04:59:48 | [diff] [blame] | 22 | import sys |
[email protected] | ec069f7 | 2013-08-21 02:44:58 | [diff] [blame] | 23 | import subprocess |
[email protected] | abab8d7 | 2012-11-14 04:59:48 | [diff] [blame] | 24 | import time |
| 25 | |
petrcermak | af7f4c0 | 2015-06-22 12:41:49 | [diff] [blame] | 26 | import clobber |
[email protected] | ec069f7 | 2013-08-21 02:44:58 | [diff] [blame] | 27 | import landmine_utils |
| 28 | |
| 29 | |
thakis | 08e9878 | 2016-07-30 00:35:24 | [diff] [blame] | 30 | def get_build_dir(src_dir): |
[email protected] | abab8d7 | 2012-11-14 04:59:48 | [diff] [blame] | 31 | """ |
| 32 | Returns output directory absolute path dependent on build and targets. |
| 33 | Examples: |
[email protected] | 9372bec | 2014-08-14 14:03:30 | [diff] [blame] | 34 | r'c:\b\build\slave\win\build\src\out' |
| 35 | '/mnt/data/b/build/slave/linux/build/src/out' |
thakis | 08e9878 | 2016-07-30 00:35:24 | [diff] [blame] | 36 | '/b/build/slave/ios_rel_device/build/src/out' |
[email protected] | abab8d7 | 2012-11-14 04:59:48 | [diff] [blame] | 37 | |
| 38 | Keep this function in sync with tools/build/scripts/slave/compile.py |
| 39 | """ |
thakis | 08e9878 | 2016-07-30 00:35:24 | [diff] [blame] | 40 | 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] | abab8d7 | 2012-11-14 04:59:48 | [diff] [blame] | 44 | else: |
thakis | 08e9878 | 2016-07-30 00:35:24 | [diff] [blame] | 45 | 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] | abab8d7 | 2012-11-14 04:59:48 | [diff] [blame] | 47 | |
| 48 | |
kjellander | 20b2915 | 2015-10-14 19:28:06 | [diff] [blame] | 49 | def clobber_if_necessary(new_landmines, src_dir): |
[email protected] | c45227b | 2012-11-15 02:53:03 | [diff] [blame] | 50 | """Does the work of setting, planting, and triggering landmines.""" |
thakis | 08e9878 | 2016-07-30 00:35:24 | [diff] [blame] | 51 | out_dir = get_build_dir(src_dir) |
kjellander | 20b2915 | 2015-10-14 19:28:06 | [diff] [blame] | 52 | landmines_path = os.path.normpath(os.path.join(src_dir, '.landmines')) |
[email protected] | d99e6bf | 2014-04-23 04:19:23 | [diff] [blame] | 53 | try: |
[email protected] | c45227b | 2012-11-15 02:53:03 | [diff] [blame] | 54 | os.makedirs(out_dir) |
[email protected] | d99e6bf | 2014-04-23 04:19:23 | [diff] [blame] | 55 | except OSError as e: |
| 56 | if e.errno == errno.EEXIST: |
| 57 | pass |
[email protected] | c45227b | 2012-11-15 02:53:03 | [diff] [blame] | 58 | |
[email protected] | f3d0d1c | 2014-05-22 22:12:48 | [diff] [blame] | 59 | if os.path.exists(landmines_path): |
[email protected] | c45227b | 2012-11-15 02:53:03 | [diff] [blame] | 60 | 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] | 9372bec | 2014-08-14 14:03:30 | [diff] [blame] | 67 | sys.stdout.write('Clobbering due to:\n') |
| 68 | sys.stdout.writelines(diff) |
thakis | d7d6455c | 2016-05-03 18:00:05 | [diff] [blame] | 69 | sys.stdout.flush() |
[email protected] | c45227b | 2012-11-15 02:53:03 | [diff] [blame] | 70 | |
petrcermak | af7f4c0 | 2015-06-22 12:41:49 | [diff] [blame] | 71 | clobber.clobber(out_dir) |
[email protected] | 9372bec | 2014-08-14 14:03:30 | [diff] [blame] | 72 | |
| 73 | # Save current set of landmines for next time. |
[email protected] | f3d0d1c | 2014-05-22 22:12:48 | [diff] [blame] | 74 | with open(landmines_path, 'w') as f: |
| 75 | f.writelines(new_landmines) |
[email protected] | c45227b | 2012-11-15 02:53:03 | [diff] [blame] | 76 | |
| 77 | |
[email protected] | c36d6293 | 2013-09-02 21:51:18 | [diff] [blame] | 78 | def process_options(): |
kjellander | 20b2915 | 2015-10-14 19:28:06 | [diff] [blame] | 79 | """Returns an options object containing the configuration for this script.""" |
[email protected] | c45227b | 2012-11-15 02:53:03 | [diff] [blame] | 80 | parser = optparse.OptionParser() |
[email protected] | ec069f7 | 2013-08-21 02:44:58 | [diff] [blame] | 81 | parser.add_option( |
| 82 | '-s', '--landmine-scripts', action='append', |
[email protected] | ec069f7 | 2013-08-21 02:44:58 | [diff] [blame] | 83 | help='Path to the script which emits landmines to stdout. The target ' |
[email protected] | c36d6293 | 2013-09-02 21:51:18 | [diff] [blame] | 84 | 'is passed to this script via option -t. Note that an extra ' |
| 85 | 'script can be specified via an env var EXTRA_LANDMINES_SCRIPT.') |
kjellander | 20b2915 | 2015-10-14 19:28:06 | [diff] [blame] | 86 | 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] | c45227b | 2012-11-15 02:53:03 | [diff] [blame] | 89 | 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] | ec069f7 | 2013-08-21 02:44:58 | [diff] [blame] | 94 | |
[email protected] | c45227b | 2012-11-15 02:53:03 | [diff] [blame] | 95 | 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] | abab8d7 | 2012-11-14 04:59:48 | [diff] [blame] | 102 | |
kjellander | 20b2915 | 2015-10-14 19:28:06 | [diff] [blame] | 103 | 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] | c36d6293 | 2013-09-02 21:51:18 | [diff] [blame] | 115 | extra_script = os.environ.get('EXTRA_LANDMINES_SCRIPT') |
| 116 | if extra_script: |
kjellander | 20b2915 | 2015-10-14 19:28:06 | [diff] [blame] | 117 | options.landmine_scripts += [extra_script] |
| 118 | |
| 119 | return options |
[email protected] | c36d6293 | 2013-09-02 21:51:18 | [diff] [blame] | 120 | |
| 121 | |
| 122 | def main(): |
kjellander | 20b2915 | 2015-10-14 19:28:06 | [diff] [blame] | 123 | options = process_options() |
[email protected] | 07e5563 | 2014-02-15 05:23:29 | [diff] [blame] | 124 | |
[email protected] | 9372bec | 2014-08-14 14:03:30 | [diff] [blame] | 125 | gyp_environment.SetEnvironment() |
| 126 | |
| 127 | landmines = [] |
kjellander | 20b2915 | 2015-10-14 19:28:06 | [diff] [blame] | 128 | for s in options.landmine_scripts: |
[email protected] | 9372bec | 2014-08-14 14:03:30 | [diff] [blame] | 129 | 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()]) |
kjellander | 20b2915 | 2015-10-14 19:28:06 | [diff] [blame] | 132 | clobber_if_necessary(landmines, options.src_dir) |
[email protected] | abab8d7 | 2012-11-14 04:59:48 | [diff] [blame] | 133 | |
| 134 | return 0 |
| 135 | |
| 136 | |
| 137 | if __name__ == '__main__': |
[email protected] | c45227b | 2012-11-15 02:53:03 | [diff] [blame] | 138 | sys.exit(main()) |