blob: d83fb3048095ff07d2322bd60c2e3fd4626b3900 [file] [log] [blame]
Alex Klein8212d492018-08-27 07:47:23 -06001# Copyright 2018 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"""Installs cross toolchain libraries into a sysroot.
6
7This script is not meant to be used by developers directly.
8Run at your own risk.
9"""
10
Alex Klein8212d492018-08-27 07:47:23 -060011from chromite.lib import commandline
12from chromite.lib import cros_build_lib
13from chromite.lib import sysroot_lib
14from chromite.lib import toolchain
15
16
17def GetParser():
18 """Build the argument parser."""
19 parser = commandline.ArgumentParser(description=__doc__)
20
21 parser.add_argument('--noconfigure', dest='configure', default=True,
22 action='store_false',
23 help='Disable updating config files in <sysroot>/etc '
24 'after installation.')
25 parser.add_argument('--force', default=False, action='store_true',
26 help='Install toolchain even if already up to date.')
27 parser.add_argument('--toolchain',
28 help='Toolchain. For example: i686-pc-linux-gnu, '
29 'armv7a-softfloat-linux-gnueabi.')
30 parser.add_argument('--sysroot', type='path', required=True,
31 help='The sysroot to install the toolchain for.')
32
33 return parser
34
35
36def _GetToolchain(toolchain_name, sysroot):
37 """Get the toolchain value or exit with an error message."""
38 if toolchain_name:
39 # Use the CLI argument when provided.
40 return toolchain_name
41
42 # Fetch the value from the sysroot.
43 toolchain_name = sysroot.GetStandardField(sysroot_lib.STANDARD_FIELD_CHOST)
44 if not toolchain_name:
45 cros_build_lib.Die('No toolchain specified in the sysroot or command line.')
46
47 return toolchain_name
48
49
50def _ParseArgs(argv):
51 """Parse and validate arguments."""
52 parser = GetParser()
53 opts = parser.parse_args(argv)
54
55 # Expand the sysroot path to a sysroot object.
56 opts.sysroot = sysroot_lib.Sysroot(opts.sysroot)
57 # Make sure the toolchain value reflects the toolchain we will be using.
58 opts.toolchain = _GetToolchain(opts.toolchain, opts.sysroot)
59
60 opts.Freeze()
61 return opts
62
63
64def main(argv):
65 cros_build_lib.AssertInsideChroot()
66
67 opts = _ParseArgs(argv)
68 try:
69 toolchain.InstallToolchain(sysroot=opts.sysroot,
70 toolchain=opts.toolchain,
71 force=opts.force, configure=opts.configure)
72 except (toolchain.Error, cros_build_lib.RunCommandError, ValueError) as e:
Mike Frysinger6b5c3cd2019-08-27 16:51:00 -040073 cros_build_lib.Die(e)