Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / chromite / cbuildbot / stages / sdk_stages.py
1 # Copyright (c) 2013 The Chromium OS 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 """Module containing SDK stages."""
6
7 from __future__ import print_function
8
9 import json
10 import os
11
12 from chromite.cbuildbot import constants
13 from chromite.cbuildbot.stages import generic_stages
14 from chromite.lib import cros_build_lib
15 from chromite.lib import git
16 from chromite.lib import osutils
17 from chromite.lib import portage_util
18
19
20 class SDKPackageStage(generic_stages.BuilderStage):
21   """Stage that performs preparing and packaging SDK files"""
22
23   # Version of the Manifest file being generated. Should be incremented for
24   # Major format changes.
25   MANIFEST_VERSION = '1'
26   _EXCLUDED_PATHS = ('usr/lib/debug', constants.AUTOTEST_BUILD_PATH,
27                      'packages', 'tmp')
28
29   def PerformStage(self):
30     tarball_name = 'built-sdk.tar.xz'
31     tarball_location = os.path.join(self._build_root, tarball_name)
32     chroot_location = os.path.join(self._build_root,
33                                    constants.DEFAULT_CHROOT_DIR)
34     board_location = os.path.join(chroot_location, 'build/amd64-host')
35     manifest_location = os.path.join(self._build_root,
36                                      '%s.Manifest' % tarball_name)
37
38     # Create a tarball of the latest SDK.
39     self.CreateSDKTarball(chroot_location, board_location, tarball_location)
40
41     # Create a package manifest for the tarball.
42     self.CreateManifestFromSDK(board_location, manifest_location)
43
44     # Create toolchain packages.
45     self.CreateRedistributableToolchains(chroot_location)
46
47     # Make sure the regular user has the permission to read.
48     cmd = ['chmod', 'a+r', tarball_location]
49     cros_build_lib.SudoRunCommand(cmd, cwd=board_location)
50
51   def CreateRedistributableToolchains(self, chroot_location):
52     """Create the toolchain packages"""
53     osutils.RmDir(os.path.join(chroot_location,
54                                constants.SDK_TOOLCHAINS_OUTPUT),
55                   ignore_missing=True)
56
57     # We need to run this as root because the tool creates hard links to root
58     # owned files and our bots enable security features which disallow that.
59     # Specifically, these features cause problems:
60     #  /proc/sys/kernel/yama/protected_nonaccess_hardlinks
61     #  /proc/sys/fs/protected_hardlinks
62     cmd = [
63         git.ReinterpretPathForChroot(os.path.join(
64             constants.CHROMITE_BIN_DIR, 'cros_setup_toolchains')),
65         '--create-packages',
66         '--output-dir', os.path.join('/', constants.SDK_TOOLCHAINS_OUTPUT),
67     ]
68     cros_build_lib.SudoRunCommand(cmd, enter_chroot=True)
69
70   def CreateSDKTarball(self, _chroot, sdk_path, dest_tarball):
71     """Creates an SDK tarball from a given source chroot.
72
73     Args:
74       chroot: A chroot used for finding compression tool.
75       sdk_path: Path to the root of newly generated SDK image.
76       dest_tarball: Path of the tarball that should be created.
77     """
78     # TODO(zbehan): We cannot use xz from the chroot unless it's
79     # statically linked.
80     extra_args = ['--exclude=%s/*' % path for path in self._EXCLUDED_PATHS]
81     # Options for maximum compression.
82     extra_env = { 'XZ_OPT' : '-e9' }
83     cros_build_lib.CreateTarball(
84         dest_tarball, sdk_path, sudo=True, extra_args=extra_args,
85         extra_env=extra_env)
86
87   def CreateManifestFromSDK(self, sdk_path, dest_manifest):
88     """Creates a manifest from a given source chroot.
89
90     Args:
91       sdk_path: Path to the root of the SDK to describe.
92       dest_manifest: Path to the manifest that should be generated.
93     """
94     package_data = {}
95     for key, version in portage_util.ListInstalledPackages(sdk_path):
96       package_data.setdefault(key, []).append((version, {}))
97     self._WriteManifest(package_data, dest_manifest)
98
99   def _WriteManifest(self, data, manifest):
100     """Encode manifest into a json file."""
101     json_input = dict(version=self.MANIFEST_VERSION, packages=data)
102     osutils.WriteFile(manifest, json.dumps(json_input))
103
104
105 class SDKTestStage(generic_stages.BuilderStage):
106   """Stage that performs testing an SDK created in a previous stage"""
107
108   option_name = 'tests'
109
110   def PerformStage(self):
111     tarball_location = os.path.join(self._build_root, 'built-sdk.tar.xz')
112     new_chroot_cmd = ['cros_sdk', '--chroot', 'new-sdk-chroot']
113     # Build a new SDK using the provided tarball.
114     cmd = new_chroot_cmd + ['--download', '--replace', '--nousepkg',
115         '--url', 'file://' + tarball_location]
116     cros_build_lib.RunCommand(cmd, cwd=self._build_root,
117                               extra_env=self._portage_extra_env)
118
119     for board in self._boards:
120       cros_build_lib.PrintBuildbotStepText(board)
121       cmd = new_chroot_cmd + ['--', './setup_board',
122           '--board', board, '--skip_chroot_upgrade']
123       cros_build_lib.RunCommand(cmd, cwd=self._build_root)
124       cmd = new_chroot_cmd + ['--', './build_packages',
125           '--board', board, '--nousepkg', '--skip_chroot_upgrade']
126       cros_build_lib.RunCommand(cmd, cwd=self._build_root,
127                                 extra_env=self._portage_extra_env)