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 def _GetTargets(self
):
146 """Returns a list of build targets."""
147 return ['chrome_shell_apk', 'cc_perftests_apk', 'android_tools']
149 def Build(self
, depot
, opts
):
150 """Builds the android content shell and other necessary tools.
153 depot: Current depot being bisected.
154 opts: The options parsed from the command line.
157 True if build was successful.
163 build_success
= False
164 if opts
.build_preference
== 'ninja':
165 build_success
= BuildWithNinja(
166 threads
, self
._GetTargets
(), opts
.target_build_type
)
168 assert False, 'No build system defined.'
173 class AndroidChromeBuilder(AndroidBuilder
):
174 """AndroidChromeBuilder is used to build "android-chrome".
176 This is slightly different from AndroidBuilder.
179 def __init__(self
, opts
):
180 super(AndroidChromeBuilder
, self
).__init
__(opts
)
182 def _GetTargets(self
):
183 """Returns a list of build targets."""
184 return AndroidBuilder
._GetTargets
(self
) + ['chrome_apk']
187 class CrosBuilder(Builder
):
188 """CrosBuilder is used to build and image ChromeOS/Chromium.
190 WARNING(qyearsley, 2014-08-15): This hasn't been tested recently.
193 def __init__(self
, opts
):
194 super(CrosBuilder
, self
).__init
__(opts
)
197 def ImageToTarget(opts
):
198 """Installs latest image to target specified by opts.cros_remote_ip.
201 opts: Program options containing cros_board and cros_remote_ip.
207 # Keys will most likely be set to 0640 after wiping the chroot.
208 os
.chmod(bisect_utils
.CROS_SCRIPT_KEY_PATH
, 0600)
209 os
.chmod(bisect_utils
.CROS_TEST_KEY_PATH
, 0600)
210 cmd
= [bisect_utils
.CROS_SDK_PATH
, '--', './bin/cros_image_to_target.py',
211 '--remote=%s' % opts
.cros_remote_ip
,
212 '--board=%s' % opts
.cros_board
, '--test', '--verbose']
214 return_code
= bisect_utils
.RunProcess(cmd
)
215 return not return_code
220 def BuildPackages(opts
, depot
):
221 """Builds packages for cros.
224 opts: Program options containing cros_board.
225 depot: The depot being bisected.
230 cmd
= [bisect_utils
.CROS_SDK_PATH
]
233 path_to_chrome
= os
.path
.join(os
.getcwd(), '..')
234 cmd
+= ['--chrome_root=%s' % path_to_chrome
]
239 cmd
+= ['CHROME_ORIGIN=LOCAL_SOURCE']
241 cmd
+= ['BUILDTYPE=%s' % opts
.target_build_type
, './build_packages',
242 '--board=%s' % opts
.cros_board
]
243 return_code
= bisect_utils
.RunProcess(cmd
)
245 return not return_code
248 def BuildImage(opts
, depot
):
249 """Builds test image for cros.
252 opts: Program options containing cros_board.
253 depot: The depot being bisected.
258 cmd
= [bisect_utils
.CROS_SDK_PATH
]
261 path_to_chrome
= os
.path
.join(os
.getcwd(), '..')
262 cmd
+= ['--chrome_root=%s' % path_to_chrome
]
267 cmd
+= ['CHROME_ORIGIN=LOCAL_SOURCE']
269 cmd
+= ['BUILDTYPE=%s' % opts
.target_build_type
, '--', './build_image',
270 '--board=%s' % opts
.cros_board
, 'test']
272 return_code
= bisect_utils
.RunProcess(cmd
)
274 return not return_code
276 def Build(self
, depot
, opts
):
277 """Builds targets using options passed into the script.
280 depot: Current depot being bisected.
281 opts: The options parsed from the command line.
284 True if build was successful.
286 if self
.BuildPackages(opts
, depot
):
287 if self
.BuildImage(opts
, depot
):
288 return self
.ImageToTarget(opts
)
292 def SetBuildSystemDefault(build_system
, use_goma
, goma_dir
):
293 """Sets up any environment variables needed to build with the specified build
297 build_system: A string specifying build system. Currently only 'ninja' or
298 'make' are supported.
300 if build_system
== 'ninja':
301 gyp_var
= os
.getenv('GYP_GENERATORS', default
='')
303 if not gyp_var
or not 'ninja' in gyp_var
:
305 os
.environ
['GYP_GENERATORS'] = gyp_var
+ ',ninja'
307 os
.environ
['GYP_GENERATORS'] = 'ninja'
309 if bisect_utils
.IsWindowsHost():
310 os
.environ
['GYP_DEFINES'] = 'component=shared_library '\
311 'incremental_chrome_dll=1 disable_nacl=1 fastbuild=1 '\
314 elif build_system
== 'make':
315 os
.environ
['GYP_GENERATORS'] = 'make'
317 raise RuntimeError('%s build not supported.' % build_system
)
320 os
.environ
['GYP_DEFINES'] = '%s %s' % (os
.getenv('GYP_DEFINES', default
=''),
323 os
.environ
['GYP_DEFINES'] += ' gomadir=%s' % goma_dir
326 def SetupPlatformBuildEnvironment(opts
):
327 """Performs any platform-specific setup.
330 opts: The options parsed from the command line through parse_args().
335 if 'android' in opts
.target_platform
:
336 CopyAndSaveOriginalEnvironmentVars()
337 return SetupAndroidBuildEnvironment(opts
)
338 elif opts
.target_platform
== 'cros':
339 return bisect_utils
.SetupCrosRepo()
343 def BuildWithMake(threads
, targets
, build_type
='Release'):
344 """Runs a make command with the given targets.
347 threads: The number of threads to use. None means unspecified/unlimited.
348 targets: List of make targets.
349 build_type: Release or Debug.
352 True if the command had a 0 exit code, False otherwise.
354 cmd
= ['make', 'BUILDTYPE=%s' % build_type
]
356 cmd
.append('-j%d' % threads
)
358 return_code
= bisect_utils
.RunProcess(cmd
)
359 return not return_code
362 def BuildWithNinja(threads
, targets
, build_type
='Release'):
363 """Runs a ninja command with the given targets."""
364 cmd
= ['ninja', '-C', os
.path
.join('out', build_type
)]
366 cmd
.append('-j%d' % threads
)
368 return_code
= bisect_utils
.RunProcess(cmd
)
369 return not return_code
372 def BuildWithVisualStudio(targets
, build_type
='Release'):
373 """Runs a command to build the given targets with Visual Studio."""
374 path_to_devenv
= os
.path
.abspath(
375 os
.path
.join(os
.environ
['VS100COMNTOOLS'], '..', 'IDE', 'devenv.com'))
376 path_to_sln
= os
.path
.join(os
.getcwd(), 'chrome', 'chrome.sln')
377 cmd
= [path_to_devenv
, '/build', build_type
, path_to_sln
]
379 cmd
.extend(['/Project', t
])
380 return_code
= bisect_utils
.RunProcess(cmd
)
381 return not return_code
384 def CopyAndSaveOriginalEnvironmentVars():
385 """Makes a copy of the current environment variables.
387 Before making a copy of the environment variables and setting a global
388 variable, this function unsets a certain set of environment variables.
390 # TODO: Waiting on crbug.com/255689, will remove this after.
397 'GYP_GENERATOR_FLAGS',
400 for key
in os
.environ
:
402 vars_to_remove
.append(key
)
403 for key
in vars_to_remove
:
404 if os
.environ
.has_key(key
):
408 ORIGINAL_ENV
= os
.environ
.copy()
411 def SetupAndroidBuildEnvironment(opts
, path_to_src
=None):
412 """Sets up the android build environment.
415 opts: The options parsed from the command line through parse_args().
416 path_to_src: Path to the src checkout.
421 # Revert the environment variables back to default before setting them up
423 env_vars
= os
.environ
.copy()
424 for k
, _
in env_vars
.iteritems():
426 for k
, v
in ORIGINAL_ENV
.iteritems():
429 envsetup_path
= os
.path
.join('build', 'android', 'envsetup.sh')
430 proc
= subprocess
.Popen(['bash', '-c', 'source %s && env' % envsetup_path
],
431 stdout
=subprocess
.PIPE
,
432 stderr
=subprocess
.PIPE
,
434 out
, _
= proc
.communicate()
436 for line
in out
.splitlines():
437 k
, _
, v
= line
.partition('=')
440 # envsetup.sh no longer sets OS=android in GYP_DEFINES environment variable.
441 # (See http://crrev.com/170273005). So, we set this variable explicitly here
442 # in order to build Chrome on Android.
443 if 'GYP_DEFINES' not in os
.environ
:
444 os
.environ
['GYP_DEFINES'] = 'OS=android'
446 os
.environ
['GYP_DEFINES'] += ' OS=android'
449 os
.environ
['GYP_DEFINES'] += ' use_goma=1'
450 return not proc
.returncode