blob: 4bd0303906aa5f4d4b9eb0014b6633657c374c70 [file] [log] [blame]
Ryan Cui6290f032012-11-20 15:44:43 -08001# Copyright (c) 2012 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"""Generates a sysroot tarball for building a specific package.
6
7Meant for use after setup_board and build_packages have been run.
8"""
9
10import os
11
Mike Frysinger06a51c82021-04-06 11:39:17 -040012from chromite.lib import build_target_lib
Aviv Keshetb7519e12016-10-04 00:50:00 -070013from chromite.lib import constants
Ryan Cui6290f032012-11-20 15:44:43 -080014from chromite.lib import cros_build_lib
15from chromite.lib import commandline
16from chromite.lib import osutils
17from chromite.lib import sudo
Bertrand SIMONNETa5d8b552015-03-25 16:03:34 -070018from chromite.lib import sysroot_lib
Alex Klein8212d492018-08-27 07:47:23 -060019from chromite.lib import toolchain
Ryan Cui6290f032012-11-20 15:44:43 -080020
21DEFAULT_NAME = 'sysroot_%(package)s.tar.xz'
22PACKAGE_SEPARATOR = '/'
23SYSROOT = 'sysroot'
24
25
26def ParseCommandLine(argv):
27 """Parse args, and run environment-independent checks."""
28 parser = commandline.ArgumentParser(description=__doc__)
29 parser.add_argument('--board', required=True,
Alex Klein8212d492018-08-27 07:47:23 -060030 help='The board to generate the sysroot for.')
Ryan Cui6290f032012-11-20 15:44:43 -080031 parser.add_argument('--package', required=True,
Alex Klein8212d492018-08-27 07:47:23 -060032 help='The packages to generate the sysroot for.')
Manoj Gupta2c2a2a22018-02-12 13:13:32 -080033 parser.add_argument('--deps-only', action='store_true',
34 default=False,
35 help='Build dependencies only.')
Michael Spang46c52fb2015-05-28 00:20:18 -040036 parser.add_argument('--out-dir', type='path', required=True,
Ryan Cui6290f032012-11-20 15:44:43 -080037 help='Directory to place the generated tarball.')
Mike Frysinger8557a292017-08-15 11:23:51 -040038 parser.add_argument('--out-file', default=DEFAULT_NAME,
39 help='The name to give to the tarball. '
40 'Defaults to %(default)s.')
Ryan Cui6290f032012-11-20 15:44:43 -080041 options = parser.parse_args(argv)
42
Mike Frysinger8557a292017-08-15 11:23:51 -040043 options.out_file %= {
Manoj Gupta2c2a2a22018-02-12 13:13:32 -080044 'package': options.package.split()[0].replace(PACKAGE_SEPARATOR, '_'),
Mike Frysinger8557a292017-08-15 11:23:51 -040045 }
Ryan Cui6290f032012-11-20 15:44:43 -080046
47 return options
48
49
50class GenerateSysroot(object):
51 """Wrapper for generation functionality."""
52
53 PARALLEL_EMERGE = os.path.join(constants.CHROMITE_BIN_DIR, 'parallel_emerge')
54
55 def __init__(self, sysroot, options):
56 """Initialize
57
Mike Frysinger02e1e072013-11-10 22:11:34 -050058 Args:
Ryan Cui6290f032012-11-20 15:44:43 -080059 sysroot: Path to sysroot.
60 options: Parsed options.
61 """
62 self.sysroot = sysroot
63 self.options = options
David James78d2e942013-07-31 15:34:45 -070064 self.extra_env = {'ROOT': self.sysroot, 'USE': os.environ.get('USE', '')}
65
66 def _Emerge(self, *args, **kwargs):
67 """Emerge the given packages using parallel_emerge."""
68 cmd = [self.PARALLEL_EMERGE, '--board=%s' % self.options.board,
69 '--usepkgonly', '--noreplace'] + list(args)
70 kwargs.setdefault('extra_env', self.extra_env)
Mike Frysinger45602c72019-09-22 02:15:11 -040071 cros_build_lib.sudo_run(cmd, **kwargs)
Ryan Cui6290f032012-11-20 15:44:43 -080072
73 def _InstallToolchain(self):
Bertrand SIMONNETa5d8b552015-03-25 16:03:34 -070074 # Create the sysroot's config.
Bertrand SIMONNETe2cec3f2015-04-06 16:12:54 -070075 sysroot = sysroot_lib.Sysroot(self.sysroot)
Alex Klein34581082018-12-03 12:56:53 -070076 sysroot.WriteConfig(sysroot.GenerateBoardSetupConfig(self.options.board))
Alex Klein8212d492018-08-27 07:47:23 -060077 toolchain.InstallToolchain(sysroot, configure=False)
Ryan Cui6290f032012-11-20 15:44:43 -080078
79 def _InstallKernelHeaders(self):
David James78d2e942013-07-31 15:34:45 -070080 self._Emerge('sys-kernel/linux-headers')
Ryan Cui6290f032012-11-20 15:44:43 -080081
82 def _InstallBuildDependencies(self):
David James78d2e942013-07-31 15:34:45 -070083 # Calculate buildtime deps that are not runtime deps.
Mike Frysinger06a51c82021-04-06 11:39:17 -040084 raw_sysroot = build_target_lib.get_default_sysroot_path(self.options.board)
Manoj Gupta2c2a2a22018-02-12 13:13:32 -080085 packages = []
86 if not self.options.deps_only:
87 packages = self.options.package.split()
88 else:
89 for pkg in self.options.package.split():
90 cmd = ['qdepends', '-q', '-C', pkg]
Mike Frysinger45602c72019-09-22 02:15:11 -040091 output = cros_build_lib.run(
Mike Frysingerc04ddb92019-12-09 21:19:47 -050092 cmd, extra_env={'ROOT': raw_sysroot}, capture_output=True,
93 encoding='utf-8').stdout
David James78d2e942013-07-31 15:34:45 -070094
Manoj Gupta2c2a2a22018-02-12 13:13:32 -080095 if output.count('\n') > 1:
96 raise AssertionError('Too many packages matched for given pattern')
David James78d2e942013-07-31 15:34:45 -070097
Manoj Gupta2c2a2a22018-02-12 13:13:32 -080098 # qdepend outputs "package: deps", so only grab the deps.
99 deps = output.partition(':')[2].split()
100 packages.extend(deps)
101 # Install the required packages.
102 if packages:
103 self._Emerge(*packages)
Ryan Cui6290f032012-11-20 15:44:43 -0800104
105 def _CreateTarball(self):
Eliot Courtney93f76212020-10-22 11:16:33 +0900106 tarball_path = os.path.join(self.options.out_dir, self.options.out_file)
107 cros_build_lib.CreateTarball(tarball_path, self.sysroot, sudo=True)
Ryan Cui6290f032012-11-20 15:44:43 -0800108
109 def Perform(self):
110 """Generate the sysroot."""
111 self._InstallToolchain()
112 self._InstallKernelHeaders()
113 self._InstallBuildDependencies()
114 self._CreateTarball()
115
116
117def FinishParsing(options):
118 """Run environment dependent checks on parsed args."""
119 target = os.path.join(options.out_dir, options.out_file)
120 if os.path.exists(target):
121 cros_build_lib.Die('Output file %r already exists.' % target)
122
123 if not os.path.isdir(options.out_dir):
124 cros_build_lib.Die(
125 'Non-existent directory %r specified for --out-dir' % options.out_dir)
126
127
128def main(argv):
129 options = ParseCommandLine(argv)
130 FinishParsing(options)
131
Mike Frysinger8fd67dc2012-12-03 23:51:18 -0500132 cros_build_lib.AssertInsideChroot()
Ryan Cui6290f032012-11-20 15:44:43 -0800133
134 with sudo.SudoKeepAlive(ttyless_sudo=False):
David James4bc13702013-03-26 08:08:04 -0700135 with osutils.TempDir(set_global=True, sudo_rm=True) as tempdir:
Ryan Cui6290f032012-11-20 15:44:43 -0800136 sysroot = os.path.join(tempdir, SYSROOT)
137 os.mkdir(sysroot)
138 GenerateSysroot(sysroot, options).Perform()