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.
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
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
18 import gyp_environment
30 SRC_DIR
= os
.path
.dirname(os
.path
.dirname(os
.path
.realpath(__file__
)))
33 def get_build_dir(build_tool
, is_iphone
=False):
35 Returns output directory absolute path dependent on build and targets.
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'
41 Keep this function in sync with tools/build/scripts/slave/compile.py
44 if build_tool
== 'xcode':
45 ret
= os
.path
.join(SRC_DIR
, 'xcodebuild')
46 elif build_tool
in ['make', 'ninja', 'ninja-ios']: # TODO: Remove ninja-ios.
47 if 'CHROMIUM_OUT_DIR' in os
.environ
:
48 output_dir
= os
.environ
.get('CHROMIUM_OUT_DIR').strip()
50 raise Error('CHROMIUM_OUT_DIR environment variable is set but blank!')
52 output_dir
= landmine_utils
.gyp_generator_flags().get('output_dir', 'out')
53 ret
= os
.path
.join(SRC_DIR
, output_dir
)
55 raise NotImplementedError('Unexpected GYP_GENERATORS (%s)' % build_tool
)
56 return os
.path
.abspath(ret
)
59 def clobber_if_necessary(new_landmines
):
60 """Does the work of setting, planting, and triggering landmines."""
61 out_dir
= get_build_dir(landmine_utils
.builder())
62 landmines_path
= os
.path
.normpath(os
.path
.join(out_dir
, '..', '.landmines'))
66 if e
.errno
== errno
.EEXIST
:
69 if os
.path
.exists(landmines_path
):
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)
77 sys
.stdout
.write('Clobbering due to:\n')
78 sys
.stdout
.writelines(diff
)
80 clobber
.clobber(out_dir
)
82 # Save current set of landmines for next time.
83 with
open(landmines_path
, 'w') as f
:
84 f
.writelines(new_landmines
)
87 def process_options():
88 """Returns a list of landmine emitting scripts."""
89 parser
= optparse
.OptionParser()
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 '
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.')
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 '
102 options
, args
= parser
.parse_args()
105 parser
.error('Unknown arguments %s' % args
)
108 level
=logging
.DEBUG
if options
.verbose
else logging
.ERROR
)
110 extra_script
= os
.environ
.get('EXTRA_LANDMINES_SCRIPT')
112 return options
.landmine_scripts
+ [extra_script
]
114 return options
.landmine_scripts
118 landmine_scripts
= process_options()
120 if landmine_utils
.builder() in ('dump_dependency_json', 'eclipse'):
123 gyp_environment
.SetEnvironment()
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
)
135 if __name__
== '__main__':