blob: b2b5516f1dba2d9036e881e1315bf2df3582aa42 [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 )
70 advanced.add_argument(
71 "--accept_licenses",
72 deprecated=deprecated % "--accept-licenses",
73 help="Deprecated form of --accept-licenses.",
74 )
Alex Klein34581082018-12-03 12:56:53 -070075
Alex Klein1699fab2022-09-08 08:46:06 -060076 # Build target related arguments.
77 target = parser.add_argument_group("Advanced Build Target Options")
78 target.add_argument(
79 "--profile",
80 help="The portage configuration profile to use. Profile "
81 "must be located in overlay-board/profiles.",
82 )
83 target.add_argument("--variant", help="Board variant.")
84 target.add_argument("--board-root", type="path", help="Board root.")
85 target.add_argument(
86 "--board_root",
87 type="path",
88 deprecated=deprecated % "--board-root",
89 help="Deprecated form of --board-root.",
90 )
Alex Klein34581082018-12-03 12:56:53 -070091
Alex Klein1699fab2022-09-08 08:46:06 -060092 # Arguments related to the build itself.
93 build = parser.add_argument_group("Advanced Build Modification Options")
94 build.add_argument(
95 "--jobs",
96 type=int,
97 help="Maximum number of packages to build in parallel.",
98 )
99 build.add_argument(
100 "--regen-configs",
101 action="store_true",
102 default=False,
103 help="Regenerate all config files (useful for "
104 "modifying profiles without rebuild).",
105 )
106 build.add_argument(
107 "--regen_configs",
108 action="store_true",
109 default=False,
110 deprecated=deprecated % "--regen-configs",
111 help="Deprecated form of --regen-configs.",
112 )
113 build.add_argument(
114 "--quiet",
115 action="store_true",
116 default=False,
117 help="Don't print warnings when board already exists.",
118 )
119 build.add_argument(
120 "--skip-toolchain-update",
121 action="store_true",
122 default=False,
123 help="Don't update toolchain automatically.",
124 )
125 build.add_argument(
126 "--skip_toolchain_update",
127 action="store_true",
128 default=False,
129 deprecated=deprecated % "--skip-toolchain-update",
130 help="Deprecated form of --skip-toolchain-update.",
131 )
132 build.add_argument(
133 "--skip-chroot-upgrade",
134 action="store_true",
135 default=False,
136 help="Don't run the chroot upgrade automatically; " "use with care.",
137 )
138 build.add_argument(
139 "--skip_chroot_upgrade",
140 action="store_true",
141 default=False,
142 deprecated=deprecated % "--skip-chroot-upgrade",
143 help="Deprecated form of --skip-chroot-upgrade.",
144 )
145 build.add_argument(
146 "--skip-board-pkg-init",
147 action="store_true",
148 default=False,
149 help="Don't emerge any packages during setup_board into "
150 "the board root.",
151 )
152 build.add_argument(
153 "--skip_board_pkg_init",
154 action="store_true",
155 default=False,
156 deprecated=deprecated % "--skip-board-pkg-init",
157 help="Deprecated form of --skip-board-pkg-init.",
158 )
159 build.add_argument(
160 "--reuse-pkgs-from-local-boards",
161 dest="reuse_local",
162 action="store_true",
163 default=False,
164 help="Bootstrap from local packages instead of remote " "packages.",
165 )
166 build.add_argument(
167 "--reuse_pkgs_from_local_boards",
168 dest="reuse_local",
169 action="store_true",
170 default=False,
171 deprecated=deprecated % "--reuse-pkgs-from-local-boards",
172 help="Deprecated form of --reuse-pkgs-from-local-boards.",
173 )
174 build.add_argument(
175 "--backtrack",
176 type=int,
177 default=sysroot.BACKTRACK_DEFAULT,
178 help="See emerge --backtrack.",
179 )
Alex Klein34581082018-12-03 12:56:53 -0700180
Alex Klein1699fab2022-09-08 08:46:06 -0600181 parser.add_argument(
182 "--fewer-binhosts",
183 dest="expanded_binhost_inheritance",
184 default=True,
185 action="store_false",
186 help=argparse.SUPPRESS,
187 )
Alex Klein1b031e32021-03-08 15:28:30 -0700188
Alex Klein1699fab2022-09-08 08:46:06 -0600189 return parser
Alex Klein34581082018-12-03 12:56:53 -0700190
191
192def _ParseArgs(args):
Alex Klein1699fab2022-09-08 08:46:06 -0600193 """Parse and validate arguments."""
194 parser = GetParser()
195 opts = parser.parse_args(args)
Alex Klein34581082018-12-03 12:56:53 -0700196
Alex Klein1699fab2022-09-08 08:46:06 -0600197 # Translate raw options to config objects.
198 name = "%s_%s" % (opts.board, opts.variant) if opts.variant else opts.board
199 opts.build_target = build_target_lib.BuildTarget(
200 name, build_root=opts.board_root, profile=opts.profile
201 )
Alex Klein34581082018-12-03 12:56:53 -0700202
Alex Klein1699fab2022-09-08 08:46:06 -0600203 opts.run_config = sysroot.SetupBoardRunConfig(
204 set_default=opts.default,
205 force=opts.force,
206 usepkg=opts.usepkg,
207 jobs=opts.jobs,
208 regen_configs=opts.regen_configs,
209 quiet=opts.quiet,
210 update_toolchain=not opts.skip_toolchain_update,
211 upgrade_chroot=not opts.skip_chroot_upgrade,
212 init_board_pkgs=not opts.skip_board_pkg_init,
213 local_build=opts.reuse_local,
214 expanded_binhost_inheritance=opts.expanded_binhost_inheritance,
215 backtrack=opts.backtrack,
216 )
Alex Klein34581082018-12-03 12:56:53 -0700217
Alex Klein1699fab2022-09-08 08:46:06 -0600218 opts.Freeze()
219 return opts
Alex Klein34581082018-12-03 12:56:53 -0700220
221
222def main(argv):
Jack Rosenthal77c07c32023-04-21 15:31:34 -0600223 commandline.RunInsideChroot()
Alex Klein1699fab2022-09-08 08:46:06 -0600224 opts = _ParseArgs(argv)
225 try:
226 sysroot.SetupBoard(
227 opts.build_target, opts.accept_licenses, opts.run_config
228 )
229 except portage_util.MissingOverlayError as e:
230 # Add a bit more user friendly message as people can typo names easily.
231 cros_build_lib.Die(
232 "%s\n"
233 "Double check the --board setting and make sure you're syncing the "
234 "right manifest (internal-vs-external).",
235 e,
236 )
237 except sysroot.Error as e:
238 cros_build_lib.Die(e)