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.
20 class Builder(object):
21 """Subclasses of the Builder class are used by the bisect script to build
24 def __init__(self
, opts
):
25 """Performs setup for building with target build system.
28 opts: Options parsed from command line.
31 RuntimeError: Some condition necessary for building was not met.
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'):
40 'Path to visual studio could not be determined.')
42 SetBuildSystemDefault(opts
.build_preference
, opts
.use_goma
,
45 if not opts
.build_preference
:
46 if 'ninja' in os
.getenv('GYP_GENERATORS', default
=''):
47 opts
.build_preference
= 'ninja'
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.')
58 """Constructs and returns a Builder object.
61 opts: Options parsed from the command-line.
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
)
71 builder
= DesktopBuilder(opts
)
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.
85 opts: Command-line options.
86 src_dir: Path to chromium/src directory.
89 A path to the directory to use as build output directory.
92 NotImplementedError: The platform according to sys.platform is unexpected.
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.
114 depot: Name of current depot being bisected.
115 opts: The options parsed from the command line.
118 True if build was successful.
120 targets
= ['chromium_builder_perf']
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
)
135 assert False, 'No build system defined.'
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
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.
156 depot: Current depot being bisected.
157 opts: The options parsed from the command line.
160 True if build was successful.
166 build_success
= False
167 if opts
.build_preference
== 'ninja':
168 build_success
= BuildWithNinja(
169 threads
, self
._GetTargets
(), opts
.target_build_type
)
171 assert False, 'No build system defined.'
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
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
)
203 def ImageToTarget(opts
):
204 """Installs latest image to target specified by opts.cros_remote_ip.
207 opts: Program options containing cros_board and cros_remote_ip.
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
226 def BuildPackages(opts
, depot
):
227 """Builds packages for cros.
230 opts: Program options containing cros_board.
231 depot: The depot being bisected.
236 cmd
= [bisect_utils
.CROS_SDK_PATH
]
239 path_to_chrome
= os
.path
.join(os
.getcwd(), '..')
240 cmd
+= ['--chrome_root=%s' % path_to_chrome
]
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
254 def BuildImage(opts
, depot
):
255 """Builds test image for cros.
258 opts: Program options containing cros_board.
259 depot: The depot being bisected.
264 cmd
= [bisect_utils
.CROS_SDK_PATH
]
267 path_to_chrome
= os
.path
.join(os
.getcwd(), '..')
268 cmd
+= ['--chrome_root=%s' % path_to_chrome
]
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.
286 depot: Current depot being bisected.
287 opts: The options parsed from the command line.
290 True if build was successful.
292 if self
.BuildPackages(opts
, depot
):
293 if self
.BuildImage(opts
, depot
):
294 return self
.ImageToTarget(opts
)
298 def SetBuildSystemDefault(build_system
, use_goma
, goma_dir
):
299 """Sets up any environment variables needed to build with the specified build
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
:
311 os
.environ
['GYP_GENERATORS'] = gyp_var
+ ',ninja'
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 '\
320 elif build_system
== 'make':
321 os
.environ
['GYP_GENERATORS'] = 'make'
323 raise RuntimeError('%s build not supported.' % build_system
)
326 os
.environ
['GYP_DEFINES'] = '%s %s' % (os
.getenv('GYP_DEFINES', default
=''),
329 os
.environ
['GYP_DEFINES'] += ' gomadir=%s' % goma_dir
332 def SetupPlatformBuildEnvironment(opts
):
333 """Performs any platform-specific setup.
336 opts: The options parsed from the command line through parse_args().
341 if 'android' in opts
.target_platform
:
342 CopyAndSaveOriginalEnvironmentVars()
343 return SetupAndroidBuildEnvironment(opts
)
344 elif opts
.target_platform
== 'cros':
345 return bisect_utils
.SetupCrosRepo()
349 def BuildWithMake(threads
, targets
, build_type
='Release'):
350 """Runs a make command with the given targets.
353 threads: The number of threads to use. None means unspecified/unlimited.
354 targets: List of make targets.
355 build_type: Release or Debug.
358 True if the command had a 0 exit code, False otherwise.
360 cmd
= ['make', 'BUILDTYPE=%s' % build_type
]
362 cmd
.append('-j%d' % threads
)
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
)]
372 cmd
.append('-j%d' % threads
)
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
]
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.
403 'GYP_GENERATOR_FLAGS',
406 for key
in os
.environ
:
408 vars_to_remove
.append(key
)
409 for key
in vars_to_remove
:
410 if os
.environ
.has_key(key
):
414 ORIGINAL_ENV
= os
.environ
.copy()
417 def SetupAndroidBuildEnvironment(opts
, path_to_src
=None):
418 """Sets up the android build environment.
421 opts: The options parsed from the command line through parse_args().
422 path_to_src: Path to the src checkout.
427 # Revert the environment variables back to default before setting them up
429 env_vars
= os
.environ
.copy()
430 for k
, _
in env_vars
.iteritems():
432 for k
, v
in ORIGINAL_ENV
.iteritems():
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
,
440 out
, _
= proc
.communicate()
442 for line
in out
.splitlines():
443 k
, _
, v
= line
.partition('=')
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'
452 os
.environ
['GYP_DEFINES'] += ' OS=android'
455 os
.environ
['GYP_DEFINES'] += ' use_goma=1'
456 return not proc
.returncode