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 | """ |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 7 | This script runs every build as a hook. If it detects that the build should |
| 8 | be clobbered, it will touch the file <build_dir>/.landmine_triggered. The |
| 9 | various build scripts will then check for the presence of this file and clobber |
| 10 | accordingly. The script will also emit the reasons for the clobber to stdout. |
| 11 | |
| 12 | A landmine is tripped when a builder checks out a different revision, and the |
| 13 | diff between the new landmines and the old ones is non-null. At this point, the |
| 14 | build is clobbered. |
| 15 | """ |
| 16 | |
| 17 | import difflib |
iannucci@chromium.org | b48a638 | 2012-11-15 11:53:03 +0900 | [diff] [blame] | 18 | import logging |
| 19 | import optparse |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 20 | import os |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 21 | import sys |
sivachandra@chromium.org | 54da968 | 2013-08-21 11:44:58 +0900 | [diff] [blame] | 22 | import subprocess |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 23 | import time |
| 24 | |
sivachandra@chromium.org | 54da968 | 2013-08-21 11:44:58 +0900 | [diff] [blame] | 25 | import landmine_utils |
| 26 | |
| 27 | |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 28 | SRC_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| 29 | |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 30 | |
| 31 | def get_target_build_dir(build_tool, target, is_iphone=False): |
| 32 | """ |
| 33 | Returns output directory absolute path dependent on build and targets. |
| 34 | Examples: |
| 35 | r'c:\b\build\slave\win\build\src\out\Release' |
| 36 | '/mnt/data/b/build/slave/linux/build/src/out/Debug' |
| 37 | '/b/build/slave/ios_rel_device/build/src/xcodebuild/Release-iphoneos' |
| 38 | |
| 39 | Keep this function in sync with tools/build/scripts/slave/compile.py |
| 40 | """ |
| 41 | ret = None |
| 42 | if build_tool == 'xcode': |
| 43 | ret = os.path.join(SRC_DIR, 'xcodebuild', |
| 44 | target + ('-iphoneos' if is_iphone else '')) |
thakis@chromium.org | cf894a7 | 2013-05-12 04:13:21 +0900 | [diff] [blame] | 45 | elif build_tool in ['make', 'ninja', 'ninja-ios']: # TODO: Remove ninja-ios. |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 46 | ret = os.path.join(SRC_DIR, 'out', target) |
iannucci@chromium.org | f2710aa | 2012-11-15 10:52:07 +0900 | [diff] [blame] | 47 | elif build_tool in ['msvs', 'vs', 'ib']: |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 48 | ret = os.path.join(SRC_DIR, 'build', target) |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 49 | else: |
scottmg@chromium.org | 3946518 | 2013-01-18 08:50:02 +0900 | [diff] [blame] | 50 | raise NotImplementedError('Unexpected GYP_GENERATORS (%s)' % build_tool) |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 51 | return os.path.abspath(ret) |
| 52 | |
| 53 | |
sivachandra@chromium.org | 54da968 | 2013-08-21 11:44:58 +0900 | [diff] [blame] | 54 | def set_up_landmines(target, new_landmines): |
iannucci@chromium.org | b48a638 | 2012-11-15 11:53:03 +0900 | [diff] [blame] | 55 | """Does the work of setting, planting, and triggering landmines.""" |
sivachandra@chromium.org | 54da968 | 2013-08-21 11:44:58 +0900 | [diff] [blame] | 56 | out_dir = get_target_build_dir(landmine_utils.builder(), target, |
| 57 | landmine_utils.platform() == 'ios') |
iannucci@chromium.org | b48a638 | 2012-11-15 11:53:03 +0900 | [diff] [blame] | 58 | |
| 59 | landmines_path = os.path.join(out_dir, '.landmines') |
| 60 | if not os.path.exists(out_dir): |
| 61 | os.makedirs(out_dir) |
| 62 | |
iannucci@chromium.org | b48a638 | 2012-11-15 11:53:03 +0900 | [diff] [blame] | 63 | if not os.path.exists(landmines_path): |
| 64 | with open(landmines_path, 'w') as f: |
| 65 | f.writelines(new_landmines) |
| 66 | else: |
| 67 | triggered = os.path.join(out_dir, '.landmines_triggered') |
| 68 | with open(landmines_path, 'r') as f: |
| 69 | old_landmines = f.readlines() |
| 70 | if old_landmines != new_landmines: |
| 71 | old_date = time.ctime(os.stat(landmines_path).st_ctime) |
| 72 | diff = difflib.unified_diff(old_landmines, new_landmines, |
| 73 | fromfile='old_landmines', tofile='new_landmines', |
| 74 | fromfiledate=old_date, tofiledate=time.ctime(), n=0) |
| 75 | |
| 76 | with open(triggered, 'w') as f: |
| 77 | f.writelines(diff) |
| 78 | elif os.path.exists(triggered): |
| 79 | # Remove false triggered landmines. |
| 80 | os.remove(triggered) |
| 81 | |
| 82 | |
tkent@chromium.org | 05c93a7 | 2013-09-03 06:51:18 +0900 | [diff] [blame] | 83 | def process_options(): |
| 84 | """Returns a list of landmine emitting scripts.""" |
iannucci@chromium.org | b48a638 | 2012-11-15 11:53:03 +0900 | [diff] [blame] | 85 | parser = optparse.OptionParser() |
sivachandra@chromium.org | 54da968 | 2013-08-21 11:44:58 +0900 | [diff] [blame] | 86 | parser.add_option( |
| 87 | '-s', '--landmine-scripts', action='append', |
| 88 | default=[os.path.join(SRC_DIR, 'build', 'get_landmines.py')], |
| 89 | 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] | 90 | 'is passed to this script via option -t. Note that an extra ' |
| 91 | 'script can be specified via an env var EXTRA_LANDMINES_SCRIPT.') |
iannucci@chromium.org | b48a638 | 2012-11-15 11:53:03 +0900 | [diff] [blame] | 92 | parser.add_option('-v', '--verbose', action='store_true', |
| 93 | default=('LANDMINES_VERBOSE' in os.environ), |
| 94 | help=('Emit some extra debugging information (default off). This option ' |
| 95 | 'is also enabled by the presence of a LANDMINES_VERBOSE environment ' |
| 96 | 'variable.')) |
sivachandra@chromium.org | 54da968 | 2013-08-21 11:44:58 +0900 | [diff] [blame] | 97 | |
iannucci@chromium.org | b48a638 | 2012-11-15 11:53:03 +0900 | [diff] [blame] | 98 | options, args = parser.parse_args() |
| 99 | |
| 100 | if args: |
| 101 | parser.error('Unknown arguments %s' % args) |
| 102 | |
| 103 | logging.basicConfig( |
| 104 | level=logging.DEBUG if options.verbose else logging.ERROR) |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 105 | |
tkent@chromium.org | 05c93a7 | 2013-09-03 06:51:18 +0900 | [diff] [blame] | 106 | extra_script = os.environ.get('EXTRA_LANDMINES_SCRIPT') |
| 107 | if extra_script: |
| 108 | return options.landmine_scripts + [extra_script] |
| 109 | else: |
| 110 | return options.landmine_scripts |
| 111 | |
| 112 | |
| 113 | def main(): |
| 114 | landmine_scripts = process_options() |
scottmg@chromium.org | 87ca279 | 2014-02-15 14:23:29 +0900 | [diff] [blame^] | 115 | |
| 116 | if landmine_utils.builder() == 'dump_dependency_json': |
| 117 | return 0 |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 118 | |
iannucci@chromium.org | 7cfb154 | 2013-03-30 06:17:44 +0900 | [diff] [blame] | 119 | for target in ('Debug', 'Release', 'Debug_x64', 'Release_x64'): |
sivachandra@chromium.org | 54da968 | 2013-08-21 11:44:58 +0900 | [diff] [blame] | 120 | landmines = [] |
tkent@chromium.org | 05c93a7 | 2013-09-03 06:51:18 +0900 | [diff] [blame] | 121 | for s in landmine_scripts: |
sivachandra@chromium.org | 54da968 | 2013-08-21 11:44:58 +0900 | [diff] [blame] | 122 | proc = subprocess.Popen([sys.executable, s, '-t', target], |
| 123 | stdout=subprocess.PIPE) |
| 124 | output, _ = proc.communicate() |
| 125 | landmines.extend([('%s\n' % l.strip()) for l in output.splitlines()]) |
| 126 | set_up_landmines(target, landmines) |
iannucci@chromium.org | dccbcf1 | 2012-11-14 13:59:48 +0900 | [diff] [blame] | 127 | |
| 128 | return 0 |
| 129 | |
| 130 | |
| 131 | if __name__ == '__main__': |
iannucci@chromium.org | b48a638 | 2012-11-15 11:53:03 +0900 | [diff] [blame] | 132 | sys.exit(main()) |