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 | |
Bertrand SIMONNET | 7d66e23 | 2015-03-24 17:45:50 -0700 | [diff] [blame] | 13 | from chromite.lib import brick_lib |
Bertrand SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 14 | from chromite.lib import commandline |
| 15 | from chromite.lib import cros_build_lib |
Bertrand SIMONNET | 7d66e23 | 2015-03-24 17:45:50 -0700 | [diff] [blame] | 16 | from chromite.lib import osutils |
Bertrand SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 17 | from chromite.lib import sysroot_lib |
| 18 | |
| 19 | |
| 20 | def ParseArgs(argv): |
| 21 | """Parse arguments. |
| 22 | |
| 23 | Args: |
| 24 | argv: array of arguments passed to the script. |
| 25 | """ |
| 26 | parser = commandline.ArgumentParser(description=__doc__) |
| 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 | 41af265 | 2015-03-27 13:21:29 -0700 | [diff] [blame^] | 31 | wrapper.add_argument('--toolchain', help='Toolchain used in this sysroot.') |
Bertrand SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 32 | wrapper.set_defaults(command='create-wrappers') |
| 33 | |
Bertrand SIMONNET | 7d66e23 | 2015-03-24 17:45:50 -0700 | [diff] [blame] | 34 | config = subparser.add_parser('generate-config') |
| 35 | target = config.add_mutually_exclusive_group(required=True) |
| 36 | target.add_argument('--board', help='Board to generate the config for.') |
| 37 | target.add_argument('--brick', help='Brick to generate the config for.') |
| 38 | config.add_argument('--out-file', dest='out_file', |
| 39 | help='File to write into. If not specified, the ' |
| 40 | 'configuration will be printed to stdout.') |
| 41 | config.add_argument('--sysroot', help='Path to the sysroot.', required=True) |
| 42 | config.set_defaults(command='generate-config') |
| 43 | |
Bertrand SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 44 | options = parser.parse_args(argv) |
| 45 | options.Freeze() |
| 46 | return options |
| 47 | |
| 48 | |
| 49 | def main(argv): |
| 50 | opts = ParseArgs(argv) |
| 51 | if not cros_build_lib.IsInsideChroot(): |
| 52 | raise commandline.ChrootRequiredError() |
| 53 | |
| 54 | if os.geteuid() != 0: |
Bertrand SIMONNET | 7d66e23 | 2015-03-24 17:45:50 -0700 | [diff] [blame] | 55 | cros_build_lib.SudoRunCommand(sys.argv, print_cmd=False) |
Bertrand SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 56 | return |
| 57 | |
| 58 | if opts.command == 'create-wrappers': |
| 59 | sysroot_lib.CreateAllWrappers(opts.sysroot, opts.toolchain, |
| 60 | opts.friendlyname) |
Bertrand SIMONNET | 7d66e23 | 2015-03-24 17:45:50 -0700 | [diff] [blame] | 61 | elif opts.command == 'generate-config': |
| 62 | if opts.brick: |
| 63 | config = sysroot_lib.GenerateBrickConfig( |
| 64 | opts.sysroot, brick=brick_lib.Brick(opts.brick)) |
| 65 | else: |
| 66 | config = sysroot_lib.GenerateBoardConfig(opts.sysroot, board=opts.board) |
| 67 | |
| 68 | if opts.out_file: |
| 69 | osutils.WriteFile(opts.out_file, config, makedirs=True) |
| 70 | else: |
| 71 | print(config) |