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