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