blob: effbfcce11361ec708b0e96feb361f68ad011c11 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2015 The ChromiumOS Authors
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -07002# 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:
Trent Apted66736d82023-05-25 10:38:28 +100019 argv: array of arguments passed to the script.
Alex Klein1699fab2022-09-08 08:46:06 -060020 """
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",
Trent Apted66736d82023-05-25 10:38:28 +100039 help=(
40 "File to write into. If not specified, the "
41 "configuration will be printed to stdout."
42 ),
Alex Klein1699fab2022-09-08 08:46:06 -060043 )
44 config.add_argument("--sysroot", help="Path to the sysroot.", required=True)
45 config.set_defaults(command="generate-config")
Bertrand SIMONNET7d66e232015-03-24 17:45:50 -070046
Alex Klein1699fab2022-09-08 08:46:06 -060047 makeconf = subparser.add_parser("generate-make-conf")
48 makeconf.add_argument("--sysroot", help="Sysroot to use.")
49 makeconf.add_argument(
50 "--out-file",
51 dest="out_file",
Trent Apted66736d82023-05-25 10:38:28 +100052 help=(
53 "File to write the configuration into. If not "
54 "specified, the configuration will be printed to "
55 "stdout."
56 ),
Alex Klein1699fab2022-09-08 08:46:06 -060057 )
58 makeconf.add_argument(
59 "--accepted-licenses", help="List of accepted licenses."
60 )
61 makeconf.set_defaults(command="generate-make-conf")
Bertrand SIMONNETba364242015-03-30 15:02:44 -070062
Alex Klein1699fab2022-09-08 08:46:06 -060063 binhost = subparser.add_parser("generate-binhosts")
64 binhost.add_argument("--sysroot", help="Sysroot to use.")
65 binhost.add_argument(
66 "--out-file",
67 dest="out_file",
Trent Apted66736d82023-05-25 10:38:28 +100068 help=(
69 "File to write the configuration into. If not "
70 "specified, the configuration will be printed to "
71 "stdout."
72 ),
Alex Klein1699fab2022-09-08 08:46:06 -060073 )
74 binhost.add_argument(
75 "--local-only",
76 dest="local_only",
77 action="store_true",
78 help="Use compatible local boards only.",
79 )
80 binhost.set_defaults(command="generate-binhosts")
Bertrand SIMONNETba364242015-03-30 15:02:44 -070081
Alex Klein1699fab2022-09-08 08:46:06 -060082 options = parser.parse_args(argv)
83 options.Freeze()
84 return options
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -070085
86
87def main(argv):
Alex Klein1699fab2022-09-08 08:46:06 -060088 opts = ParseArgs(argv)
89 if not cros_build_lib.IsInsideChroot():
90 raise commandline.ChrootRequiredError(argv)
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -070091
Alex Klein1699fab2022-09-08 08:46:06 -060092 if osutils.IsNonRootUser():
93 cros_build_lib.sudo_run(sys.argv, print_cmd=False)
94 return
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -070095
Alex Klein1699fab2022-09-08 08:46:06 -060096 # This short script is using |output| for the whole duration, thus we are
Alex Klein68b270c2023-04-14 14:42:50 -060097 # not concerned with leaking it, and `with` is awkward to use with
98 # sys.stdout.
Alex Klein1699fab2022-09-08 08:46:06 -060099 # pylint: disable=consider-using-with
100 output = sys.stdout
101 if opts.out_file:
Mike Frysinger31fdddd2023-02-24 15:50:55 -0500102 output = open(opts.out_file, "w", encoding="utf-8")
Bertrand SIMONNETba364242015-03-30 15:02:44 -0700103
Alex Klein1699fab2022-09-08 08:46:06 -0600104 sysroot = sysroot_lib.Sysroot(opts.sysroot)
105 if opts.command == "create-wrappers":
106 sysroot.CreateAllWrappers(opts.friendlyname)
107 elif opts.command == "generate-config":
108 output.write("\n" + sysroot.GenerateBoardSetupConfig(opts.board))
109 elif opts.command == "generate-make-conf":
110 output.write(
111 "\n" + sysroot.GenerateBoardMakeConf(opts.accepted_licenses)
112 )
113 elif opts.command == "generate-binhosts":
114 output.write("\n" + sysroot.GenerateBinhostConf(opts.local_only))