blob: 6c95669f6caca84765aecd7f464a8ff61fbf3277 [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 )
Mike Frysinger4a0708f2023-06-14 11:37:20 -0400107 # TODO(build): Delete by end of 2023.
Alex Klein1699fab2022-09-08 08:46:06 -0600108 build.add_argument(
109 "--skip_toolchain_update",
110 action="store_true",
111 default=False,
112 deprecated=deprecated % "--skip-toolchain-update",
113 help="Deprecated form of --skip-toolchain-update.",
114 )
115 build.add_argument(
116 "--skip-chroot-upgrade",
117 action="store_true",
118 default=False,
Jack Rosenthal17414e82023-05-12 13:18:03 -0600119 help="Don't run the chroot upgrade automatically; use with care.",
Alex Klein1699fab2022-09-08 08:46:06 -0600120 )
Mike Frysinger4a0708f2023-06-14 11:37:20 -0400121 # TODO(build): Delete by end of 2023.
Alex Klein1699fab2022-09-08 08:46:06 -0600122 build.add_argument(
123 "--skip_chroot_upgrade",
124 action="store_true",
125 default=False,
126 deprecated=deprecated % "--skip-chroot-upgrade",
127 help="Deprecated form of --skip-chroot-upgrade.",
128 )
129 build.add_argument(
130 "--skip-board-pkg-init",
131 action="store_true",
132 default=False,
133 help="Don't emerge any packages during setup_board into "
134 "the board root.",
135 )
136 build.add_argument(
Alex Klein1699fab2022-09-08 08:46:06 -0600137 "--reuse-pkgs-from-local-boards",
138 dest="reuse_local",
139 action="store_true",
140 default=False,
Jack Rosenthal17414e82023-05-12 13:18:03 -0600141 help="Bootstrap from local packages instead of remote packages.",
Alex Klein1699fab2022-09-08 08:46:06 -0600142 )
143 build.add_argument(
144 "--backtrack",
145 type=int,
146 default=sysroot.BACKTRACK_DEFAULT,
147 help="See emerge --backtrack.",
148 )
Alex Klein34581082018-12-03 12:56:53 -0700149
Alex Klein1699fab2022-09-08 08:46:06 -0600150 parser.add_argument(
151 "--fewer-binhosts",
152 dest="expanded_binhost_inheritance",
153 default=True,
154 action="store_false",
155 help=argparse.SUPPRESS,
156 )
Alex Klein1b031e32021-03-08 15:28:30 -0700157
Alex Klein1699fab2022-09-08 08:46:06 -0600158 return parser
Alex Klein34581082018-12-03 12:56:53 -0700159
160
161def _ParseArgs(args):
Alex Klein1699fab2022-09-08 08:46:06 -0600162 """Parse and validate arguments."""
163 parser = GetParser()
164 opts = parser.parse_args(args)
Alex Klein34581082018-12-03 12:56:53 -0700165
Alex Klein1699fab2022-09-08 08:46:06 -0600166 # Translate raw options to config objects.
167 name = "%s_%s" % (opts.board, opts.variant) if opts.variant else opts.board
168 opts.build_target = build_target_lib.BuildTarget(
169 name, build_root=opts.board_root, profile=opts.profile
170 )
Alex Klein34581082018-12-03 12:56:53 -0700171
Alex Klein1699fab2022-09-08 08:46:06 -0600172 opts.run_config = sysroot.SetupBoardRunConfig(
173 set_default=opts.default,
174 force=opts.force,
175 usepkg=opts.usepkg,
176 jobs=opts.jobs,
177 regen_configs=opts.regen_configs,
178 quiet=opts.quiet,
179 update_toolchain=not opts.skip_toolchain_update,
180 upgrade_chroot=not opts.skip_chroot_upgrade,
181 init_board_pkgs=not opts.skip_board_pkg_init,
182 local_build=opts.reuse_local,
183 expanded_binhost_inheritance=opts.expanded_binhost_inheritance,
Cindy Linadc610a2023-05-23 18:46:12 +0000184 use_cq_prebuilts=opts.usepkg,
Alex Klein1699fab2022-09-08 08:46:06 -0600185 backtrack=opts.backtrack,
186 )
Alex Klein34581082018-12-03 12:56:53 -0700187
Alex Klein1699fab2022-09-08 08:46:06 -0600188 opts.Freeze()
189 return opts
Alex Klein34581082018-12-03 12:56:53 -0700190
191
192def main(argv):
Jack Rosenthal77c07c32023-04-21 15:31:34 -0600193 commandline.RunInsideChroot()
Alex Klein1699fab2022-09-08 08:46:06 -0600194 opts = _ParseArgs(argv)
195 try:
196 sysroot.SetupBoard(
197 opts.build_target, opts.accept_licenses, opts.run_config
198 )
199 except portage_util.MissingOverlayError as e:
200 # Add a bit more user friendly message as people can typo names easily.
201 cros_build_lib.Die(
202 "%s\n"
203 "Double check the --board setting and make sure you're syncing the "
204 "right manifest (internal-vs-external).",
205 e,
206 )
207 except sysroot.Error as e:
208 cros_build_lib.Die(e)