blob: 604d2f6aec846e4d1cdd955428d22a0daa1a8ce3 [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):
Alex Klein1699fab2022-09-08 08:46:06 -060016 """Parse arguments.
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -070017
Alex Klein1699fab2022-09-08 08:46:06 -060018 Args:
19 argv: array of arguments passed to the script.
20 """
21 parser = commandline.ArgumentParser(description=__doc__)
22 parser.set_defaults(out_file=None)
23 subparser = parser.add_subparsers()
24 wrapper = subparser.add_parser("create-wrappers")
25 wrapper.add_argument(
26 "--sysroot", help="Path to the sysroot.", required=True
27 )
28 wrapper.add_argument(
29 "--friendlyname", help="Name to append to the commands."
30 )
31 wrapper.set_defaults(command="create-wrappers")
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -070032
Alex Klein1699fab2022-09-08 08:46:06 -060033 config = subparser.add_parser("generate-config")
34 target = config.add_mutually_exclusive_group(required=True)
35 target.add_argument("--board", help="Board to generate the config for.")
36 config.add_argument(
37 "--out-file",
38 dest="out_file",
39 help="File to write into. If not specified, the "
40 "configuration will be printed to stdout.",
41 )
42 config.add_argument("--sysroot", help="Path to the sysroot.", required=True)
43 config.set_defaults(command="generate-config")
Bertrand SIMONNET7d66e232015-03-24 17:45:50 -070044
Alex Klein1699fab2022-09-08 08:46:06 -060045 makeconf = subparser.add_parser("generate-make-conf")
46 makeconf.add_argument("--sysroot", help="Sysroot to use.")
47 makeconf.add_argument(
48 "--out-file",
49 dest="out_file",
50 help="File to write the configuration into. If not "
51 "specified, the configuration will be printed to "
52 "stdout.",
53 )
54 makeconf.add_argument(
55 "--accepted-licenses", help="List of accepted licenses."
56 )
57 makeconf.set_defaults(command="generate-make-conf")
Bertrand SIMONNETba364242015-03-30 15:02:44 -070058
Alex Klein1699fab2022-09-08 08:46:06 -060059 binhost = subparser.add_parser("generate-binhosts")
60 binhost.add_argument("--sysroot", help="Sysroot to use.")
61 binhost.add_argument(
62 "--out-file",
63 dest="out_file",
64 help="File to write the configuration into. If not "
65 "specified, the configuration will be printed to "
66 "stdout.",
67 )
68 binhost.add_argument(
69 "--local-only",
70 dest="local_only",
71 action="store_true",
72 help="Use compatible local boards only.",
73 )
74 binhost.set_defaults(command="generate-binhosts")
Bertrand SIMONNETba364242015-03-30 15:02:44 -070075
Alex Klein1699fab2022-09-08 08:46:06 -060076 options = parser.parse_args(argv)
77 options.Freeze()
78 return options
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -070079
80
81def main(argv):
Alex Klein1699fab2022-09-08 08:46:06 -060082 opts = ParseArgs(argv)
83 if not cros_build_lib.IsInsideChroot():
84 raise commandline.ChrootRequiredError(argv)
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -070085
Alex Klein1699fab2022-09-08 08:46:06 -060086 if osutils.IsNonRootUser():
87 cros_build_lib.sudo_run(sys.argv, print_cmd=False)
88 return
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -070089
Alex Klein1699fab2022-09-08 08:46:06 -060090 # This short script is using |output| for the whole duration, thus we are
91 # not concerned with leaking it, and `with` is awkward to use with sys.stdout.
92 # pylint: disable=consider-using-with
93 output = sys.stdout
94 if opts.out_file:
95 output = open(opts.out_file, "w")
Bertrand SIMONNETba364242015-03-30 15:02:44 -070096
Alex Klein1699fab2022-09-08 08:46:06 -060097 sysroot = sysroot_lib.Sysroot(opts.sysroot)
98 if opts.command == "create-wrappers":
99 sysroot.CreateAllWrappers(opts.friendlyname)
100 elif opts.command == "generate-config":
101 output.write("\n" + sysroot.GenerateBoardSetupConfig(opts.board))
102 elif opts.command == "generate-make-conf":
103 output.write(
104 "\n" + sysroot.GenerateBoardMakeConf(opts.accepted_licenses)
105 )
106 elif opts.command == "generate-binhosts":
107 output.write("\n" + sysroot.GenerateBinhostConf(opts.local_only))