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