blob: b0910c2aadfcf9c111a82c9c647647bbf40f73d6 [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
Zdenek Behan508dcce2011-12-05 15:39:32 +01002# 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
Mike Frysinger750c5f52014-09-16 16:16:57 -04006"""This script manages the installed toolchains in the chroot."""
Zdenek Behan508dcce2011-12-05 15:39:32 +01007
Mike Frysinger383367e2014-09-16 15:06:17 -04008from __future__ import print_function
9
Mike Frysinger3ed47722017-08-08 14:59:08 -040010import errno
Mike Frysinger35247af2012-11-16 18:58:06 -050011import glob
Mike Frysinger3ed47722017-08-08 14:59:08 -040012import hashlib
Mike Frysinger7ccee992012-06-01 21:27:59 -040013import json
Zdenek Behan508dcce2011-12-05 15:39:32 +010014import os
Rahul Chaudhrya8127bb2016-07-22 15:53:17 -070015import re
Tobias Boschddd16492019-08-14 09:29:54 -070016import shutil
Mike Frysinger499ca392020-04-28 07:35:54 -040017import sys
Zdenek Behan508dcce2011-12-05 15:39:32 +010018
Aviv Keshetb7519e12016-10-04 00:50:00 -070019from chromite.lib import constants
Mike Frysinger506e75f2012-12-17 14:21:13 -050020from chromite.lib import commandline
Brian Harring503f3ab2012-03-09 21:39:41 -080021from chromite.lib import cros_build_lib
Ralph Nathan03047282015-03-23 11:09:32 -070022from chromite.lib import cros_logging as logging
Brian Harringaf019fb2012-05-10 15:06:13 -070023from chromite.lib import osutils
Mike Frysinger35247af2012-11-16 18:58:06 -050024from chromite.lib import parallel
David James27ac4ae2012-12-03 23:16:15 -080025from chromite.lib import toolchain
Mike Frysingere652ba12019-09-08 00:57:43 -040026from chromite.utils import key_value_store
Mike Frysinger35247af2012-11-16 18:58:06 -050027
28# Needs to be after chromite imports.
Mike Frysingerabb7d812020-05-15 00:13:10 -040029import lddtree # pylint: disable=wrong-import-order
Zdenek Behan508dcce2011-12-05 15:39:32 +010030
Mike Frysinger31596002012-12-03 23:54:24 -050031if cros_build_lib.IsInsideChroot():
32 # Only import portage after we've checked that we're inside the chroot.
33 # Outside may not have portage, in which case the above may not happen.
34 # We'll check in main() if the operation needs portage.
Mike Frysinger27e21b72018-07-12 14:20:21 -040035 # pylint: disable=import-error
Mike Frysinger31596002012-12-03 23:54:24 -050036 import portage
Zdenek Behan508dcce2011-12-05 15:39:32 +010037
38
Mike Frysinger499ca392020-04-28 07:35:54 -040039assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
40
41
Matt Tennantf1e30972012-03-02 16:30:07 -080042EMERGE_CMD = os.path.join(constants.CHROMITE_BIN_DIR, 'parallel_emerge')
Zdenek Behan508dcce2011-12-05 15:39:32 +010043PACKAGE_STABLE = '[stable]'
Zdenek Behan4eb6fd22012-03-12 17:00:56 +010044
45CHROMIUMOS_OVERLAY = '/usr/local/portage/chromiumos'
Christopher Wileyb22c0712015-06-02 10:37:03 -070046ECLASS_OVERLAY = '/usr/local/portage/eclass-overlay'
Zdenek Behan4eb6fd22012-03-12 17:00:56 +010047STABLE_OVERLAY = '/usr/local/portage/stable'
48CROSSDEV_OVERLAY = '/usr/local/portage/crossdev'
Zdenek Behan508dcce2011-12-05 15:39:32 +010049
50
Mike Frysinger66bfde52017-09-12 16:42:57 -040051# The exact list of host toolchain packages we care about. These are the
52# packages that bots/devs install only from binpkgs and rely on the SDK bot
53# (chromiumos-sdk) to validate+uprev.
54#
Mike Frysinger66bfde52017-09-12 16:42:57 -040055# We don't use crossdev to manage the host toolchain for us, especially since
56# we diverge significantly now (with llvm/clang/etc...), and we don't need or
57# want crossdev managing /etc/portage config files for the sdk
58HOST_PACKAGES = (
59 'dev-lang/go',
Yunlian Jiang2cb91dc2018-03-08 10:56:27 -080060 'dev-libs/elfutils',
Mike Frysinger66bfde52017-09-12 16:42:57 -040061 'sys-devel/binutils',
Mike Frysinger66bfde52017-09-12 16:42:57 -040062 'sys-devel/gcc',
63 'sys-devel/llvm',
64 'sys-kernel/linux-headers',
65 'sys-libs/glibc',
66 'sys-libs/libcxx',
67 'sys-libs/libcxxabi',
Manoj Guptade64cb22019-03-31 18:48:58 -070068 'sys-libs/llvm-libunwind',
Mike Frysinger66bfde52017-09-12 16:42:57 -040069)
70
Mike Frysinger785b0c32017-09-13 01:35:59 -040071# These packages are also installed into the host SDK. However, they require
72# the cross-compilers to be installed first (because they need them to actually
73# build), so we have to delay their installation.
74HOST_POST_CROSS_PACKAGES = (
Manoj Gupta65f88442018-04-12 22:42:19 -070075 'dev-lang/rust',
George Burgess IVa61f65b2021-03-19 11:56:10 -070076 'dev-lang/rust-bootstrap:0',
Mike Frysinger61a24392017-10-17 17:14:27 -040077 'virtual/target-sdk-post-cross',
Patrick Georgi043ce6e2019-02-20 22:27:09 +010078 'dev-embedded/coreboot-sdk',
George Burgess IV288ecc82021-04-21 07:52:45 -070079 'dev-embedded/ti50-sdk',
Mike Frysinger785b0c32017-09-13 01:35:59 -040080)
81
82# New packages that we're in the process of adding to the SDK. Since the SDK
83# bot hasn't had a chance to run yet, there are no binary packages available,
84# so we have to list them here and wait. Once it completes, entries here can
85# be removed so they'll end up on bots & dev's systems.
George Burgess IV288ecc82021-04-21 07:52:45 -070086NEW_PACKAGES = (
87 'dev-embedded/ti50-sdk',
88)
Mike Frysinger785b0c32017-09-13 01:35:59 -040089
Rahul Chaudhry4b803052015-05-13 15:25:56 -070090# Enable the Go compiler for these targets.
91TARGET_GO_ENABLED = (
92 'x86_64-cros-linux-gnu',
Rahul Chaudhry4b803052015-05-13 15:25:56 -070093 'armv7a-cros-linux-gnueabi',
Yunlian Jiang16c1a422017-10-04 10:33:22 -070094 'armv7a-cros-linux-gnueabihf',
Rahul Chaudhry4d416582017-10-25 12:31:58 -070095 'aarch64-cros-linux-gnu',
Rahul Chaudhry4b803052015-05-13 15:25:56 -070096)
97CROSSDEV_GO_ARGS = ['--ex-pkg', 'dev-lang/go']
98
Manoj Gupta1b5642e2017-03-08 16:44:12 -080099# Enable llvm's compiler-rt for these targets.
100TARGET_COMPILER_RT_ENABLED = (
101 'armv7a-cros-linux-gnueabi',
Yunlian Jiang1b77ee42017-10-06 13:44:29 -0700102 'armv7a-cros-linux-gnueabihf',
Manoj Gupta946abb42017-04-12 14:27:19 -0700103 'aarch64-cros-linux-gnu',
Manoj Gupta21f3a082018-03-06 21:25:39 -0800104 'armv7m-cros-eabi',
Manoj Gupta1b5642e2017-03-08 16:44:12 -0800105)
106CROSSDEV_COMPILER_RT_ARGS = ['--ex-pkg', 'sys-libs/compiler-rt']
107
Manoj Gupta946abb42017-04-12 14:27:19 -0700108TARGET_LLVM_PKGS_ENABLED = (
109 'armv7a-cros-linux-gnueabi',
Yunlian Jiang16c1a422017-10-04 10:33:22 -0700110 'armv7a-cros-linux-gnueabihf',
Manoj Gupta946abb42017-04-12 14:27:19 -0700111 'aarch64-cros-linux-gnu',
Manoj Gupta93713122020-10-29 17:52:16 -0700112 'i686-pc-linux-gnu',
Manoj Gupta946abb42017-04-12 14:27:19 -0700113 'x86_64-cros-linux-gnu',
114)
115
116LLVM_PKGS_TABLE = {
Yunlian Jiangbb2a3d32017-04-26 14:44:09 -0700117 'ex_libcxxabi' : ['--ex-pkg', 'sys-libs/libcxxabi'],
118 'ex_libcxx' : ['--ex-pkg', 'sys-libs/libcxx'],
Chirantan Ekbotee694cad2018-07-12 15:07:24 -0700119 'ex_llvm-libunwind' : ['--ex-pkg', 'sys-libs/llvm-libunwind'],
Manoj Gupta946abb42017-04-12 14:27:19 -0700120}
121
David James66a09c42012-11-05 13:31:38 -0800122class Crossdev(object):
123 """Class for interacting with crossdev and caching its output."""
124
125 _CACHE_FILE = os.path.join(CROSSDEV_OVERLAY, '.configured.json')
126 _CACHE = {}
Han Shene23782f2016-02-18 12:20:00 -0800127 # Packages that needs separate handling, in addition to what we have from
128 # crossdev.
129 MANUAL_PKGS = {
George Burgess IVca1d7612020-10-01 00:38:32 -0700130 'rust': 'dev-lang',
Han Shene23782f2016-02-18 12:20:00 -0800131 'llvm': 'sys-devel',
Manoj Gupta20cfc6c2017-07-19 15:49:55 -0700132 'libcxxabi': 'sys-libs',
133 'libcxx': 'sys-libs',
Yunlian Jiangda3ce5f2018-04-25 14:10:01 -0700134 'elfutils': 'dev-libs',
Chirantan Ekbotee694cad2018-07-12 15:07:24 -0700135 'llvm-libunwind': 'sys-libs',
Han Shene23782f2016-02-18 12:20:00 -0800136 }
David James66a09c42012-11-05 13:31:38 -0800137
138 @classmethod
139 def Load(cls, reconfig):
Mike Frysinger3ed47722017-08-08 14:59:08 -0400140 """Load crossdev cache from disk.
141
142 We invalidate the cache when crossdev updates or this script changes.
143 """
David James90239b92012-11-05 15:31:34 -0800144 crossdev_version = GetStablePackageVersion('sys-devel/crossdev', True)
Mike Frysinger3ed47722017-08-08 14:59:08 -0400145 # If we run the compiled/cached .pyc file, we'll read/hash that when we
146 # really always want to track the source .py file.
147 script = os.path.abspath(__file__)
148 if script.endswith('.pyc'):
149 script = script[:-1]
Mike Frysingerb3202be2019-11-15 22:25:59 -0500150 setup_toolchains_hash = hashlib.md5(
151 osutils.ReadFile(script, mode='rb')).hexdigest()
Mike Frysinger3ed47722017-08-08 14:59:08 -0400152
153 cls._CACHE = {
154 'crossdev_version': crossdev_version,
155 'setup_toolchains_hash': setup_toolchains_hash,
156 }
157
158 logging.debug('cache: checking file: %s', cls._CACHE_FILE)
159 if reconfig:
160 logging.debug('cache: forcing regen due to reconfig')
161 return
162
163 try:
164 file_data = osutils.ReadFile(cls._CACHE_FILE)
165 except IOError as e:
166 if e.errno != errno.ENOENT:
167 logging.warning('cache: reading failed: %s', e)
168 osutils.SafeUnlink(cls._CACHE_FILE)
169 return
170
171 try:
172 data = json.loads(file_data)
173 except ValueError as e:
174 logging.warning('cache: ignoring invalid content: %s', e)
175 return
176
177 if crossdev_version != data.get('crossdev_version'):
178 logging.debug('cache: rebuilding after crossdev upgrade')
179 elif setup_toolchains_hash != data.get('setup_toolchains_hash'):
180 logging.debug('cache: rebuilding after cros_setup_toolchains change')
181 else:
182 logging.debug('cache: content is up-to-date!')
183 cls._CACHE = data
David James66a09c42012-11-05 13:31:38 -0800184
185 @classmethod
186 def Save(cls):
187 """Store crossdev cache on disk."""
188 # Save the cache from the successful run.
189 with open(cls._CACHE_FILE, 'w') as f:
190 json.dump(cls._CACHE, f)
191
192 @classmethod
193 def GetConfig(cls, target):
194 """Returns a map of crossdev provided variables about a tuple."""
195 CACHE_ATTR = '_target_tuple_map'
196
197 val = cls._CACHE.setdefault(CACHE_ATTR, {})
198 if not target in val:
Mike Frysinger785b0c32017-09-13 01:35:59 -0400199 if target.startswith('host'):
Mike Frysinger66bfde52017-09-12 16:42:57 -0400200 conf = {
201 'crosspkgs': [],
202 'target': toolchain.GetHostTuple(),
203 }
Mike Frysinger785b0c32017-09-13 01:35:59 -0400204 if target == 'host':
205 packages_list = HOST_PACKAGES
206 else:
207 packages_list = HOST_POST_CROSS_PACKAGES
Mike Frysinger66bfde52017-09-12 16:42:57 -0400208 manual_pkgs = dict((pkg, cat) for cat, pkg in
Mike Frysinger785b0c32017-09-13 01:35:59 -0400209 [x.split('/') for x in packages_list])
Mike Frysinger66bfde52017-09-12 16:42:57 -0400210 else:
211 # Build the crossdev command.
212 cmd = ['crossdev', '--show-target-cfg', '--ex-gdb']
213 if target in TARGET_COMPILER_RT_ENABLED:
214 cmd.extend(CROSSDEV_COMPILER_RT_ARGS)
215 if target in TARGET_GO_ENABLED:
216 cmd.extend(CROSSDEV_GO_ARGS)
217 if target in TARGET_LLVM_PKGS_ENABLED:
218 for pkg in LLVM_PKGS_TABLE:
219 cmd.extend(LLVM_PKGS_TABLE[pkg])
220 cmd.extend(['-t', target])
221 # Catch output of crossdev.
Mike Frysinger45602c72019-09-22 02:15:11 -0400222 out = cros_build_lib.run(
Mike Frysinger0282d222019-12-17 17:15:48 -0500223 cmd, print_cmd=False, stdout=True,
Mike Frysingerb3202be2019-11-15 22:25:59 -0500224 encoding='utf-8').stdout.splitlines()
Mike Frysinger66bfde52017-09-12 16:42:57 -0400225 # List of tuples split at the first '=', converted into dict.
226 conf = dict((k, cros_build_lib.ShellUnquote(v))
227 for k, v in (x.split('=', 1) for x in out))
228 conf['crosspkgs'] = conf['crosspkgs'].split()
Han Shene23782f2016-02-18 12:20:00 -0800229
Mike Frysinger66bfde52017-09-12 16:42:57 -0400230 manual_pkgs = cls.MANUAL_PKGS
231
232 for pkg, cat in manual_pkgs.items():
Mike Frysinger3bba5032016-09-20 14:15:04 -0400233 conf[pkg + '_pn'] = pkg
234 conf[pkg + '_category'] = cat
235 if pkg not in conf['crosspkgs']:
236 conf['crosspkgs'].append(pkg)
Han Shene23782f2016-02-18 12:20:00 -0800237
238 val[target] = conf
239
David James66a09c42012-11-05 13:31:38 -0800240 return val[target]
241
242 @classmethod
243 def UpdateTargets(cls, targets, usepkg, config_only=False):
244 """Calls crossdev to initialize a cross target.
245
246 Args:
Nicolas Boichat3fecf392019-11-25 02:58:28 +0000247 targets: The list of targets to initialize using crossdev.
Don Garrett25f309a2014-03-19 14:02:12 -0700248 usepkg: Copies the commandline opts.
249 config_only: Just update.
David James66a09c42012-11-05 13:31:38 -0800250 """
251 configured_targets = cls._CACHE.setdefault('configured_targets', [])
252
253 cmdbase = ['crossdev', '--show-fail-log']
254 cmdbase.extend(['--env', 'FEATURES=splitdebug'])
255 # Pick stable by default, and override as necessary.
256 cmdbase.extend(['-P', '--oneshot'])
257 if usepkg:
258 cmdbase.extend(['-P', '--getbinpkg',
259 '-P', '--usepkgonly',
260 '--without-headers'])
261
Christopher Wileyb22c0712015-06-02 10:37:03 -0700262 overlays = ' '.join((CHROMIUMOS_OVERLAY, ECLASS_OVERLAY, STABLE_OVERLAY))
David James66a09c42012-11-05 13:31:38 -0800263 cmdbase.extend(['--overlays', overlays])
264 cmdbase.extend(['--ov-output', CROSSDEV_OVERLAY])
265
Nicolas Boichat3fecf392019-11-25 02:58:28 +0000266 # Build target by the reversed alphabetical order to make sure
267 # armv7a-cros-linux-gnueabihf builds before armv7a-cros-linux-gnueabi
268 # because some dependency issue. This can be reverted once we
269 # migrated to armv7a-cros-linux-gnueabihf. crbug.com/711369
270 for target in sorted(targets, reverse=True):
271 if config_only and target in configured_targets:
272 continue
David James66a09c42012-11-05 13:31:38 -0800273
Nicolas Boichat3fecf392019-11-25 02:58:28 +0000274 cmd = cmdbase + ['-t', target]
275
276 for pkg in GetTargetPackages(target):
277 if pkg == 'gdb':
278 # Gdb does not have selectable versions.
279 cmd.append('--ex-gdb')
280 elif pkg == 'ex_compiler-rt':
281 cmd.extend(CROSSDEV_COMPILER_RT_ARGS)
282 elif pkg == 'ex_go':
283 # Go does not have selectable versions.
284 cmd.extend(CROSSDEV_GO_ARGS)
285 elif pkg in LLVM_PKGS_TABLE:
286 cmd.extend(LLVM_PKGS_TABLE[pkg])
287 elif pkg in cls.MANUAL_PKGS:
288 pass
289 else:
290 # The first of the desired versions is the "primary" one.
291 version = GetDesiredPackageVersions(target, pkg)[0]
292 cmd.extend(['--%s' % pkg, version])
293
294 cmd.extend(targets[target]['crossdev'].split())
295 if config_only:
296 # In this case we want to just quietly reinit
297 cmd.append('--init-target')
Mike Frysinger0282d222019-12-17 17:15:48 -0500298 cros_build_lib.run(cmd, print_cmd=False, stdout=True)
David James66a09c42012-11-05 13:31:38 -0800299 else:
Nicolas Boichat3fecf392019-11-25 02:58:28 +0000300 cros_build_lib.run(cmd)
David James66a09c42012-11-05 13:31:38 -0800301
Nicolas Boichat3fecf392019-11-25 02:58:28 +0000302 configured_targets.append(target)
David James66a09c42012-11-05 13:31:38 -0800303
Zdenek Behan4eb6fd22012-03-12 17:00:56 +0100304
Zdenek Behanf4d18a02012-03-22 15:45:05 +0100305def GetTargetPackages(target):
306 """Returns a list of packages for a given target."""
David James66a09c42012-11-05 13:31:38 -0800307 conf = Crossdev.GetConfig(target)
Zdenek Behanf4d18a02012-03-22 15:45:05 +0100308 # Undesired packages are denoted by empty ${pkg}_pn variable.
Han Shene23782f2016-02-18 12:20:00 -0800309 return [x for x in conf['crosspkgs'] if conf.get(x+'_pn')]
Zdenek Behanf4d18a02012-03-22 15:45:05 +0100310
311
Zdenek Behan4eb6fd22012-03-12 17:00:56 +0100312# Portage helper functions:
313def GetPortagePackage(target, package):
314 """Returns a package name for the given target."""
David James66a09c42012-11-05 13:31:38 -0800315 conf = Crossdev.GetConfig(target)
Zdenek Behan4eb6fd22012-03-12 17:00:56 +0100316 # Portage category:
Mike Frysinger785b0c32017-09-13 01:35:59 -0400317 if target.startswith('host') or package in Crossdev.MANUAL_PKGS:
Zdenek Behan4eb6fd22012-03-12 17:00:56 +0100318 category = conf[package + '_category']
Zdenek Behan508dcce2011-12-05 15:39:32 +0100319 else:
Zdenek Behan4eb6fd22012-03-12 17:00:56 +0100320 category = conf['category']
321 # Portage package:
322 pn = conf[package + '_pn']
323 # Final package name:
Mike Frysinger422faf42014-11-12 23:16:48 -0500324 assert category
325 assert pn
Zdenek Behan4eb6fd22012-03-12 17:00:56 +0100326 return '%s/%s' % (category, pn)
327
328
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700329def PortageTrees(root):
330 """Return the portage trees for a given root."""
331 if root == '/':
332 return portage.db['/']
333 # The portage logic requires the path always end in a slash.
334 root = root.rstrip('/') + '/'
335 return portage.create_trees(target_root=root, config_root=root)[root]
336
337
338def GetInstalledPackageVersions(atom, root='/'):
Zdenek Behan508dcce2011-12-05 15:39:32 +0100339 """Extracts the list of current versions of a target, package pair.
340
Mike Frysingerdc649ae2014-11-26 19:29:24 -0500341 Args:
342 atom: The atom to operate on (e.g. sys-devel/gcc)
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700343 root: The root to check for installed packages.
Zdenek Behan508dcce2011-12-05 15:39:32 +0100344
Mike Frysingerdc649ae2014-11-26 19:29:24 -0500345 Returns:
346 The list of versions of the package currently installed.
Zdenek Behan508dcce2011-12-05 15:39:32 +0100347 """
Zdenek Behan508dcce2011-12-05 15:39:32 +0100348 versions = []
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700349 for pkg in PortageTrees(root)['vartree'].dbapi.match(atom, use_cache=0):
Zdenek Behan508dcce2011-12-05 15:39:32 +0100350 version = portage.versions.cpv_getversion(pkg)
351 versions.append(version)
352 return versions
353
354
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700355def GetStablePackageVersion(atom, installed, root='/'):
Zdenek Behan508dcce2011-12-05 15:39:32 +0100356 """Extracts the current stable version for a given package.
357
Mike Frysingerdc649ae2014-11-26 19:29:24 -0500358 Args:
359 atom: The target/package to operate on eg. i686-pc-linux-gnu,gcc
360 installed: Whether we want installed packages or ebuilds
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700361 root: The root to use when querying packages.
Zdenek Behan508dcce2011-12-05 15:39:32 +0100362
Mike Frysingerdc649ae2014-11-26 19:29:24 -0500363 Returns:
364 A string containing the latest version.
Zdenek Behan508dcce2011-12-05 15:39:32 +0100365 """
David James90239b92012-11-05 15:31:34 -0800366 pkgtype = 'vartree' if installed else 'porttree'
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700367 cpv = portage.best(PortageTrees(root)[pkgtype].dbapi.match(atom, use_cache=0))
David James90239b92012-11-05 15:31:34 -0800368 return portage.versions.cpv_getversion(cpv) if cpv else None
Zdenek Behan508dcce2011-12-05 15:39:32 +0100369
370
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700371def VersionListToNumeric(target, package, versions, installed, root='/'):
Zdenek Behan508dcce2011-12-05 15:39:32 +0100372 """Resolves keywords in a given version list for a particular package.
373
374 Resolving means replacing PACKAGE_STABLE with the actual number.
375
Mike Frysingerdc649ae2014-11-26 19:29:24 -0500376 Args:
377 target: The target to operate on (e.g. i686-pc-linux-gnu)
378 package: The target/package to operate on (e.g. gcc)
379 versions: List of versions to resolve
380 installed: Query installed packages
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700381 root: The install root to use; ignored if |installed| is False.
Zdenek Behan508dcce2011-12-05 15:39:32 +0100382
Mike Frysingerdc649ae2014-11-26 19:29:24 -0500383 Returns:
384 List of purely numeric versions equivalent to argument
Zdenek Behan508dcce2011-12-05 15:39:32 +0100385 """
386 resolved = []
David James90239b92012-11-05 15:31:34 -0800387 atom = GetPortagePackage(target, package)
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700388 if not installed:
389 root = '/'
Zdenek Behan508dcce2011-12-05 15:39:32 +0100390 for version in versions:
391 if version == PACKAGE_STABLE:
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700392 resolved.append(GetStablePackageVersion(atom, installed, root=root))
Mike Frysinger7f0e1982017-09-12 17:08:50 -0400393 else:
Zdenek Behan508dcce2011-12-05 15:39:32 +0100394 resolved.append(version)
395 return resolved
396
397
398def GetDesiredPackageVersions(target, package):
399 """Produces the list of desired versions for each target, package pair.
400
401 The first version in the list is implicitly treated as primary, ie.
402 the version that will be initialized by crossdev and selected.
403
404 If the version is PACKAGE_STABLE, it really means the current version which
405 is emerged by using the package atom with no particular version key.
406 Since crossdev unmasks all packages by default, this will actually
407 mean 'unstable' in most cases.
408
Mike Frysingerdc649ae2014-11-26 19:29:24 -0500409 Args:
410 target: The target to operate on (e.g. i686-pc-linux-gnu)
411 package: The target/package to operate on (e.g. gcc)
Zdenek Behan508dcce2011-12-05 15:39:32 +0100412
Mike Frysingerdc649ae2014-11-26 19:29:24 -0500413 Returns:
414 A list composed of either a version string, PACKAGE_STABLE
Zdenek Behan508dcce2011-12-05 15:39:32 +0100415 """
Mike Frysingerd23be272017-09-12 17:18:44 -0400416 if package in GetTargetPackages(target):
417 return [PACKAGE_STABLE]
418 else:
419 return []
Zdenek Behan508dcce2011-12-05 15:39:32 +0100420
421
422def TargetIsInitialized(target):
423 """Verifies if the given list of targets has been correctly initialized.
424
425 This determines whether we have to call crossdev while emerging
426 toolchain packages or can do it using emerge. Emerge is naturally
427 preferred, because all packages can be updated in a single pass.
428
Mike Frysingerdc649ae2014-11-26 19:29:24 -0500429 Args:
430 target: The target to operate on (e.g. i686-pc-linux-gnu)
Zdenek Behan508dcce2011-12-05 15:39:32 +0100431
Mike Frysingerdc649ae2014-11-26 19:29:24 -0500432 Returns:
433 True if |target| is completely initialized, else False
Zdenek Behan508dcce2011-12-05 15:39:32 +0100434 """
435 # Check if packages for the given target all have a proper version.
436 try:
Zdenek Behanf4d18a02012-03-22 15:45:05 +0100437 for package in GetTargetPackages(target):
David James66a09c42012-11-05 13:31:38 -0800438 atom = GetPortagePackage(target, package)
Zdenek Behan4eb6fd22012-03-12 17:00:56 +0100439 # Do we even want this package && is it initialized?
Mike Frysinger7f0e1982017-09-12 17:08:50 -0400440 if not (GetStablePackageVersion(atom, True) and
441 GetStablePackageVersion(atom, False)):
Zdenek Behan508dcce2011-12-05 15:39:32 +0100442 return False
443 return True
444 except cros_build_lib.RunCommandError:
445 # Fails - The target has likely never been initialized before.
446 return False
447
448
449def RemovePackageMask(target):
450 """Removes a package.mask file for the given platform.
451
452 The pre-existing package.mask files can mess with the keywords.
453
Mike Frysingerdc649ae2014-11-26 19:29:24 -0500454 Args:
455 target: The target to operate on (e.g. i686-pc-linux-gnu)
Zdenek Behan508dcce2011-12-05 15:39:32 +0100456 """
457 maskfile = os.path.join('/etc/portage/package.mask', 'cross-' + target)
Brian Harringaf019fb2012-05-10 15:06:13 -0700458 osutils.SafeUnlink(maskfile)
Zdenek Behan508dcce2011-12-05 15:39:32 +0100459
460
Zdenek Behan508dcce2011-12-05 15:39:32 +0100461# Main functions performing the actual update steps.
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700462def RebuildLibtool(root='/'):
Mike Frysingerc880a962013-11-08 13:59:06 -0500463 """Rebuild libtool as needed
464
465 Libtool hardcodes full paths to internal gcc files, so whenever we upgrade
466 gcc, libtool will break. We can't use binary packages either as those will
467 most likely be compiled against the previous version of gcc.
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700468
469 Args:
470 root: The install root where we want libtool rebuilt.
Mike Frysingerc880a962013-11-08 13:59:06 -0500471 """
472 needs_update = False
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700473 with open(os.path.join(root, 'usr/bin/libtool')) as f:
Mike Frysingerc880a962013-11-08 13:59:06 -0500474 for line in f:
475 # Look for a line like:
476 # sys_lib_search_path_spec="..."
477 # It'll be a list of paths and gcc will be one of them.
478 if line.startswith('sys_lib_search_path_spec='):
479 line = line.rstrip()
480 for path in line.split('=', 1)[1].strip('"').split():
Mike Frysinger3bba5032016-09-20 14:15:04 -0400481 root_path = os.path.join(root, path.lstrip(os.path.sep))
482 logging.debug('Libtool: checking %s', root_path)
483 if not os.path.exists(root_path):
484 logging.info('Rebuilding libtool after gcc upgrade')
485 logging.info(' %s', line)
486 logging.info(' missing path: %s', path)
Mike Frysingerc880a962013-11-08 13:59:06 -0500487 needs_update = True
488 break
489
490 if needs_update:
491 break
492
493 if needs_update:
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700494 cmd = [EMERGE_CMD, '--oneshot']
495 if root != '/':
496 cmd.extend(['--sysroot=%s' % root, '--root=%s' % root])
497 cmd.append('sys-devel/libtool')
Mike Frysinger45602c72019-09-22 02:15:11 -0400498 cros_build_lib.run(cmd)
Mike Frysinger3bba5032016-09-20 14:15:04 -0400499 else:
500 logging.debug('Libtool is up-to-date; no need to rebuild')
Mike Frysingerc880a962013-11-08 13:59:06 -0500501
502
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700503def UpdateTargets(targets, usepkg, root='/'):
Zdenek Behan508dcce2011-12-05 15:39:32 +0100504 """Determines which packages need update/unmerge and defers to portage.
505
Mike Frysingerdc649ae2014-11-26 19:29:24 -0500506 Args:
507 targets: The list of targets to update
508 usepkg: Copies the commandline option
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700509 root: The install root in which we want packages updated.
Zdenek Behan508dcce2011-12-05 15:39:32 +0100510 """
Zdenek Behan508dcce2011-12-05 15:39:32 +0100511 # For each target, we do two things. Figure out the list of updates,
512 # and figure out the appropriate keywords/masks. Crossdev will initialize
513 # these, but they need to be regenerated on every update.
Mike Frysinger3bba5032016-09-20 14:15:04 -0400514 logging.info('Determining required toolchain updates...')
David James90239b92012-11-05 15:31:34 -0800515 mergemap = {}
ChromeOS Developereb8a5c42021-01-12 00:05:02 +0000516 # Used to keep track of post-cross packages. These are allowed to have
517 # implicit dependencies on toolchain packages, and therefore need to
518 # be built last.
519 post_cross_pkgs = set()
Zdenek Behan508dcce2011-12-05 15:39:32 +0100520 for target in targets:
ChromeOS Developereb8a5c42021-01-12 00:05:02 +0000521 is_post_cross_target = target.endswith('-post-cross')
Mike Frysinger3bba5032016-09-20 14:15:04 -0400522 logging.debug('Updating target %s', target)
Zdenek Behan508dcce2011-12-05 15:39:32 +0100523 # Record the highest needed version for each target, for masking purposes.
524 RemovePackageMask(target)
Zdenek Behanf4d18a02012-03-22 15:45:05 +0100525 for package in GetTargetPackages(target):
Zdenek Behan508dcce2011-12-05 15:39:32 +0100526 # Portage name for the package
Mike Frysinger7f0e1982017-09-12 17:08:50 -0400527 logging.debug(' Checking package %s', package)
Zdenek Behan4eb6fd22012-03-12 17:00:56 +0100528 pkg = GetPortagePackage(target, package)
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700529 current = GetInstalledPackageVersions(pkg, root=root)
Zdenek Behan508dcce2011-12-05 15:39:32 +0100530 desired = GetDesiredPackageVersions(target, package)
Zdenek Behan699ddd32012-04-13 07:14:08 +0200531 desired_num = VersionListToNumeric(target, package, desired, False)
Mike Frysinger785b0c32017-09-13 01:35:59 -0400532 if pkg in NEW_PACKAGES and usepkg:
533 # Skip this binary package (for now).
534 continue
Zdenek Behan4eb6fd22012-03-12 17:00:56 +0100535 mergemap[pkg] = set(desired_num).difference(current)
Mike Frysinger7f0e1982017-09-12 17:08:50 -0400536 logging.debug(' %s -> %s', current, desired_num)
ChromeOS Developereb8a5c42021-01-12 00:05:02 +0000537 if is_post_cross_target:
538 post_cross_pkgs.add(pkg)
Zdenek Behan508dcce2011-12-05 15:39:32 +0100539
Mike Frysinger7f0e1982017-09-12 17:08:50 -0400540 packages = [pkg for pkg, vers in mergemap.items() if vers]
Zdenek Behan508dcce2011-12-05 15:39:32 +0100541 if not packages:
Mike Frysinger3bba5032016-09-20 14:15:04 -0400542 logging.info('Nothing to update!')
David Jamesf8c672f2012-11-06 13:38:11 -0800543 return False
Zdenek Behan508dcce2011-12-05 15:39:32 +0100544
Mike Frysinger3bba5032016-09-20 14:15:04 -0400545 logging.info('Updating packages:')
546 logging.info('%s', packages)
Zdenek Behan508dcce2011-12-05 15:39:32 +0100547
Zdenek Behan7e33b4e2012-03-12 17:00:56 +0100548 cmd = [EMERGE_CMD, '--oneshot', '--update']
Zdenek Behan508dcce2011-12-05 15:39:32 +0100549 if usepkg:
550 cmd.extend(['--getbinpkg', '--usepkgonly'])
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700551 if root != '/':
552 cmd.extend(['--sysroot=%s' % root, '--root=%s' % root])
Zdenek Behan508dcce2011-12-05 15:39:32 +0100553
ChromeOS Developereb8a5c42021-01-12 00:05:02 +0000554 if usepkg:
555 # Since we are not building from source, we can handle
556 # all packages in one go.
557 cmd.extend(packages)
558 cros_build_lib.run(cmd)
559 else:
George Burgess IVd7762ab2021-04-29 10:54:55 -0700560 pre_cross_items = [pkg for pkg in packages if pkg not in post_cross_pkgs]
561 if pre_cross_items:
562 cros_build_lib.run(cmd + pre_cross_items)
ChromeOS Developereb8a5c42021-01-12 00:05:02 +0000563 post_cross_items = [pkg for pkg in packages if pkg in post_cross_pkgs]
George Burgess IVd7762ab2021-04-29 10:54:55 -0700564 if post_cross_items:
565 cros_build_lib.run(cmd + post_cross_items)
David Jamesf8c672f2012-11-06 13:38:11 -0800566 return True
Zdenek Behan508dcce2011-12-05 15:39:32 +0100567
568
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700569def CleanTargets(targets, root='/'):
570 """Unmerges old packages that are assumed unnecessary.
571
572 Args:
573 targets: The list of targets to clean up.
574 root: The install root in which we want packages cleaned up.
575 """
Zdenek Behan508dcce2011-12-05 15:39:32 +0100576 unmergemap = {}
577 for target in targets:
Mike Frysinger3bba5032016-09-20 14:15:04 -0400578 logging.debug('Cleaning target %s', target)
Zdenek Behanf4d18a02012-03-22 15:45:05 +0100579 for package in GetTargetPackages(target):
Mike Frysinger3bba5032016-09-20 14:15:04 -0400580 logging.debug(' Cleaning package %s', package)
Zdenek Behan4eb6fd22012-03-12 17:00:56 +0100581 pkg = GetPortagePackage(target, package)
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700582 current = GetInstalledPackageVersions(pkg, root=root)
Zdenek Behan508dcce2011-12-05 15:39:32 +0100583 desired = GetDesiredPackageVersions(target, package)
Gilad Arnold2fcb0aa2015-05-21 14:51:41 -0700584 # NOTE: This refers to installed packages (vartree) rather than the
585 # Portage version (porttree and/or bintree) when determining the current
586 # version. While this isn't the most accurate thing to do, it is probably
587 # a good simple compromise, which should have the desired result of
588 # uninstalling everything but the latest installed version. In
589 # particular, using the bintree (--usebinpkg) requires a non-trivial
590 # binhost sync and is probably more complex than useful.
Zdenek Behan699ddd32012-04-13 07:14:08 +0200591 desired_num = VersionListToNumeric(target, package, desired, True)
592 if not set(desired_num).issubset(current):
Mike Frysinger3bba5032016-09-20 14:15:04 -0400593 logging.warning('Error detecting stable version for %s, '
594 'skipping clean!', pkg)
Zdenek Behan699ddd32012-04-13 07:14:08 +0200595 return
Zdenek Behan508dcce2011-12-05 15:39:32 +0100596 unmergemap[pkg] = set(current).difference(desired_num)
597
598 # Cleaning doesn't care about consistency and rebuilding package.* files.
599 packages = []
Mike Frysinger0bdbc102019-06-13 15:27:29 -0400600 for pkg, vers in unmergemap.items():
Zdenek Behan508dcce2011-12-05 15:39:32 +0100601 packages.extend('=%s-%s' % (pkg, ver) for ver in vers if ver != '9999')
602
603 if packages:
Mike Frysinger3bba5032016-09-20 14:15:04 -0400604 logging.info('Cleaning packages:')
605 logging.info('%s', packages)
Zdenek Behan7e33b4e2012-03-12 17:00:56 +0100606 cmd = [EMERGE_CMD, '--unmerge']
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700607 if root != '/':
608 cmd.extend(['--sysroot=%s' % root, '--root=%s' % root])
Zdenek Behan508dcce2011-12-05 15:39:32 +0100609 cmd.extend(packages)
Mike Frysinger45602c72019-09-22 02:15:11 -0400610 cros_build_lib.run(cmd)
Zdenek Behan508dcce2011-12-05 15:39:32 +0100611 else:
Mike Frysinger3bba5032016-09-20 14:15:04 -0400612 logging.info('Nothing to clean!')
Zdenek Behan508dcce2011-12-05 15:39:32 +0100613
614
Adrian Ratiu78e534d2021-01-06 19:58:38 +0200615def SelectActiveToolchains(targets, root='/'):
Zdenek Behan508dcce2011-12-05 15:39:32 +0100616 """Runs gcc-config and binutils-config to select the desired.
617
Mike Frysingerdc649ae2014-11-26 19:29:24 -0500618 Args:
619 targets: The targets to select
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700620 root: The root where we want to select toolchain versions.
Zdenek Behan508dcce2011-12-05 15:39:32 +0100621 """
622 for package in ['gcc', 'binutils']:
623 for target in targets:
Mike Frysinger785b0c32017-09-13 01:35:59 -0400624 # See if this package is part of this target.
625 if package not in GetTargetPackages(target):
626 logging.debug('%s: %s is not used', target, package)
627 continue
628
Zdenek Behan508dcce2011-12-05 15:39:32 +0100629 # Pick the first version in the numbered list as the selected one.
630 desired = GetDesiredPackageVersions(target, package)
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700631 desired_num = VersionListToNumeric(target, package, desired, True,
632 root=root)
Zdenek Behan508dcce2011-12-05 15:39:32 +0100633 desired = desired_num[0]
634 # *-config does not play revisions, strip them, keep just PV.
635 desired = portage.versions.pkgsplit('%s-%s' % (package, desired))[1]
636
Mike Frysinger785b0c32017-09-13 01:35:59 -0400637 if target.startswith('host'):
Zdenek Behan508dcce2011-12-05 15:39:32 +0100638 # *-config is the only tool treating host identically (by tuple).
David James27ac4ae2012-12-03 23:16:15 -0800639 target = toolchain.GetHostTuple()
Zdenek Behan508dcce2011-12-05 15:39:32 +0100640
641 # And finally, attach target to it.
642 desired = '%s-%s' % (target, desired)
643
David James7ec5efc2012-11-06 09:39:49 -0800644 extra_env = {'CHOST': target}
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700645 if root != '/':
646 extra_env['ROOT'] = root
David James7ec5efc2012-11-06 09:39:49 -0800647 cmd = ['%s-config' % package, '-c', target]
Mike Frysinger45602c72019-09-22 02:15:11 -0400648 result = cros_build_lib.run(
Mike Frysinger0282d222019-12-17 17:15:48 -0500649 cmd, print_cmd=False, stdout=True, encoding='utf-8',
Mike Frysingerb3202be2019-11-15 22:25:59 -0500650 extra_env=extra_env)
Mike Frysingerd6e2df02014-11-26 02:55:04 -0500651 current = result.output.splitlines()[0]
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700652
653 # Do not reconfig when the current is live or nothing needs to be done.
654 extra_env = {'ROOT': root} if root != '/' else None
Zdenek Behan508dcce2011-12-05 15:39:32 +0100655 if current != desired and current != '9999':
Mike Frysingerd6e2df02014-11-26 02:55:04 -0500656 cmd = [package + '-config', desired]
Mike Frysinger45602c72019-09-22 02:15:11 -0400657 cros_build_lib.run(cmd, print_cmd=False, extra_env=extra_env)
Zdenek Behan508dcce2011-12-05 15:39:32 +0100658
659
Mike Frysinger35247af2012-11-16 18:58:06 -0500660def ExpandTargets(targets_wanted):
661 """Expand any possible toolchain aliases into full targets
662
663 This will expand 'all' and 'sdk' into the respective toolchain tuples.
664
665 Args:
666 targets_wanted: The targets specified by the user.
Mike Frysinger1a736a82013-12-12 01:50:59 -0500667
Mike Frysinger35247af2012-11-16 18:58:06 -0500668 Returns:
Gilad Arnold8195b532015-04-07 10:56:30 +0300669 Dictionary of concrete targets and their toolchain tuples.
Mike Frysinger35247af2012-11-16 18:58:06 -0500670 """
Mike Frysinger35247af2012-11-16 18:58:06 -0500671 targets_wanted = set(targets_wanted)
Don Garrettc0c74002015-10-09 12:58:19 -0700672 if targets_wanted == set(['boards']):
673 # Only pull targets from the included boards.
Gilad Arnold8195b532015-04-07 10:56:30 +0300674 return {}
675
676 all_targets = toolchain.GetAllTargets()
Mike Frysinger35247af2012-11-16 18:58:06 -0500677 if targets_wanted == set(['all']):
Gilad Arnold8195b532015-04-07 10:56:30 +0300678 return all_targets
679 if targets_wanted == set(['sdk']):
Mike Frysinger35247af2012-11-16 18:58:06 -0500680 # Filter out all the non-sdk toolchains as we don't want to mess
681 # with those in all of our builds.
Gilad Arnold8195b532015-04-07 10:56:30 +0300682 return toolchain.FilterToolchains(all_targets, 'sdk', True)
683
684 # Verify user input.
685 nonexistent = targets_wanted.difference(all_targets)
686 if nonexistent:
Mike Frysingercd790662019-10-17 00:23:13 -0400687 raise ValueError('Invalid targets: %s' % (','.join(nonexistent),))
Gilad Arnold8195b532015-04-07 10:56:30 +0300688 return {t: all_targets[t] for t in targets_wanted}
Mike Frysinger35247af2012-11-16 18:58:06 -0500689
690
David Jamesf8c672f2012-11-06 13:38:11 -0800691def UpdateToolchains(usepkg, deleteold, hostonly, reconfig,
Don Garrettc0c74002015-10-09 12:58:19 -0700692 targets_wanted, boards_wanted, root='/'):
Zdenek Behan508dcce2011-12-05 15:39:32 +0100693 """Performs all steps to create a synchronized toolchain enviroment.
694
Mike Frysingerdc649ae2014-11-26 19:29:24 -0500695 Args:
696 usepkg: Use prebuilt packages
697 deleteold: Unmerge deprecated packages
698 hostonly: Only setup the host toolchain
699 reconfig: Reload crossdev config and reselect toolchains
700 targets_wanted: All the targets to update
701 boards_wanted: Load targets from these boards
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700702 root: The root in which to install the toolchains.
Zdenek Behan508dcce2011-12-05 15:39:32 +0100703 """
David Jamesf8c672f2012-11-06 13:38:11 -0800704 targets, crossdev_targets, reconfig_targets = {}, {}, {}
Zdenek Behan7e33b4e2012-03-12 17:00:56 +0100705 if not hostonly:
706 # For hostonly, we can skip most of the below logic, much of which won't
707 # work on bare systems where this is useful.
Mike Frysinger35247af2012-11-16 18:58:06 -0500708 targets = ExpandTargets(targets_wanted)
Mike Frysinger7ccee992012-06-01 21:27:59 -0400709
Don Garrettc0c74002015-10-09 12:58:19 -0700710 # Now re-add any targets that might be from this board. This is to
Gilad Arnold8195b532015-04-07 10:56:30 +0300711 # allow unofficial boards to declare their own toolchains.
Mike Frysinger7ccee992012-06-01 21:27:59 -0400712 for board in boards_wanted:
David James27ac4ae2012-12-03 23:16:15 -0800713 targets.update(toolchain.GetToolchainsForBoard(board))
Zdenek Behan508dcce2011-12-05 15:39:32 +0100714
Zdenek Behan7e33b4e2012-03-12 17:00:56 +0100715 # First check and initialize all cross targets that need to be.
Mike Frysinger7ccee992012-06-01 21:27:59 -0400716 for target in targets:
717 if TargetIsInitialized(target):
718 reconfig_targets[target] = targets[target]
719 else:
720 crossdev_targets[target] = targets[target]
Zdenek Behan7e33b4e2012-03-12 17:00:56 +0100721 if crossdev_targets:
Mike Frysinger3bba5032016-09-20 14:15:04 -0400722 logging.info('The following targets need to be re-initialized:')
723 logging.info('%s', crossdev_targets)
David James66a09c42012-11-05 13:31:38 -0800724 Crossdev.UpdateTargets(crossdev_targets, usepkg)
Zdenek Behan8be29ba2012-05-29 23:10:34 +0200725 # Those that were not initialized may need a config update.
David James66a09c42012-11-05 13:31:38 -0800726 Crossdev.UpdateTargets(reconfig_targets, usepkg, config_only=True)
Zdenek Behan508dcce2011-12-05 15:39:32 +0100727
Mike Frysinger66814c32017-10-09 18:11:46 -0400728 # If we're building a subset of toolchains for a board, we might not have
729 # all the tuples that the packages expect. We don't define the "full" set
730 # of tuples currently other than "whatever the full sdk has normally".
731 if usepkg or set(('all', 'sdk')) & targets_wanted:
732 # Since we have cross-compilers now, we can update these packages.
733 targets['host-post-cross'] = {}
Mike Frysinger785b0c32017-09-13 01:35:59 -0400734
Zdenek Behan7e33b4e2012-03-12 17:00:56 +0100735 # We want host updated.
Mike Frysinger7ccee992012-06-01 21:27:59 -0400736 targets['host'] = {}
Zdenek Behan7e33b4e2012-03-12 17:00:56 +0100737
738 # Now update all packages.
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700739 if UpdateTargets(targets, usepkg, root=root) or crossdev_targets or reconfig:
Adrian Ratiu78e534d2021-01-06 19:58:38 +0200740 SelectActiveToolchains(targets, root=root)
David James7ec5efc2012-11-06 09:39:49 -0800741
742 if deleteold:
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700743 CleanTargets(targets, root=root)
Zdenek Behan508dcce2011-12-05 15:39:32 +0100744
Mike Frysingerc880a962013-11-08 13:59:06 -0500745 # Now that we've cleared out old versions, see if we need to rebuild
746 # anything. Can't do this earlier as it might not be broken.
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700747 RebuildLibtool(root=root)
Mike Frysingerc880a962013-11-08 13:59:06 -0500748
Zdenek Behan508dcce2011-12-05 15:39:32 +0100749
Bertrand SIMONNETcae9d5f2015-03-09 15:58:01 -0700750def ShowConfig(name):
751 """Show the toolchain tuples used by |name|
Mike Frysinger35247af2012-11-16 18:58:06 -0500752
753 Args:
Don Garrettc0c74002015-10-09 12:58:19 -0700754 name: The board name to query.
Mike Frysinger35247af2012-11-16 18:58:06 -0500755 """
Don Garrettc0c74002015-10-09 12:58:19 -0700756 toolchains = toolchain.GetToolchainsForBoard(name)
Mike Frysinger35247af2012-11-16 18:58:06 -0500757 # Make sure we display the default toolchain first.
Mike Frysinger3bba5032016-09-20 14:15:04 -0400758 # Note: Do not use logging here as this is meant to be used by other tools.
Mike Frysinger383367e2014-09-16 15:06:17 -0400759 print(','.join(
Mike Frysinger818d9632019-08-24 14:43:05 -0400760 list(toolchain.FilterToolchains(toolchains, 'default', True)) +
761 list(toolchain.FilterToolchains(toolchains, 'default', False))))
Mike Frysinger35247af2012-11-16 18:58:06 -0500762
763
Mike Frysinger35247af2012-11-16 18:58:06 -0500764def GeneratePathWrapper(root, wrappath, path):
765 """Generate a shell script to execute another shell script
766
767 Since we can't symlink a wrapped ELF (see GenerateLdsoWrapper) because the
768 argv[0] won't be pointing to the correct path, generate a shell script that
769 just executes another program with its full path.
770
771 Args:
772 root: The root tree to generate scripts inside of
773 wrappath: The full path (inside |root|) to create the wrapper
774 path: The target program which this wrapper will execute
775 """
776 replacements = {
777 'path': path,
778 'relroot': os.path.relpath('/', os.path.dirname(wrappath)),
779 }
Takuto Ikuta58403972018-08-16 18:52:51 +0900780
781 # Do not use exec here, because exec invokes script with absolute path in
782 # argv0. Keeping relativeness allows us to remove abs path from compile result
783 # and leads directory independent build cache sharing in some distributed
784 # build system.
Mike Frysinger35247af2012-11-16 18:58:06 -0500785 wrapper = """#!/bin/sh
Takuto Ikuta58403972018-08-16 18:52:51 +0900786basedir=$(dirname "$0")
787"${basedir}/%(relroot)s%(path)s" "$@"
788exit "$?"
Mike Frysinger35247af2012-11-16 18:58:06 -0500789""" % replacements
790 root_wrapper = root + wrappath
791 if os.path.islink(root_wrapper):
792 os.unlink(root_wrapper)
793 else:
794 osutils.SafeMakedirs(os.path.dirname(root_wrapper))
795 osutils.WriteFile(root_wrapper, wrapper)
Mike Frysinger60ec1012013-10-21 00:11:10 -0400796 os.chmod(root_wrapper, 0o755)
Mike Frysinger35247af2012-11-16 18:58:06 -0500797
798
Rahul Chaudhrya8127bb2016-07-22 15:53:17 -0700799def FixClangXXWrapper(root, path):
800 """Fix wrapper shell scripts and symlinks for invoking clang++
801
802 In a typical installation, clang++ symlinks to clang, which symlinks to the
803 elf executable. The executable distinguishes between clang and clang++ based
804 on argv[0].
805
806 When invoked through the LdsoWrapper, argv[0] always contains the path to the
807 executable elf file, making clang/clang++ invocations indistinguishable.
808
809 This function detects if the elf executable being wrapped is clang-X.Y, and
810 fixes wrappers/symlinks as necessary so that clang++ will work correctly.
811
812 The calling sequence now becomes:
813 -) clang++ invocation turns into clang++-3.9 (which is a copy of clang-3.9,
814 the Ldsowrapper).
815 -) clang++-3.9 uses the Ldso to invoke clang++-3.9.elf, which is a symlink
816 to the original clang-3.9 elf.
817 -) The difference this time is that inside the elf file execution, $0 is
818 set as .../usr/bin/clang++-3.9.elf, which contains 'clang++' in the name.
819
Manoj Guptaae268142018-04-27 23:28:36 -0700820 Update: Starting since clang 7, the clang and clang++ are symlinks to
821 clang-7 binary, not clang-7.0. The pattern match is extended to handle
822 both clang-7 and clang-7.0 cases for now. (https://crbug.com/837889)
823
Rahul Chaudhrya8127bb2016-07-22 15:53:17 -0700824 Args:
825 root: The root tree to generate scripts / symlinks inside of
826 path: The target elf for which LdsoWrapper was created
827 """
Manoj Guptaae268142018-04-27 23:28:36 -0700828 if re.match(r'/usr/bin/clang-\d+(\.\d+)*$', path):
Rahul Chaudhrya8127bb2016-07-22 15:53:17 -0700829 logging.info('fixing clang++ invocation for %s', path)
830 clangdir = os.path.dirname(root + path)
831 clang = os.path.basename(path)
832 clangxx = clang.replace('clang', 'clang++')
833
834 # Create a symlink clang++-X.Y.elf to point to clang-X.Y.elf
835 os.symlink(clang + '.elf', os.path.join(clangdir, clangxx + '.elf'))
836
837 # Create a hardlink clang++-X.Y pointing to clang-X.Y
838 os.link(os.path.join(clangdir, clang), os.path.join(clangdir, clangxx))
839
840 # Adjust the clang++ symlink to point to clang++-X.Y
841 os.unlink(os.path.join(clangdir, 'clang++'))
842 os.symlink(clangxx, os.path.join(clangdir, 'clang++'))
843
844
Mike Frysinger35247af2012-11-16 18:58:06 -0500845def FileIsCrosSdkElf(elf):
846 """Determine if |elf| is an ELF that we execute in the cros_sdk
847
848 We don't need this to be perfect, just quick. It makes sure the ELF
849 is a 64bit LSB x86_64 ELF. That is the native type of cros_sdk.
850
851 Args:
852 elf: The file to check
Mike Frysinger1a736a82013-12-12 01:50:59 -0500853
Mike Frysinger35247af2012-11-16 18:58:06 -0500854 Returns:
855 True if we think |elf| is a native ELF
856 """
Mike Frysinger4a12bf12019-12-04 04:20:10 -0500857 with open(elf, 'rb') as f:
Mike Frysinger35247af2012-11-16 18:58:06 -0500858 data = f.read(20)
859 # Check the magic number, EI_CLASS, EI_DATA, and e_machine.
Mike Frysinger4a12bf12019-12-04 04:20:10 -0500860 return (data[0:4] == b'\x7fELF' and
Mike Frysingerdd34dfe2019-12-10 16:25:47 -0500861 data[4:5] == b'\x02' and
862 data[5:6] == b'\x01' and
863 data[18:19] == b'\x3e')
Mike Frysinger35247af2012-11-16 18:58:06 -0500864
865
866def IsPathPackagable(ptype, path):
867 """Should the specified file be included in a toolchain package?
868
869 We only need to handle files as we'll create dirs as we need them.
870
871 Further, trim files that won't be useful:
872 - non-english translations (.mo) since it'd require env vars
873 - debug files since these are for the host compiler itself
874 - info/man pages as they're big, and docs are online, and the
875 native docs should work fine for the most part (`man gcc`)
876
877 Args:
878 ptype: A string describing the path type (i.e. 'file' or 'dir' or 'sym')
879 path: The full path to inspect
Mike Frysinger1a736a82013-12-12 01:50:59 -0500880
Mike Frysinger35247af2012-11-16 18:58:06 -0500881 Returns:
882 True if we want to include this path in the package
883 """
884 return not (ptype in ('dir',) or
885 path.startswith('/usr/lib/debug/') or
886 os.path.splitext(path)[1] == '.mo' or
887 ('/man/' in path or '/info/' in path))
888
889
890def ReadlinkRoot(path, root):
891 """Like os.readlink(), but relative to a |root|
892
893 Args:
894 path: The symlink to read
895 root: The path to use for resolving absolute symlinks
Mike Frysinger1a736a82013-12-12 01:50:59 -0500896
Mike Frysinger35247af2012-11-16 18:58:06 -0500897 Returns:
898 A fully resolved symlink path
899 """
900 while os.path.islink(root + path):
901 path = os.path.join(os.path.dirname(path), os.readlink(root + path))
902 return path
903
904
905def _GetFilesForTarget(target, root='/'):
906 """Locate all the files to package for |target|
907
908 This does not cover ELF dependencies.
909
910 Args:
911 target: The toolchain target name
912 root: The root path to pull all packages from
Mike Frysinger1a736a82013-12-12 01:50:59 -0500913
Mike Frysinger35247af2012-11-16 18:58:06 -0500914 Returns:
915 A tuple of a set of all packable paths, and a set of all paths which
916 are also native ELFs
917 """
918 paths = set()
919 elfs = set()
920
921 # Find all the files owned by the packages for this target.
922 for pkg in GetTargetPackages(target):
Mike Frysinger35247af2012-11-16 18:58:06 -0500923
Rahul Chaudhry4b803052015-05-13 15:25:56 -0700924 # Skip Go compiler from redistributable packages.
925 # The "go" executable has GOROOT=/usr/lib/go/${CTARGET} hardcoded
926 # into it. Due to this, the toolchain cannot be unpacked anywhere
927 # else and be readily useful. To enable packaging Go, we need to:
928 # -) Tweak the wrappers/environment to override GOROOT
929 # automatically based on the unpack location.
930 # -) Make sure the ELF dependency checking and wrapping logic
931 # below skips the Go toolchain executables and libraries.
932 # -) Make sure the packaging process maintains the relative
933 # timestamps of precompiled standard library packages.
934 # (see dev-lang/go ebuild for details).
935 if pkg == 'ex_go':
936 continue
937
Yunlian Jiang36f35242018-04-27 10:18:40 -0700938 # Use armv7a-cros-linux-gnueabi/compiler-rt for
939 # armv7a-cros-linux-gnueabihf/compiler-rt.
940 # Currently the armv7a-cros-linux-gnueabi is actually
941 # the same as armv7a-cros-linux-gnueabihf with different names.
942 # Because of that, for compiler-rt, it generates the same binary in
943 # the same location. To avoid the installation conflict, we do not
944 # install anything for 'armv7a-cros-linux-gnueabihf'. This would cause
945 # problem if other people try to use standalone armv7a-cros-linux-gnueabihf
946 # toolchain.
947 if 'compiler-rt' in pkg and 'armv7a-cros-linux-gnueabi' in target:
948 atom = GetPortagePackage(target, pkg)
949 cat, pn = atom.split('/')
950 ver = GetInstalledPackageVersions(atom, root=root)[0]
Yunlian Jiang36f35242018-04-27 10:18:40 -0700951 dblink = portage.dblink(cat, pn + '-' + ver, myroot=root,
952 settings=portage.settings)
953 contents = dblink.getcontents()
954 if not contents:
955 if 'hf' in target:
956 new_target = 'armv7a-cros-linux-gnueabi'
957 else:
958 new_target = 'armv7a-cros-linux-gnueabihf'
959 atom = GetPortagePackage(new_target, pkg)
960 else:
961 atom = GetPortagePackage(target, pkg)
962
Mike Frysinger35247af2012-11-16 18:58:06 -0500963 cat, pn = atom.split('/')
Gilad Arnold2dab78c2015-05-21 14:43:33 -0700964 ver = GetInstalledPackageVersions(atom, root=root)[0]
Ralph Nathan03047282015-03-23 11:09:32 -0700965 logging.info('packaging %s-%s', atom, ver)
Mike Frysinger35247af2012-11-16 18:58:06 -0500966
Mike Frysinger35247af2012-11-16 18:58:06 -0500967 dblink = portage.dblink(cat, pn + '-' + ver, myroot=root,
968 settings=portage.settings)
969 contents = dblink.getcontents()
970 for obj in contents:
971 ptype = contents[obj][0]
972 if not IsPathPackagable(ptype, obj):
973 continue
974
975 if ptype == 'obj':
976 # For native ELFs, we need to pull in their dependencies too.
977 if FileIsCrosSdkElf(obj):
Mike Frysinger60a2f6c2019-12-04 04:18:58 -0500978 logging.debug('Adding ELF %s', obj)
Mike Frysinger35247af2012-11-16 18:58:06 -0500979 elfs.add(obj)
Mike Frysinger60a2f6c2019-12-04 04:18:58 -0500980 logging.debug('Adding path %s', obj)
Mike Frysinger35247af2012-11-16 18:58:06 -0500981 paths.add(obj)
982
983 return paths, elfs
984
985
986def _BuildInitialPackageRoot(output_dir, paths, elfs, ldpaths,
Mike Frysingerd6e2df02014-11-26 02:55:04 -0500987 path_rewrite_func=lambda x: x, root='/'):
Mike Frysinger35247af2012-11-16 18:58:06 -0500988 """Link in all packable files and their runtime dependencies
989
990 This also wraps up executable ELFs with helper scripts.
991
992 Args:
993 output_dir: The output directory to store files
994 paths: All the files to include
995 elfs: All the files which are ELFs (a subset of |paths|)
996 ldpaths: A dict of static ldpath information
997 path_rewrite_func: User callback to rewrite paths in output_dir
998 root: The root path to pull all packages/files from
999 """
1000 # Link in all the files.
Mike Frysinger221bd822017-09-29 02:51:47 -04001001 sym_paths = {}
Mike Frysinger35247af2012-11-16 18:58:06 -05001002 for path in paths:
1003 new_path = path_rewrite_func(path)
Mike Frysinger60a2f6c2019-12-04 04:18:58 -05001004 logging.debug('Transformed %s to %s', path, new_path)
Mike Frysinger35247af2012-11-16 18:58:06 -05001005 dst = output_dir + new_path
1006 osutils.SafeMakedirs(os.path.dirname(dst))
1007
1008 # Is this a symlink which we have to rewrite or wrap?
1009 # Delay wrap check until after we have created all paths.
1010 src = root + path
1011 if os.path.islink(src):
1012 tgt = os.readlink(src)
1013 if os.path.sep in tgt:
Mike Frysinger221bd822017-09-29 02:51:47 -04001014 sym_paths[lddtree.normpath(ReadlinkRoot(src, root))] = new_path
Mike Frysinger35247af2012-11-16 18:58:06 -05001015
1016 # Rewrite absolute links to relative and then generate the symlink
1017 # ourselves. All other symlinks can be hardlinked below.
1018 if tgt[0] == '/':
1019 tgt = os.path.relpath(tgt, os.path.dirname(new_path))
1020 os.symlink(tgt, dst)
1021 continue
1022
Mike Frysinger60a2f6c2019-12-04 04:18:58 -05001023 logging.debug('Linking path %s -> %s', src, dst)
Mike Frysinger35247af2012-11-16 18:58:06 -05001024 os.link(src, dst)
1025
Mike Frysinger35247af2012-11-16 18:58:06 -05001026 # Locate all the dependencies for all the ELFs. Stick them all in the
1027 # top level "lib" dir to make the wrapper simpler. This exact path does
1028 # not matter since we execute ldso directly, and we tell the ldso the
1029 # exact path to search for its libraries.
1030 libdir = os.path.join(output_dir, 'lib')
1031 osutils.SafeMakedirs(libdir)
1032 donelibs = set()
Mike Frysinger9fe02342019-12-12 17:52:53 -05001033 basenamelibs = set()
Mike Frysinger4331fc62017-09-28 14:03:25 -04001034 glibc_re = re.compile(r'/lib(c|pthread)-[0-9.]+\.so$')
Mike Frysinger35247af2012-11-16 18:58:06 -05001035 for elf in elfs:
Mike Frysingerea7688e2014-07-31 22:40:33 -04001036 e = lddtree.ParseELF(elf, root=root, ldpaths=ldpaths)
Mike Frysinger60a2f6c2019-12-04 04:18:58 -05001037 logging.debug('Parsed elf %s data: %s', elf, e)
Mike Frysinger35247af2012-11-16 18:58:06 -05001038 interp = e['interp']
Mike Frysinger221bd822017-09-29 02:51:47 -04001039
Mike Frysinger9fe02342019-12-12 17:52:53 -05001040 # TODO(crbug.com/917193): Drop this hack once libopcodes linkage is fixed.
1041 if os.path.basename(elf).startswith('libopcodes-'):
1042 continue
Mike Frysinger35247af2012-11-16 18:58:06 -05001043
Mike Frysinger00b129f2021-04-21 18:11:48 -04001044 # Copy all the dependencies before we copy the program & generate wrappers.
Mike Frysinger9fe02342019-12-12 17:52:53 -05001045 for lib, lib_data in e['libs'].items():
Mike Frysinger35247af2012-11-16 18:58:06 -05001046 src = path = lib_data['path']
1047 if path is None:
Ralph Nathan446aee92015-03-23 14:44:56 -07001048 logging.warning('%s: could not locate %s', elf, lib)
Mike Frysinger35247af2012-11-16 18:58:06 -05001049 continue
Mike Frysinger9fe02342019-12-12 17:52:53 -05001050
1051 # No need to try and copy the same source lib multiple times.
1052 if path in donelibs:
1053 continue
1054 donelibs.add(path)
1055
1056 # Die if we try to normalize different source libs with the same basename.
1057 if lib in basenamelibs:
1058 logging.error('Multiple sources detected for %s:\n new: %s\n old: %s',
1059 os.path.join('/lib', lib), path,
1060 ' '.join(x for x in donelibs
1061 if x != path and os.path.basename(x) == lib))
1062 # TODO(crbug.com/917193): Make this fatal.
1063 # cros_build_lib.Die('Unable to resolve lib conflicts')
1064 continue
1065 basenamelibs.add(lib)
Mike Frysinger35247af2012-11-16 18:58:06 -05001066
1067 # Needed libs are the SONAME, but that is usually a symlink, not a
1068 # real file. So link in the target rather than the symlink itself.
1069 # We have to walk all the possible symlinks (SONAME could point to a
1070 # symlink which points to a symlink), and we have to handle absolute
1071 # ourselves (since we have a "root" argument).
1072 dst = os.path.join(libdir, os.path.basename(path))
1073 src = ReadlinkRoot(src, root)
1074
Mike Frysinger60a2f6c2019-12-04 04:18:58 -05001075 logging.debug('Linking lib %s -> %s', root + src, dst)
Mike Frysinger35247af2012-11-16 18:58:06 -05001076 os.link(root + src, dst)
1077
Mike Frysinger00b129f2021-04-21 18:11:48 -04001078 # Do not create wrapper for libc. crbug.com/766827
1079 if interp and not glibc_re.search(elf):
1080 # Generate a wrapper if it is executable.
1081 interp = os.path.join('/lib', os.path.basename(interp))
1082 lddtree.GenerateLdsoWrapper(output_dir, path_rewrite_func(elf), interp,
1083 libpaths=e['rpath'] + e['runpath'])
1084 FixClangXXWrapper(output_dir, path_rewrite_func(elf))
1085
1086 # Wrap any symlinks to the wrapper.
1087 if elf in sym_paths:
1088 link = sym_paths[elf]
1089 GeneratePathWrapper(output_dir, link, elf)
1090
Mike Frysinger35247af2012-11-16 18:58:06 -05001091
1092def _EnvdGetVar(envd, var):
1093 """Given a Gentoo env.d file, extract a var from it
1094
1095 Args:
1096 envd: The env.d file to load (may be a glob path)
1097 var: The var to extract
Mike Frysinger1a736a82013-12-12 01:50:59 -05001098
Mike Frysinger35247af2012-11-16 18:58:06 -05001099 Returns:
1100 The value of |var|
1101 """
1102 envds = glob.glob(envd)
1103 assert len(envds) == 1, '%s: should have exactly 1 env.d file' % envd
1104 envd = envds[0]
Mike Frysingere652ba12019-09-08 00:57:43 -04001105 return key_value_store.LoadFile(envd)[var]
Mike Frysinger35247af2012-11-16 18:58:06 -05001106
1107
1108def _ProcessBinutilsConfig(target, output_dir):
1109 """Do what binutils-config would have done"""
1110 binpath = os.path.join('/bin', target + '-')
Mike Frysingerd4d40fd2014-11-06 17:30:57 -05001111
Adrian Ratiu78e534d2021-01-06 19:58:38 +02001112 # Locate the bin dir holding the linker and perform some sanity checks
Mike Frysingerd4d40fd2014-11-06 17:30:57 -05001113 binutils_bin_path = os.path.join(output_dir, 'usr', toolchain.GetHostTuple(),
1114 target, 'binutils-bin')
Adrian Ratiu78e534d2021-01-06 19:58:38 +02001115 globpath = os.path.join(binutils_bin_path, '*')
Mike Frysinger35247af2012-11-16 18:58:06 -05001116 srcpath = glob.glob(globpath)
Adrian Ratiu78e534d2021-01-06 19:58:38 +02001117 assert len(srcpath) == 1, ('%s: matched more than one path. Is Gold enabled?'
1118 % globpath)
1119 srcpath = srcpath[0]
1120 ld_path = os.path.join(srcpath, 'ld')
1121 assert os.path.exists(ld_path), '%s: linker is missing!' % ld_path
1122 ld_path = os.path.join(srcpath, 'ld.bfd')
1123 assert os.path.exists(ld_path), '%s: linker is missing!' % ld_path
Rahul Chaudhry4891b4d2017-03-08 10:31:27 -08001124
Mike Frysinger78b7a812014-11-26 19:45:23 -05001125 srcpath = srcpath[len(output_dir):]
Mike Frysinger35247af2012-11-16 18:58:06 -05001126 gccpath = os.path.join('/usr', 'libexec', 'gcc')
1127 for prog in os.listdir(output_dir + srcpath):
1128 # Skip binaries already wrapped.
1129 if not prog.endswith('.real'):
1130 GeneratePathWrapper(output_dir, binpath + prog,
1131 os.path.join(srcpath, prog))
1132 GeneratePathWrapper(output_dir, os.path.join(gccpath, prog),
1133 os.path.join(srcpath, prog))
1134
David James27ac4ae2012-12-03 23:16:15 -08001135 libpath = os.path.join('/usr', toolchain.GetHostTuple(), target, 'lib')
Mike Frysingerd4d40fd2014-11-06 17:30:57 -05001136 envd = os.path.join(output_dir, 'etc', 'env.d', 'binutils', '*')
Mike Frysinger35247af2012-11-16 18:58:06 -05001137 srcpath = _EnvdGetVar(envd, 'LIBPATH')
1138 os.symlink(os.path.relpath(srcpath, os.path.dirname(libpath)),
1139 output_dir + libpath)
1140
1141
1142def _ProcessGccConfig(target, output_dir):
1143 """Do what gcc-config would have done"""
1144 binpath = '/bin'
1145 envd = os.path.join(output_dir, 'etc', 'env.d', 'gcc', '*')
1146 srcpath = _EnvdGetVar(envd, 'GCC_PATH')
1147 for prog in os.listdir(output_dir + srcpath):
1148 # Skip binaries already wrapped.
1149 if (not prog.endswith('.real') and
1150 not prog.endswith('.elf') and
1151 prog.startswith(target)):
1152 GeneratePathWrapper(output_dir, os.path.join(binpath, prog),
1153 os.path.join(srcpath, prog))
1154 return srcpath
1155
1156
Frank Henigman179ec7c2015-02-06 03:01:09 -05001157def _ProcessSysrootWrappers(_target, output_dir, srcpath):
1158 """Remove chroot-specific things from our sysroot wrappers"""
Mike Frysinger35247af2012-11-16 18:58:06 -05001159 # Disable ccache since we know it won't work outside of chroot.
Tobias Boschddd16492019-08-14 09:29:54 -07001160
Tobias Boschddd16492019-08-14 09:29:54 -07001161 # Use the version of the wrapper that does not use ccache.
Frank Henigman179ec7c2015-02-06 03:01:09 -05001162 for sysroot_wrapper in glob.glob(os.path.join(
Tobias Boschddd16492019-08-14 09:29:54 -07001163 output_dir + srcpath, 'sysroot_wrapper*.ccache')):
1164 # Can't update the wrapper in place to not affect the chroot,
1165 # but only the extracted toolchain.
1166 os.unlink(sysroot_wrapper)
1167 shutil.copy(sysroot_wrapper[:-6] + 'noccache', sysroot_wrapper)
Tobias Bosch7bc907a2019-10-09 11:52:40 -07001168 shutil.copy(sysroot_wrapper[:-6] + 'noccache.elf', sysroot_wrapper + '.elf')
Mike Frysinger35247af2012-11-16 18:58:06 -05001169
1170
Yunlian Jiang5ad6b512017-09-20 09:27:45 -07001171def _CreateMainLibDir(target, output_dir):
1172 """Create some lib dirs so that compiler can get the right Gcc paths"""
1173 osutils.SafeMakedirs(os.path.join(output_dir, 'usr', target, 'lib'))
1174 osutils.SafeMakedirs(os.path.join(output_dir, 'usr', target, 'usr/lib'))
1175
1176
Mike Frysinger35247af2012-11-16 18:58:06 -05001177def _ProcessDistroCleanups(target, output_dir):
1178 """Clean up the tree and remove all distro-specific requirements
1179
1180 Args:
1181 target: The toolchain target name
1182 output_dir: The output directory to clean up
1183 """
1184 _ProcessBinutilsConfig(target, output_dir)
1185 gcc_path = _ProcessGccConfig(target, output_dir)
Frank Henigman179ec7c2015-02-06 03:01:09 -05001186 _ProcessSysrootWrappers(target, output_dir, gcc_path)
Yunlian Jiang5ad6b512017-09-20 09:27:45 -07001187 _CreateMainLibDir(target, output_dir)
Mike Frysinger35247af2012-11-16 18:58:06 -05001188
1189 osutils.RmDir(os.path.join(output_dir, 'etc'))
1190
1191
1192def CreatePackagableRoot(target, output_dir, ldpaths, root='/'):
1193 """Setup a tree from the packages for the specified target
1194
1195 This populates a path with all the files from toolchain packages so that
1196 a tarball can easily be generated from the result.
1197
1198 Args:
1199 target: The target to create a packagable root from
1200 output_dir: The output directory to place all the files
1201 ldpaths: A dict of static ldpath information
1202 root: The root path to pull all packages/files from
1203 """
1204 # Find all the files owned by the packages for this target.
1205 paths, elfs = _GetFilesForTarget(target, root=root)
1206
1207 # Link in all the package's files, any ELF dependencies, and wrap any
1208 # executable ELFs with helper scripts.
1209 def MoveUsrBinToBin(path):
Han Shen699ea192016-03-02 10:42:47 -08001210 """Move /usr/bin to /bin so people can just use that toplevel dir
1211
George Burgess IVca1d7612020-10-01 00:38:32 -07001212 Note we do not apply this to clang or rust - there is correlation between
1213 clang's search path for libraries / inclusion and its installation path.
Han Shen699ea192016-03-02 10:42:47 -08001214 """
George Burgess IVca1d7612020-10-01 00:38:32 -07001215 if (path.startswith('/usr/bin/') and
1216 not any(x in path for x in ('clang', 'rust', 'cargo'))):
Han Shen699ea192016-03-02 10:42:47 -08001217 return path[4:]
1218 return path
Mike Frysinger35247af2012-11-16 18:58:06 -05001219 _BuildInitialPackageRoot(output_dir, paths, elfs, ldpaths,
1220 path_rewrite_func=MoveUsrBinToBin, root=root)
1221
1222 # The packages, when part of the normal distro, have helper scripts
1223 # that setup paths and such. Since we are making this standalone, we
1224 # need to preprocess all that ourselves.
1225 _ProcessDistroCleanups(target, output_dir)
1226
1227
1228def CreatePackages(targets_wanted, output_dir, root='/'):
1229 """Create redistributable cross-compiler packages for the specified targets
1230
1231 This creates toolchain packages that should be usable in conjunction with
1232 a downloaded sysroot (created elsewhere).
1233
1234 Tarballs (one per target) will be created in $PWD.
1235
1236 Args:
Don Garrett25f309a2014-03-19 14:02:12 -07001237 targets_wanted: The targets to package up.
1238 output_dir: The directory to put the packages in.
1239 root: The root path to pull all packages/files from.
Mike Frysinger35247af2012-11-16 18:58:06 -05001240 """
Ralph Nathan03047282015-03-23 11:09:32 -07001241 logging.info('Writing tarballs to %s', output_dir)
Mike Frysinger35247af2012-11-16 18:58:06 -05001242 osutils.SafeMakedirs(output_dir)
1243 ldpaths = lddtree.LoadLdpaths(root)
1244 targets = ExpandTargets(targets_wanted)
1245
Mike Frysinger221bd822017-09-29 02:51:47 -04001246 with osutils.TempDir(prefix='create-packages') as tempdir:
1247 logging.debug('Using tempdir: %s', tempdir)
1248
Mike Frysinger35247af2012-11-16 18:58:06 -05001249 # We have to split the root generation from the compression stages. This is
1250 # because we hardlink in all the files (to avoid overhead of reading/writing
1251 # the copies multiple times). But tar gets angry if a file's hardlink count
1252 # changes from when it starts reading a file to when it finishes.
1253 with parallel.BackgroundTaskRunner(CreatePackagableRoot) as queue:
1254 for target in targets:
1255 output_target_dir = os.path.join(tempdir, target)
1256 queue.put([target, output_target_dir, ldpaths, root])
1257
1258 # Build the tarball.
1259 with parallel.BackgroundTaskRunner(cros_build_lib.CreateTarball) as queue:
1260 for target in targets:
1261 tar_file = os.path.join(output_dir, target + '.tar.xz')
1262 queue.put([tar_file, os.path.join(tempdir, target)])
1263
1264
Mike Frysinger07534cf2017-09-12 17:40:21 -04001265def GetParser():
1266 """Return a command line parser."""
Mike Frysinger0c808452014-11-06 17:30:23 -05001267 parser = commandline.ArgumentParser(description=__doc__)
1268 parser.add_argument('-u', '--nousepkg',
1269 action='store_false', dest='usepkg', default=True,
1270 help='Use prebuilt packages if possible')
1271 parser.add_argument('-d', '--deleteold',
1272 action='store_true', dest='deleteold', default=False,
1273 help='Unmerge deprecated packages')
1274 parser.add_argument('-t', '--targets',
1275 dest='targets', default='sdk',
Mike Frysinger80de5012019-08-01 14:10:53 -04001276 help='Comma separated list of tuples. Special keywords '
Don Garrettc0c74002015-10-09 12:58:19 -07001277 "'host', 'sdk', 'boards', and 'all' are "
Gilad Arnold8195b532015-04-07 10:56:30 +03001278 "allowed. Defaults to 'sdk'.")
1279 parser.add_argument('--include-boards', default='', metavar='BOARDS',
1280 help='Comma separated list of boards whose toolchains we '
1281 'will always include. Default: none')
Mike Frysinger0c808452014-11-06 17:30:23 -05001282 parser.add_argument('--hostonly',
1283 dest='hostonly', default=False, action='store_true',
1284 help='Only setup the host toolchain. '
1285 'Useful for bootstrapping chroot')
Bertrand SIMONNETcae9d5f2015-03-09 15:58:01 -07001286 parser.add_argument('--show-board-cfg', '--show-cfg',
1287 dest='cfg_name', default=None,
Don Garrettc0c74002015-10-09 12:58:19 -07001288 help='Board to list toolchains tuples for')
Mike Frysinger91f52882017-09-13 00:16:58 -04001289 parser.add_argument('--show-packages', default=None,
1290 help='List all packages the specified target uses')
Mike Frysinger0c808452014-11-06 17:30:23 -05001291 parser.add_argument('--create-packages',
1292 action='store_true', default=False,
1293 help='Build redistributable packages')
1294 parser.add_argument('--output-dir', default=os.getcwd(), type='path',
1295 help='Output directory')
1296 parser.add_argument('--reconfig', default=False, action='store_true',
1297 help='Reload crossdev config and reselect toolchains')
Gilad Arnold2dab78c2015-05-21 14:43:33 -07001298 parser.add_argument('--sysroot', type='path',
1299 help='The sysroot in which to install the toolchains')
Mike Frysinger07534cf2017-09-12 17:40:21 -04001300 return parser
Zdenek Behan508dcce2011-12-05 15:39:32 +01001301
Mike Frysinger07534cf2017-09-12 17:40:21 -04001302
1303def main(argv):
1304 parser = GetParser()
Mike Frysinger0c808452014-11-06 17:30:23 -05001305 options = parser.parse_args(argv)
1306 options.Freeze()
Zdenek Behan508dcce2011-12-05 15:39:32 +01001307
Mike Frysinger35247af2012-11-16 18:58:06 -05001308 # Figure out what we're supposed to do and reject conflicting options.
Mike Frysinger91f52882017-09-13 00:16:58 -04001309 conflicting_options = (
1310 options.cfg_name,
1311 options.show_packages,
1312 options.create_packages,
1313 )
1314 if sum(bool(x) for x in conflicting_options) > 1:
1315 parser.error('conflicting options: create-packages & show-packages & '
1316 'show-board-cfg')
Mike Frysinger984d0622012-06-01 16:08:44 -04001317
Gilad Arnold8195b532015-04-07 10:56:30 +03001318 targets_wanted = set(options.targets.split(','))
1319 boards_wanted = (set(options.include_boards.split(','))
1320 if options.include_boards else set())
Mike Frysinger35247af2012-11-16 18:58:06 -05001321
Bertrand SIMONNETcae9d5f2015-03-09 15:58:01 -07001322 if options.cfg_name:
1323 ShowConfig(options.cfg_name)
Mike Frysinger91f52882017-09-13 00:16:58 -04001324 elif options.show_packages is not None:
1325 cros_build_lib.AssertInsideChroot()
1326 target = options.show_packages
1327 Crossdev.Load(False)
1328 for package in GetTargetPackages(target):
1329 print(GetPortagePackage(target, package))
Mike Frysinger35247af2012-11-16 18:58:06 -05001330 elif options.create_packages:
1331 cros_build_lib.AssertInsideChroot()
1332 Crossdev.Load(False)
Gilad Arnold8195b532015-04-07 10:56:30 +03001333 CreatePackages(targets_wanted, options.output_dir)
Mike Frysinger35247af2012-11-16 18:58:06 -05001334 else:
1335 cros_build_lib.AssertInsideChroot()
1336 # This has to be always run as root.
1337 if os.geteuid() != 0:
1338 cros_build_lib.Die('this script must be run as root')
1339
1340 Crossdev.Load(options.reconfig)
Gilad Arnold2dab78c2015-05-21 14:43:33 -07001341 root = options.sysroot or '/'
Mike Frysinger35247af2012-11-16 18:58:06 -05001342 UpdateToolchains(options.usepkg, options.deleteold, options.hostonly,
Gilad Arnold8195b532015-04-07 10:56:30 +03001343 options.reconfig, targets_wanted, boards_wanted,
Don Garrettc0c74002015-10-09 12:58:19 -07001344 root=root)
Mike Frysinger35247af2012-11-16 18:58:06 -05001345 Crossdev.Save()
1346
1347 return 0