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 |
| 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 | 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 | ba36424 | 2015-03-30 15:02:44 -0700 | [diff] [blame^] | 44 | makeconf = subparser.add_parser('generate-make-conf') |
| 45 | makeconf.add_argument('--sysroot', help='Sysroot to use.') |
| 46 | makeconf.add_argument('--out-file', dest='out_file', |
| 47 | help='File to write the configuration into. If not ' |
| 48 | 'specified, the configuration will be printed to ' |
| 49 | 'stdout.') |
| 50 | makeconf.add_argument('--accepted-licenses', |
| 51 | help='List of accepted licenses.') |
| 52 | makeconf.set_defaults(command='generate-make-conf') |
| 53 | |
| 54 | binhost = subparser.add_parser('generate-binhosts') |
| 55 | binhost.add_argument('--sysroot', help='Sysroot to use.') |
| 56 | binhost.add_argument('--out-file', dest='out_file', |
| 57 | help='File to write the configuration into. If not ' |
| 58 | 'specified, the configuration will be printed to ' |
| 59 | 'stdout.') |
| 60 | binhost.add_argument('--chrome-only', dest='chrome_only', action='store_true', |
| 61 | help='Generate only the chrome binhost.') |
| 62 | binhost.add_argument('--local-only', dest='local_only', action='store_true', |
| 63 | help='Use compatible local boards only.') |
| 64 | binhost.set_defaults(command='generate-binhosts') |
| 65 | |
Bertrand SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 66 | options = parser.parse_args(argv) |
| 67 | options.Freeze() |
| 68 | return options |
| 69 | |
| 70 | |
| 71 | def main(argv): |
| 72 | opts = ParseArgs(argv) |
| 73 | if not cros_build_lib.IsInsideChroot(): |
| 74 | raise commandline.ChrootRequiredError() |
| 75 | |
| 76 | if os.geteuid() != 0: |
Bertrand SIMONNET | 7d66e23 | 2015-03-24 17:45:50 -0700 | [diff] [blame] | 77 | cros_build_lib.SudoRunCommand(sys.argv, print_cmd=False) |
Bertrand SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 78 | return |
| 79 | |
Bertrand SIMONNET | ba36424 | 2015-03-30 15:02:44 -0700 | [diff] [blame^] | 80 | output = sys.stdout |
| 81 | if opts.out_file: |
| 82 | output = open(opts.out_file, 'w') |
| 83 | |
Bertrand SIMONNET | 9562e2c | 2015-03-13 13:57:43 -0700 | [diff] [blame] | 84 | if opts.command == 'create-wrappers': |
| 85 | sysroot_lib.CreateAllWrappers(opts.sysroot, opts.toolchain, |
| 86 | opts.friendlyname) |
Bertrand SIMONNET | 7d66e23 | 2015-03-24 17:45:50 -0700 | [diff] [blame] | 87 | elif opts.command == 'generate-config': |
| 88 | if opts.brick: |
| 89 | config = sysroot_lib.GenerateBrickConfig( |
| 90 | opts.sysroot, brick=brick_lib.Brick(opts.brick)) |
| 91 | else: |
| 92 | config = sysroot_lib.GenerateBoardConfig(opts.sysroot, board=opts.board) |
| 93 | |
Bertrand SIMONNET | ba36424 | 2015-03-30 15:02:44 -0700 | [diff] [blame^] | 94 | output.write('\n' + config) |
| 95 | elif opts.command == 'generate-make-conf': |
| 96 | output.write('\n' + sysroot_lib.GenerateMakeConf(opts.sysroot, |
| 97 | opts.accepted_licenses)) |
| 98 | elif opts.command == 'generate-binhosts': |
| 99 | output.write('\n' + sysroot_lib.GenerateBinhostConf(opts.sysroot, |
| 100 | opts.chrome_only, |
| 101 | opts.local_only)) |