[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 | |
[email protected] | abab8d7 | 2012-11-14 04:59:48 | [diff] [blame] | 30 | SRC_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| 31 | |
[email protected] | abab8d7 | 2012-11-14 04:59:48 | [diff] [blame] | 32 | |
[email protected] | 9372bec | 2014-08-14 14:03:30 | [diff] [blame] | 33 | def get_build_dir(build_tool, is_iphone=False): |
[email protected] | abab8d7 | 2012-11-14 04:59:48 | [diff] [blame] | 34 | """ |
| 35 | Returns output directory absolute path dependent on build and targets. |
| 36 | Examples: |
[email protected] | 9372bec | 2014-08-14 14:03:30 | [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' |
[email protected] | abab8d7 | 2012-11-14 04:59:48 | [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': |
[email protected] | 9372bec | 2014-08-14 14:03:30 | [diff] [blame] | 45 | ret = os.path.join(SRC_DIR, 'xcodebuild') |
[email protected] | ddca2f7 | 2013-05-11 19:13:21 | [diff] [blame] | 46 | elif build_tool in ['make', 'ninja', 'ninja-ios']: # TODO: Remove ninja-ios. |
johnme | 89d303a | 2015-01-13 21:28:41 | [diff] [blame] | 47 | if 'CHROMIUM_OUT_DIR' in os.environ: |
| 48 | output_dir = os.environ.get('CHROMIUM_OUT_DIR').strip() |
| 49 | if not output_dir: |
| 50 | raise Error('CHROMIUM_OUT_DIR environment variable is set but blank!') |
oetuaho | 6bad0d4 | 2014-11-03 09:09:53 | [diff] [blame] | 51 | else: |
johnme | 89d303a | 2015-01-13 21:28:41 | [diff] [blame] | 52 | output_dir = landmine_utils.gyp_generator_flags().get('output_dir', 'out') |
oetuaho | 6bad0d4 | 2014-11-03 09:09:53 | [diff] [blame] | 53 | ret = os.path.join(SRC_DIR, output_dir) |
[email protected] | abab8d7 | 2012-11-14 04:59:48 | [diff] [blame] | 54 | else: |
[email protected] | 68d8069 | 2013-01-17 23:50:02 | [diff] [blame] | 55 | raise NotImplementedError('Unexpected GYP_GENERATORS (%s)' % build_tool) |
[email protected] | abab8d7 | 2012-11-14 04:59:48 | [diff] [blame] | 56 | return os.path.abspath(ret) |
| 57 | |
| 58 | |
[email protected] | 9372bec | 2014-08-14 14:03:30 | [diff] [blame] | 59 | def clobber_if_necessary(new_landmines): |
[email protected] | c45227b | 2012-11-15 02:53:03 | [diff] [blame] | 60 | """Does the work of setting, planting, and triggering landmines.""" |
[email protected] | 9372bec | 2014-08-14 14:03:30 | [diff] [blame] | 61 | out_dir = get_build_dir(landmine_utils.builder()) |
| 62 | landmines_path = os.path.normpath(os.path.join(out_dir, '..', '.landmines')) |
[email protected] | d99e6bf | 2014-04-23 04:19:23 | [diff] [blame] | 63 | try: |
[email protected] | c45227b | 2012-11-15 02:53:03 | [diff] [blame] | 64 | os.makedirs(out_dir) |
[email protected] | d99e6bf | 2014-04-23 04:19:23 | [diff] [blame] | 65 | except OSError as e: |
| 66 | if e.errno == errno.EEXIST: |
| 67 | pass |
[email protected] | c45227b | 2012-11-15 02:53:03 | [diff] [blame] | 68 | |
[email protected] | f3d0d1c | 2014-05-22 22:12:48 | [diff] [blame] | 69 | if os.path.exists(landmines_path): |
[email protected] | c45227b | 2012-11-15 02:53:03 | [diff] [blame] | 70 | with open(landmines_path, 'r') as f: |
| 71 | old_landmines = f.readlines() |
| 72 | if old_landmines != new_landmines: |
| 73 | old_date = time.ctime(os.stat(landmines_path).st_ctime) |
| 74 | diff = difflib.unified_diff(old_landmines, new_landmines, |
| 75 | fromfile='old_landmines', tofile='new_landmines', |
| 76 | fromfiledate=old_date, tofiledate=time.ctime(), n=0) |
[email protected] | 9372bec | 2014-08-14 14:03:30 | [diff] [blame] | 77 | sys.stdout.write('Clobbering due to:\n') |
| 78 | sys.stdout.writelines(diff) |
[email protected] | c45227b | 2012-11-15 02:53:03 | [diff] [blame] | 79 | |
petrcermak | af7f4c0 | 2015-06-22 12:41:49 | [diff] [blame] | 80 | clobber.clobber(out_dir) |
[email protected] | 9372bec | 2014-08-14 14:03:30 | [diff] [blame] | 81 | |
| 82 | # Save current set of landmines for next time. |
[email protected] | f3d0d1c | 2014-05-22 22:12:48 | [diff] [blame] | 83 | with open(landmines_path, 'w') as f: |
| 84 | f.writelines(new_landmines) |
[email protected] | c45227b | 2012-11-15 02:53:03 | [diff] [blame] | 85 | |
| 86 | |
[email protected] | c36d6293 | 2013-09-02 21:51:18 | [diff] [blame] | 87 | def process_options(): |
| 88 | """Returns a list of landmine emitting scripts.""" |
[email protected] | c45227b | 2012-11-15 02:53:03 | [diff] [blame] | 89 | parser = optparse.OptionParser() |
[email protected] | ec069f7 | 2013-08-21 02:44:58 | [diff] [blame] | 90 | parser.add_option( |
| 91 | '-s', '--landmine-scripts', action='append', |
| 92 | default=[os.path.join(SRC_DIR, 'build', 'get_landmines.py')], |
| 93 | help='Path to the script which emits landmines to stdout. The target ' |
[email protected] | c36d6293 | 2013-09-02 21:51:18 | [diff] [blame] | 94 | 'is passed to this script via option -t. Note that an extra ' |
| 95 | 'script can be specified via an env var EXTRA_LANDMINES_SCRIPT.') |
[email protected] | c45227b | 2012-11-15 02:53:03 | [diff] [blame] | 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.')) |
[email protected] | ec069f7 | 2013-08-21 02:44:58 | [diff] [blame] | 101 | |
[email protected] | c45227b | 2012-11-15 02:53:03 | [diff] [blame] | 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) |
[email protected] | abab8d7 | 2012-11-14 04:59:48 | [diff] [blame] | 109 | |
[email protected] | c36d6293 | 2013-09-02 21:51:18 | [diff] [blame] | 110 | extra_script = os.environ.get('EXTRA_LANDMINES_SCRIPT') |
| 111 | if extra_script: |
| 112 | return options.landmine_scripts + [extra_script] |
| 113 | else: |
| 114 | return options.landmine_scripts |
| 115 | |
| 116 | |
| 117 | def main(): |
| 118 | landmine_scripts = process_options() |
[email protected] | 07e5563 | 2014-02-15 05:23:29 | [diff] [blame] | 119 | |
[email protected] | a403a3d | 2014-04-12 01:13:21 | [diff] [blame] | 120 | if landmine_utils.builder() in ('dump_dependency_json', 'eclipse'): |
[email protected] | 07e5563 | 2014-02-15 05:23:29 | [diff] [blame] | 121 | return 0 |
[email protected] | abab8d7 | 2012-11-14 04:59:48 | [diff] [blame] | 122 | |
[email protected] | 9372bec | 2014-08-14 14:03:30 | [diff] [blame] | 123 | gyp_environment.SetEnvironment() |
| 124 | |
| 125 | landmines = [] |
| 126 | for s in landmine_scripts: |
| 127 | proc = subprocess.Popen([sys.executable, s], stdout=subprocess.PIPE) |
| 128 | output, _ = proc.communicate() |
| 129 | landmines.extend([('%s\n' % l.strip()) for l in output.splitlines()]) |
| 130 | clobber_if_necessary(landmines) |
[email protected] | abab8d7 | 2012-11-14 04:59:48 | [diff] [blame] | 131 | |
| 132 | return 0 |
| 133 | |
| 134 | |
| 135 | if __name__ == '__main__': |
[email protected] | c45227b | 2012-11-15 02:53:03 | [diff] [blame] | 136 | sys.exit(main()) |