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 | |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import os |
| 11 | import sys |
| 12 | |
| 13 | from chromite.lib import commandline |
| 14 | from chromite.lib import cros_build_lib |
| 15 | from chromite.lib import sysroot_lib |
| 16 | |
| 17 | |
| 18 | def ParseArgs(argv): |
| 19 | """Parse arguments. |
| 20 | |
| 21 | Args: |
| 22 | argv: array of arguments passed to the script. |
| 23 | """ |
| 24 | parser = commandline.ArgumentParser(description=__doc__) |
Bertrand SIMONNET | ba36424 | 2015-03-30 15:02:44 -0700 | [diff] [blame] | 25 | parser.set_defaults(out_file=None) |
Bertrand SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 26 | subparser = parser.add_subparsers() |
| 27 | wrapper = subparser.add_parser('create-wrappers') |
| 28 | wrapper.add_argument('--sysroot', help='Path to the sysroot.', required=True) |
| 29 | wrapper.add_argument('--friendlyname', help='Name to append to the commands.') |
Bertrand SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 30 | wrapper.set_defaults(command='create-wrappers') |
| 31 | |
Bertrand SIMONNET | 7d66e23 | 2015-03-24 17:45:50 -0700 | [diff] [blame] | 32 | config = subparser.add_parser('generate-config') |
| 33 | target = config.add_mutually_exclusive_group(required=True) |
| 34 | target.add_argument('--board', help='Board to generate the config for.') |
Bertrand SIMONNET | 7d66e23 | 2015-03-24 17:45:50 -0700 | [diff] [blame] | 35 | config.add_argument('--out-file', dest='out_file', |
| 36 | help='File to write into. If not specified, the ' |
| 37 | 'configuration will be printed to stdout.') |
| 38 | config.add_argument('--sysroot', help='Path to the sysroot.', required=True) |
| 39 | config.set_defaults(command='generate-config') |
| 40 | |
Bertrand SIMONNET | ba36424 | 2015-03-30 15:02:44 -0700 | [diff] [blame] | 41 | makeconf = subparser.add_parser('generate-make-conf') |
| 42 | makeconf.add_argument('--sysroot', help='Sysroot to use.') |
| 43 | makeconf.add_argument('--out-file', dest='out_file', |
| 44 | help='File to write the configuration into. If not ' |
| 45 | 'specified, the configuration will be printed to ' |
| 46 | 'stdout.') |
| 47 | makeconf.add_argument('--accepted-licenses', |
| 48 | help='List of accepted licenses.') |
| 49 | makeconf.set_defaults(command='generate-make-conf') |
| 50 | |
| 51 | binhost = subparser.add_parser('generate-binhosts') |
| 52 | binhost.add_argument('--sysroot', help='Sysroot to use.') |
| 53 | binhost.add_argument('--out-file', dest='out_file', |
| 54 | help='File to write the configuration into. If not ' |
| 55 | 'specified, the configuration will be printed to ' |
| 56 | 'stdout.') |
| 57 | binhost.add_argument('--chrome-only', dest='chrome_only', action='store_true', |
| 58 | help='Generate only the chrome binhost.') |
| 59 | binhost.add_argument('--local-only', dest='local_only', action='store_true', |
| 60 | help='Use compatible local boards only.') |
| 61 | binhost.set_defaults(command='generate-binhosts') |
| 62 | |
Bertrand SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 63 | options = parser.parse_args(argv) |
| 64 | options.Freeze() |
| 65 | return options |
| 66 | |
| 67 | |
| 68 | def main(argv): |
| 69 | opts = ParseArgs(argv) |
| 70 | if not cros_build_lib.IsInsideChroot(): |
| 71 | raise commandline.ChrootRequiredError() |
| 72 | |
| 73 | if os.geteuid() != 0: |
Bertrand SIMONNET | 7d66e23 | 2015-03-24 17:45:50 -0700 | [diff] [blame] | 74 | cros_build_lib.SudoRunCommand(sys.argv, print_cmd=False) |
Bertrand SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 75 | return |
| 76 | |
Bertrand SIMONNET | ba36424 | 2015-03-30 15:02:44 -0700 | [diff] [blame] | 77 | output = sys.stdout |
| 78 | if opts.out_file: |
| 79 | output = open(opts.out_file, 'w') |
| 80 | |
Bertrand SIMONNET | e2cec3f | 2015-04-06 16:12:54 -0700 | [diff] [blame] | 81 | sysroot = sysroot_lib.Sysroot(opts.sysroot) |
Bertrand SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 82 | if opts.command == 'create-wrappers': |
Bertrand SIMONNET | e2cec3f | 2015-04-06 16:12:54 -0700 | [diff] [blame] | 83 | sysroot.CreateAllWrappers(opts.friendlyname) |
Bertrand SIMONNET | 7d66e23 | 2015-03-24 17:45:50 -0700 | [diff] [blame] | 84 | elif opts.command == 'generate-config': |
Don Garrett | c0c7400 | 2015-10-09 12:58:19 -0700 | [diff] [blame] | 85 | config = sysroot.GenerateBoardConfig(opts.board) |
Bertrand SIMONNET | 7d66e23 | 2015-03-24 17:45:50 -0700 | [diff] [blame] | 86 | |
Bertrand SIMONNET | ba36424 | 2015-03-30 15:02:44 -0700 | [diff] [blame] | 87 | output.write('\n' + config) |
| 88 | elif opts.command == 'generate-make-conf': |
Bertrand SIMONNET | e2cec3f | 2015-04-06 16:12:54 -0700 | [diff] [blame] | 89 | output.write('\n' + sysroot.GenerateMakeConf(opts.accepted_licenses)) |
Bertrand SIMONNET | ba36424 | 2015-03-30 15:02:44 -0700 | [diff] [blame] | 90 | elif opts.command == 'generate-binhosts': |
Bertrand SIMONNET | e2cec3f | 2015-04-06 16:12:54 -0700 | [diff] [blame] | 91 | output.write('\n' + sysroot.GenerateBinhostConf(opts.chrome_only, |
| 92 | opts.local_only)) |