Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [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 | """ |
| 7 | This script runs every build as the first hook (See DEPS). If it detects that |
| 8 | the build should be clobbered, it will delete the contents of the build |
| 9 | directory. |
| 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 |
| 17 | import errno |
| 18 | import gyp_environment |
| 19 | import logging |
| 20 | import optparse |
| 21 | import os |
| 22 | import sys |
| 23 | import subprocess |
| 24 | import time |
| 25 | |
| 26 | import clobber |
| 27 | import landmine_utils |
| 28 | |
| 29 | |
| 30 | def get_build_dir(build_tool, src_dir, is_iphone=False): |
| 31 | """ |
| 32 | Returns output directory absolute path dependent on build and targets. |
| 33 | Examples: |
| 34 | r'c:\b\build\slave\win\build\src\out' |
| 35 | '/mnt/data/b/build/slave/linux/build/src/out' |
| 36 | '/b/build/slave/ios_rel_device/build/src/xcodebuild' |
| 37 | |
| 38 | Keep this function in sync with tools/build/scripts/slave/compile.py |
| 39 | """ |
| 40 | ret = None |
| 41 | if build_tool == 'xcode': |
| 42 | ret = os.path.join(src_dir, 'xcodebuild') |
| 43 | elif build_tool in ['make', 'ninja', 'ninja-ios']: # TODO: Remove ninja-ios. |
| 44 | if 'CHROMIUM_OUT_DIR' in os.environ: |
| 45 | output_dir = os.environ.get('CHROMIUM_OUT_DIR').strip() |
| 46 | if not output_dir: |
| 47 | raise Error('CHROMIUM_OUT_DIR environment variable is set but blank!') |
| 48 | else: |
| 49 | output_dir = landmine_utils.gyp_generator_flags().get('output_dir', 'out') |
| 50 | ret = os.path.join(src_dir, output_dir) |
| 51 | else: |
| 52 | raise NotImplementedError('Unexpected GYP_GENERATORS (%s)' % build_tool) |
| 53 | return os.path.abspath(ret) |
| 54 | |
| 55 | |
| 56 | def clobber_if_necessary(new_landmines, src_dir): |
| 57 | """Does the work of setting, planting, and triggering landmines.""" |
| 58 | out_dir = get_build_dir(landmine_utils.builder(), src_dir) |
| 59 | landmines_path = os.path.normpath(os.path.join(src_dir, '.landmines')) |
| 60 | try: |
| 61 | os.makedirs(out_dir) |
| 62 | except OSError as e: |
| 63 | if e.errno == errno.EEXIST: |
| 64 | pass |
| 65 | |
| 66 | if os.path.exists(landmines_path): |
| 67 | with open(landmines_path, 'r') as f: |
| 68 | old_landmines = f.readlines() |
| 69 | if old_landmines != new_landmines: |
| 70 | old_date = time.ctime(os.stat(landmines_path).st_ctime) |
| 71 | diff = difflib.unified_diff(old_landmines, new_landmines, |
| 72 | fromfile='old_landmines', tofile='new_landmines', |
| 73 | fromfiledate=old_date, tofiledate=time.ctime(), n=0) |
| 74 | sys.stdout.write('Clobbering due to:\n') |
| 75 | sys.stdout.writelines(diff) |
| 76 | sys.stdout.flush() |
| 77 | |
| 78 | clobber.clobber(out_dir) |
| 79 | |
| 80 | # Save current set of landmines for next time. |
| 81 | with open(landmines_path, 'w') as f: |
| 82 | f.writelines(new_landmines) |
| 83 | |
| 84 | |
| 85 | def process_options(): |
| 86 | """Returns an options object containing the configuration for this script.""" |
| 87 | parser = optparse.OptionParser() |
| 88 | parser.add_option( |
| 89 | '-s', '--landmine-scripts', action='append', |
| 90 | help='Path to the script which emits landmines to stdout. The target ' |
| 91 | 'is passed to this script via option -t. Note that an extra ' |
| 92 | 'script can be specified via an env var EXTRA_LANDMINES_SCRIPT.') |
| 93 | parser.add_option('-d', '--src-dir', |
| 94 | help='Path of the source root dir. Overrides the default location of the ' |
| 95 | 'source root dir when calculating the build directory.') |
| 96 | parser.add_option('-v', '--verbose', action='store_true', |
| 97 | default=('LANDMINES_VERBOSE' in os.environ), |
| 98 | help=('Emit some extra debugging information (default off). This option ' |
| 99 | 'is also enabled by the presence of a LANDMINES_VERBOSE environment ' |
| 100 | 'variable.')) |
| 101 | |
| 102 | options, args = parser.parse_args() |
| 103 | |
| 104 | if args: |
| 105 | parser.error('Unknown arguments %s' % args) |
| 106 | |
| 107 | logging.basicConfig( |
| 108 | level=logging.DEBUG if options.verbose else logging.ERROR) |
| 109 | |
| 110 | if options.src_dir: |
| 111 | if not os.path.isdir(options.src_dir): |
| 112 | parser.error('Cannot find source root dir at %s' % options.src_dir) |
| 113 | logging.debug('Overriding source root dir. Using: %s', options.src_dir) |
| 114 | else: |
| 115 | options.src_dir = \ |
| 116 | os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| 117 | |
| 118 | if not options.landmine_scripts: |
| 119 | options.landmine_scripts = [os.path.join(options.src_dir, 'build', |
| 120 | 'get_landmines.py')] |
| 121 | |
| 122 | extra_script = os.environ.get('EXTRA_LANDMINES_SCRIPT') |
| 123 | if extra_script: |
| 124 | options.landmine_scripts += [extra_script] |
| 125 | |
| 126 | return options |
| 127 | |
| 128 | |
| 129 | def main(): |
| 130 | options = process_options() |
| 131 | |
| 132 | if landmine_utils.builder() in ('dump_dependency_json', 'eclipse'): |
| 133 | return 0 |
| 134 | |
| 135 | gyp_environment.SetEnvironment() |
| 136 | |
| 137 | landmines = [] |
| 138 | for s in options.landmine_scripts: |
| 139 | proc = subprocess.Popen([sys.executable, s], stdout=subprocess.PIPE) |
| 140 | output, _ = proc.communicate() |
| 141 | landmines.extend([('%s\n' % l.strip()) for l in output.splitlines()]) |
| 142 | clobber_if_necessary(landmines, options.src_dir) |
| 143 | |
| 144 | return 0 |
| 145 | |
| 146 | |
| 147 | if __name__ == '__main__': |
| 148 | sys.exit(main()) |