Bertrand SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 1 | # 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 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): |
| 16 | """Parse arguments. |
| 17 | |
| 18 | Args: |
| 19 | argv: array of arguments passed to the script. |
| 20 | """ |
| 21 | parser = commandline.ArgumentParser(description=__doc__) |
Bertrand SIMONNET | ba36424 | 2015-03-30 15:02:44 -0700 | [diff] [blame] | 22 | parser.set_defaults(out_file=None) |
Bertrand SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 23 | 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 SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 27 | wrapper.set_defaults(command='create-wrappers') |
| 28 | |
Bertrand SIMONNET | 7d66e23 | 2015-03-24 17:45:50 -0700 | [diff] [blame] | 29 | 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 SIMONNET | 7d66e23 | 2015-03-24 17:45:50 -0700 | [diff] [blame] | 32 | 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 SIMONNET | ba36424 | 2015-03-30 15:02:44 -0700 | [diff] [blame] | 38 | 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 SIMONNET | ba36424 | 2015-03-30 15:02:44 -0700 | [diff] [blame] | 54 | 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 SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 58 | options = parser.parse_args(argv) |
| 59 | options.Freeze() |
| 60 | return options |
| 61 | |
| 62 | |
| 63 | def main(argv): |
| 64 | opts = ParseArgs(argv) |
| 65 | if not cros_build_lib.IsInsideChroot(): |
Mike Frysinger | a620b8e | 2018-07-14 00:59:58 -0400 | [diff] [blame] | 66 | raise commandline.ChrootRequiredError(argv) |
Bertrand SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 67 | |
Ram Chandrasekar | 6975128 | 2022-02-25 21:07:36 +0000 | [diff] [blame^] | 68 | if osutils.IsNonRootUser(): |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 69 | cros_build_lib.sudo_run(sys.argv, print_cmd=False) |
Bertrand SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 70 | return |
| 71 | |
Bertrand SIMONNET | ba36424 | 2015-03-30 15:02:44 -0700 | [diff] [blame] | 72 | output = sys.stdout |
| 73 | if opts.out_file: |
| 74 | output = open(opts.out_file, 'w') |
| 75 | |
Bertrand SIMONNET | e2cec3f | 2015-04-06 16:12:54 -0700 | [diff] [blame] | 76 | sysroot = sysroot_lib.Sysroot(opts.sysroot) |
Bertrand SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 77 | if opts.command == 'create-wrappers': |
Bertrand SIMONNET | e2cec3f | 2015-04-06 16:12:54 -0700 | [diff] [blame] | 78 | sysroot.CreateAllWrappers(opts.friendlyname) |
Bertrand SIMONNET | 7d66e23 | 2015-03-24 17:45:50 -0700 | [diff] [blame] | 79 | elif opts.command == 'generate-config': |
Alex Klein | 3458108 | 2018-12-03 12:56:53 -0700 | [diff] [blame] | 80 | output.write('\n' + sysroot.GenerateBoardSetupConfig(opts.board)) |
Bertrand SIMONNET | ba36424 | 2015-03-30 15:02:44 -0700 | [diff] [blame] | 81 | elif opts.command == 'generate-make-conf': |
Alex Klein | 3458108 | 2018-12-03 12:56:53 -0700 | [diff] [blame] | 82 | output.write('\n' + sysroot.GenerateBoardMakeConf(opts.accepted_licenses)) |
Bertrand SIMONNET | ba36424 | 2015-03-30 15:02:44 -0700 | [diff] [blame] | 83 | elif opts.command == 'generate-binhosts': |
Don Garrett | e7b2aee | 2018-09-14 17:13:53 -0700 | [diff] [blame] | 84 | output.write('\n' + sysroot.GenerateBinhostConf(opts.local_only)) |