blob: d43af013cc1b7cf8444c16fc96c145e8cbdba73d [file] [log] [blame]
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -07001# Copyright 2015 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"""Collection of tools to create sysroots."""
6
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -07007import sys
8
9from chromite.lib import commandline
10from chromite.lib import cros_build_lib
Ram Chandrasekar69751282022-02-25 21:07:36 +000011from chromite.lib import osutils
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -070012from chromite.lib import sysroot_lib
13
14
15def ParseArgs(argv):
16 """Parse arguments.
17
18 Args:
19 argv: array of arguments passed to the script.
20 """
21 parser = commandline.ArgumentParser(description=__doc__)
Bertrand SIMONNETba364242015-03-30 15:02:44 -070022 parser.set_defaults(out_file=None)
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -070023 subparser = parser.add_subparsers()
24 wrapper = subparser.add_parser('create-wrappers')
25 wrapper.add_argument('--sysroot', help='Path to the sysroot.', required=True)
26 wrapper.add_argument('--friendlyname', help='Name to append to the commands.')
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -070027 wrapper.set_defaults(command='create-wrappers')
28
Bertrand SIMONNET7d66e232015-03-24 17:45:50 -070029 config = subparser.add_parser('generate-config')
30 target = config.add_mutually_exclusive_group(required=True)
31 target.add_argument('--board', help='Board to generate the config for.')
Bertrand SIMONNET7d66e232015-03-24 17:45:50 -070032 config.add_argument('--out-file', dest='out_file',
33 help='File to write into. If not specified, the '
34 'configuration will be printed to stdout.')
35 config.add_argument('--sysroot', help='Path to the sysroot.', required=True)
36 config.set_defaults(command='generate-config')
37
Bertrand SIMONNETba364242015-03-30 15:02:44 -070038 makeconf = subparser.add_parser('generate-make-conf')
39 makeconf.add_argument('--sysroot', help='Sysroot to use.')
40 makeconf.add_argument('--out-file', dest='out_file',
41 help='File to write the configuration into. If not '
42 'specified, the configuration will be printed to '
43 'stdout.')
44 makeconf.add_argument('--accepted-licenses',
45 help='List of accepted licenses.')
46 makeconf.set_defaults(command='generate-make-conf')
47
48 binhost = subparser.add_parser('generate-binhosts')
49 binhost.add_argument('--sysroot', help='Sysroot to use.')
50 binhost.add_argument('--out-file', dest='out_file',
51 help='File to write the configuration into. If not '
52 'specified, the configuration will be printed to '
53 'stdout.')
Bertrand SIMONNETba364242015-03-30 15:02:44 -070054 binhost.add_argument('--local-only', dest='local_only', action='store_true',
55 help='Use compatible local boards only.')
56 binhost.set_defaults(command='generate-binhosts')
57
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -070058 options = parser.parse_args(argv)
59 options.Freeze()
60 return options
61
62
63def main(argv):
64 opts = ParseArgs(argv)
65 if not cros_build_lib.IsInsideChroot():
Mike Frysingera620b8e2018-07-14 00:59:58 -040066 raise commandline.ChrootRequiredError(argv)
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -070067
Ram Chandrasekar69751282022-02-25 21:07:36 +000068 if osutils.IsNonRootUser():
Mike Frysinger45602c72019-09-22 02:15:11 -040069 cros_build_lib.sudo_run(sys.argv, print_cmd=False)
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -070070 return
71
Bertrand SIMONNETba364242015-03-30 15:02:44 -070072 output = sys.stdout
73 if opts.out_file:
74 output = open(opts.out_file, 'w')
75
Bertrand SIMONNETe2cec3f2015-04-06 16:12:54 -070076 sysroot = sysroot_lib.Sysroot(opts.sysroot)
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -070077 if opts.command == 'create-wrappers':
Bertrand SIMONNETe2cec3f2015-04-06 16:12:54 -070078 sysroot.CreateAllWrappers(opts.friendlyname)
Bertrand SIMONNET7d66e232015-03-24 17:45:50 -070079 elif opts.command == 'generate-config':
Alex Klein34581082018-12-03 12:56:53 -070080 output.write('\n' + sysroot.GenerateBoardSetupConfig(opts.board))
Bertrand SIMONNETba364242015-03-30 15:02:44 -070081 elif opts.command == 'generate-make-conf':
Alex Klein34581082018-12-03 12:56:53 -070082 output.write('\n' + sysroot.GenerateBoardMakeConf(opts.accepted_licenses))
Bertrand SIMONNETba364242015-03-30 15:02:44 -070083 elif opts.command == 'generate-binhosts':
Don Garrette7b2aee2018-09-14 17:13:53 -070084 output.write('\n' + sysroot.GenerateBinhostConf(opts.local_only))