Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2015 The ChromiumOS Authors |
Bertrand SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 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 SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 7 | import sys |
| 8 | |
| 9 | from chromite.lib import commandline |
| 10 | from chromite.lib import cros_build_lib |
Ram Chandrasekar | 6975128 | 2022-02-25 21:07:36 +0000 | [diff] [blame] | 11 | from chromite.lib import osutils |
Bertrand SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 12 | from chromite.lib import sysroot_lib |
| 13 | |
| 14 | |
| 15 | def ParseArgs(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 16 | """Parse arguments. |
Bertrand SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 17 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 18 | 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 SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 32 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 33 | 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 SIMONNET | 7d66e23 | 2015-03-24 17:45:50 -0700 | [diff] [blame] | 44 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 45 | 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 SIMONNET | ba36424 | 2015-03-30 15:02:44 -0700 | [diff] [blame] | 58 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 59 | 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 SIMONNET | ba36424 | 2015-03-30 15:02:44 -0700 | [diff] [blame] | 75 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 76 | options = parser.parse_args(argv) |
| 77 | options.Freeze() |
| 78 | return options |
Bertrand SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 79 | |
| 80 | |
| 81 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 82 | opts = ParseArgs(argv) |
| 83 | if not cros_build_lib.IsInsideChroot(): |
| 84 | raise commandline.ChrootRequiredError(argv) |
Bertrand SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 85 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 86 | if osutils.IsNonRootUser(): |
| 87 | cros_build_lib.sudo_run(sys.argv, print_cmd=False) |
| 88 | return |
Bertrand SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 89 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 90 | # 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: |
Mike Frysinger | 31fdddd | 2023-02-24 15:50:55 -0500 | [diff] [blame] | 95 | output = open(opts.out_file, "w", encoding="utf-8") |
Bertrand SIMONNET | ba36424 | 2015-03-30 15:02:44 -0700 | [diff] [blame] | 96 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 97 | 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)) |