Cindy Lin | c06b4ea | 2022-01-27 18:13:04 +0000 | [diff] [blame] | 1 | # 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 | |
| 7 | The build_packages process cross compiles all packages that have been |
| 8 | updated into the given sysroot and builds binary packages as a side-effect. |
| 9 | The output packages will be used by the build_image script to create a |
| 10 | bootable Chrome OS image. |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 11 | |
| 12 | If packages are specified in cli, only build those specific packages and any |
| 13 | dependencies they might need. |
| 14 | |
| 15 | For the fastest builds, use --nowithautotest --noworkon. |
Cindy Lin | c06b4ea | 2022-01-27 18:13:04 +0000 | [diff] [blame] | 16 | """ |
| 17 | |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 18 | import argparse |
Alex Klein | fba23ba | 2022-02-03 11:58:48 -0700 | [diff] [blame] | 19 | import logging |
Cindy Lin | c06b4ea | 2022-01-27 18:13:04 +0000 | [diff] [blame] | 20 | import os |
Alex Klein | fba23ba | 2022-02-03 11:58:48 -0700 | [diff] [blame] | 21 | import urllib.error |
| 22 | import urllib.request |
Ram Chandrasekar | bdec0f0 | 2022-02-24 01:20:17 +0000 | [diff] [blame] | 23 | from typing import List, Optional, Tuple |
Cindy Lin | c06b4ea | 2022-01-27 18:13:04 +0000 | [diff] [blame] | 24 | |
Cindy Lin | 7487daa | 2022-02-23 04:14:10 +0000 | [diff] [blame] | 25 | from chromite.lib import build_target_lib |
Alex Klein | a39dc98 | 2022-02-03 12:13:14 -0700 | [diff] [blame] | 26 | from chromite.lib import commandline |
Cindy Lin | c06b4ea | 2022-01-27 18:13:04 +0000 | [diff] [blame] | 27 | from chromite.lib import cros_build_lib |
Cindy Lin | 7487daa | 2022-02-23 04:14:10 +0000 | [diff] [blame] | 28 | from chromite.lib import sysroot_lib |
Ram Chandrasekar | bdec0f0 | 2022-02-24 01:20:17 +0000 | [diff] [blame] | 29 | from chromite.service import sysroot |
Cindy Lin | c06b4ea | 2022-01-27 18:13:04 +0000 | [diff] [blame] | 30 | |
| 31 | |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 32 | def build_shell_bool_style_args(parser: commandline.ArgumentParser, |
| 33 | name: str, |
| 34 | default_val: bool, |
| 35 | help_str: str, |
| 36 | deprecation_note: str, |
| 37 | alternate_name: Optional[str] = None) -> None: |
| 38 | """Build the shell boolean input argument equivalent. |
Alex Klein | a39dc98 | 2022-02-03 12:13:14 -0700 | [diff] [blame] | 39 | |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 40 | There are two cases which we will need to handle, |
| 41 | case 1: A shell boolean arg, which doesn't need to be re-worded in python. |
| 42 | case 2: A shell boolean arg, which needs to be re-worded in python. |
| 43 | Example below. |
| 44 | For Case 1, for a given input arg name 'argA', we create three python |
| 45 | arguments. |
| 46 | --argA, --noargA, --no-argA. The arguments --argA and --no-argA will be |
| 47 | retained after deprecating --noargA. |
| 48 | For Case 2, for a given input arg name 'arg_A' we need to use alternate |
| 49 | argument name 'arg-A'. we create four python arguments in this case. |
| 50 | --arg_A, --noarg_A, --arg-A, --no-arg-A. The first two arguments will be |
| 51 | deprecated later. |
| 52 | TODO(b/218522717): Remove the creation of --noargA in case 1 and --arg_A and |
| 53 | --noarg_A in case 2. |
| 54 | |
| 55 | Args: |
| 56 | parser: The parser to update. |
| 57 | name: The input argument name. This will be used as 'dest' variable name. |
| 58 | default_val: The default value to assign. |
| 59 | help_str: The help string for the input argument. |
| 60 | deprecation_note: A deprecation note to use. |
| 61 | alternate_name: Alternate argument to be used after deprecation. |
| 62 | """ |
| 63 | arg = f'--{name}' |
| 64 | shell_narg = f'--no{name}' |
| 65 | py_narg = f'--no-{name}' |
| 66 | alt_arg = f'--{alternate_name}' if alternate_name else None |
| 67 | alt_py_narg = f'--no-{alternate_name}' if alternate_name else None |
| 68 | default_val_str = f'{help_str} (Default: %(default)s).' |
| 69 | |
| 70 | if alternate_name: |
| 71 | parser.add_argument( |
| 72 | alt_arg, |
| 73 | action='store_true', |
| 74 | default=default_val, |
| 75 | dest=name, |
| 76 | help=default_val_str) |
| 77 | parser.add_argument( |
| 78 | alt_py_narg, |
| 79 | action='store_false', |
| 80 | dest=name, |
| 81 | help="Don't " + help_str.lower()) |
| 82 | |
| 83 | parser.add_argument( |
| 84 | arg, |
| 85 | action='store_true', |
| 86 | default=default_val, |
| 87 | dest=name, |
| 88 | deprecated=deprecation_note % alt_arg if alternate_name else None, |
| 89 | help=default_val_str if not alternate_name else argparse.SUPPRESS) |
| 90 | parser.add_argument( |
| 91 | shell_narg, |
| 92 | action='store_false', |
| 93 | dest=name, |
| 94 | deprecated=deprecation_note % |
| 95 | (alt_py_narg if alternate_name else py_narg), |
| 96 | 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 | |
| 106 | def 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 | |
| 115 | # TODO(rchandrasekar): Check if the board input is set. |
| 116 | # Don't proceed if not set. |
| 117 | parser.add_argument( |
| 118 | '--board', |
| 119 | default=cros_build_lib.GetDefaultBoard(), |
| 120 | help='The board to build packages for.') |
| 121 | |
| 122 | build_shell_bool_style_args( |
| 123 | parser, 'usepkg', True, 'Use binary packages to bootstrap when possible.', |
| 124 | deprecation_note) |
| 125 | build_shell_bool_style_args( |
| 126 | parser, 'usepkgonly', False, |
| 127 | 'Use binary packages only to bootstrap; abort if any are missing.', |
| 128 | deprecation_note) |
| 129 | build_shell_bool_style_args(parser, 'workon', True, |
| 130 | 'Force-build workon packages.', deprecation_note) |
| 131 | build_shell_bool_style_args(parser, 'showoutput', False, |
| 132 | 'Show all output from parallel_emerge.', |
| 133 | deprecation_note) |
| 134 | build_shell_bool_style_args( |
| 135 | parser, 'withrevdeps', True, |
| 136 | 'Calculate reverse dependencies on changed ebuilds.', deprecation_note) |
| 137 | build_shell_bool_style_args( |
| 138 | parser, 'cleanbuild', False, |
| 139 | 'Perform a clean build; delete sysroot if it exists before building.', |
| 140 | deprecation_note) |
| 141 | build_shell_bool_style_args( |
| 142 | parser, 'pretend', False, |
| 143 | 'Pretend building packages, just display which packages would have ' |
| 144 | 'been installed.', deprecation_note) |
| 145 | |
| 146 | # The --sysroot flag specifies the environment variables ROOT and PKGDIR. |
| 147 | # This allows fetching and emerging of all packages to specified sysroot. |
| 148 | # Note that --sysroot will setup the board normally in /build/$BOARD, if |
| 149 | # it's not setup yet. It also expects the toolchain to already be installed |
| 150 | # in the sysroot. |
| 151 | # --usepkgonly and --norebuild are required, because building is not |
| 152 | # supported when board_root is set. |
| 153 | parser.add_argument( |
| 154 | '--sysroot', type='path', help='Emerge packages to sysroot.') |
| 155 | parser.add_argument( |
| 156 | '--board_root', |
| 157 | type='path', |
| 158 | dest='sysroot', |
| 159 | deprecated=deprecation_note % '--sysroot', |
| 160 | help=argparse.SUPPRESS) |
| 161 | |
| 162 | # CPU Governor related options. |
| 163 | group = parser.add_argument_group('CPU Governor Options') |
| 164 | build_shell_bool_style_args( |
| 165 | group, 'autosetgov', False, |
| 166 | "Automatically set cpu governor to 'performance'.", deprecation_note) |
| 167 | build_shell_bool_style_args( |
| 168 | group, |
| 169 | 'autosetgov_sticky', |
| 170 | False, |
| 171 | 'Remember --autosetgov setting for future runs.', |
| 172 | deprecation_note, |
| 173 | alternate_name='autosetgov-sticky') |
| 174 | |
| 175 | # Chrome building related options. |
| 176 | group = parser.add_argument_group('Chrome Options') |
| 177 | build_shell_bool_style_args( |
| 178 | group, |
| 179 | 'use_any_chrome', |
| 180 | True, |
| 181 | "Use any Chrome prebuilt available, even if the prebuilt doesn't " |
| 182 | 'match exactly.', |
| 183 | deprecation_note, |
| 184 | alternate_name='use-any-chrome') |
| 185 | build_shell_bool_style_args( |
| 186 | group, 'internal', False, |
| 187 | 'Build the internal version of chrome(set the chrome_internal USE flag).', |
| 188 | deprecation_note) |
| 189 | build_shell_bool_style_args( |
| 190 | group, 'chrome', False, 'Ensure chrome instead of chromium. Alias for ' |
| 191 | '--internal --no-use-any-chrome.', deprecation_note) |
| 192 | |
| 193 | # Setup board related options. |
| 194 | group = parser.add_argument_group('Setup Board Config Options') |
| 195 | build_shell_bool_style_args( |
| 196 | group, |
| 197 | 'skip_chroot_upgrade', |
| 198 | False, |
| 199 | 'Skip the automatic chroot upgrade; use with care.', |
| 200 | deprecation_note, |
| 201 | alternate_name='skip-chroot-upgrade') |
| 202 | build_shell_bool_style_args( |
| 203 | group, |
| 204 | 'skip_toolchain_update', |
| 205 | False, |
| 206 | 'Skip automatic toolchain update', |
| 207 | deprecation_note, |
| 208 | alternate_name='skip-toolchain-update') |
| 209 | build_shell_bool_style_args( |
| 210 | group, |
| 211 | 'skip_setup_board', |
| 212 | False, |
| 213 | 'Skip running setup_board. Implies ' |
| 214 | '--skip-chroot-upgrade --skip-toolchain-update.', |
| 215 | deprecation_note, |
| 216 | alternate_name='skip-setup-board') |
| 217 | |
| 218 | # Image Type selection related options. |
| 219 | group = parser.add_argument_group('Image Type Options') |
| 220 | build_shell_bool_style_args(group, 'withdev', True, |
| 221 | 'Build useful developer friendly utilities.', |
| 222 | deprecation_note) |
| 223 | build_shell_bool_style_args( |
| 224 | group, 'withdebug', True, |
| 225 | 'Build debug versions of Chromium-OS-specific packages.', |
| 226 | deprecation_note) |
| 227 | build_shell_bool_style_args(group, 'withfactory', True, |
| 228 | 'Build factory installer.', deprecation_note) |
| 229 | build_shell_bool_style_args(group, 'withtest', True, |
| 230 | 'Build packages required for testing.', |
| 231 | deprecation_note) |
| 232 | build_shell_bool_style_args(group, 'withautotest', True, |
| 233 | 'Build autotest client code.', deprecation_note) |
| 234 | build_shell_bool_style_args(group, 'withdebugsymbols', False, |
| 235 | 'Install the debug symbols for all packages.', |
| 236 | deprecation_note) |
| 237 | |
| 238 | # Advanced Options. |
| 239 | group = parser.add_argument_group('Advanced Options') |
| 240 | group.add_argument( |
| 241 | '--accept-licenses', help='Licenses to append to the accept list.') |
| 242 | group.add_argument( |
| 243 | '--accept_licenses', |
| 244 | deprecated=deprecation_note % '--accept-licenses', |
| 245 | help=argparse.SUPPRESS) |
| 246 | build_shell_bool_style_args(group, 'eclean', True, |
| 247 | 'Run eclean to delete old binpkgs.', |
| 248 | deprecation_note) |
| 249 | group.add_argument( |
| 250 | '--jobs', |
| 251 | type=int, |
| 252 | default=os.cpu_count(), |
| 253 | help='Number of packages to build in parallel. ' |
| 254 | '(Default: %(default)s)') |
| 255 | build_shell_bool_style_args(group, 'rebuild', True, |
| 256 | 'Automatically rebuild dependencies.', |
| 257 | deprecation_note) |
| 258 | # TODO(b/218522717): Remove the --nonorebuild argument support. |
| 259 | group.add_argument( |
| 260 | '--nonorebuild', |
| 261 | action='store_true', |
| 262 | dest='rebuild', |
| 263 | deprecated=deprecation_note % '--rebuild', |
| 264 | help=argparse.SUPPRESS) |
| 265 | build_shell_bool_style_args(group, 'expandedbinhosts', True, |
| 266 | 'Allow expanded binhost inheritance.', |
| 267 | deprecation_note) |
| 268 | # The --reuse-pkgs-from-local-boards flag tells Portage to share binary |
| 269 | # packages between boards that are built locally, so that the total time |
| 270 | # required to build several boards is reduced. This flag is only useful |
| 271 | # when you are not able to use remote binary packages, since remote binary |
| 272 | # packages are usually more up to date than anything you have locally. |
| 273 | build_shell_bool_style_args( |
| 274 | group, |
| 275 | 'reuse_pkgs_from_local_boards', |
| 276 | False, |
| 277 | 'Bootstrap from local packages instead of remote packages.', |
| 278 | deprecation_note, |
| 279 | alternate_name='reuse-pkgs-from-local-boards') |
| 280 | |
| 281 | # --run-goma option is designed to be used on bots. |
| 282 | # If you're trying to build packages with goma in your local dev env, this is |
| 283 | # *not* the option you're looking for. Please see comments below. |
| 284 | # This option; 1) starts goma, 2) builds packages (expecting that goma is |
| 285 | # used), then 3) stops goma explicitly. |
| 286 | # 4) is a request from the goma team, so that stats/logs can be taken. |
| 287 | # Note: GOMA_DIR and GOMA_SERVICE_ACCOUNT_JSON_FILE are expected to be passed |
| 288 | # via env var. |
| 289 | # |
| 290 | # In local dev env cases, compiler_proxy is expected to keep running. |
| 291 | # In such a case; |
| 292 | # $ python ${GOMA_DIR}/goma_ctl.py ensure_start |
| 293 | # $ ./build_packages (... and options without --run-goma ...) |
| 294 | # is an expected commandline sequence. If you set --run-goma flag while |
| 295 | # compiler_proxy is already running, the existing compiler_proxy will be |
| 296 | # stopped. |
| 297 | # |
| 298 | # TODO(rchandrasekar): Use of env variable as input is not right. Added |
| 299 | # options to provide them as input arguments. Migrate users to use these |
| 300 | # options and deprecate the '--run-goma' arg. |
| 301 | build_shell_bool_style_args( |
| 302 | group, |
| 303 | 'run_goma', |
| 304 | False, |
| 305 | 'If set to true, (re)starts goma, builds packages, and then stops goma..', |
| 306 | deprecation_note, |
| 307 | alternate_name='run-goma') |
| 308 | group.add_argument('--goma-dir', type='path', help='Goma directory path.') |
| 309 | group.add_argument( |
| 310 | '--goma-cfg', type='path', help='Goma service account json file to use.') |
| 311 | # This option is for building chrome remotely. |
| 312 | # 1) starts reproxy 2) builds chrome with reproxy and 3) stops reproxy so |
| 313 | # logs/stats can be collected. |
| 314 | # Note: RECLIENT_DIR and REPROXY_CFG env var will be deprecated July, 2022. |
| 315 | # Use --reclient-dir and --reproxy-cfg input options instead. |
| 316 | # |
| 317 | # TODO(rchandrasekar): Use of env variable as input is not right. Added |
| 318 | # options to provide them as input arguments. Migrate users to use these |
| 319 | # options and deprecate the '--run-remoteexec' arg. |
| 320 | build_shell_bool_style_args( |
| 321 | group, 'run_remoteexec', False, |
| 322 | 'If set to true, starts RBE reproxy, builds packages, and then stops ' |
| 323 | 'reproxy.', deprecation_note) |
| 324 | group.add_argument('--reclient-dir', type='path', help='Reproxy client path.') |
| 325 | group.add_argument( |
| 326 | '--reproxy-cfg', type='path', help='Reproxy config file to use.') |
| 327 | |
| 328 | parser.add_argument('packages', nargs='*', help='Packages to build.') |
| 329 | return parser |
| 330 | |
| 331 | |
Ram Chandrasekar | bdec0f0 | 2022-02-24 01:20:17 +0000 | [diff] [blame] | 332 | def parse_args(argv: List[str]) -> Tuple[commandline.ArgumentParser, |
| 333 | commandline.ArgumentNamespace]: |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 334 | """Parse and validate CLI arguments. |
| 335 | |
| 336 | Args: |
| 337 | argv: Arguments passed via CLI. |
| 338 | |
| 339 | Returns: |
Ram Chandrasekar | bdec0f0 | 2022-02-24 01:20:17 +0000 | [diff] [blame] | 340 | Tuple having the below two, |
| 341 | Argument Parser |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 342 | Validated argument namespace. |
| 343 | """ |
| 344 | parser = get_parser() |
| 345 | opts = parser.parse_args(argv) |
Ram Chandrasekar | bdec0f0 | 2022-02-24 01:20:17 +0000 | [diff] [blame] | 346 | |
| 347 | if opts.chrome: |
| 348 | opts.internal_chrome = True |
| 349 | opts.use_any_chrome = False |
| 350 | |
| 351 | opts.build_run_config = sysroot.BuildPackagesRunConfig( |
| 352 | usepkg=opts.usepkg, |
| 353 | install_debug_symbols=opts.withdebugsymbols, |
| 354 | packages=opts.packages, |
| 355 | use_goma=opts.run_goma, |
| 356 | use_remoteexec=opts.run_remoteexec, |
| 357 | incremental_build=opts.withrevdeps, |
| 358 | expanded_binhosts=opts.expandedbinhosts, |
| 359 | setup_board=not opts.skip_setup_board, |
| 360 | dryrun=opts.pretend, |
| 361 | usepkgonly=opts.usepkgonly, |
| 362 | workon=opts.workon, |
| 363 | verbose=opts.showoutput, |
| 364 | install_auto_test=opts.withautotest, |
| 365 | autosetgov=opts.autosetgov, |
| 366 | autosetgov_sticky=opts.autosetgov_sticky, |
| 367 | use_any_chrome=opts.use_any_chrome, |
| 368 | internal_chrome=opts.internal, |
| 369 | clean_build=opts.cleanbuild, |
| 370 | eclean=opts.eclean, |
| 371 | rebuild_dep=opts.rebuild, |
| 372 | accept_licenses=opts.accept_licenses, |
| 373 | jobs=opts.jobs, |
| 374 | local_pkg=opts.reuse_pkgs_from_local_boards, |
| 375 | dev_image=opts.withdev, |
| 376 | factory_image=opts.withfactory, |
| 377 | test_image=opts.withtest, |
| 378 | debug_version=opts.withdebug, |
| 379 | update_toolchain=not opts.skip_toolchain_update, |
| 380 | upgrade_chroot=not opts.skip_chroot_upgrade) |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 381 | opts.Freeze() |
Ram Chandrasekar | bdec0f0 | 2022-02-24 01:20:17 +0000 | [diff] [blame] | 382 | return parser, opts |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 383 | |
| 384 | |
| 385 | def main(argv: Optional[List[str]] = None) -> Optional[int]: |
| 386 | commandline.RunInsideChroot() |
Ram Chandrasekar | bdec0f0 | 2022-02-24 01:20:17 +0000 | [diff] [blame] | 387 | parser, opts = parse_args(argv) |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 388 | |
Ram Chandrasekar | bdec0f0 | 2022-02-24 01:20:17 +0000 | [diff] [blame] | 389 | # If the opts.board is not set, then it means user hasn't specified a default |
| 390 | # board in 'src/scripts/.default_board' and didn't specify it as input |
| 391 | # argument. |
| 392 | if not opts.board: |
| 393 | parser.error('--board is required') |
| 394 | |
Cindy Lin | 7487daa | 2022-02-23 04:14:10 +0000 | [diff] [blame] | 395 | sysroot_path = opts.sysroot if opts.sysroot else f'/build/{opts.board}' |
| 396 | build_target = build_target_lib.BuildTarget( |
| 397 | opts.board, build_root=sysroot_path) |
| 398 | board_root = sysroot_lib.Sysroot(sysroot_path) |
| 399 | |
Cindy Lin | c06b4ea | 2022-01-27 18:13:04 +0000 | [diff] [blame] | 400 | try: |
Cindy Lin | 7487daa | 2022-02-23 04:14:10 +0000 | [diff] [blame] | 401 | # TODO(xcl): Update to conditionally call sysroot.SetupBoard here instead |
| 402 | # of having sysroot.BuildPackages call SetupBoard. Also update run_configs |
| 403 | # to remove duplicated options and refactor to have a common base set of |
| 404 | # configs. |
| 405 | sysroot.BuildPackages(build_target, board_root, opts.build_run_config) |
| 406 | except sysroot_lib.PackageInstallError as e: |
Alex Klein | fba23ba | 2022-02-03 11:58:48 -0700 | [diff] [blame] | 407 | try: |
| 408 | request = urllib.request.urlopen( |
| 409 | 'https://chromiumos-status.appspot.com/current?format=raw') |
| 410 | logging.notice('Tree Status: %s', request.read().decode()) |
| 411 | except urllib.error.HTTPError: |
| 412 | pass |
Cindy Lin | c06b4ea | 2022-01-27 18:13:04 +0000 | [diff] [blame] | 413 | cros_build_lib.Die(e) |