Instrumental test for BookmarksBridge. It currently tests these functionalities:...
[chromium-blink-merge.git] / tools / auto_bisect / builder.py
blobf039a57e5d988449cc9d4782ea0c9e51e90637a0
1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Classes and functions for building Chrome.
7 This includes functions for running commands to build, as well as
8 specific rules about which targets to build.
9 """
11 import os
12 import subprocess
13 import sys
15 import bisect_utils
17 ORIGINAL_ENV = {}
20 class Builder(object):
21 """Subclasses of the Builder class are used by the bisect script to build
22 relevant targets.
23 """
24 def __init__(self, opts):
25 """Performs setup for building with target build system.
27 Args:
28 opts: Options parsed from command line.
30 Raises:
31 RuntimeError: Some condition necessary for building was not met.
32 """
33 if bisect_utils.IsWindowsHost():
34 if not opts.build_preference:
35 opts.build_preference = 'msvs'
37 if opts.build_preference == 'msvs':
38 if not os.getenv('VS100COMNTOOLS'):
39 raise RuntimeError(
40 'Path to visual studio could not be determined.')
41 else:
42 SetBuildSystemDefault(opts.build_preference, opts.use_goma,
43 opts.goma_dir)
44 else:
45 if not opts.build_preference:
46 if 'ninja' in os.getenv('GYP_GENERATORS', default=''):
47 opts.build_preference = 'ninja'
48 else:
49 opts.build_preference = 'make'
51 SetBuildSystemDefault(opts.build_preference, opts.use_goma, opts.goma_dir)
53 if not SetupPlatformBuildEnvironment(opts):
54 raise RuntimeError('Failed to set platform environment.')
56 @staticmethod
57 def FromOpts(opts):
58 """Constructs and returns a Builder object.
60 Args:
61 opts: Options parsed from the command-line.
62 """
63 builder = None
64 if opts.target_platform == 'cros':
65 builder = CrosBuilder(opts)
66 elif opts.target_platform == 'android':
67 builder = AndroidBuilder(opts)
68 elif opts.target_platform == 'android-chrome':
69 builder = AndroidChromeBuilder(opts)
70 else:
71 builder = DesktopBuilder(opts)
72 return builder
74 def Build(self, depot, opts):
75 """Runs a command to build Chrome."""
76 raise NotImplementedError()
79 def GetBuildOutputDirectory(opts, src_dir=None):
80 """Returns the path to the build directory, relative to the checkout root.
82 Assumes that the current working directory is the checkout root.
84 Args:
85 opts: Command-line options.
86 src_dir: Path to chromium/src directory.
88 Returns:
89 A path to the directory to use as build output directory.
91 Raises:
92 NotImplementedError: The platform according to sys.platform is unexpected.
93 """
94 src_dir = src_dir or 'src'
95 if opts.build_preference == 'ninja' or bisect_utils.IsLinuxHost():
96 return os.path.join(src_dir, 'out')
97 if bisect_utils.IsMacHost():
98 return os.path.join(src_dir, 'xcodebuild')
99 if bisect_utils.IsWindowsHost():
100 return os.path.join(src_dir, 'build')
101 raise NotImplementedError('Unexpected platform %s' % sys.platform)
104 class DesktopBuilder(Builder):
105 """DesktopBuilder is used to build Chromium on Linux, Mac, or Windows."""
107 def __init__(self, opts):
108 super(DesktopBuilder, self).__init__(opts)
110 def Build(self, depot, opts):
111 """Builds chromium_builder_perf target using options passed into the script.
113 Args:
114 depot: Name of current depot being bisected.
115 opts: The options parsed from the command line.
117 Returns:
118 True if build was successful.
120 targets = ['chromium_builder_perf']
122 threads = None
123 if opts.use_goma:
124 threads = 64
126 build_success = False
127 if opts.build_preference == 'make':
128 build_success = BuildWithMake(threads, targets, opts.target_build_type)
129 elif opts.build_preference == 'ninja':
130 build_success = BuildWithNinja(threads, targets, opts.target_build_type)
131 elif opts.build_preference == 'msvs':
132 assert bisect_utils.IsWindowsHost(), 'msvs is only supported on Windows.'
133 build_success = BuildWithVisualStudio(targets, opts.target_build_type)
134 else:
135 assert False, 'No build system defined.'
136 return build_success
139 class AndroidBuilder(Builder):
140 """AndroidBuilder is used to build on android."""
142 def __init__(self, opts):
143 super(AndroidBuilder, self).__init__(opts)
145 # TODO(qyearsley): Make this a class method and verify that it works with
146 # a unit test.
147 # pylint: disable=R0201
148 def _GetTargets(self):
149 """Returns a list of build targets."""
150 return ['chrome_shell_apk', 'cc_perftests_apk', 'android_tools']
152 def Build(self, depot, opts):
153 """Builds the android content shell and other necessary tools.
155 Args:
156 depot: Current depot being bisected.
157 opts: The options parsed from the command line.
159 Returns:
160 True if build was successful.
162 threads = None
163 if opts.use_goma:
164 threads = 64
166 build_success = False
167 if opts.build_preference == 'ninja':
168 build_success = BuildWithNinja(
169 threads, self._GetTargets(), opts.target_build_type)
170 else:
171 assert False, 'No build system defined.'
173 return build_success
176 class AndroidChromeBuilder(AndroidBuilder):
177 """AndroidChromeBuilder is used to build "android-chrome".
179 This is slightly different from AndroidBuilder.
182 def __init__(self, opts):
183 super(AndroidChromeBuilder, self).__init__(opts)
185 # TODO(qyearsley): Make this a class method and verify that it works with
186 # a unit test.
187 # pylint: disable=R0201
188 def _GetTargets(self):
189 """Returns a list of build targets."""
190 return AndroidBuilder._GetTargets(self) + ['chrome_apk']
193 class CrosBuilder(Builder):
194 """CrosBuilder is used to build and image ChromeOS/Chromium.
196 WARNING(qyearsley, 2014-08-15): This hasn't been tested recently.
199 def __init__(self, opts):
200 super(CrosBuilder, self).__init__(opts)
202 @staticmethod
203 def ImageToTarget(opts):
204 """Installs latest image to target specified by opts.cros_remote_ip.
206 Args:
207 opts: Program options containing cros_board and cros_remote_ip.
209 Returns:
210 True if successful.
212 try:
213 # Keys will most likely be set to 0640 after wiping the chroot.
214 os.chmod(bisect_utils.CROS_SCRIPT_KEY_PATH, 0600)
215 os.chmod(bisect_utils.CROS_TEST_KEY_PATH, 0600)
216 cmd = [bisect_utils.CROS_SDK_PATH, '--', './bin/cros_image_to_target.py',
217 '--remote=%s' % opts.cros_remote_ip,
218 '--board=%s' % opts.cros_board, '--test', '--verbose']
220 return_code = bisect_utils.RunProcess(cmd)
221 return not return_code
222 except OSError:
223 return False
225 @staticmethod
226 def BuildPackages(opts, depot):
227 """Builds packages for cros.
229 Args:
230 opts: Program options containing cros_board.
231 depot: The depot being bisected.
233 Returns:
234 True if successful.
236 cmd = [bisect_utils.CROS_SDK_PATH]
238 if depot != 'cros':
239 path_to_chrome = os.path.join(os.getcwd(), '..')
240 cmd += ['--chrome_root=%s' % path_to_chrome]
242 cmd += ['--']
244 if depot != 'cros':
245 cmd += ['CHROME_ORIGIN=LOCAL_SOURCE']
247 cmd += ['BUILDTYPE=%s' % opts.target_build_type, './build_packages',
248 '--board=%s' % opts.cros_board]
249 return_code = bisect_utils.RunProcess(cmd)
251 return not return_code
253 @staticmethod
254 def BuildImage(opts, depot):
255 """Builds test image for cros.
257 Args:
258 opts: Program options containing cros_board.
259 depot: The depot being bisected.
261 Returns:
262 True if successful.
264 cmd = [bisect_utils.CROS_SDK_PATH]
266 if depot != 'cros':
267 path_to_chrome = os.path.join(os.getcwd(), '..')
268 cmd += ['--chrome_root=%s' % path_to_chrome]
270 cmd += ['--']
272 if depot != 'cros':
273 cmd += ['CHROME_ORIGIN=LOCAL_SOURCE']
275 cmd += ['BUILDTYPE=%s' % opts.target_build_type, '--', './build_image',
276 '--board=%s' % opts.cros_board, 'test']
278 return_code = bisect_utils.RunProcess(cmd)
280 return not return_code
282 def Build(self, depot, opts):
283 """Builds targets using options passed into the script.
285 Args:
286 depot: Current depot being bisected.
287 opts: The options parsed from the command line.
289 Returns:
290 True if build was successful.
292 if self.BuildPackages(opts, depot):
293 if self.BuildImage(opts, depot):
294 return self.ImageToTarget(opts)
295 return False
298 def SetBuildSystemDefault(build_system, use_goma, goma_dir):
299 """Sets up any environment variables needed to build with the specified build
300 system.
302 Args:
303 build_system: A string specifying build system. Currently only 'ninja' or
304 'make' are supported.
306 if build_system == 'ninja':
307 gyp_var = os.getenv('GYP_GENERATORS', default='')
309 if not gyp_var or not 'ninja' in gyp_var:
310 if gyp_var:
311 os.environ['GYP_GENERATORS'] = gyp_var + ',ninja'
312 else:
313 os.environ['GYP_GENERATORS'] = 'ninja'
315 if bisect_utils.IsWindowsHost():
316 os.environ['GYP_DEFINES'] = 'component=shared_library '\
317 'incremental_chrome_dll=1 disable_nacl=1 fastbuild=1 '\
318 'chromium_win_pch=0'
320 elif build_system == 'make':
321 os.environ['GYP_GENERATORS'] = 'make'
322 else:
323 raise RuntimeError('%s build not supported.' % build_system)
325 if use_goma:
326 os.environ['GYP_DEFINES'] = '%s %s' % (os.getenv('GYP_DEFINES', default=''),
327 'use_goma=1')
328 if goma_dir:
329 os.environ['GYP_DEFINES'] += ' gomadir=%s' % goma_dir
332 def SetupPlatformBuildEnvironment(opts):
333 """Performs any platform-specific setup.
335 Args:
336 opts: The options parsed from the command line through parse_args().
338 Returns:
339 True if successful.
341 if 'android' in opts.target_platform:
342 CopyAndSaveOriginalEnvironmentVars()
343 return SetupAndroidBuildEnvironment(opts)
344 elif opts.target_platform == 'cros':
345 return bisect_utils.SetupCrosRepo()
346 return True
349 def BuildWithMake(threads, targets, build_type='Release'):
350 """Runs a make command with the given targets.
352 Args:
353 threads: The number of threads to use. None means unspecified/unlimited.
354 targets: List of make targets.
355 build_type: Release or Debug.
357 Returns:
358 True if the command had a 0 exit code, False otherwise.
360 cmd = ['make', 'BUILDTYPE=%s' % build_type]
361 if threads:
362 cmd.append('-j%d' % threads)
363 cmd += targets
364 return_code = bisect_utils.RunProcess(cmd)
365 return not return_code
368 def BuildWithNinja(threads, targets, build_type='Release'):
369 """Runs a ninja command with the given targets."""
370 cmd = ['ninja', '-C', os.path.join('out', build_type)]
371 if threads:
372 cmd.append('-j%d' % threads)
373 cmd += targets
374 return_code = bisect_utils.RunProcess(cmd)
375 return not return_code
378 def BuildWithVisualStudio(targets, build_type='Release'):
379 """Runs a command to build the given targets with Visual Studio."""
380 path_to_devenv = os.path.abspath(
381 os.path.join(os.environ['VS100COMNTOOLS'], '..', 'IDE', 'devenv.com'))
382 path_to_sln = os.path.join(os.getcwd(), 'chrome', 'chrome.sln')
383 cmd = [path_to_devenv, '/build', build_type, path_to_sln]
384 for t in targets:
385 cmd.extend(['/Project', t])
386 return_code = bisect_utils.RunProcess(cmd)
387 return not return_code
390 def CopyAndSaveOriginalEnvironmentVars():
391 """Makes a copy of the current environment variables.
393 Before making a copy of the environment variables and setting a global
394 variable, this function unsets a certain set of environment variables.
396 # TODO: Waiting on crbug.com/255689, will remove this after.
397 vars_to_remove = [
398 'CHROME_SRC',
399 'CHROMIUM_GYP_FILE',
400 'GYP_CROSSCOMPILE',
401 'GYP_DEFINES',
402 'GYP_GENERATORS',
403 'GYP_GENERATOR_FLAGS',
404 'OBJCOPY',
406 for key in os.environ:
407 if 'ANDROID' in key:
408 vars_to_remove.append(key)
409 for key in vars_to_remove:
410 if os.environ.has_key(key):
411 del os.environ[key]
413 global ORIGINAL_ENV
414 ORIGINAL_ENV = os.environ.copy()
417 def SetupAndroidBuildEnvironment(opts, path_to_src=None):
418 """Sets up the android build environment.
420 Args:
421 opts: The options parsed from the command line through parse_args().
422 path_to_src: Path to the src checkout.
424 Returns:
425 True if successful.
427 # Revert the environment variables back to default before setting them up
428 # with envsetup.sh.
429 env_vars = os.environ.copy()
430 for k, _ in env_vars.iteritems():
431 del os.environ[k]
432 for k, v in ORIGINAL_ENV.iteritems():
433 os.environ[k] = v
435 envsetup_path = os.path.join('build', 'android', 'envsetup.sh')
436 proc = subprocess.Popen(['bash', '-c', 'source %s && env' % envsetup_path],
437 stdout=subprocess.PIPE,
438 stderr=subprocess.PIPE,
439 cwd=path_to_src)
440 out, _ = proc.communicate()
442 for line in out.splitlines():
443 k, _, v = line.partition('=')
444 os.environ[k] = v
446 # envsetup.sh no longer sets OS=android in GYP_DEFINES environment variable.
447 # (See http://crrev.com/170273005). So, we set this variable explicitly here
448 # in order to build Chrome on Android.
449 if 'GYP_DEFINES' not in os.environ:
450 os.environ['GYP_DEFINES'] = 'OS=android'
451 else:
452 os.environ['GYP_DEFINES'] += ' OS=android'
454 if opts.use_goma:
455 os.environ['GYP_DEFINES'] += ' use_goma=1'
456 return not proc.returncode