blob: 0e0c7e1ae85e9af6a09badbecc2e5fdc7af8344b [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():
Alex Klein1699fab2022-09-08 08:46:06 -060018 """Build the argument parser."""
19 parser = commandline.ArgumentParser(description=__doc__)
Alex Klein8212d492018-08-27 07:47:23 -060020
Alex Klein1699fab2022-09-08 08:46:06 -060021 parser.add_argument(
22 "--noconfigure",
23 dest="configure",
24 default=True,
25 action="store_false",
26 help="Disable updating config files in <sysroot>/etc "
27 "after installation.",
28 )
29 parser.add_argument(
30 "--force",
31 default=False,
32 action="store_true",
33 help="Install toolchain even if already up to date.",
34 )
35 parser.add_argument(
36 "--toolchain",
37 help="Toolchain. For example: i686-pc-linux-gnu, "
38 "armv7a-softfloat-linux-gnueabi.",
39 )
40 parser.add_argument(
41 "--sysroot",
42 type="path",
43 required=True,
44 help="The sysroot to install the toolchain for.",
45 )
Alex Klein8212d492018-08-27 07:47:23 -060046
Alex Klein1699fab2022-09-08 08:46:06 -060047 return parser
Alex Klein8212d492018-08-27 07:47:23 -060048
49
50def _GetToolchain(toolchain_name, sysroot):
Alex Klein1699fab2022-09-08 08:46:06 -060051 """Get the toolchain value or exit with an error message."""
52 if toolchain_name:
53 # Use the CLI argument when provided.
54 return toolchain_name
55
56 # Fetch the value from the sysroot.
57 toolchain_name = sysroot.GetStandardField(sysroot_lib.STANDARD_FIELD_CHOST)
58 if not toolchain_name:
59 cros_build_lib.Die(
60 "No toolchain specified in the sysroot or command line."
61 )
62
Alex Klein8212d492018-08-27 07:47:23 -060063 return toolchain_name
64
Alex Klein8212d492018-08-27 07:47:23 -060065
66def _ParseArgs(argv):
Alex Klein1699fab2022-09-08 08:46:06 -060067 """Parse and validate arguments."""
68 parser = GetParser()
69 opts = parser.parse_args(argv)
Alex Klein8212d492018-08-27 07:47:23 -060070
Alex Klein1699fab2022-09-08 08:46:06 -060071 # Expand the sysroot path to a sysroot object.
72 opts.sysroot = sysroot_lib.Sysroot(opts.sysroot)
73 # Make sure the toolchain value reflects the toolchain we will be using.
74 opts.toolchain = _GetToolchain(opts.toolchain, opts.sysroot)
Alex Klein8212d492018-08-27 07:47:23 -060075
Alex Klein1699fab2022-09-08 08:46:06 -060076 opts.Freeze()
77 return opts
Alex Klein8212d492018-08-27 07:47:23 -060078
79
80def main(argv):
Alex Klein1699fab2022-09-08 08:46:06 -060081 cros_build_lib.AssertInsideChroot()
Alex Klein8212d492018-08-27 07:47:23 -060082
Alex Klein1699fab2022-09-08 08:46:06 -060083 opts = _ParseArgs(argv)
84 try:
85 toolchain.InstallToolchain(
86 sysroot=opts.sysroot,
87 toolchain=opts.toolchain,
88 force=opts.force,
89 configure=opts.configure,
90 )
91 except (toolchain.Error, cros_build_lib.RunCommandError, ValueError) as e:
92 cros_build_lib.Die(e)