iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [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 | """ |
scottmg@chromium.org | 2f8c83b | 2014-08-14 23:03:30 +0900 | [diff] [blame] | 7 | This script runs every build as the first hook (See DEPS). If it detects that |
scherkus | da2b552 | 2014-09-04 06:28:23 +0900 | [diff] [blame] | 8 | the build should be clobbered, it will delete the contents of the build |
| 9 | directory. |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [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 |
smut@google.com | e121fc2 | 2014-04-23 13:19:23 +0900 | [diff] [blame] | 17 | import errno |
scottmg@chromium.org | 2f8c83b | 2014-08-14 23:03:30 +0900 | [diff] [blame] | 18 | import gyp_environment |
iannucci@chromium.org | b48a638 | 2012-11-15 11:53:03 +0900 | [diff] [blame] | 19 | import logging |
| 20 | import optparse |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 21 | import os |
scottmg@chromium.org | 2f8c83b | 2014-08-14 23:03:30 +0900 | [diff] [blame] | 22 | import shutil |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 23 | import sys |
sivachandra@chromium.org | 54da968 | 2013-08-21 11:44:58 +0900 | [diff] [blame] | 24 | import subprocess |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 25 | import time |
| 26 | |
sivachandra@chromium.org | 54da968 | 2013-08-21 11:44:58 +0900 | [diff] [blame] | 27 | import landmine_utils |
| 28 | |
| 29 | |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 30 | SRC_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| 31 | |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 32 | |
scottmg@chromium.org | 2f8c83b | 2014-08-14 23:03:30 +0900 | [diff] [blame] | 33 | def get_build_dir(build_tool, is_iphone=False): |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 34 | """ |
| 35 | Returns output directory absolute path dependent on build and targets. |
| 36 | Examples: |
scottmg@chromium.org | 2f8c83b | 2014-08-14 23:03:30 +0900 | [diff] [blame] | 37 | 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.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 40 | |
| 41 | Keep this function in sync with tools/build/scripts/slave/compile.py |
| 42 | """ |
| 43 | ret = None |
| 44 | if build_tool == 'xcode': |
scottmg@chromium.org | 2f8c83b | 2014-08-14 23:03:30 +0900 | [diff] [blame] | 45 | ret = os.path.join(SRC_DIR, 'xcodebuild') |
thakis@chromium.org | cf894a7 | 2013-05-12 04:13:21 +0900 | [diff] [blame] | 46 | elif build_tool in ['make', 'ninja', 'ninja-ios']: # TODO: Remove ninja-ios. |
oetuaho | 0c7f69f | 2014-11-03 18:09:53 +0900 | [diff] [blame^] | 47 | if ('CHROMIUM_OUT_DIR' not in os.environ and |
| 48 | 'output_dir' in landmine_utils.gyp_generator_flags()): |
| 49 | output_dir = landmine_utils.gyp_generator_flags()['output_dir'] |
| 50 | else: |
| 51 | output_dir = os.environ.get('CHROMIUM_OUT_DIR', 'out') |
| 52 | ret = os.path.join(SRC_DIR, output_dir) |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 53 | else: |
scottmg@chromium.org | 3946518 | 2013-01-18 08:50:02 +0900 | [diff] [blame] | 54 | raise NotImplementedError('Unexpected GYP_GENERATORS (%s)' % build_tool) |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 55 | return os.path.abspath(ret) |
| 56 | |
| 57 | |
scottmg@chromium.org | 2f8c83b | 2014-08-14 23:03:30 +0900 | [diff] [blame] | 58 | def clobber_if_necessary(new_landmines): |
iannucci@chromium.org | b48a638 | 2012-11-15 11:53:03 +0900 | [diff] [blame] | 59 | """Does the work of setting, planting, and triggering landmines.""" |
scottmg@chromium.org | 2f8c83b | 2014-08-14 23:03:30 +0900 | [diff] [blame] | 60 | out_dir = get_build_dir(landmine_utils.builder()) |
| 61 | landmines_path = os.path.normpath(os.path.join(out_dir, '..', '.landmines')) |
smut@google.com | e121fc2 | 2014-04-23 13:19:23 +0900 | [diff] [blame] | 62 | try: |
iannucci@chromium.org | b48a638 | 2012-11-15 11:53:03 +0900 | [diff] [blame] | 63 | os.makedirs(out_dir) |
smut@google.com | e121fc2 | 2014-04-23 13:19:23 +0900 | [diff] [blame] | 64 | except OSError as e: |
| 65 | if e.errno == errno.EEXIST: |
| 66 | pass |
iannucci@chromium.org | b48a638 | 2012-11-15 11:53:03 +0900 | [diff] [blame] | 67 | |
scottmg@chromium.org | 4bf07be | 2014-05-23 07:12:48 +0900 | [diff] [blame] | 68 | if os.path.exists(landmines_path): |
iannucci@chromium.org | b48a638 | 2012-11-15 11:53:03 +0900 | [diff] [blame] | 69 | with open(landmines_path, 'r') as f: |
| 70 | old_landmines = f.readlines() |
| 71 | if old_landmines != new_landmines: |
| 72 | old_date = time.ctime(os.stat(landmines_path).st_ctime) |
| 73 | diff = difflib.unified_diff(old_landmines, new_landmines, |
| 74 | fromfile='old_landmines', tofile='new_landmines', |
| 75 | fromfiledate=old_date, tofiledate=time.ctime(), n=0) |
scottmg@chromium.org | 2f8c83b | 2014-08-14 23:03:30 +0900 | [diff] [blame] | 76 | sys.stdout.write('Clobbering due to:\n') |
| 77 | sys.stdout.writelines(diff) |
iannucci@chromium.org | b48a638 | 2012-11-15 11:53:03 +0900 | [diff] [blame] | 78 | |
scherkus | da2b552 | 2014-09-04 06:28:23 +0900 | [diff] [blame] | 79 | # Clobber contents of build directory but not directory itself: some |
| 80 | # checkouts have the build directory mounted. |
| 81 | for f in os.listdir(out_dir): |
| 82 | path = os.path.join(out_dir, f) |
| 83 | if os.path.isfile(path): |
| 84 | os.unlink(path) |
| 85 | elif os.path.isdir(path): |
| 86 | shutil.rmtree(path) |
scottmg@chromium.org | 2f8c83b | 2014-08-14 23:03:30 +0900 | [diff] [blame] | 87 | |
| 88 | # Save current set of landmines for next time. |
scottmg@chromium.org | 4bf07be | 2014-05-23 07:12:48 +0900 | [diff] [blame] | 89 | with open(landmines_path, 'w') as f: |
| 90 | f.writelines(new_landmines) |
iannucci@chromium.org | b48a638 | 2012-11-15 11:53:03 +0900 | [diff] [blame] | 91 | |
| 92 | |
tkent@chromium.org | 05c93a7 | 2013-09-03 06:51:18 +0900 | [diff] [blame] | 93 | def process_options(): |
| 94 | """Returns a list of landmine emitting scripts.""" |
iannucci@chromium.org | b48a638 | 2012-11-15 11:53:03 +0900 | [diff] [blame] | 95 | parser = optparse.OptionParser() |
sivachandra@chromium.org | 54da968 | 2013-08-21 11:44:58 +0900 | [diff] [blame] | 96 | parser.add_option( |
| 97 | '-s', '--landmine-scripts', action='append', |
| 98 | default=[os.path.join(SRC_DIR, 'build', 'get_landmines.py')], |
| 99 | help='Path to the script which emits landmines to stdout. The target ' |
tkent@chromium.org | 05c93a7 | 2013-09-03 06:51:18 +0900 | [diff] [blame] | 100 | 'is passed to this script via option -t. Note that an extra ' |
| 101 | 'script can be specified via an env var EXTRA_LANDMINES_SCRIPT.') |
iannucci@chromium.org | b48a638 | 2012-11-15 11:53:03 +0900 | [diff] [blame] | 102 | parser.add_option('-v', '--verbose', action='store_true', |
| 103 | default=('LANDMINES_VERBOSE' in os.environ), |
| 104 | help=('Emit some extra debugging information (default off). This option ' |
| 105 | 'is also enabled by the presence of a LANDMINES_VERBOSE environment ' |
| 106 | 'variable.')) |
sivachandra@chromium.org | 54da968 | 2013-08-21 11:44:58 +0900 | [diff] [blame] | 107 | |
iannucci@chromium.org | b48a638 | 2012-11-15 11:53:03 +0900 | [diff] [blame] | 108 | options, args = parser.parse_args() |
| 109 | |
| 110 | if args: |
| 111 | parser.error('Unknown arguments %s' % args) |
| 112 | |
| 113 | logging.basicConfig( |
| 114 | level=logging.DEBUG if options.verbose else logging.ERROR) |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 115 | |
tkent@chromium.org | 05c93a7 | 2013-09-03 06:51:18 +0900 | [diff] [blame] | 116 | extra_script = os.environ.get('EXTRA_LANDMINES_SCRIPT') |
| 117 | if extra_script: |
| 118 | return options.landmine_scripts + [extra_script] |
| 119 | else: |
| 120 | return options.landmine_scripts |
| 121 | |
| 122 | |
| 123 | def main(): |
| 124 | landmine_scripts = process_options() |
scottmg@chromium.org | 87ca279 | 2014-02-15 14:23:29 +0900 | [diff] [blame] | 125 | |
newt@chromium.org | 776db5e | 2014-04-12 10:13:21 +0900 | [diff] [blame] | 126 | if landmine_utils.builder() in ('dump_dependency_json', 'eclipse'): |
scottmg@chromium.org | 87ca279 | 2014-02-15 14:23:29 +0900 | [diff] [blame] | 127 | return 0 |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 128 | |
scottmg@chromium.org | 2f8c83b | 2014-08-14 23:03:30 +0900 | [diff] [blame] | 129 | gyp_environment.SetEnvironment() |
| 130 | |
| 131 | landmines = [] |
| 132 | for s in landmine_scripts: |
| 133 | proc = subprocess.Popen([sys.executable, s], stdout=subprocess.PIPE) |
| 134 | output, _ = proc.communicate() |
| 135 | landmines.extend([('%s\n' % l.strip()) for l in output.splitlines()]) |
| 136 | clobber_if_necessary(landmines) |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 137 | |
| 138 | return 0 |
| 139 | |
| 140 | |
| 141 | if __name__ == '__main__': |
iannucci@chromium.org | b48a638 | 2012-11-15 11:53:03 +0900 | [diff] [blame] | 142 | sys.exit(main()) |