Esempio n. 1
0
def _FixPermissions(dest_base):
    """Last minute permission fixes."""
    # Set the suid bit for the chrome sandbox.
    # TODO(rcui): Implement this through a permission mask attribute in the Path
    # class.
    cros_build_lib.DebugRunCommand(['chmod', '-R', 'a+r', dest_base])
    cros_build_lib.DebugRunCommand([
        'find', dest_base, '-perm', '/110', '-exec', 'chmod', 'a+x', '{}', '+'
    ])
    target = os.path.join(dest_base, _CHROME_SANDBOX_DEST)
    if os.path.exists(target):
        cros_build_lib.DebugRunCommand(['chmod', '4755', target])
Esempio n. 2
0
def _PrepareStagingDir(options,
                       tempdir,
                       staging_dir,
                       copy_paths=None,
                       chrome_dir=_CHROME_DIR):
    """Place the necessary files in the staging directory.

  The staging directory is the directory used to rsync the build artifacts over
  to the device.  Only the necessary Chrome build artifacts are put into the
  staging directory.
  """
    osutils.SafeMakedirs(staging_dir)
    os.chmod(staging_dir, 0o755)
    if options.build_dir:
        with _StripBinContext(options) as strip_bin:
            strip_flags = (None if options.strip_flags is None else
                           shlex.split(options.strip_flags))
            chrome_util.StageChromeFromBuildDir(
                staging_dir,
                options.build_dir,
                strip_bin,
                sloppy=options.sloppy,
                gyp_defines=options.gyp_defines,
                staging_flags=options.staging_flags,
                strip_flags=strip_flags,
                copy_paths=copy_paths)
    else:
        pkg_path = options.local_pkg_path
        if options.gs_path:
            pkg_path = _FetchChromePackage(options.cache_dir, tempdir,
                                           options.gs_path)

        assert pkg_path
        logging.info('Extracting %s...', pkg_path)
        # Extract only the ./opt/google/chrome contents, directly into the staging
        # dir, collapsing the directory hierarchy.
        if pkg_path[-4:] == '.zip':
            cros_build_lib.DebugRunCommand([
                'unzip', '-X', pkg_path, _ANDROID_DIR_EXTRACT_PATH, '-d',
                staging_dir
            ])
            for filename in glob.glob(
                    os.path.join(staging_dir, 'system/chrome/*')):
                shutil.move(filename, staging_dir)
            osutils.RmDir(os.path.join(staging_dir, 'system'),
                          ignore_missing=True)
        else:
            cros_build_lib.DebugRunCommand([
                'tar', '--strip-components', '4', '--extract',
                '--preserve-permissions', '--file', pkg_path,
                '.%s' % chrome_dir
            ],
                                           cwd=staging_dir)
    def _FetchGoma(self):
        """Fetch, install, and start Goma, using cached version if it exists.

    Returns:
      A tuple (dir, port) containing the path to the cached goma/ dir and the
      Goma port.
    """
        common_path = os.path.join(self.options.cache_dir,
                                   constants.COMMON_CACHE)
        common_cache = cache.DiskCache(common_path)

        goma_dir = self.options.gomadir
        if not goma_dir:
            ref = common_cache.Lookup(('goma', '2'))
            if not ref.Exists():
                Log('Installing Goma.', silent=self.silent)
                with osutils.TempDir() as tempdir:
                    goma_dir = os.path.join(tempdir, 'goma')
                    os.mkdir(goma_dir)
                    result = cros_build_lib.DebugRunCommand(
                        self.FETCH_GOMA_CMD, cwd=goma_dir, error_code_ok=True)
                    if result.returncode:
                        raise GomaError('Failed to fetch Goma')
                # Update to latest version of goma. We choose the outside-chroot
                # version ('goobuntu') over the chroot version ('chromeos') by
                # supplying input='1' to the following prompt:
                #
                # What is your platform?
                #  1. Goobuntu  2. Precise (32bit)  3. Lucid (32bit)  4. Debian
                #  5. Chrome OS  6. MacOS ? -->
                    cros_build_lib.DebugRunCommand(
                        ['python2', 'goma_ctl.py', 'update'],
                        cwd=goma_dir,
                        input='1\n')
                    ref.SetDefault(goma_dir)
            goma_dir = ref.path

        port = None
        if self.options.start_goma:
            Log('Starting Goma.', silent=self.silent)
            cros_build_lib.DebugRunCommand(
                ['python2', 'goma_ctl.py', 'ensure_start'], cwd=goma_dir)
            port = self._GomaPort(goma_dir)
            Log('Goma is started on port %s', port, silent=self.silent)
            if not port:
                raise GomaError('No Goma port detected')

        return goma_dir, port
Esempio n. 4
0
    def _CopyFile(self, src, dest, path):
        """Perform the copy.

    Args:
      src: The path of the file/directory to copy.
      dest: The exact path of the destination. Does nothing if it already
            exists.
      path: The Path instance containing copy operation modifiers (such as
            Path.exe, Path.strip, etc.)
    """
        assert not os.path.isdir(src), '%s: Not expecting a directory!' % src

        # This file has already been copied by an earlier Path.
        if os.path.exists(dest):
            return

        osutils.SafeMakedirs(os.path.dirname(dest), mode=self.dir_mode)
        if path.exe and self.strip_bin and path.strip and os.path.getsize(
                src) > 0:
            strip_flags = (['--strip-unneeded']
                           if self.strip_flags is None else self.strip_flags)
            cros_build_lib.DebugRunCommand([self.strip_bin] + strip_flags +
                                           ['-o', dest, src])
            shutil.copystat(src, dest)
        else:
            shutil.copy2(src, dest)

        mode = path.mode
        if mode is None:
            mode = self.exe_mode if path.exe else self.default_mode
        os.chmod(dest, mode)
Esempio n. 5
0
    def Copy(self, src, dest, exe):
        """Perform the copy.

    Arguments:
      src: The path of the file/directory to copy.
      dest: The exact path of the destination.  Should not already exist.
      exe: If |src| is a file, whether the file is an executable.  If |src| is a
           directory, whether to treat the contents of the directory as
           executables.
    """
        def Log(directory):
            sep = ' [d] -> ' if directory else ' -> '
            logging.debug('%s %s %s', src, sep, dest)

        osutils.SafeMakedirs(os.path.dirname(dest))
        src_is_dir = os.path.isdir(src)
        Log(src_is_dir)
        if src_is_dir:
            # copytree() does not know about copying to a containing directory.
            if os.path.isdir(dest):
                dest = os.path.join(dest, os.path.basename(src))
            shutil.copytree(src, dest)
        elif exe and os.path.getsize(src) > 0:
            if self.strip_bin:
                cros_build_lib.DebugRunCommand(
                    [self.strip_bin, '--strip-unneeded', '-o', dest, src])
                shutil.copystat(src, dest)
            if self.exe_opts is not None:
                os.chmod(dest, self.exe_opts)
        else:
            shutil.copy2(src, dest)
Esempio n. 6
0
    def _FetchGoma(self):
        """Fetch, install, and start Goma, using cached version if it exists.

    Returns:
      A tuple (dir, port) containing the path to the cached goma/ dir and the
      Goma port.
    """
        common_path = os.path.join(self.options.cache_dir,
                                   constants.COMMON_CACHE)
        common_cache = cache.DiskCache(common_path)

        ref = common_cache.Lookup(('goma', ))
        if not ref.Exists():
            Log('Installing Goma.', silent=self.silent)
            with osutils.TempDir() as tempdir:
                goma_dir = os.path.join(tempdir, 'goma')
                os.mkdir(goma_dir)
                result = cros_build_lib.DebugRunCommand(self.FETCH_GOMA_CMD,
                                                        cwd=goma_dir,
                                                        error_code_ok=True)
                if result.returncode:
                    raise GomaError('Failed to fetch Goma')
                cros_build_lib.DebugRunCommand(
                    ['bash', 'goma_ctl.sh', 'update'],
                    cwd=goma_dir,
                    input='2\n')
                ref.SetDefault(goma_dir)
        goma_dir = os.path.join(ref.path, 'goma')

        port = self._GomaPort(goma_dir)
        if not port:
            Log('Starting Goma.', silent=self.silent)
            cros_build_lib.DebugRunCommand(['./goma_ctl.sh', 'ensure_start'],
                                           cwd=goma_dir)
            port = self._GomaPort(goma_dir)
            Log('Goma is started on port %s', port, silent=self.silent)
            if not port:
                raise GomaError('No Goma port detected')
        return goma_dir, port
Esempio n. 7
0
def StageChromeFromBuildDir(staging_dir,
                            build_dir,
                            strip_bin,
                            strict=False,
                            sloppy=False,
                            gyp_defines=None,
                            staging_flags=None):
    """Populates a staging directory with necessary build artifacts.

  If |strict| is set, then we decide what to stage based on the |gyp_defines|
  and |staging_flags| passed in.  Otherwise, we stage everything that we know
  about, that we can find.

  Arguments:
    staging_dir: Path to an empty staging directory.
    build_dir: Path to location of Chrome build artifacts.
    strip_bin: Path to executable used for stripping binaries.
    strict: If set, decide what to stage based on the |gyp_defines| and
            |staging_flags| passed in, and enforce that all optional files
            are copied.  Otherwise, we stage optional files if they are
            there, but we don't complain if they're not.
    sloppy: Ignore when mandatory artifacts are missing.
    gyp_defines: A dictionary (i.e., one returned by ProcessGypDefines)
      containing GYP_DEFINES Chrome was built with.
    staging_flags: A list of extra staging flags.  Valid flags are specified in
      STAGING_FLAGS.
  """
    if os.path.exists(staging_dir) and os.listdir(staging_dir):
        raise StagingError('Staging directory %s must be empty.' % staging_dir)

    osutils.SafeMakedirs(os.path.join(staging_dir, 'plugins'))
    cros_build_lib.DebugRunCommand(['chmod', '-R', '0755', staging_dir])

    if gyp_defines is None:
        gyp_defines = {}
    if staging_flags is None:
        staging_flags = []

    copier = Copier(strip_bin=strip_bin, exe_opts=0755)
    copied_paths = []
    for p in _COPY_PATHS:
        if not strict or p.ShouldProcess(gyp_defines, staging_flags):
            copied_paths += p.Copy(build_dir, staging_dir, copier, strict,
                                   sloppy)

    if not copied_paths:
        raise MissingPathError('Couldn\'t find anything to copy!\n'
                               'Are you looking in the right directory?\n'
                               'Aborting copy...')

    _FixPermissions(staging_dir)
Esempio n. 8
0
 def _SetupClang(self):
     """Install clang if needed."""
     clang_path = os.path.join(self.options.chrome_src, self._CLANG_DIR)
     if not os.path.exists(clang_path):
         try:
             update_sh = os.path.join(self.options.chrome_src,
                                      self._CLANG_UPDATE_SH)
             if not os.path.isfile(update_sh):
                 raise ClangError('%s not found.' % update_sh)
             results = cros_build_lib.DebugRunCommand(
                 [update_sh],
                 cwd=self.options.chrome_src,
                 error_code_ok=True)
             if results.returncode:
                 raise ClangError('Clang update failed with error code %s' %
                                  (results.returncode, ))
             if not os.path.exists(clang_path):
                 raise ClangError('%s not found.' % clang_path)
         except ClangError as e:
             logging.error(
                 'Encountered errors while installing/updating clang: %s',
                 e)
Esempio n. 9
0
    def _SetupTCEnvironment(self, sdk_ctx, options, env, goma_dir=None):
        """Sets up toolchain-related environment variables."""
        target_tc = sdk_ctx.key_map[self.sdk.TARGET_TOOLCHAIN_KEY].path
        tc_bin = os.path.join(target_tc, 'bin')
        env['PATH'] = '%s:%s' % (tc_bin, os.environ['PATH'])

        for var in ('CXX', 'CC', 'LD'):
            env[var] = self._FixGoldPath(env[var], target_tc)

        if options.clang:
            # clang++ requires C++ header paths to be explicitly specified.
            # See discussion on crbug.com/86037.
            tc_tuple = sdk_ctx.metadata['toolchain-tuple'][0]
            gcc_path = os.path.join(tc_bin, '%s-gcc' % tc_tuple)
            gcc_version = cros_build_lib.DebugRunCommand(
                [gcc_path, '-dumpversion'],
                redirect_stdout=True).output.strip()
            gcc_lib = 'usr/lib/gcc/%(tuple)s/%(ver)s/include/g++-v%(major_ver)s' % {
                'tuple': tc_tuple,
                'ver': gcc_version,
                'major_ver': gcc_version[0],
            }
            tc_gcc_lib = os.path.join(target_tc, gcc_lib)
            includes = []
            for p in ('', tc_tuple, 'backward'):
                includes.append('-isystem %s' % os.path.join(tc_gcc_lib, p))
            env['CC'] = 'clang'
            env['CXX'] = 'clang++ %s' % ' '.join(includes)

        env['CXX_host'] = 'g++'
        env['CC_host'] = 'gcc'

        if options.clang:
            clang_path = os.path.join(options.chrome_src, self._CLANG_DIR)
            env['PATH'] = '%s:%s' % (clang_path, env['PATH'])

        # The Goma path must appear before all the local compiler paths.
        if goma_dir:
            env['PATH'] = '%s:%s' % (goma_dir, env['PATH'])
Esempio n. 10
0
def _PrepareStagingDir(options, tempdir, staging_dir):
    """Place the necessary files in the staging directory.

  The staging directory is the directory used to rsync the build artifacts over
  to the device.  Only the necessary Chrome build artifacts are put into the
  staging directory.
  """
    osutils.SafeMakedirs(staging_dir)
    os.chmod(staging_dir, 0755)
    if options.build_dir:
        with _StripBinContext(options) as strip_bin:
            strip_flags = (None if options.strip_flags is None else
                           shlex.split(options.strip_flags))
            chrome_util.StageChromeFromBuildDir(
                staging_dir,
                options.build_dir,
                strip_bin,
                strict=options.strict,
                sloppy=options.sloppy,
                gyp_defines=options.gyp_defines,
                staging_flags=options.staging_flags,
                strip_flags=strip_flags)
    else:
        pkg_path = options.local_pkg_path
        if options.gs_path:
            pkg_path = _FetchChromePackage(options.cache_dir, tempdir,
                                           options.gs_path)

        assert pkg_path
        logging.info('Extracting %s...', pkg_path)
        # Extract only the ./opt/google/chrome contents, directly into the staging
        # dir, collapsing the directory hierarchy.
        cros_build_lib.DebugRunCommand([
            'tar', '--strip-components', '4', '--extract',
            '--preserve-permissions', '--file', pkg_path,
            '.%s' % _CHROME_DIR
        ],
                                       cwd=staging_dir)
Esempio n. 11
0
def _PrepareStagingDir(options, tempdir, staging_dir):
    """Place the necessary files in the staging directory.

  The staging directory is the directory used to rsync the build artifacts over
  to the device.  Only the necessary Chrome build artifacts are put into the
  staging directory.
  """
    if options.build_dir:
        sdk = cros_chrome_sdk.SDKFetcher(options.cache_dir, options.board)
        components = (sdk.TARGET_TOOLCHAIN_KEY, constants.CHROME_ENV_TAR)
        with sdk.Prepare(components=components) as ctx:
            env_path = os.path.join(ctx.key_map[constants.CHROME_ENV_TAR].path,
                                    constants.CHROME_ENV_FILE)
            strip_bin = osutils.SourceEnvironment(env_path, ['STRIP'])['STRIP']
            strip_bin = os.path.join(
                ctx.key_map[sdk.TARGET_TOOLCHAIN_KEY].path, 'bin',
                os.path.basename(strip_bin))
            chrome_util.StageChromeFromBuildDir(
                staging_dir,
                options.build_dir,
                strip_bin,
                strict=options.strict,
                sloppy=options.sloppy,
                gyp_defines=options.gyp_defines,
                staging_flags=options.staging_flags)
    else:
        pkg_path = options.local_pkg_path
        if options.gs_path:
            pkg_path = _FetchChromePackage(options.cache_dir, tempdir,
                                           options.gs_path)

        assert pkg_path
        logging.info('Extracting %s...', pkg_path)
        osutils.SafeMakedirs(staging_dir)
        cros_build_lib.DebugRunCommand(['tar', '-xpf', pkg_path],
                                       cwd=staging_dir)
Esempio n. 12
0
 def _GetStagingDirSize(self):
     result = cros_build_lib.DebugRunCommand(
         ['du', '-ks', self.staging_dir],
         redirect_stdout=True,
         capture_output=True)
     return int(result.output.split()[0])
Esempio n. 13
0
def _FixPermissions(dest_base):
    """Last minute permission fixes."""
    cros_build_lib.DebugRunCommand(['chmod', '-R', 'a+r', dest_base])
    cros_build_lib.DebugRunCommand([
        'find', dest_base, '-perm', '/110', '-exec', 'chmod', 'a+x', '{}', '+'
    ])