blob: 77f01b897c5239c45bc865f6d643d8e933132788 [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -07002# 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
9from __future__ import print_function
10
11import os
12import sys
13
14from chromite.lib import commandline
15from chromite.lib import cros_build_lib
16from chromite.lib import sysroot_lib
17
18
19def 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 SIMONNETba364242015-03-30 15:02:44 -070026 parser.set_defaults(out_file=None)
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -070027 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 SIMONNET9562e2c2015-03-13 13:57:43 -070031 wrapper.set_defaults(command='create-wrappers')
32
Bertrand SIMONNET7d66e232015-03-24 17:45:50 -070033 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 SIMONNET7d66e232015-03-24 17:45:50 -070036 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 SIMONNETba364242015-03-30 15:02:44 -070042 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 SIMONNET9562e2c2015-03-13 13:57:43 -070064 options = parser.parse_args(argv)
65 options.Freeze()
66 return options
67
68
69def main(argv):
70 opts = ParseArgs(argv)
71 if not cros_build_lib.IsInsideChroot():
Mike Frysingera620b8e2018-07-14 00:59:58 -040072 raise commandline.ChrootRequiredError(argv)
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -070073
74 if os.geteuid() != 0:
Bertrand SIMONNET7d66e232015-03-24 17:45:50 -070075 cros_build_lib.SudoRunCommand(sys.argv, print_cmd=False)
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -070076 return
77
Bertrand SIMONNETba364242015-03-30 15:02:44 -070078 output = sys.stdout
79 if opts.out_file:
80 output = open(opts.out_file, 'w')
81
Bertrand SIMONNETe2cec3f2015-04-06 16:12:54 -070082 sysroot = sysroot_lib.Sysroot(opts.sysroot)
Bertrand SIMONNET9562e2c2015-03-13 13:57:43 -070083 if opts.command == 'create-wrappers':
Bertrand SIMONNETe2cec3f2015-04-06 16:12:54 -070084 sysroot.CreateAllWrappers(opts.friendlyname)
Bertrand SIMONNET7d66e232015-03-24 17:45:50 -070085 elif opts.command == 'generate-config':
Don Garrettc0c74002015-10-09 12:58:19 -070086 config = sysroot.GenerateBoardConfig(opts.board)
Bertrand SIMONNET7d66e232015-03-24 17:45:50 -070087
Bertrand SIMONNETba364242015-03-30 15:02:44 -070088 output.write('\n' + config)
89 elif opts.command == 'generate-make-conf':
Bertrand SIMONNETe2cec3f2015-04-06 16:12:54 -070090 output.write('\n' + sysroot.GenerateMakeConf(opts.accepted_licenses))
Bertrand SIMONNETba364242015-03-30 15:02:44 -070091 elif opts.command == 'generate-binhosts':
Bertrand SIMONNETe2cec3f2015-04-06 16:12:54 -070092 output.write('\n' + sysroot.GenerateBinhostConf(opts.chrome_only,
93 opts.local_only))