blob: 96780c15f697841871a89c70aff944606fd9385c [file] [log] [blame]
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -07001# 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
8from __future__ import print_function
9
10import os
11import sys
12
Bertrand SIMONNET7d66e232015-03-24 17:45:50 -070013from chromite.lib import brick_lib
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -070014from chromite.lib import commandline
15from chromite.lib import cros_build_lib
Bertrand SIMONNET7d66e232015-03-24 17:45:50 -070016from chromite.lib import osutils
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -070017from chromite.lib import sysroot_lib
18
19
20def 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.')
31 wrapper.add_argument('--toolchain', help='Toolchain used in this sysroot.',
32 required=True)
33 wrapper.set_defaults(command='create-wrappers')
34
Bertrand SIMONNET7d66e232015-03-24 17:45:50 -070035 config = subparser.add_parser('generate-config')
36 target = config.add_mutually_exclusive_group(required=True)
37 target.add_argument('--board', help='Board to generate the config for.')
38 target.add_argument('--brick', help='Brick to generate the config for.')
39 config.add_argument('--out-file', dest='out_file',
40 help='File to write into. If not specified, the '
41 'configuration will be printed to stdout.')
42 config.add_argument('--sysroot', help='Path to the sysroot.', required=True)
43 config.set_defaults(command='generate-config')
44
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -070045 options = parser.parse_args(argv)
46 options.Freeze()
47 return options
48
49
50def main(argv):
51 opts = ParseArgs(argv)
52 if not cros_build_lib.IsInsideChroot():
53 raise commandline.ChrootRequiredError()
54
55 if os.geteuid() != 0:
Bertrand SIMONNET7d66e232015-03-24 17:45:50 -070056 cros_build_lib.SudoRunCommand(sys.argv, print_cmd=False)
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -070057 return
58
59 if opts.command == 'create-wrappers':
60 sysroot_lib.CreateAllWrappers(opts.sysroot, opts.toolchain,
61 opts.friendlyname)
Bertrand SIMONNET7d66e232015-03-24 17:45:50 -070062 elif opts.command == 'generate-config':
63 if opts.brick:
64 config = sysroot_lib.GenerateBrickConfig(
65 opts.sysroot, brick=brick_lib.Brick(opts.brick))
66 else:
67 config = sysroot_lib.GenerateBoardConfig(opts.sysroot, board=opts.board)
68
69 if opts.out_file:
70 osutils.WriteFile(opts.out_file, config, makedirs=True)
71 else:
72 print(config)