blob: 07a89bd704cc9bf48a4f2bb51f3e1b1422dd709b [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2018 The ChromiumOS Authors
Alex Klein34581082018-12-03 12:56:53 -07002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""setup_board builds the sysroot for a board.
6
7The setup_board process includes the simple directory creations, installs
8several configuration files, sets up portage command wrappers and configs,
9and installs the toolchain and some core dependency packages (e.g. kernel
10headers, gcc-libs).
11"""
12
Alex Klein07547042021-03-12 11:47:03 -070013import argparse
Mike Frysinger6a2b0f22020-02-20 13:34:07 -050014
Alex Klein2960c752020-03-09 13:43:38 -060015from chromite.lib import build_target_lib
Alex Klein34581082018-12-03 12:56:53 -070016from chromite.lib import commandline
Alex Kleinda35fcf2019-03-07 16:01:15 -070017from chromite.lib import cros_build_lib
Mike Frysingerc14d9a02019-08-26 15:44:16 -040018from chromite.lib import portage_util
Alex Kleinda35fcf2019-03-07 16:01:15 -070019from chromite.service import sysroot
Alex Klein34581082018-12-03 12:56:53 -070020
21
22def GetParser():
Alex Klein1699fab2022-09-08 08:46:06 -060023 """Build the argument parser."""
24 # TODO(crbug.com/922144) Remove underscore separated arguments and the
25 # deprecated message after 2019-06-01.
26 deprecated = "Argument will be removed 2019-06-01. Use %s instead."
27 parser = commandline.ArgumentParser(description=__doc__)
Alex Klein34581082018-12-03 12:56:53 -070028
Alex Klein1699fab2022-09-08 08:46:06 -060029 parser.add_argument(
30 "-b",
31 "--board",
32 "--build-target",
33 required=True,
34 dest="board",
35 help="The name of the board to set up.",
36 )
37 parser.add_argument(
38 "--default",
39 action="store_true",
40 default=False,
41 help="Set the board to the default board in your chroot.",
42 )
43 parser.add_argument(
44 "--force",
45 action="store_true",
46 default=False,
47 help="Force re-creating the board root.",
48 )
49 # The positive and negative versions of the arguments are used.
50 # TODO(saklein) Simplify usages to a single version of the argument.
51 parser.add_argument(
52 "--usepkg",
53 action="store_true",
54 default=True,
55 dest="usepkg",
56 help="Use binary packages to bootstrap.",
57 )
58 parser.add_argument(
59 "--nousepkg",
60 action="store_false",
61 default=True,
62 dest="usepkg",
63 help="Do not use binary packages to bootstrap.",
64 )
Alex Klein34581082018-12-03 12:56:53 -070065
Alex Klein1699fab2022-09-08 08:46:06 -060066 advanced = parser.add_argument_group("Advanced Options")
67 advanced.add_argument(
68 "--accept-licenses", help="Licenses to append to the accept list."
69 )
Alex Klein34581082018-12-03 12:56:53 -070070
Alex Klein1699fab2022-09-08 08:46:06 -060071 # Build target related arguments.
72 target = parser.add_argument_group("Advanced Build Target Options")
73 target.add_argument(
74 "--profile",
75 help="The portage configuration profile to use. Profile "
76 "must be located in overlay-board/profiles.",
77 )
78 target.add_argument("--variant", help="Board variant.")
79 target.add_argument("--board-root", type="path", help="Board root.")
Alex Klein34581082018-12-03 12:56:53 -070080
Alex Klein1699fab2022-09-08 08:46:06 -060081 # Arguments related to the build itself.
82 build = parser.add_argument_group("Advanced Build Modification Options")
83 build.add_argument(
84 "--jobs",
85 type=int,
86 help="Maximum number of packages to build in parallel.",
87 )
88 build.add_argument(
89 "--regen-configs",
90 action="store_true",
91 default=False,
92 help="Regenerate all config files (useful for "
93 "modifying profiles without rebuild).",
94 )
95 build.add_argument(
Alex Klein1699fab2022-09-08 08:46:06 -060096 "--quiet",
97 action="store_true",
98 default=False,
99 help="Don't print warnings when board already exists.",
100 )
101 build.add_argument(
102 "--skip-toolchain-update",
103 action="store_true",
104 default=False,
105 help="Don't update toolchain automatically.",
106 )
107 build.add_argument(
108 "--skip_toolchain_update",
109 action="store_true",
110 default=False,
111 deprecated=deprecated % "--skip-toolchain-update",
112 help="Deprecated form of --skip-toolchain-update.",
113 )
114 build.add_argument(
115 "--skip-chroot-upgrade",
116 action="store_true",
117 default=False,
Jack Rosenthal17414e82023-05-12 13:18:03 -0600118 help="Don't run the chroot upgrade automatically; use with care.",
Alex Klein1699fab2022-09-08 08:46:06 -0600119 )
120 build.add_argument(
121 "--skip_chroot_upgrade",
122 action="store_true",
123 default=False,
124 deprecated=deprecated % "--skip-chroot-upgrade",
125 help="Deprecated form of --skip-chroot-upgrade.",
126 )
127 build.add_argument(
128 "--skip-board-pkg-init",
129 action="store_true",
130 default=False,
131 help="Don't emerge any packages during setup_board into "
132 "the board root.",
133 )
134 build.add_argument(
Alex Klein1699fab2022-09-08 08:46:06 -0600135 "--reuse-pkgs-from-local-boards",
136 dest="reuse_local",
137 action="store_true",
138 default=False,
Jack Rosenthal17414e82023-05-12 13:18:03 -0600139 help="Bootstrap from local packages instead of remote packages.",
Alex Klein1699fab2022-09-08 08:46:06 -0600140 )
141 build.add_argument(
142 "--backtrack",
143 type=int,
144 default=sysroot.BACKTRACK_DEFAULT,
145 help="See emerge --backtrack.",
146 )
Alex Klein34581082018-12-03 12:56:53 -0700147
Alex Klein1699fab2022-09-08 08:46:06 -0600148 parser.add_argument(
149 "--fewer-binhosts",
150 dest="expanded_binhost_inheritance",
151 default=True,
152 action="store_false",
153 help=argparse.SUPPRESS,
154 )
Alex Klein1b031e32021-03-08 15:28:30 -0700155
Alex Klein1699fab2022-09-08 08:46:06 -0600156 return parser
Alex Klein34581082018-12-03 12:56:53 -0700157
158
159def _ParseArgs(args):
Alex Klein1699fab2022-09-08 08:46:06 -0600160 """Parse and validate arguments."""
161 parser = GetParser()
162 opts = parser.parse_args(args)
Alex Klein34581082018-12-03 12:56:53 -0700163
Alex Klein1699fab2022-09-08 08:46:06 -0600164 # Translate raw options to config objects.
165 name = "%s_%s" % (opts.board, opts.variant) if opts.variant else opts.board
166 opts.build_target = build_target_lib.BuildTarget(
167 name, build_root=opts.board_root, profile=opts.profile
168 )
Alex Klein34581082018-12-03 12:56:53 -0700169
Alex Klein1699fab2022-09-08 08:46:06 -0600170 opts.run_config = sysroot.SetupBoardRunConfig(
171 set_default=opts.default,
172 force=opts.force,
173 usepkg=opts.usepkg,
174 jobs=opts.jobs,
175 regen_configs=opts.regen_configs,
176 quiet=opts.quiet,
177 update_toolchain=not opts.skip_toolchain_update,
178 upgrade_chroot=not opts.skip_chroot_upgrade,
179 init_board_pkgs=not opts.skip_board_pkg_init,
180 local_build=opts.reuse_local,
181 expanded_binhost_inheritance=opts.expanded_binhost_inheritance,
182 backtrack=opts.backtrack,
183 )
Alex Klein34581082018-12-03 12:56:53 -0700184
Alex Klein1699fab2022-09-08 08:46:06 -0600185 opts.Freeze()
186 return opts
Alex Klein34581082018-12-03 12:56:53 -0700187
188
189def main(argv):
Jack Rosenthal77c07c32023-04-21 15:31:34 -0600190 commandline.RunInsideChroot()
Alex Klein1699fab2022-09-08 08:46:06 -0600191 opts = _ParseArgs(argv)
192 try:
193 sysroot.SetupBoard(
194 opts.build_target, opts.accept_licenses, opts.run_config
195 )
196 except portage_util.MissingOverlayError as e:
197 # Add a bit more user friendly message as people can typo names easily.
198 cros_build_lib.Die(
199 "%s\n"
200 "Double check the --board setting and make sure you're syncing the "
201 "right manifest (internal-vs-external).",
202 e,
203 )
204 except sysroot.Error as e:
205 cros_build_lib.Die(e)