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 from . 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 def __init__(self, opts): |
| 191 super(CrosBuilder, self).__init__(opts) |
| 192 |
| 193 @staticmethod |
| 194 def ImageToTarget(opts): |
| 195 """Installs latest image to target specified by opts.cros_remote_ip. |
| 196 |
| 197 Args: |
| 198 opts: Program options containing cros_board and cros_remote_ip. |
| 199 |
| 200 Returns: |
| 201 True if successful. |
| 202 """ |
| 203 try: |
| 204 # Keys will most likely be set to 0640 after wiping the chroot. |
| 205 os.chmod(bisect_utils.CROS_SCRIPT_KEY_PATH, 0600) |
| 206 os.chmod(bisect_utils.CROS_TEST_KEY_PATH, 0600) |
| 207 cmd = [bisect_utils.CROS_SDK_PATH, '--', './bin/cros_image_to_target.py', |
| 208 '--remote=%s' % opts.cros_remote_ip, |
| 209 '--board=%s' % opts.cros_board, '--test', '--verbose'] |
| 210 |
| 211 return_code = bisect_utils.RunProcess(cmd) |
| 212 return not return_code |
| 213 except OSError: |
| 214 return False |
| 215 |
| 216 @staticmethod |
| 217 def BuildPackages(opts, depot): |
| 218 """Builds packages for cros. |
| 219 |
| 220 Args: |
| 221 opts: Program options containing cros_board. |
| 222 depot: The depot being bisected. |
| 223 |
| 224 Returns: |
| 225 True if successful. |
| 226 """ |
| 227 cmd = [bisect_utils.CROS_SDK_PATH] |
| 228 |
| 229 if depot != 'cros': |
| 230 path_to_chrome = os.path.join(os.getcwd(), '..') |
| 231 cmd += ['--chrome_root=%s' % path_to_chrome] |
| 232 |
| 233 cmd += ['--'] |
| 234 |
| 235 if depot != 'cros': |
| 236 cmd += ['CHROME_ORIGIN=LOCAL_SOURCE'] |
| 237 |
| 238 cmd += ['BUILDTYPE=%s' % opts.target_build_type, './build_packages', |
| 239 '--board=%s' % opts.cros_board] |
| 240 return_code = bisect_utils.RunProcess(cmd) |
| 241 |
| 242 return not return_code |
| 243 |
| 244 @staticmethod |
| 245 def BuildImage(opts, depot): |
| 246 """Builds test image for cros. |
| 247 |
| 248 Args: |
| 249 opts: Program options containing cros_board. |
| 250 depot: The depot being bisected. |
| 251 |
| 252 Returns: |
| 253 True if successful. |
| 254 """ |
| 255 cmd = [bisect_utils.CROS_SDK_PATH] |
| 256 |
| 257 if depot != 'cros': |
| 258 path_to_chrome = os.path.join(os.getcwd(), '..') |
| 259 cmd += ['--chrome_root=%s' % path_to_chrome] |
| 260 |
| 261 cmd += ['--'] |
| 262 |
| 263 if depot != 'cros': |
| 264 cmd += ['CHROME_ORIGIN=LOCAL_SOURCE'] |
| 265 |
| 266 cmd += ['BUILDTYPE=%s' % opts.target_build_type, '--', './build_image', |
| 267 '--board=%s' % opts.cros_board, 'test'] |
| 268 |
| 269 return_code = bisect_utils.RunProcess(cmd) |
| 270 |
| 271 return not return_code |
| 272 |
| 273 def Build(self, depot, opts): |
| 274 """Builds targets using options passed into the script. |
| 275 |
| 276 Args: |
| 277 depot: Current depot being bisected. |
| 278 opts: The options parsed from the command line. |
| 279 |
| 280 Returns: |
| 281 True if build was successful. |
| 282 """ |
| 283 if self.BuildPackages(opts, depot): |
| 284 if self.BuildImage(opts, depot): |
| 285 return self.ImageToTarget(opts) |
| 286 return False |
| 287 |
| 288 |
| 289 def SetBuildSystemDefault(build_system, use_goma, goma_dir): |
| 290 """Sets up any environment variables needed to build with the specified build |
| 291 system. |
| 292 |
| 293 Args: |
| 294 build_system: A string specifying build system. Currently only 'ninja' or |
| 295 'make' are supported. |
| 296 """ |
| 297 if build_system == 'ninja': |
| 298 gyp_var = os.getenv('GYP_GENERATORS', default='') |
| 299 |
| 300 if not gyp_var or not 'ninja' in gyp_var: |
| 301 if gyp_var: |
| 302 os.environ['GYP_GENERATORS'] = gyp_var + ',ninja' |
| 303 else: |
| 304 os.environ['GYP_GENERATORS'] = 'ninja' |
| 305 |
| 306 if bisect_utils.IsWindowsHost(): |
| 307 os.environ['GYP_DEFINES'] = 'component=shared_library '\ |
| 308 'incremental_chrome_dll=1 disable_nacl=1 fastbuild=1 '\ |
| 309 'chromium_win_pch=0' |
| 310 |
| 311 elif build_system == 'make': |
| 312 os.environ['GYP_GENERATORS'] = 'make' |
| 313 else: |
| 314 raise RuntimeError('%s build not supported.' % build_system) |
| 315 |
| 316 if use_goma: |
| 317 os.environ['GYP_DEFINES'] = '%s %s' % (os.getenv('GYP_DEFINES', default=''), |
| 318 'use_goma=1') |
| 319 if goma_dir: |
| 320 os.environ['GYP_DEFINES'] += ' gomadir=%s' % goma_dir |
| 321 |
| 322 |
| 323 def SetupPlatformBuildEnvironment(opts): |
| 324 """Performs any platform-specific setup. |
| 325 |
| 326 Args: |
| 327 opts: The options parsed from the command line through parse_args(). |
| 328 |
| 329 Returns: |
| 330 True if successful. |
| 331 """ |
| 332 if 'android' in opts.target_platform: |
| 333 CopyAndSaveOriginalEnvironmentVars() |
| 334 return SetupAndroidBuildEnvironment(opts) |
| 335 elif opts.target_platform == 'cros': |
| 336 return bisect_utils.SetupCrosRepo() |
| 337 return True |
| 338 |
| 339 |
| 340 def BuildWithMake(threads, targets, build_type='Release'): |
| 341 """Runs a make command with the given targets. |
| 342 |
| 343 Args: |
| 344 threads: The number of threads to use. None means unspecified/unlimited. |
| 345 targets: List of make targets. |
| 346 build_type: Release or Debug. |
| 347 |
| 348 Returns: |
| 349 True if the command had a 0 exit code, False otherwise. |
| 350 """ |
| 351 cmd = ['make', 'BUILDTYPE=%s' % build_type] |
| 352 if threads: |
| 353 cmd.append('-j%d' % threads) |
| 354 cmd += targets |
| 355 return_code = bisect_utils.RunProcess(cmd) |
| 356 return not return_code |
| 357 |
| 358 |
| 359 def BuildWithNinja(threads, targets, build_type='Release'): |
| 360 """Runs a ninja command with the given targets.""" |
| 361 cmd = ['ninja', '-C', os.path.join('out', build_type)] |
| 362 if threads: |
| 363 cmd.append('-j%d' % threads) |
| 364 cmd += targets |
| 365 return_code = bisect_utils.RunProcess(cmd) |
| 366 return not return_code |
| 367 |
| 368 |
| 369 def BuildWithVisualStudio(targets, build_type='Release'): |
| 370 """Runs a command to build the given targets with Visual Studio.""" |
| 371 path_to_devenv = os.path.abspath( |
| 372 os.path.join(os.environ['VS100COMNTOOLS'], '..', 'IDE', 'devenv.com')) |
| 373 path_to_sln = os.path.join(os.getcwd(), 'chrome', 'chrome.sln') |
| 374 cmd = [path_to_devenv, '/build', build_type, path_to_sln] |
| 375 for t in targets: |
| 376 cmd.extend(['/Project', t]) |
| 377 return_code = bisect_utils.RunProcess(cmd) |
| 378 return not return_code |
| 379 |
| 380 |
| 381 def CopyAndSaveOriginalEnvironmentVars(): |
| 382 """Makes a copy of the current environment variables. |
| 383 |
| 384 Before making a copy of the environment variables and setting a global |
| 385 variable, this function unsets a certain set of environment variables. |
| 386 """ |
| 387 # TODO: Waiting on crbug.com/255689, will remove this after. |
| 388 vars_to_remove = [ |
| 389 'CHROME_SRC', |
| 390 'CHROMIUM_GYP_FILE', |
| 391 'GYP_CROSSCOMPILE', |
| 392 'GYP_DEFINES', |
| 393 'GYP_GENERATORS', |
| 394 'GYP_GENERATOR_FLAGS', |
| 395 'OBJCOPY', |
| 396 ] |
| 397 for key in os.environ: |
| 398 if 'ANDROID' in key: |
| 399 vars_to_remove.append(key) |
| 400 for key in vars_to_remove: |
| 401 if os.environ.has_key(key): |
| 402 del os.environ[key] |
| 403 |
| 404 global ORIGINAL_ENV |
| 405 ORIGINAL_ENV = os.environ.copy() |
| 406 |
| 407 |
| 408 def SetupAndroidBuildEnvironment(opts, path_to_src=None): |
| 409 """Sets up the android build environment. |
| 410 |
| 411 Args: |
| 412 opts: The options parsed from the command line through parse_args(). |
| 413 path_to_src: Path to the src checkout. |
| 414 |
| 415 Returns: |
| 416 True if successful. |
| 417 """ |
| 418 # Revert the environment variables back to default before setting them up |
| 419 # with envsetup.sh. |
| 420 env_vars = os.environ.copy() |
| 421 for k, _ in env_vars.iteritems(): |
| 422 del os.environ[k] |
| 423 for k, v in ORIGINAL_ENV.iteritems(): |
| 424 os.environ[k] = v |
| 425 |
| 426 envsetup_path = os.path.join('build', 'android', 'envsetup.sh') |
| 427 proc = subprocess.Popen(['bash', '-c', 'source %s && env' % envsetup_path], |
| 428 stdout=subprocess.PIPE, |
| 429 stderr=subprocess.PIPE, |
| 430 cwd=path_to_src) |
| 431 out, _ = proc.communicate() |
| 432 |
| 433 for line in out.splitlines(): |
| 434 k, _, v = line.partition('=') |
| 435 os.environ[k] = v |
| 436 |
| 437 # envsetup.sh no longer sets OS=android in GYP_DEFINES environment variable. |
| 438 # (See http://crrev.com/170273005). So, we set this variable explicitly here |
| 439 # in order to build Chrome on Android. |
| 440 if 'GYP_DEFINES' not in os.environ: |
| 441 os.environ['GYP_DEFINES'] = 'OS=android' |
| 442 else: |
| 443 os.environ['GYP_DEFINES'] += ' OS=android' |
| 444 |
| 445 if opts.use_goma: |
| 446 os.environ['GYP_DEFINES'] += ' use_goma=1' |
| 447 return not proc.returncode |
OLD | NEW |