blob: 6f37060a4404b398cfe5005d78a512d0f8012392 [file] [log] [blame]
Cindy Linc06b4ea2022-01-27 18:13:04 +00001# Copyright 2022 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"""build_packages updates the set of binary packages needed by Chrome OS.
6
7The build_packages process cross compiles all packages that have been
8updated into the given sysroot and builds binary packages as a side-effect.
9The output packages will be used by the build_image script to create a
10bootable Chrome OS image.
Ram Chandrasekarbaa89632022-02-11 23:22:09 +000011
12If packages are specified in cli, only build those specific packages and any
13dependencies they might need.
14
15For the fastest builds, use --nowithautotest --noworkon.
Cindy Linc06b4ea2022-01-27 18:13:04 +000016"""
17
Ram Chandrasekarbaa89632022-02-11 23:22:09 +000018import argparse
Alex Kleinfba23ba2022-02-03 11:58:48 -070019import logging
Cindy Linc06b4ea2022-01-27 18:13:04 +000020import os
Mike Frysinger807d8282022-04-28 22:45:17 -040021from typing import List, Optional, Tuple
Alex Kleinfba23ba2022-02-03 11:58:48 -070022import urllib.error
23import urllib.request
Cindy Linc06b4ea2022-01-27 18:13:04 +000024
Cindy Lin7487daa2022-02-23 04:14:10 +000025from chromite.lib import build_target_lib
Alex Kleina39dc982022-02-03 12:13:14 -070026from chromite.lib import commandline
Cindy Linc06b4ea2022-01-27 18:13:04 +000027from chromite.lib import cros_build_lib
Cindy Lin7487daa2022-02-23 04:14:10 +000028from chromite.lib import sysroot_lib
Ram Chandrasekarbdec0f02022-02-24 01:20:17 +000029from chromite.service import sysroot
Cindy Linb7f89a42022-05-23 16:50:00 +000030from chromite.utils import timer
Cindy Linc06b4ea2022-01-27 18:13:04 +000031
32
Ram Chandrasekarbaa89632022-02-11 23:22:09 +000033def build_shell_bool_style_args(parser: commandline.ArgumentParser,
34 name: str,
35 default_val: bool,
36 help_str: str,
37 deprecation_note: str,
38 alternate_name: Optional[str] = None) -> None:
39 """Build the shell boolean input argument equivalent.
Alex Kleina39dc982022-02-03 12:13:14 -070040
Ram Chandrasekarbaa89632022-02-11 23:22:09 +000041 There are two cases which we will need to handle,
42 case 1: A shell boolean arg, which doesn't need to be re-worded in python.
43 case 2: A shell boolean arg, which needs to be re-worded in python.
44 Example below.
45 For Case 1, for a given input arg name 'argA', we create three python
46 arguments.
47 --argA, --noargA, --no-argA. The arguments --argA and --no-argA will be
48 retained after deprecating --noargA.
49 For Case 2, for a given input arg name 'arg_A' we need to use alternate
50 argument name 'arg-A'. we create four python arguments in this case.
51 --arg_A, --noarg_A, --arg-A, --no-arg-A. The first two arguments will be
52 deprecated later.
53 TODO(b/218522717): Remove the creation of --noargA in case 1 and --arg_A and
54 --noarg_A in case 2.
55
56 Args:
57 parser: The parser to update.
58 name: The input argument name. This will be used as 'dest' variable name.
59 default_val: The default value to assign.
60 help_str: The help string for the input argument.
61 deprecation_note: A deprecation note to use.
62 alternate_name: Alternate argument to be used after deprecation.
63 """
64 arg = f'--{name}'
65 shell_narg = f'--no{name}'
66 py_narg = f'--no-{name}'
67 alt_arg = f'--{alternate_name}' if alternate_name else None
68 alt_py_narg = f'--no-{alternate_name}' if alternate_name else None
69 default_val_str = f'{help_str} (Default: %(default)s).'
70
71 if alternate_name:
72 parser.add_argument(
73 alt_arg,
74 action='store_true',
75 default=default_val,
76 dest=name,
77 help=default_val_str)
78 parser.add_argument(
79 alt_py_narg,
80 action='store_false',
81 dest=name,
82 help="Don't " + help_str.lower())
83
84 parser.add_argument(
85 arg,
86 action='store_true',
87 default=default_val,
88 dest=name,
89 deprecated=deprecation_note % alt_arg if alternate_name else None,
90 help=default_val_str if not alternate_name else argparse.SUPPRESS)
91 parser.add_argument(
92 shell_narg,
93 action='store_false',
94 dest=name,
95 deprecated=deprecation_note %
96 (alt_py_narg if alternate_name else py_narg),
97 help=argparse.SUPPRESS)
98
99 if not alternate_name:
100 parser.add_argument(
101 py_narg,
102 action='store_false',
103 dest=name,
104 help="Don't " + help_str.lower())
105
106
107def get_parser() -> commandline.ArgumentParser:
108 """Creates the cmdline argparser, populates the options and description.
109
110 Returns:
111 Argument parser.
112 """
113 deprecation_note = 'Argument will be removed July, 2022. Use %s instead.'
114 parser = commandline.ArgumentParser(description=__doc__)
115
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000116 parser.add_argument(
Alex Klein78a4bc02022-04-14 15:59:52 -0600117 '-b',
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000118 '--board',
Alex Klein78a4bc02022-04-14 15:59:52 -0600119 '--build-target',
120 dest='board',
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000121 default=cros_build_lib.GetDefaultBoard(),
122 help='The board to build packages for.')
123
124 build_shell_bool_style_args(
125 parser, 'usepkg', True, 'Use binary packages to bootstrap when possible.',
126 deprecation_note)
127 build_shell_bool_style_args(
128 parser, 'usepkgonly', False,
129 'Use binary packages only to bootstrap; abort if any are missing.',
130 deprecation_note)
131 build_shell_bool_style_args(parser, 'workon', True,
132 'Force-build workon packages.', deprecation_note)
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000133 build_shell_bool_style_args(
134 parser, 'withrevdeps', True,
135 'Calculate reverse dependencies on changed ebuilds.', deprecation_note)
136 build_shell_bool_style_args(
137 parser, 'cleanbuild', False,
138 'Perform a clean build; delete sysroot if it exists before building.',
139 deprecation_note)
140 build_shell_bool_style_args(
141 parser, 'pretend', False,
142 'Pretend building packages, just display which packages would have '
143 'been installed.', deprecation_note)
144
145 # The --sysroot flag specifies the environment variables ROOT and PKGDIR.
146 # This allows fetching and emerging of all packages to specified sysroot.
147 # Note that --sysroot will setup the board normally in /build/$BOARD, if
148 # it's not setup yet. It also expects the toolchain to already be installed
149 # in the sysroot.
150 # --usepkgonly and --norebuild are required, because building is not
151 # supported when board_root is set.
152 parser.add_argument(
153 '--sysroot', type='path', help='Emerge packages to sysroot.')
154 parser.add_argument(
155 '--board_root',
156 type='path',
157 dest='sysroot',
158 deprecated=deprecation_note % '--sysroot',
159 help=argparse.SUPPRESS)
160
161 # CPU Governor related options.
162 group = parser.add_argument_group('CPU Governor Options')
163 build_shell_bool_style_args(
164 group, 'autosetgov', False,
165 "Automatically set cpu governor to 'performance'.", deprecation_note)
166 build_shell_bool_style_args(
167 group,
168 'autosetgov_sticky',
169 False,
170 'Remember --autosetgov setting for future runs.',
171 deprecation_note,
172 alternate_name='autosetgov-sticky')
173
174 # Chrome building related options.
175 group = parser.add_argument_group('Chrome Options')
176 build_shell_bool_style_args(
177 group,
178 'use_any_chrome',
179 True,
180 "Use any Chrome prebuilt available, even if the prebuilt doesn't "
181 'match exactly.',
182 deprecation_note,
183 alternate_name='use-any-chrome')
184 build_shell_bool_style_args(
185 group, 'internal', False,
186 'Build the internal version of chrome(set the chrome_internal USE flag).',
187 deprecation_note)
188 build_shell_bool_style_args(
189 group, 'chrome', False, 'Ensure chrome instead of chromium. Alias for '
190 '--internal --no-use-any-chrome.', deprecation_note)
191
192 # Setup board related options.
193 group = parser.add_argument_group('Setup Board Config Options')
194 build_shell_bool_style_args(
195 group,
196 'skip_chroot_upgrade',
197 False,
198 'Skip the automatic chroot upgrade; use with care.',
199 deprecation_note,
200 alternate_name='skip-chroot-upgrade')
201 build_shell_bool_style_args(
202 group,
203 'skip_toolchain_update',
204 False,
205 'Skip automatic toolchain update',
206 deprecation_note,
207 alternate_name='skip-toolchain-update')
208 build_shell_bool_style_args(
209 group,
210 'skip_setup_board',
211 False,
212 'Skip running setup_board. Implies '
213 '--skip-chroot-upgrade --skip-toolchain-update.',
214 deprecation_note,
215 alternate_name='skip-setup-board')
216
217 # Image Type selection related options.
218 group = parser.add_argument_group('Image Type Options')
219 build_shell_bool_style_args(group, 'withdev', True,
220 'Build useful developer friendly utilities.',
221 deprecation_note)
222 build_shell_bool_style_args(
223 group, 'withdebug', True,
224 'Build debug versions of Chromium-OS-specific packages.',
225 deprecation_note)
226 build_shell_bool_style_args(group, 'withfactory', True,
227 'Build factory installer.', deprecation_note)
228 build_shell_bool_style_args(group, 'withtest', True,
229 'Build packages required for testing.',
230 deprecation_note)
231 build_shell_bool_style_args(group, 'withautotest', True,
232 'Build autotest client code.', deprecation_note)
233 build_shell_bool_style_args(group, 'withdebugsymbols', False,
234 'Install the debug symbols for all packages.',
235 deprecation_note)
236
237 # Advanced Options.
238 group = parser.add_argument_group('Advanced Options')
239 group.add_argument(
240 '--accept-licenses', help='Licenses to append to the accept list.')
241 group.add_argument(
242 '--accept_licenses',
243 deprecated=deprecation_note % '--accept-licenses',
244 help=argparse.SUPPRESS)
245 build_shell_bool_style_args(group, 'eclean', True,
246 'Run eclean to delete old binpkgs.',
247 deprecation_note)
248 group.add_argument(
249 '--jobs',
250 type=int,
251 default=os.cpu_count(),
252 help='Number of packages to build in parallel. '
253 '(Default: %(default)s)')
254 build_shell_bool_style_args(group, 'rebuild', True,
255 'Automatically rebuild dependencies.',
256 deprecation_note)
257 # TODO(b/218522717): Remove the --nonorebuild argument support.
258 group.add_argument(
259 '--nonorebuild',
260 action='store_true',
261 dest='rebuild',
262 deprecated=deprecation_note % '--rebuild',
263 help=argparse.SUPPRESS)
264 build_shell_bool_style_args(group, 'expandedbinhosts', True,
265 'Allow expanded binhost inheritance.',
266 deprecation_note)
267 # The --reuse-pkgs-from-local-boards flag tells Portage to share binary
268 # packages between boards that are built locally, so that the total time
269 # required to build several boards is reduced. This flag is only useful
270 # when you are not able to use remote binary packages, since remote binary
271 # packages are usually more up to date than anything you have locally.
272 build_shell_bool_style_args(
273 group,
274 'reuse_pkgs_from_local_boards',
275 False,
276 'Bootstrap from local packages instead of remote packages.',
277 deprecation_note,
278 alternate_name='reuse-pkgs-from-local-boards')
279
280 # --run-goma option is designed to be used on bots.
281 # If you're trying to build packages with goma in your local dev env, this is
282 # *not* the option you're looking for. Please see comments below.
283 # This option; 1) starts goma, 2) builds packages (expecting that goma is
284 # used), then 3) stops goma explicitly.
285 # 4) is a request from the goma team, so that stats/logs can be taken.
286 # Note: GOMA_DIR and GOMA_SERVICE_ACCOUNT_JSON_FILE are expected to be passed
287 # via env var.
288 #
289 # In local dev env cases, compiler_proxy is expected to keep running.
290 # In such a case;
291 # $ python ${GOMA_DIR}/goma_ctl.py ensure_start
292 # $ ./build_packages (... and options without --run-goma ...)
293 # is an expected commandline sequence. If you set --run-goma flag while
294 # compiler_proxy is already running, the existing compiler_proxy will be
295 # stopped.
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000296 build_shell_bool_style_args(
297 group,
298 'run_goma',
299 False,
300 'If set to true, (re)starts goma, builds packages, and then stops goma..',
301 deprecation_note,
302 alternate_name='run-goma')
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000303 # This option is for building chrome remotely.
304 # 1) starts reproxy 2) builds chrome with reproxy and 3) stops reproxy so
305 # logs/stats can be collected.
306 # Note: RECLIENT_DIR and REPROXY_CFG env var will be deprecated July, 2022.
307 # Use --reclient-dir and --reproxy-cfg input options instead.
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000308 build_shell_bool_style_args(
309 group, 'run_remoteexec', False,
310 'If set to true, starts RBE reproxy, builds packages, and then stops '
311 'reproxy.', deprecation_note)
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000312
313 parser.add_argument('packages', nargs='*', help='Packages to build.')
314 return parser
315
316
Ram Chandrasekarbdec0f02022-02-24 01:20:17 +0000317def parse_args(argv: List[str]) -> Tuple[commandline.ArgumentParser,
318 commandline.ArgumentNamespace]:
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000319 """Parse and validate CLI arguments.
320
321 Args:
322 argv: Arguments passed via CLI.
323
324 Returns:
Ram Chandrasekarbdec0f02022-02-24 01:20:17 +0000325 Tuple having the below two,
326 Argument Parser
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000327 Validated argument namespace.
328 """
329 parser = get_parser()
330 opts = parser.parse_args(argv)
Ram Chandrasekarbdec0f02022-02-24 01:20:17 +0000331
332 if opts.chrome:
333 opts.internal_chrome = True
334 opts.use_any_chrome = False
335
Cindy Lin317083d2022-03-14 17:07:25 +0000336 opts.setup_board_run_config = sysroot.SetupBoardRunConfig(
337 force=opts.cleanbuild,
338 usepkg=opts.usepkg,
339 jobs=opts.jobs,
340 quiet=True,
341 update_toolchain=not opts.skip_toolchain_update,
342 upgrade_chroot=not opts.skip_chroot_upgrade,
343 local_build=opts.reuse_pkgs_from_local_boards,
344 expanded_binhost_inheritance=opts.expandedbinhosts)
Ram Chandrasekarbdec0f02022-02-24 01:20:17 +0000345 opts.build_run_config = sysroot.BuildPackagesRunConfig(
346 usepkg=opts.usepkg,
347 install_debug_symbols=opts.withdebugsymbols,
348 packages=opts.packages,
349 use_goma=opts.run_goma,
350 use_remoteexec=opts.run_remoteexec,
351 incremental_build=opts.withrevdeps,
Ram Chandrasekarbdec0f02022-02-24 01:20:17 +0000352 dryrun=opts.pretend,
353 usepkgonly=opts.usepkgonly,
354 workon=opts.workon,
Ram Chandrasekarbdec0f02022-02-24 01:20:17 +0000355 install_auto_test=opts.withautotest,
356 autosetgov=opts.autosetgov,
357 autosetgov_sticky=opts.autosetgov_sticky,
358 use_any_chrome=opts.use_any_chrome,
359 internal_chrome=opts.internal,
360 clean_build=opts.cleanbuild,
361 eclean=opts.eclean,
362 rebuild_dep=opts.rebuild,
Ram Chandrasekarbdec0f02022-02-24 01:20:17 +0000363 jobs=opts.jobs,
364 local_pkg=opts.reuse_pkgs_from_local_boards,
365 dev_image=opts.withdev,
366 factory_image=opts.withfactory,
367 test_image=opts.withtest,
Cindy Lin317083d2022-03-14 17:07:25 +0000368 debug_version=opts.withdebug)
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000369 opts.Freeze()
Ram Chandrasekarbdec0f02022-02-24 01:20:17 +0000370 return parser, opts
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000371
372
Cindy Linb7f89a42022-05-23 16:50:00 +0000373@timer.timed('Elapsed time (build_packages)')
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000374def main(argv: Optional[List[str]] = None) -> Optional[int]:
375 commandline.RunInsideChroot()
Ram Chandrasekarbdec0f02022-02-24 01:20:17 +0000376 parser, opts = parse_args(argv)
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000377
Ram Chandrasekarbdec0f02022-02-24 01:20:17 +0000378 # If the opts.board is not set, then it means user hasn't specified a default
379 # board in 'src/scripts/.default_board' and didn't specify it as input
380 # argument.
381 if not opts.board:
382 parser.error('--board is required')
383
Cindy Lin7487daa2022-02-23 04:14:10 +0000384 build_target = build_target_lib.BuildTarget(
Alex Klein78a4bc02022-04-14 15:59:52 -0600385 opts.board, build_root=opts.sysroot)
386 board_root = sysroot_lib.Sysroot(build_target.root)
Cindy Lin7487daa2022-02-23 04:14:10 +0000387
Cindy Linc06b4ea2022-01-27 18:13:04 +0000388 try:
Cindy Lin317083d2022-03-14 17:07:25 +0000389 # TODO(xcl): Update run_configs to have a common base set of configs for
390 # setup_board and build_packages.
391 if not opts.skip_setup_board:
392 sysroot.SetupBoard(
393 build_target,
394 accept_licenses=opts.accept_licenses,
395 run_configs=opts.setup_board_run_config)
Cindy Lin7487daa2022-02-23 04:14:10 +0000396 sysroot.BuildPackages(build_target, board_root, opts.build_run_config)
397 except sysroot_lib.PackageInstallError as e:
Alex Kleinfba23ba2022-02-03 11:58:48 -0700398 try:
Sergey Frolov7cf6c0b2022-06-06 16:47:44 -0600399 with urllib.request.urlopen(
400 'https://chromiumos-status.appspot.com/current?format=raw'
401 ) as request:
402 logging.notice('Tree Status: %s', request.read().decode())
Alex Kleinfba23ba2022-02-03 11:58:48 -0700403 except urllib.error.HTTPError:
404 pass
Cindy Linc06b4ea2022-01-27 18:13:04 +0000405 cros_build_lib.Die(e)