blob: 9eb589b24c24eef84eec61a877c17a81d6c788bf [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:
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
Alex Klein68b270c2023-04-14 14:42:50 -060091 # not concerned with leaking it, and `with` is awkward to use with
92 # sys.stdout.
Alex Klein1699fab2022-09-08 08:46:06 -060093 # pylint: disable=consider-using-with
94 output = sys.stdout
95 if opts.out_file:
Mike Frysinger31fdddd2023-02-24 15:50:55 -050096 output = open(opts.out_file, "w", encoding="utf-8")
Bertrand SIMONNETba364242015-03-30 15:02:44 -070097
Alex Klein1699fab2022-09-08 08:46:06 -060098 sysroot = sysroot_lib.Sysroot(opts.sysroot)
99 if opts.command == "create-wrappers":
100 sysroot.CreateAllWrappers(opts.friendlyname)
101 elif opts.command == "generate-config":
102 output.write("\n" + sysroot.GenerateBoardSetupConfig(opts.board))
103 elif opts.command == "generate-make-conf":
104 output.write(
105 "\n" + sysroot.GenerateBoardMakeConf(opts.accepted_licenses)
106 )
107 elif opts.command == "generate-binhosts":
108 output.write("\n" + sysroot.GenerateBinhostConf(opts.local_only))