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