blob: c25a1a46b1e651f827dada3f51582015c8fe224f [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,
Alex Klein062d7282022-07-28 09:03:26 -060095 deprecated=deprecation_note % alt_py_narg if alternate_name else py_narg,
Ram Chandrasekarbaa89632022-02-11 23:22:09 +000096 help=argparse.SUPPRESS)
97
98 if not alternate_name:
99 parser.add_argument(
100 py_narg,
101 action='store_false',
102 dest=name,
103 help="Don't " + help_str.lower())
104
105
106def get_parser() -> commandline.ArgumentParser:
107 """Creates the cmdline argparser, populates the options and description.
108
109 Returns:
110 Argument parser.
111 """
112 deprecation_note = 'Argument will be removed July, 2022. Use %s instead.'
113 parser = commandline.ArgumentParser(description=__doc__)
114
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000115 parser.add_argument(
Alex Klein78a4bc02022-04-14 15:59:52 -0600116 '-b',
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000117 '--board',
Alex Klein78a4bc02022-04-14 15:59:52 -0600118 '--build-target',
119 dest='board',
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000120 default=cros_build_lib.GetDefaultBoard(),
121 help='The board to build packages for.')
122
123 build_shell_bool_style_args(
124 parser, 'usepkg', True, 'Use binary packages to bootstrap when possible.',
125 deprecation_note)
126 build_shell_bool_style_args(
127 parser, 'usepkgonly', False,
128 'Use binary packages only to bootstrap; abort if any are missing.',
129 deprecation_note)
130 build_shell_bool_style_args(parser, 'workon', True,
131 'Force-build workon packages.', deprecation_note)
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000132 build_shell_bool_style_args(
133 parser, 'withrevdeps', True,
134 'Calculate reverse dependencies on changed ebuilds.', deprecation_note)
135 build_shell_bool_style_args(
136 parser, 'cleanbuild', False,
137 'Perform a clean build; delete sysroot if it exists before building.',
138 deprecation_note)
139 build_shell_bool_style_args(
140 parser, 'pretend', False,
141 'Pretend building packages, just display which packages would have '
142 'been installed.', deprecation_note)
143
144 # The --sysroot flag specifies the environment variables ROOT and PKGDIR.
145 # This allows fetching and emerging of all packages to specified sysroot.
146 # Note that --sysroot will setup the board normally in /build/$BOARD, if
147 # it's not setup yet. It also expects the toolchain to already be installed
148 # in the sysroot.
149 # --usepkgonly and --norebuild are required, because building is not
150 # supported when board_root is set.
151 parser.add_argument(
152 '--sysroot', type='path', help='Emerge packages to sysroot.')
153 parser.add_argument(
154 '--board_root',
155 type='path',
156 dest='sysroot',
157 deprecated=deprecation_note % '--sysroot',
158 help=argparse.SUPPRESS)
159
160 # CPU Governor related options.
161 group = parser.add_argument_group('CPU Governor Options')
162 build_shell_bool_style_args(
163 group, 'autosetgov', False,
164 "Automatically set cpu governor to 'performance'.", deprecation_note)
165 build_shell_bool_style_args(
166 group,
167 'autosetgov_sticky',
168 False,
169 'Remember --autosetgov setting for future runs.',
170 deprecation_note,
171 alternate_name='autosetgov-sticky')
172
173 # Chrome building related options.
174 group = parser.add_argument_group('Chrome Options')
175 build_shell_bool_style_args(
176 group,
177 'use_any_chrome',
178 True,
179 "Use any Chrome prebuilt available, even if the prebuilt doesn't "
180 'match exactly.',
181 deprecation_note,
182 alternate_name='use-any-chrome')
183 build_shell_bool_style_args(
184 group, 'internal', False,
185 'Build the internal version of chrome(set the chrome_internal USE flag).',
186 deprecation_note)
187 build_shell_bool_style_args(
188 group, 'chrome', False, 'Ensure chrome instead of chromium. Alias for '
Alex Klein062d7282022-07-28 09:03:26 -0600189 '--internal --no-use-any-chrome.',
190 deprecation_note)
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000191
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)
Alex Klein062d7282022-07-28 09:03:26 -0600267 group.add_argument(
268 '--backtrack',
269 type=int,
270 default=sysroot.BACKTRACK_DEFAULT,
271 help='See emerge --backtrack.')
272
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000273 # The --reuse-pkgs-from-local-boards flag tells Portage to share binary
274 # packages between boards that are built locally, so that the total time
275 # required to build several boards is reduced. This flag is only useful
276 # when you are not able to use remote binary packages, since remote binary
277 # packages are usually more up to date than anything you have locally.
278 build_shell_bool_style_args(
279 group,
280 'reuse_pkgs_from_local_boards',
281 False,
282 'Bootstrap from local packages instead of remote packages.',
283 deprecation_note,
284 alternate_name='reuse-pkgs-from-local-boards')
285
286 # --run-goma option is designed to be used on bots.
287 # If you're trying to build packages with goma in your local dev env, this is
288 # *not* the option you're looking for. Please see comments below.
289 # This option; 1) starts goma, 2) builds packages (expecting that goma is
290 # used), then 3) stops goma explicitly.
291 # 4) is a request from the goma team, so that stats/logs can be taken.
292 # Note: GOMA_DIR and GOMA_SERVICE_ACCOUNT_JSON_FILE are expected to be passed
293 # via env var.
294 #
295 # In local dev env cases, compiler_proxy is expected to keep running.
296 # In such a case;
297 # $ python ${GOMA_DIR}/goma_ctl.py ensure_start
Cindy Lin29758152022-08-18 20:38:42 +0000298 # $ build_packages (... and options without --run-goma ...)
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000299 # is an expected commandline sequence. If you set --run-goma flag while
300 # compiler_proxy is already running, the existing compiler_proxy will be
301 # stopped.
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000302 build_shell_bool_style_args(
303 group,
304 'run_goma',
305 False,
306 'If set to true, (re)starts goma, builds packages, and then stops goma..',
307 deprecation_note,
308 alternate_name='run-goma')
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000309 # This option is for building chrome remotely.
310 # 1) starts reproxy 2) builds chrome with reproxy and 3) stops reproxy so
311 # logs/stats can be collected.
312 # Note: RECLIENT_DIR and REPROXY_CFG env var will be deprecated July, 2022.
313 # Use --reclient-dir and --reproxy-cfg input options instead.
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000314 build_shell_bool_style_args(
315 group, 'run_remoteexec', False,
316 'If set to true, starts RBE reproxy, builds packages, and then stops '
317 'reproxy.', deprecation_note)
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000318
319 parser.add_argument('packages', nargs='*', help='Packages to build.')
320 return parser
321
322
Ram Chandrasekarbdec0f02022-02-24 01:20:17 +0000323def parse_args(argv: List[str]) -> Tuple[commandline.ArgumentParser,
324 commandline.ArgumentNamespace]:
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000325 """Parse and validate CLI arguments.
326
327 Args:
328 argv: Arguments passed via CLI.
329
330 Returns:
Ram Chandrasekarbdec0f02022-02-24 01:20:17 +0000331 Tuple having the below two,
332 Argument Parser
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000333 Validated argument namespace.
334 """
335 parser = get_parser()
336 opts = parser.parse_args(argv)
Ram Chandrasekarbdec0f02022-02-24 01:20:17 +0000337
338 if opts.chrome:
339 opts.internal_chrome = True
340 opts.use_any_chrome = False
341
Cindy Lin317083d2022-03-14 17:07:25 +0000342 opts.setup_board_run_config = sysroot.SetupBoardRunConfig(
343 force=opts.cleanbuild,
344 usepkg=opts.usepkg,
345 jobs=opts.jobs,
346 quiet=True,
347 update_toolchain=not opts.skip_toolchain_update,
348 upgrade_chroot=not opts.skip_chroot_upgrade,
349 local_build=opts.reuse_pkgs_from_local_boards,
Alex Klein062d7282022-07-28 09:03:26 -0600350 expanded_binhost_inheritance=opts.expandedbinhosts,
351 backtrack=opts.backtrack,
352 )
Ram Chandrasekarbdec0f02022-02-24 01:20:17 +0000353 opts.build_run_config = sysroot.BuildPackagesRunConfig(
354 usepkg=opts.usepkg,
355 install_debug_symbols=opts.withdebugsymbols,
356 packages=opts.packages,
357 use_goma=opts.run_goma,
358 use_remoteexec=opts.run_remoteexec,
359 incremental_build=opts.withrevdeps,
Ram Chandrasekarbdec0f02022-02-24 01:20:17 +0000360 dryrun=opts.pretend,
361 usepkgonly=opts.usepkgonly,
362 workon=opts.workon,
Ram Chandrasekarbdec0f02022-02-24 01:20:17 +0000363 install_auto_test=opts.withautotest,
364 autosetgov=opts.autosetgov,
365 autosetgov_sticky=opts.autosetgov_sticky,
366 use_any_chrome=opts.use_any_chrome,
367 internal_chrome=opts.internal,
368 clean_build=opts.cleanbuild,
369 eclean=opts.eclean,
370 rebuild_dep=opts.rebuild,
Ram Chandrasekarbdec0f02022-02-24 01:20:17 +0000371 jobs=opts.jobs,
372 local_pkg=opts.reuse_pkgs_from_local_boards,
373 dev_image=opts.withdev,
374 factory_image=opts.withfactory,
375 test_image=opts.withtest,
Alex Klein062d7282022-07-28 09:03:26 -0600376 debug_version=opts.withdebug,
377 backtrack=opts.backtrack,
378 )
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000379 opts.Freeze()
Ram Chandrasekarbdec0f02022-02-24 01:20:17 +0000380 return parser, opts
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000381
382
Cindy Linb7f89a42022-05-23 16:50:00 +0000383@timer.timed('Elapsed time (build_packages)')
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000384def main(argv: Optional[List[str]] = None) -> Optional[int]:
385 commandline.RunInsideChroot()
Ram Chandrasekarbdec0f02022-02-24 01:20:17 +0000386 parser, opts = parse_args(argv)
Ram Chandrasekarbaa89632022-02-11 23:22:09 +0000387
Ram Chandrasekarbdec0f02022-02-24 01:20:17 +0000388 # If the opts.board is not set, then it means user hasn't specified a default
389 # board in 'src/scripts/.default_board' and didn't specify it as input
390 # argument.
391 if not opts.board:
392 parser.error('--board is required')
393
Cindy Lin7487daa2022-02-23 04:14:10 +0000394 build_target = build_target_lib.BuildTarget(
Alex Klein78a4bc02022-04-14 15:59:52 -0600395 opts.board, build_root=opts.sysroot)
396 board_root = sysroot_lib.Sysroot(build_target.root)
Cindy Lin7487daa2022-02-23 04:14:10 +0000397
Cindy Linc06b4ea2022-01-27 18:13:04 +0000398 try:
Cindy Lin317083d2022-03-14 17:07:25 +0000399 # TODO(xcl): Update run_configs to have a common base set of configs for
400 # setup_board and build_packages.
401 if not opts.skip_setup_board:
402 sysroot.SetupBoard(
403 build_target,
404 accept_licenses=opts.accept_licenses,
405 run_configs=opts.setup_board_run_config)
Cindy Lin7487daa2022-02-23 04:14:10 +0000406 sysroot.BuildPackages(build_target, board_root, opts.build_run_config)
407 except sysroot_lib.PackageInstallError as e:
Alex Kleinfba23ba2022-02-03 11:58:48 -0700408 try:
Sergey Frolov7cf6c0b2022-06-06 16:47:44 -0600409 with urllib.request.urlopen(
410 'https://chromiumos-status.appspot.com/current?format=raw'
411 ) as request:
412 logging.notice('Tree Status: %s', request.read().decode())
Alex Kleinfba23ba2022-02-03 11:58:48 -0700413 except urllib.error.HTTPError:
414 pass
Cindy Linc06b4ea2022-01-27 18:13:04 +0000415 cros_build_lib.Die(e)