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