OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 """Classes and functions for building Chrome. |
| 6 |
| 7 This includes functions for running commands to build, as well as |
| 8 specific rules about which targets to build. |
| 9 """ |
| 10 |
| 11 import os |
| 12 import subprocess |
| 13 import sys |
| 14 |
| 15 import bisect_utils |
| 16 |
| 17 ORIGINAL_ENV = {} |
| 18 |
| 19 |
| 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. |
| 26 |
| 27 Args: |
| 28 opts: Options parsed from command line. |
| 29 |
| 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' |
| 36 |
| 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' |
| 50 |
| 51 SetBuildSystemDefault(opts.build_preference, opts.use_goma, opts.goma_dir) |
| 52 |
| 53 if not SetupPlatformBuildEnvironment(opts): |
| 54 raise RuntimeError('Failed to set platform environment.') |
| 55 |
| 56 @staticmethod |
| 57 def FromOpts(opts): |
| 58 """Constructs and returns a Builder object. |
| 59 |
| 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 |
| 73 |
| 74 def Build(self, depot, opts): |
| 75 """Runs a command to build Chrome.""" |
| 76 raise NotImplementedError() |
| 77 |
| 78 |
| 79 def GetBuildOutputDirectory(opts, src_dir=None): |
| 80 """Returns the path to the build directory, relative to the checkout root. |
| 81 |
| 82 Assumes that the current working directory is the checkout root. |
| 83 |
| 84 Args: |
| 85 opts: Command-line options. |
| 86 src_dir: Path to chromium/src directory. |
| 87 |
| 88 Returns: |
| 89 A path to the directory to use as build output directory. |
| 90 |
| 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) |
| 102 |
| 103 |
| 104 class DesktopBuilder(Builder): |
| 105 """DesktopBuilder is used to build Chromium on Linux, Mac, or Windows.""" |
| 106 |
| 107 def __init__(self, opts): |
| 108 super(DesktopBuilder, self).__init__(opts) |
| 109 |
| 110 def Build(self, depot, opts): |
| 111 """Builds chromium_builder_perf target using options passed into the script. |
| 112 |
| 113 Args: |
| 114 depot: Name of current depot being bisected. |
| 115 opts: The options parsed from the command line. |
| 116 |
| 117 Returns: |
| 118 True if build was successful. |
| 119 """ |
| 120 targets = ['chromium_builder_perf'] |
| 121 |
| 122 threads = None |
| 123 if opts.use_goma: |
| 124 threads = 64 |
| 125 |
| 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 |
| 137 |
| 138 |
| 139 class AndroidBuilder(Builder): |
| 140 """AndroidBuilder is used to build on android.""" |
| 141 |
| 142 def __init__(self, opts): |
| 143 super(AndroidBuilder, self).__init__(opts) |
| 144 |
| 145 def _GetTargets(self): |
| 146 """Returns a list of build targets.""" |
| 147 return ['chrome_shell_apk', 'cc_perftests_apk', 'android_tools'] |
| 148 |
| 149 def Build(self, depot, opts): |
| 150 """Builds the android content shell and other necessary tools. |
| 151 |
| 152 Args: |
| 153 depot: Current depot being bisected. |
| 154 opts: The options parsed from the command line. |
| 155 |
| 156 Returns: |
| 157 True if build was successful. |
| 158 """ |
| 159 threads = None |
| 160 if opts.use_goma: |
| 161 threads = 64 |
| 162 |
| 163 build_success = False |
| 164 if opts.build_preference == 'ninja': |
| 165 build_success = BuildWithNinja( |
| 166 threads, self._GetTargets(), opts.target_build_type) |
| 167 else: |
| 168 assert False, 'No build system defined.' |
| 169 |
| 170 return build_success |
| 171 |
| 172 |
| 173 class AndroidChromeBuilder(AndroidBuilder): |
| 174 """AndroidChromeBuilder is used to build "android-chrome". |
| 175 |
| 176 This is slightly different from AndroidBuilder. |
| 177 """ |
| 178 |
| 179 def __init__(self, opts): |
| 180 super(AndroidChromeBuilder, self).__init__(opts) |
| 181 |
| 182 def _GetTargets(self): |
| 183 """Returns a list of build targets.""" |
| 184 return AndroidBuilder._GetTargets(self) + ['chrome_apk'] |
| 185 |
| 186 |
| 187 class CrosBuilder(Builder): |
| 188 """CrosBuilder is used to build and image ChromeOS/Chromium. |
| 189 |
| 190 WARNING(qyearsley, 2014-08-15): This hasn't been tested recently. |
| 191 """ |
| 192 |
| 193 def __init__(self, opts): |
| 194 super(CrosBuilder, self).__init__(opts) |
| 195 |
| 196 @staticmethod |
| 197 def ImageToTarget(opts): |
| 198 """Installs latest image to target specified by opts.cros_remote_ip. |
| 199 |
| 200 Args: |
| 201 opts: Program options containing cros_board and cros_remote_ip. |
| 202 |
| 203 Returns: |
| 204 True if successful. |
| 205 """ |
| 206 try: |
| 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'] |
| 213 |
| 214 return_code = bisect_utils.RunProcess(cmd) |
| 215 return not return_code |
| 216 except OSError: |
| 217 return False |
| 218 |
| 219 @staticmethod |
| 220 def BuildPackages(opts, depot): |
| 221 """Builds packages for cros. |
| 222 |
| 223 Args: |
| 224 opts: Program options containing cros_board. |
| 225 depot: The depot being bisected. |
| 226 |
| 227 Returns: |
| 228 True if successful. |
| 229 """ |
| 230 cmd = [bisect_utils.CROS_SDK_PATH] |
| 231 |
| 232 if depot != 'cros': |
| 233 path_to_chrome = os.path.join(os.getcwd(), '..') |
| 234 cmd += ['--chrome_root=%s' % path_to_chrome] |
| 235 |
| 236 cmd += ['--'] |
| 237 |
| 238 if depot != 'cros': |
| 239 cmd += ['CHROME_ORIGIN=LOCAL_SOURCE'] |
| 240 |
| 241 cmd += ['BUILDTYPE=%s' % opts.target_build_type, './build_packages', |
| 242 '--board=%s' % opts.cros_board] |
| 243 return_code = bisect_utils.RunProcess(cmd) |
| 244 |
| 245 return not return_code |
| 246 |
| 247 @staticmethod |
| 248 def BuildImage(opts, depot): |
| 249 """Builds test image for cros. |
| 250 |
| 251 Args: |
| 252 opts: Program options containing cros_board. |
| 253 depot: The depot being bisected. |
| 254 |
| 255 Returns: |
| 256 True if successful. |
| 257 """ |
| 258 cmd = [bisect_utils.CROS_SDK_PATH] |
| 259 |
| 260 if depot != 'cros': |
| 261 path_to_chrome = os.path.join(os.getcwd(), '..') |
| 262 cmd += ['--chrome_root=%s' % path_to_chrome] |
| 263 |
| 264 cmd += ['--'] |
| 265 |
| 266 if depot != 'cros': |
| 267 cmd += ['CHROME_ORIGIN=LOCAL_SOURCE'] |
| 268 |
| 269 cmd += ['BUILDTYPE=%s' % opts.target_build_type, '--', './build_image', |
| 270 '--board=%s' % opts.cros_board, 'test'] |
| 271 |
| 272 return_code = bisect_utils.RunProcess(cmd) |
| 273 |
| 274 return not return_code |
| 275 |
| 276 def Build(self, depot, opts): |
| 277 """Builds targets using options passed into the script. |
| 278 |
| 279 Args: |
| 280 depot: Current depot being bisected. |
| 281 opts: The options parsed from the command line. |
| 282 |
| 283 Returns: |
| 284 True if build was successful. |
| 285 """ |
| 286 if self.BuildPackages(opts, depot): |
| 287 if self.BuildImage(opts, depot): |
| 288 return self.ImageToTarget(opts) |
| 289 return False |
| 290 |
| 291 |
| 292 def SetBuildSystemDefault(build_system, use_goma, goma_dir): |
| 293 """Sets up any environment variables needed to build with the specified build |
| 294 system. |
| 295 |
| 296 Args: |
| 297 build_system: A string specifying build system. Currently only 'ninja' or |
| 298 'make' are supported. |
| 299 """ |
| 300 if build_system == 'ninja': |
| 301 gyp_var = os.getenv('GYP_GENERATORS', default='') |
| 302 |
| 303 if not gyp_var or not 'ninja' in gyp_var: |
| 304 if gyp_var: |
| 305 os.environ['GYP_GENERATORS'] = gyp_var + ',ninja' |
| 306 else: |
| 307 os.environ['GYP_GENERATORS'] = 'ninja' |
| 308 |
| 309 if bisect_utils.IsWindowsHost(): |
| 310 os.environ['GYP_DEFINES'] = 'component=shared_library '\ |
| 311 'incremental_chrome_dll=1 disable_nacl=1 fastbuild=1 '\ |
| 312 'chromium_win_pch=0' |
| 313 |
| 314 elif build_system == 'make': |
| 315 os.environ['GYP_GENERATORS'] = 'make' |
| 316 else: |
| 317 raise RuntimeError('%s build not supported.' % build_system) |
| 318 |
| 319 if use_goma: |
| 320 os.environ['GYP_DEFINES'] = '%s %s' % (os.getenv('GYP_DEFINES', default=''), |
| 321 'use_goma=1') |
| 322 if goma_dir: |
| 323 os.environ['GYP_DEFINES'] += ' gomadir=%s' % goma_dir |
| 324 |
| 325 |
| 326 def SetupPlatformBuildEnvironment(opts): |
| 327 """Performs any platform-specific setup. |
| 328 |
| 329 Args: |
| 330 opts: The options parsed from the command line through parse_args(). |
| 331 |
| 332 Returns: |
| 333 True if successful. |
| 334 """ |
| 335 if 'android' in opts.target_platform: |
| 336 CopyAndSaveOriginalEnvironmentVars() |
| 337 return SetupAndroidBuildEnvironment(opts) |
| 338 elif opts.target_platform == 'cros': |
| 339 return bisect_utils.SetupCrosRepo() |
| 340 return True |
| 341 |
| 342 |
| 343 def BuildWithMake(threads, targets, build_type='Release'): |
| 344 """Runs a make command with the given targets. |
| 345 |
| 346 Args: |
| 347 threads: The number of threads to use. None means unspecified/unlimited. |
| 348 targets: List of make targets. |
| 349 build_type: Release or Debug. |
| 350 |
| 351 Returns: |
| 352 True if the command had a 0 exit code, False otherwise. |
| 353 """ |
| 354 cmd = ['make', 'BUILDTYPE=%s' % build_type] |
| 355 if threads: |
| 356 cmd.append('-j%d' % threads) |
| 357 cmd += targets |
| 358 return_code = bisect_utils.RunProcess(cmd) |
| 359 return not return_code |
| 360 |
| 361 |
| 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)] |
| 365 if threads: |
| 366 cmd.append('-j%d' % threads) |
| 367 cmd += targets |
| 368 return_code = bisect_utils.RunProcess(cmd) |
| 369 return not return_code |
| 370 |
| 371 |
| 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] |
| 378 for t in targets: |
| 379 cmd.extend(['/Project', t]) |
| 380 return_code = bisect_utils.RunProcess(cmd) |
| 381 return not return_code |
| 382 |
| 383 |
| 384 def CopyAndSaveOriginalEnvironmentVars(): |
| 385 """Makes a copy of the current environment variables. |
| 386 |
| 387 Before making a copy of the environment variables and setting a global |
| 388 variable, this function unsets a certain set of environment variables. |
| 389 """ |
| 390 # TODO: Waiting on crbug.com/255689, will remove this after. |
| 391 vars_to_remove = [ |
| 392 'CHROME_SRC', |
| 393 'CHROMIUM_GYP_FILE', |
| 394 'GYP_CROSSCOMPILE', |
| 395 'GYP_DEFINES', |
| 396 'GYP_GENERATORS', |
| 397 'GYP_GENERATOR_FLAGS', |
| 398 'OBJCOPY', |
| 399 ] |
| 400 for key in os.environ: |
| 401 if 'ANDROID' in key: |
| 402 vars_to_remove.append(key) |
| 403 for key in vars_to_remove: |
| 404 if os.environ.has_key(key): |
| 405 del os.environ[key] |
| 406 |
| 407 global ORIGINAL_ENV |
| 408 ORIGINAL_ENV = os.environ.copy() |
| 409 |
| 410 |
| 411 def SetupAndroidBuildEnvironment(opts, path_to_src=None): |
| 412 """Sets up the android build environment. |
| 413 |
| 414 Args: |
| 415 opts: The options parsed from the command line through parse_args(). |
| 416 path_to_src: Path to the src checkout. |
| 417 |
| 418 Returns: |
| 419 True if successful. |
| 420 """ |
| 421 # Revert the environment variables back to default before setting them up |
| 422 # with envsetup.sh. |
| 423 env_vars = os.environ.copy() |
| 424 for k, _ in env_vars.iteritems(): |
| 425 del os.environ[k] |
| 426 for k, v in ORIGINAL_ENV.iteritems(): |
| 427 os.environ[k] = v |
| 428 |
| 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, |
| 433 cwd=path_to_src) |
| 434 out, _ = proc.communicate() |
| 435 |
| 436 for line in out.splitlines(): |
| 437 k, _, v = line.partition('=') |
| 438 os.environ[k] = v |
| 439 |
| 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' |
| 445 else: |
| 446 os.environ['GYP_DEFINES'] += ' OS=android' |
| 447 |
| 448 if opts.use_goma: |
| 449 os.environ['GYP_DEFINES'] += ' use_goma=1' |
| 450 return not proc.returncode |
OLD | NEW |