blob: 14c8d08234084920833553b7ab1cf7ef388e7557 [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
Ryan Cui6290f032012-11-20 15:44:43 -08002# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Generates a sysroot tarball for building a specific package.
7
8Meant for use after setup_board and build_packages have been run.
9"""
10
Mike Frysinger383367e2014-09-16 15:06:17 -040011from __future__ import print_function
12
Ryan Cui6290f032012-11-20 15:44:43 -080013import os
14
Aviv Keshetb7519e12016-10-04 00:50:00 -070015from chromite.lib import constants
Ryan Cui6290f032012-11-20 15:44:43 -080016from chromite.lib import cros_build_lib
17from chromite.lib import commandline
18from chromite.lib import osutils
19from chromite.lib import sudo
Bertrand SIMONNETa5d8b552015-03-25 16:03:34 -070020from chromite.lib import sysroot_lib
Alex Klein8212d492018-08-27 07:47:23 -060021from chromite.lib import toolchain
Ryan Cui6290f032012-11-20 15:44:43 -080022
23DEFAULT_NAME = 'sysroot_%(package)s.tar.xz'
24PACKAGE_SEPARATOR = '/'
25SYSROOT = 'sysroot'
26
27
28def ParseCommandLine(argv):
29 """Parse args, and run environment-independent checks."""
30 parser = commandline.ArgumentParser(description=__doc__)
31 parser.add_argument('--board', required=True,
Alex Klein8212d492018-08-27 07:47:23 -060032 help='The board to generate the sysroot for.')
Ryan Cui6290f032012-11-20 15:44:43 -080033 parser.add_argument('--package', required=True,
Alex Klein8212d492018-08-27 07:47:23 -060034 help='The packages to generate the sysroot for.')
Manoj Gupta2c2a2a22018-02-12 13:13:32 -080035 parser.add_argument('--deps-only', action='store_true',
36 default=False,
37 help='Build dependencies only.')
Michael Spang46c52fb2015-05-28 00:20:18 -040038 parser.add_argument('--out-dir', type='path', required=True,
Ryan Cui6290f032012-11-20 15:44:43 -080039 help='Directory to place the generated tarball.')
Mike Frysinger8557a292017-08-15 11:23:51 -040040 parser.add_argument('--out-file', default=DEFAULT_NAME,
41 help='The name to give to the tarball. '
42 'Defaults to %(default)s.')
Ryan Cui6290f032012-11-20 15:44:43 -080043 options = parser.parse_args(argv)
44
Mike Frysinger8557a292017-08-15 11:23:51 -040045 options.out_file %= {
Manoj Gupta2c2a2a22018-02-12 13:13:32 -080046 'package': options.package.split()[0].replace(PACKAGE_SEPARATOR, '_'),
Mike Frysinger8557a292017-08-15 11:23:51 -040047 }
Ryan Cui6290f032012-11-20 15:44:43 -080048
49 return options
50
51
52class GenerateSysroot(object):
53 """Wrapper for generation functionality."""
54
55 PARALLEL_EMERGE = os.path.join(constants.CHROMITE_BIN_DIR, 'parallel_emerge')
56
57 def __init__(self, sysroot, options):
58 """Initialize
59
Mike Frysinger02e1e072013-11-10 22:11:34 -050060 Args:
Ryan Cui6290f032012-11-20 15:44:43 -080061 sysroot: Path to sysroot.
62 options: Parsed options.
63 """
64 self.sysroot = sysroot
65 self.options = options
David James78d2e942013-07-31 15:34:45 -070066 self.extra_env = {'ROOT': self.sysroot, 'USE': os.environ.get('USE', '')}
67
68 def _Emerge(self, *args, **kwargs):
69 """Emerge the given packages using parallel_emerge."""
70 cmd = [self.PARALLEL_EMERGE, '--board=%s' % self.options.board,
71 '--usepkgonly', '--noreplace'] + list(args)
72 kwargs.setdefault('extra_env', self.extra_env)
Mike Frysinger45602c72019-09-22 02:15:11 -040073 cros_build_lib.sudo_run(cmd, **kwargs)
Ryan Cui6290f032012-11-20 15:44:43 -080074
75 def _InstallToolchain(self):
Bertrand SIMONNETa5d8b552015-03-25 16:03:34 -070076 # Create the sysroot's config.
Bertrand SIMONNETe2cec3f2015-04-06 16:12:54 -070077 sysroot = sysroot_lib.Sysroot(self.sysroot)
Alex Klein34581082018-12-03 12:56:53 -070078 sysroot.WriteConfig(sysroot.GenerateBoardSetupConfig(self.options.board))
Alex Klein8212d492018-08-27 07:47:23 -060079 toolchain.InstallToolchain(sysroot, configure=False)
Ryan Cui6290f032012-11-20 15:44:43 -080080
81 def _InstallKernelHeaders(self):
David James78d2e942013-07-31 15:34:45 -070082 self._Emerge('sys-kernel/linux-headers')
Ryan Cui6290f032012-11-20 15:44:43 -080083
84 def _InstallBuildDependencies(self):
David James78d2e942013-07-31 15:34:45 -070085 # Calculate buildtime deps that are not runtime deps.
Jack Rosenthalb984e102021-04-07 21:18:29 +000086 raw_sysroot = cros_build_lib.GetSysroot(board=self.options.board)
Manoj Gupta2c2a2a22018-02-12 13:13:32 -080087 packages = []
88 if not self.options.deps_only:
89 packages = self.options.package.split()
90 else:
91 for pkg in self.options.package.split():
92 cmd = ['qdepends', '-q', '-C', pkg]
Mike Frysinger45602c72019-09-22 02:15:11 -040093 output = cros_build_lib.run(
Mike Frysingerc04ddb92019-12-09 21:19:47 -050094 cmd, extra_env={'ROOT': raw_sysroot}, capture_output=True,
95 encoding='utf-8').stdout
David James78d2e942013-07-31 15:34:45 -070096
Manoj Gupta2c2a2a22018-02-12 13:13:32 -080097 if output.count('\n') > 1:
98 raise AssertionError('Too many packages matched for given pattern')
David James78d2e942013-07-31 15:34:45 -070099
Manoj Gupta2c2a2a22018-02-12 13:13:32 -0800100 # qdepend outputs "package: deps", so only grab the deps.
101 deps = output.partition(':')[2].split()
102 packages.extend(deps)
103 # Install the required packages.
104 if packages:
105 self._Emerge(*packages)
Ryan Cui6290f032012-11-20 15:44:43 -0800106
107 def _CreateTarball(self):
Eliot Courtney93f76212020-10-22 11:16:33 +0900108 tarball_path = os.path.join(self.options.out_dir, self.options.out_file)
109 cros_build_lib.CreateTarball(tarball_path, self.sysroot, sudo=True)
Ryan Cui6290f032012-11-20 15:44:43 -0800110
111 def Perform(self):
112 """Generate the sysroot."""
113 self._InstallToolchain()
114 self._InstallKernelHeaders()
115 self._InstallBuildDependencies()
116 self._CreateTarball()
117
118
119def FinishParsing(options):
120 """Run environment dependent checks on parsed args."""
121 target = os.path.join(options.out_dir, options.out_file)
122 if os.path.exists(target):
123 cros_build_lib.Die('Output file %r already exists.' % target)
124
125 if not os.path.isdir(options.out_dir):
126 cros_build_lib.Die(
127 'Non-existent directory %r specified for --out-dir' % options.out_dir)
128
129
130def main(argv):
131 options = ParseCommandLine(argv)
132 FinishParsing(options)
133
Mike Frysinger8fd67dc2012-12-03 23:51:18 -0500134 cros_build_lib.AssertInsideChroot()
Ryan Cui6290f032012-11-20 15:44:43 -0800135
136 with sudo.SudoKeepAlive(ttyless_sudo=False):
David James4bc13702013-03-26 08:08:04 -0700137 with osutils.TempDir(set_global=True, sudo_rm=True) as tempdir:
Ryan Cui6290f032012-11-20 15:44:43 -0800138 sysroot = os.path.join(tempdir, SYSROOT)
139 os.mkdir(sysroot)
140 GenerateSysroot(sysroot, options).Perform()