Alex Klein | 8212d49 | 2018-08-27 07:47:23 -0600 | [diff] [blame] | 1 | # 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 | |
| 7 | This script is not meant to be used by developers directly. |
| 8 | Run at your own risk. |
| 9 | """ |
| 10 | |
Alex Klein | 8212d49 | 2018-08-27 07:47:23 -0600 | [diff] [blame] | 11 | from chromite.lib import commandline |
| 12 | from chromite.lib import cros_build_lib |
| 13 | from chromite.lib import sysroot_lib |
| 14 | from chromite.lib import toolchain |
| 15 | |
| 16 | |
| 17 | def GetParser(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 18 | """Build the argument parser.""" |
| 19 | parser = commandline.ArgumentParser(description=__doc__) |
Alex Klein | 8212d49 | 2018-08-27 07:47:23 -0600 | [diff] [blame] | 20 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 21 | 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 Klein | 8212d49 | 2018-08-27 07:47:23 -0600 | [diff] [blame] | 46 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 47 | return parser |
Alex Klein | 8212d49 | 2018-08-27 07:47:23 -0600 | [diff] [blame] | 48 | |
| 49 | |
| 50 | def _GetToolchain(toolchain_name, sysroot): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 51 | """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 Klein | 8212d49 | 2018-08-27 07:47:23 -0600 | [diff] [blame] | 63 | return toolchain_name |
| 64 | |
Alex Klein | 8212d49 | 2018-08-27 07:47:23 -0600 | [diff] [blame] | 65 | |
| 66 | def _ParseArgs(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 67 | """Parse and validate arguments.""" |
| 68 | parser = GetParser() |
| 69 | opts = parser.parse_args(argv) |
Alex Klein | 8212d49 | 2018-08-27 07:47:23 -0600 | [diff] [blame] | 70 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 71 | # 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 Klein | 8212d49 | 2018-08-27 07:47:23 -0600 | [diff] [blame] | 75 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 76 | opts.Freeze() |
| 77 | return opts |
Alex Klein | 8212d49 | 2018-08-27 07:47:23 -0600 | [diff] [blame] | 78 | |
| 79 | |
| 80 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 81 | cros_build_lib.AssertInsideChroot() |
Alex Klein | 8212d49 | 2018-08-27 07:47:23 -0600 | [diff] [blame] | 82 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 83 | 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) |