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 # Need to re-escape goma dir, see crbug.com/394990.
44 opts
.goma_dir
= opts
.goma_dir
.encode('string_escape')
45 SetBuildSystemDefault(opts
.build_preference
, opts
.use_goma
,
46 opts
.goma_dir
, opts
.target_arch
)
48 if not opts
.build_preference
:
49 if 'ninja' in os
.getenv('GYP_GENERATORS', default
=''):
50 opts
.build_preference
= 'ninja'
52 opts
.build_preference
= 'make'
54 SetBuildSystemDefault(opts
.build_preference
, opts
.use_goma
, opts
.goma_dir
)
56 if not SetupPlatformBuildEnvironment(opts
):
57 raise RuntimeError('Failed to set platform environment.')
61 """Constructs and returns a Builder object.
64 opts: Options parsed from the command-line.
67 if opts
.target_platform
== 'android':
68 builder
= AndroidBuilder(opts
)
69 elif opts
.target_platform
== 'android-chrome':
70 builder
= AndroidChromeBuilder(opts
)
72 builder
= DesktopBuilder(opts
)
75 def Build(self
, depot
, opts
):
76 """Runs a command to build Chrome."""
77 raise NotImplementedError()
80 def GetBuildOutputDirectory(opts
, src_dir
=None):
81 """Returns the path to the build directory, relative to the checkout root.
83 Assumes that the current working directory is the checkout root.
86 opts: Command-line options.
87 src_dir: Path to chromium/src directory.
90 A path to the directory to use as build output directory.
93 NotImplementedError: The platform according to sys.platform is unexpected.
95 src_dir
= src_dir
or 'src'
96 if opts
.build_preference
== 'ninja' or bisect_utils
.IsLinuxHost():
97 return os
.path
.join(src_dir
, 'out')
98 if bisect_utils
.IsMacHost():
99 return os
.path
.join(src_dir
, 'xcodebuild')
100 if bisect_utils
.IsWindowsHost():
101 return os
.path
.join(src_dir
, 'build')
102 raise NotImplementedError('Unexpected platform %s' % sys
.platform
)
105 class DesktopBuilder(Builder
):
106 """DesktopBuilder is used to build Chromium on Linux, Mac, or Windows."""
108 def __init__(self
, opts
):
109 super(DesktopBuilder
, self
).__init
__(opts
)
111 def Build(self
, depot
, opts
):
112 """Builds chromium_builder_perf target using options passed into the script.
115 depot: Name of current depot being bisected.
116 opts: The options parsed from the command line.
119 True if build was successful.
121 targets
= ['chromium_builder_perf']
125 threads
= opts
.goma_threads
127 build_success
= False
128 if opts
.build_preference
== 'make':
129 build_success
= BuildWithMake(threads
, targets
, opts
.target_build_type
)
130 elif opts
.build_preference
== 'ninja':
131 build_success
= BuildWithNinja(threads
, targets
, opts
.target_build_type
)
132 elif opts
.build_preference
== 'msvs':
133 assert bisect_utils
.IsWindowsHost(), 'msvs is only supported on Windows.'
134 build_success
= BuildWithVisualStudio(targets
, opts
.target_build_type
)
136 assert False, 'No build system defined.'
140 class AndroidBuilder(Builder
):
141 """AndroidBuilder is used to build on android."""
143 def __init__(self
, opts
):
144 super(AndroidBuilder
, self
).__init
__(opts
)
146 # TODO(qyearsley): Make this a class method and verify that it works with
148 # pylint: disable=R0201
149 def _GetTargets(self
):
150 """Returns a list of build targets."""
157 def Build(self
, depot
, opts
):
158 """Builds the android content shell and other necessary tools.
161 depot: Current depot being bisected.
162 opts: The options parsed from the command line.
165 True if build was successful.
169 threads
= opts
.goma_threads
171 build_success
= False
172 if opts
.build_preference
== 'ninja':
173 build_success
= BuildWithNinja(
174 threads
, self
._GetTargets
(), opts
.target_build_type
)
176 assert False, 'No build system defined.'
181 class AndroidChromeBuilder(AndroidBuilder
):
182 """AndroidChromeBuilder is used to build "android-chrome".
184 This is slightly different from AndroidBuilder.
187 def __init__(self
, opts
):
188 super(AndroidChromeBuilder
, self
).__init
__(opts
)
190 # TODO(qyearsley): Make this a class method and verify that it works with
192 # pylint: disable=R0201
193 def _GetTargets(self
):
194 """Returns a list of build targets."""
195 return AndroidBuilder
._GetTargets
(self
) + ['chrome_apk']
198 def SetBuildSystemDefault(build_system
, use_goma
, goma_dir
, target_arch
='ia32'):
199 """Sets up any environment variables needed to build with the specified build
203 build_system: A string specifying build system. Currently only 'ninja' or
204 'make' are supported.
205 use_goma: Determines whether to GOMA for compile.
206 goma_dir: GOMA directory path.
207 target_arch: The target build architecture, ia32 or x64. Default is ia32.
209 if build_system
== 'ninja':
210 gyp_var
= os
.getenv('GYP_GENERATORS', default
='')
212 if not gyp_var
or not 'ninja' in gyp_var
:
214 os
.environ
['GYP_GENERATORS'] = gyp_var
+ ',ninja'
216 os
.environ
['GYP_GENERATORS'] = 'ninja'
218 if bisect_utils
.IsWindowsHost():
219 os
.environ
['GYP_DEFINES'] = 'component=shared_library '\
220 'incremental_chrome_dll=1 disable_nacl=1 fastbuild=1 '\
223 elif build_system
== 'make':
224 os
.environ
['GYP_GENERATORS'] = 'make'
226 raise RuntimeError('%s build not supported.' % build_system
)
229 os
.environ
['GYP_DEFINES'] = '%s %s' % (os
.getenv('GYP_DEFINES', default
=''),
232 os
.environ
['GYP_DEFINES'] += ' gomadir=%s' % goma_dir
234 # Produce 64 bit chromium binaries when target architecure is set to x64.
235 if target_arch
== 'x64':
236 os
.environ
['GYP_DEFINES'] += ' target_arch=%s' % target_arch
238 def SetupPlatformBuildEnvironment(opts
):
239 """Performs any platform-specific setup.
242 opts: The options parsed from the command line through parse_args().
247 if 'android' in opts
.target_platform
:
248 CopyAndSaveOriginalEnvironmentVars()
249 return SetupAndroidBuildEnvironment(opts
)
253 def BuildWithMake(threads
, targets
, build_type
='Release'):
254 """Runs a make command with the given targets.
257 threads: The number of threads to use. None means unspecified/unlimited.
258 targets: List of make targets.
259 build_type: Release or Debug.
262 True if the command had a 0 exit code, False otherwise.
264 cmd
= ['make', 'BUILDTYPE=%s' % build_type
]
266 cmd
.append('-j%d' % threads
)
268 return_code
= bisect_utils
.RunProcess(cmd
)
269 return not return_code
272 def BuildWithNinja(threads
, targets
, build_type
='Release'):
273 """Runs a ninja command with the given targets."""
274 cmd
= ['ninja', '-C', os
.path
.join('out', build_type
)]
276 cmd
.append('-j%d' % threads
)
278 return_code
= bisect_utils
.RunProcess(cmd
)
279 return not return_code
282 def BuildWithVisualStudio(targets
, build_type
='Release'):
283 """Runs a command to build the given targets with Visual Studio."""
284 path_to_devenv
= os
.path
.abspath(
285 os
.path
.join(os
.environ
['VS100COMNTOOLS'], '..', 'IDE', 'devenv.com'))
286 path_to_sln
= os
.path
.join(os
.getcwd(), 'chrome', 'chrome.sln')
287 cmd
= [path_to_devenv
, '/build', build_type
, path_to_sln
]
289 cmd
.extend(['/Project', t
])
290 return_code
= bisect_utils
.RunProcess(cmd
)
291 return not return_code
294 def CopyAndSaveOriginalEnvironmentVars():
295 """Makes a copy of the current environment variables.
297 Before making a copy of the environment variables and setting a global
298 variable, this function unsets a certain set of environment variables.
300 # TODO: Waiting on crbug.com/255689, will remove this after.
306 'GYP_GENERATOR_FLAGS',
309 for key
in os
.environ
:
311 vars_to_remove
.append(key
)
312 for key
in vars_to_remove
:
313 if os
.environ
.has_key(key
):
317 ORIGINAL_ENV
= os
.environ
.copy()
320 def SetupAndroidBuildEnvironment(opts
, path_to_src
=None):
321 """Sets up the android build environment.
324 opts: The options parsed from the command line through parse_args().
325 path_to_src: Path to the src checkout.
330 # Revert the environment variables back to default before setting them up
332 env_vars
= os
.environ
.copy()
333 for k
, _
in env_vars
.iteritems():
335 for k
, v
in ORIGINAL_ENV
.iteritems():
338 envsetup_path
= os
.path
.join('build', 'android', 'envsetup.sh')
339 proc
= subprocess
.Popen(['bash', '-c', 'source %s && env' % envsetup_path
],
340 stdout
=subprocess
.PIPE
,
341 stderr
=subprocess
.PIPE
,
343 out
, _
= proc
.communicate()
345 for line
in out
.splitlines():
346 k
, _
, v
= line
.partition('=')
349 # envsetup.sh no longer sets OS=android in GYP_DEFINES environment variable.
350 # (See http://crrev.com/170273005). So, we set this variable explicitly here
351 # in order to build Chrome on Android.
352 if 'GYP_DEFINES' not in os
.environ
:
353 os
.environ
['GYP_DEFINES'] = 'OS=android'
355 os
.environ
['GYP_DEFINES'] += ' OS=android'
358 os
.environ
['GYP_DEFINES'] += ' use_goma=1'
359 return not proc
.returncode