Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2022 The ChromiumOS Authors |
Cindy Lin | c06b4ea | 2022-01-27 18:13:04 +0000 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Jack Rosenthal | 769a051 | 2023-05-25 18:04:50 -0600 | [diff] [blame] | 5 | """build_packages updates the set of binary packages needed by ChromiumOS. |
Cindy Lin | c06b4ea | 2022-01-27 18:13:04 +0000 | [diff] [blame] | 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 |
Jack Rosenthal | 769a051 | 2023-05-25 18:04:50 -0600 | [diff] [blame] | 10 | bootable ChromiumOS image. |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 11 | |
Jack Rosenthal | 769a051 | 2023-05-25 18:04:50 -0600 | [diff] [blame] | 12 | If packages are specified in the command line, only build those specific |
| 13 | packages and any dependencies they might need. |
Cindy Lin | c06b4ea | 2022-01-27 18:13:04 +0000 | [diff] [blame] | 14 | """ |
| 15 | |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 16 | import argparse |
Alex Klein | fba23ba | 2022-02-03 11:58:48 -0700 | [diff] [blame] | 17 | import logging |
Cindy Lin | c06b4ea | 2022-01-27 18:13:04 +0000 | [diff] [blame] | 18 | import os |
Mike Frysinger | 807d828 | 2022-04-28 22:45:17 -0400 | [diff] [blame] | 19 | from typing import List, Optional, Tuple |
Alex Klein | fba23ba | 2022-02-03 11:58:48 -0700 | [diff] [blame] | 20 | import urllib.error |
| 21 | import urllib.request |
Cindy Lin | c06b4ea | 2022-01-27 18:13:04 +0000 | [diff] [blame] | 22 | |
Lizzy Presland | 453237c | 2023-05-31 00:16:15 +0000 | [diff] [blame] | 23 | from chromite.third_party.opentelemetry import trace |
Anuj Jamwal | 50db51b | 2023-06-12 17:03:53 +0000 | [diff] [blame] | 24 | from chromite.third_party.opentelemetry.trace import status |
Lizzy Presland | 453237c | 2023-05-31 00:16:15 +0000 | [diff] [blame] | 25 | |
Cindy Lin | 7487daa | 2022-02-23 04:14:10 +0000 | [diff] [blame] | 26 | from chromite.lib import build_target_lib |
Alex Klein | a39dc98 | 2022-02-03 12:13:14 -0700 | [diff] [blame] | 27 | from chromite.lib import commandline |
Cindy Lin | c06b4ea | 2022-01-27 18:13:04 +0000 | [diff] [blame] | 28 | from chromite.lib import cros_build_lib |
Cindy Lin | 7487daa | 2022-02-23 04:14:10 +0000 | [diff] [blame] | 29 | from chromite.lib import sysroot_lib |
Anuj Jamwal | 50db51b | 2023-06-12 17:03:53 +0000 | [diff] [blame] | 30 | from chromite.lib import workon_helper |
Ram Chandrasekar | bdec0f0 | 2022-02-24 01:20:17 +0000 | [diff] [blame] | 31 | from chromite.service import sysroot |
Cindy Lin | b7f89a4 | 2022-05-23 16:50:00 +0000 | [diff] [blame] | 32 | from chromite.utils import timer |
Cindy Lin | c06b4ea | 2022-01-27 18:13:04 +0000 | [diff] [blame] | 33 | |
| 34 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 35 | def build_shell_bool_style_args( |
| 36 | parser: commandline.ArgumentParser, |
| 37 | name: str, |
| 38 | default_val: bool, |
| 39 | help_str: str, |
| 40 | deprecation_note: str, |
| 41 | alternate_name: Optional[str] = None, |
| 42 | ) -> None: |
| 43 | """Build the shell boolean input argument equivalent. |
Alex Klein | a39dc98 | 2022-02-03 12:13:14 -0700 | [diff] [blame] | 44 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 45 | There are two cases which we will need to handle, |
| 46 | case 1: A shell boolean arg, which doesn't need to be re-worded in python. |
| 47 | case 2: A shell boolean arg, which needs to be re-worded in python. |
| 48 | Example below. |
| 49 | For Case 1, for a given input arg name 'argA', we create three python |
| 50 | arguments. |
| 51 | --argA, --noargA, --no-argA. The arguments --argA and --no-argA will be |
| 52 | retained after deprecating --noargA. |
| 53 | For Case 2, for a given input arg name 'arg_A' we need to use alternate |
| 54 | argument name 'arg-A'. we create four python arguments in this case. |
| 55 | --arg_A, --noarg_A, --arg-A, --no-arg-A. The first two arguments will be |
| 56 | deprecated later. |
| 57 | TODO(b/218522717): Remove the creation of --noargA in case 1 and --arg_A and |
| 58 | --noarg_A in case 2. |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 59 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 60 | Args: |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 61 | parser: The parser to update. |
| 62 | name: The input argument name. This will be used as 'dest' variable |
| 63 | name. |
| 64 | default_val: The default value to assign. |
| 65 | help_str: The help string for the input argument. |
| 66 | deprecation_note: A deprecation note to use. |
| 67 | alternate_name: Alternate argument to be used after deprecation. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 68 | """ |
| 69 | arg = f"--{name}" |
| 70 | shell_narg = f"--no{name}" |
| 71 | py_narg = f"--no-{name}" |
| 72 | alt_arg = f"--{alternate_name}" if alternate_name else None |
| 73 | alt_py_narg = f"--no-{alternate_name}" if alternate_name else None |
| 74 | default_val_str = f"{help_str} (Default: %(default)s)." |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 75 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 76 | if alternate_name: |
| 77 | parser.add_argument( |
| 78 | alt_arg, |
| 79 | action="store_true", |
| 80 | default=default_val, |
| 81 | dest=name, |
| 82 | help=default_val_str, |
| 83 | ) |
| 84 | parser.add_argument( |
| 85 | alt_py_narg, |
| 86 | action="store_false", |
| 87 | dest=name, |
| 88 | help="Don't " + help_str.lower(), |
| 89 | ) |
| 90 | |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 91 | parser.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 92 | arg, |
| 93 | action="store_true", |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 94 | default=default_val, |
| 95 | dest=name, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 96 | deprecated=deprecation_note % alt_arg if alternate_name else None, |
| 97 | help=default_val_str if not alternate_name else argparse.SUPPRESS, |
| 98 | ) |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 99 | parser.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 100 | shell_narg, |
| 101 | action="store_false", |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 102 | dest=name, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 103 | deprecated=deprecation_note % alt_py_narg |
| 104 | if alternate_name |
| 105 | else py_narg, |
| 106 | help=argparse.SUPPRESS, |
| 107 | ) |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 108 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 109 | if not alternate_name: |
| 110 | parser.add_argument( |
| 111 | py_narg, |
| 112 | action="store_false", |
| 113 | dest=name, |
| 114 | help="Don't " + help_str.lower(), |
| 115 | ) |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 116 | |
| 117 | |
| 118 | def get_parser() -> commandline.ArgumentParser: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 119 | """Creates the cmdline argparser, populates the options and description. |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 120 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 121 | Returns: |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 122 | Argument parser. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 123 | """ |
| 124 | deprecation_note = "Argument will be removed July, 2022. Use %s instead." |
| 125 | parser = commandline.ArgumentParser(description=__doc__) |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 126 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 127 | parser.add_argument( |
| 128 | "-b", |
| 129 | "--board", |
| 130 | "--build-target", |
| 131 | dest="board", |
| 132 | default=cros_build_lib.GetDefaultBoard(), |
| 133 | help="The board to build packages for.", |
| 134 | ) |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 135 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 136 | build_shell_bool_style_args( |
| 137 | parser, |
| 138 | "usepkg", |
| 139 | True, |
| 140 | "Use binary packages to bootstrap when possible.", |
| 141 | deprecation_note, |
| 142 | ) |
| 143 | build_shell_bool_style_args( |
| 144 | parser, |
| 145 | "usepkgonly", |
| 146 | False, |
| 147 | "Use binary packages only to bootstrap; abort if any are missing.", |
| 148 | deprecation_note, |
| 149 | ) |
| 150 | build_shell_bool_style_args( |
| 151 | parser, "workon", True, "Force-build workon packages.", deprecation_note |
| 152 | ) |
| 153 | build_shell_bool_style_args( |
| 154 | parser, |
| 155 | "withrevdeps", |
| 156 | True, |
| 157 | "Calculate reverse dependencies on changed ebuilds.", |
| 158 | deprecation_note, |
| 159 | ) |
| 160 | build_shell_bool_style_args( |
| 161 | parser, |
| 162 | "cleanbuild", |
| 163 | False, |
| 164 | "Perform a clean build; delete sysroot if it exists before building.", |
| 165 | deprecation_note, |
| 166 | ) |
| 167 | build_shell_bool_style_args( |
| 168 | parser, |
| 169 | "pretend", |
| 170 | False, |
| 171 | "Pretend building packages, just display which packages would have " |
| 172 | "been installed.", |
| 173 | deprecation_note, |
| 174 | ) |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 175 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 176 | # The --sysroot flag specifies the environment variables ROOT and PKGDIR. |
| 177 | # This allows fetching and emerging of all packages to specified sysroot. |
| 178 | # Note that --sysroot will setup the board normally in /build/$BOARD, if |
| 179 | # it's not setup yet. It also expects the toolchain to already be installed |
| 180 | # in the sysroot. |
| 181 | # --usepkgonly and --norebuild are required, because building is not |
| 182 | # supported when board_root is set. |
| 183 | parser.add_argument( |
| 184 | "--sysroot", type="path", help="Emerge packages to sysroot." |
| 185 | ) |
| 186 | parser.add_argument( |
| 187 | "--board_root", |
| 188 | type="path", |
| 189 | dest="sysroot", |
| 190 | deprecated=deprecation_note % "--sysroot", |
| 191 | help=argparse.SUPPRESS, |
| 192 | ) |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 193 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 194 | # CPU Governor related options. |
| 195 | group = parser.add_argument_group("CPU Governor Options") |
| 196 | build_shell_bool_style_args( |
| 197 | group, |
| 198 | "autosetgov", |
| 199 | False, |
| 200 | "Automatically set cpu governor to 'performance'.", |
| 201 | deprecation_note, |
| 202 | ) |
| 203 | build_shell_bool_style_args( |
| 204 | group, |
| 205 | "autosetgov_sticky", |
| 206 | False, |
| 207 | "Remember --autosetgov setting for future runs.", |
| 208 | deprecation_note, |
| 209 | alternate_name="autosetgov-sticky", |
| 210 | ) |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 211 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 212 | # Chrome building related options. |
| 213 | group = parser.add_argument_group("Chrome Options") |
| 214 | build_shell_bool_style_args( |
| 215 | group, |
| 216 | "use_any_chrome", |
| 217 | True, |
| 218 | "Use any Chrome prebuilt available, even if the prebuilt doesn't " |
| 219 | "match exactly.", |
| 220 | deprecation_note, |
| 221 | alternate_name="use-any-chrome", |
| 222 | ) |
| 223 | build_shell_bool_style_args( |
| 224 | group, |
| 225 | "internal", |
| 226 | False, |
Alex Klein | d719740 | 2023-04-05 13:05:29 -0600 | [diff] [blame] | 227 | "Build the internal version of chrome (set the chrome_internal USE " |
| 228 | "flag).", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 229 | deprecation_note, |
| 230 | ) |
| 231 | build_shell_bool_style_args( |
| 232 | group, |
| 233 | "chrome", |
| 234 | False, |
| 235 | "Ensure chrome instead of chromium. Alias for " |
| 236 | "--internal --no-use-any-chrome.", |
| 237 | deprecation_note, |
| 238 | ) |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 239 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 240 | # Setup board related options. |
| 241 | group = parser.add_argument_group("Setup Board Config Options") |
| 242 | build_shell_bool_style_args( |
| 243 | group, |
| 244 | "skip_chroot_upgrade", |
| 245 | False, |
| 246 | "Skip the automatic chroot upgrade; use with care.", |
| 247 | deprecation_note, |
| 248 | alternate_name="skip-chroot-upgrade", |
| 249 | ) |
| 250 | build_shell_bool_style_args( |
| 251 | group, |
| 252 | "skip_toolchain_update", |
| 253 | False, |
| 254 | "Skip automatic toolchain update", |
| 255 | deprecation_note, |
| 256 | alternate_name="skip-toolchain-update", |
| 257 | ) |
| 258 | build_shell_bool_style_args( |
| 259 | group, |
| 260 | "skip_setup_board", |
| 261 | False, |
| 262 | "Skip running setup_board. Implies " |
| 263 | "--skip-chroot-upgrade --skip-toolchain-update.", |
| 264 | deprecation_note, |
| 265 | alternate_name="skip-setup-board", |
| 266 | ) |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 267 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 268 | # Image Type selection related options. |
| 269 | group = parser.add_argument_group("Image Type Options") |
| 270 | build_shell_bool_style_args( |
| 271 | group, |
| 272 | "withdev", |
| 273 | True, |
| 274 | "Build useful developer friendly utilities.", |
| 275 | deprecation_note, |
| 276 | ) |
| 277 | build_shell_bool_style_args( |
| 278 | group, |
| 279 | "withdebug", |
| 280 | True, |
| 281 | "Build debug versions of Chromium-OS-specific packages.", |
| 282 | deprecation_note, |
| 283 | ) |
| 284 | build_shell_bool_style_args( |
| 285 | group, "withfactory", True, "Build factory installer.", deprecation_note |
| 286 | ) |
| 287 | build_shell_bool_style_args( |
| 288 | group, |
| 289 | "withtest", |
| 290 | True, |
| 291 | "Build packages required for testing.", |
| 292 | deprecation_note, |
| 293 | ) |
| 294 | build_shell_bool_style_args( |
| 295 | group, |
| 296 | "withautotest", |
| 297 | True, |
| 298 | "Build autotest client code.", |
| 299 | deprecation_note, |
| 300 | ) |
| 301 | build_shell_bool_style_args( |
| 302 | group, |
| 303 | "withdebugsymbols", |
| 304 | False, |
| 305 | "Install the debug symbols for all packages.", |
| 306 | deprecation_note, |
| 307 | ) |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 308 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 309 | # Advanced Options. |
| 310 | group = parser.add_argument_group("Advanced Options") |
| 311 | group.add_argument( |
| 312 | "--accept-licenses", help="Licenses to append to the accept list." |
| 313 | ) |
| 314 | group.add_argument( |
| 315 | "--accept_licenses", |
| 316 | deprecated=deprecation_note % "--accept-licenses", |
| 317 | help=argparse.SUPPRESS, |
| 318 | ) |
| 319 | build_shell_bool_style_args( |
| 320 | group, |
| 321 | "eclean", |
| 322 | True, |
| 323 | "Run eclean to delete old binpkgs.", |
| 324 | deprecation_note, |
| 325 | ) |
| 326 | group.add_argument( |
| 327 | "--jobs", |
| 328 | type=int, |
| 329 | default=os.cpu_count(), |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 330 | help="Number of packages to build in parallel. (Default: %(default)s)", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 331 | ) |
| 332 | build_shell_bool_style_args( |
| 333 | group, |
| 334 | "rebuild", |
| 335 | True, |
| 336 | "Automatically rebuild dependencies.", |
| 337 | deprecation_note, |
| 338 | ) |
| 339 | # TODO(b/218522717): Remove the --nonorebuild argument support. |
| 340 | group.add_argument( |
| 341 | "--nonorebuild", |
| 342 | action="store_true", |
| 343 | dest="rebuild", |
| 344 | deprecated=deprecation_note % "--rebuild", |
| 345 | help=argparse.SUPPRESS, |
| 346 | ) |
| 347 | build_shell_bool_style_args( |
| 348 | group, |
| 349 | "expandedbinhosts", |
| 350 | True, |
| 351 | "Allow expanded binhost inheritance.", |
| 352 | deprecation_note, |
| 353 | ) |
| 354 | group.add_argument( |
| 355 | "--backtrack", |
| 356 | type=int, |
| 357 | default=sysroot.BACKTRACK_DEFAULT, |
| 358 | help="See emerge --backtrack.", |
| 359 | ) |
Alex Klein | 062d728 | 2022-07-28 09:03:26 -0600 | [diff] [blame] | 360 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 361 | # The --reuse-pkgs-from-local-boards flag tells Portage to share binary |
| 362 | # packages between boards that are built locally, so that the total time |
| 363 | # required to build several boards is reduced. This flag is only useful |
| 364 | # when you are not able to use remote binary packages, since remote binary |
| 365 | # packages are usually more up to date than anything you have locally. |
| 366 | build_shell_bool_style_args( |
| 367 | group, |
| 368 | "reuse_pkgs_from_local_boards", |
| 369 | False, |
| 370 | "Bootstrap from local packages instead of remote packages.", |
| 371 | deprecation_note, |
| 372 | alternate_name="reuse-pkgs-from-local-boards", |
| 373 | ) |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 374 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 375 | # --run-goma option is designed to be used on bots. |
Alex Klein | d719740 | 2023-04-05 13:05:29 -0600 | [diff] [blame] | 376 | # If you're trying to build packages with goma in your local dev env, this |
| 377 | # is *not* the option you're looking for. Please see comments below. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 378 | # This option; 1) starts goma, 2) builds packages (expecting that goma is |
| 379 | # used), then 3) stops goma explicitly. |
| 380 | # 4) is a request from the goma team, so that stats/logs can be taken. |
Fumitoshi Ukai | 00becd4 | 2023-01-13 12:51:26 +0900 | [diff] [blame] | 381 | # Note: GOMA_DIR is expected to be passed via env var. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 382 | # |
| 383 | # In local dev env cases, compiler_proxy is expected to keep running. |
| 384 | # In such a case; |
| 385 | # $ python ${GOMA_DIR}/goma_ctl.py ensure_start |
| 386 | # $ build_packages (... and options without --run-goma ...) |
| 387 | # is an expected commandline sequence. If you set --run-goma flag while |
| 388 | # compiler_proxy is already running, the existing compiler_proxy will be |
| 389 | # stopped. |
| 390 | build_shell_bool_style_args( |
| 391 | group, |
| 392 | "run_goma", |
| 393 | False, |
Alex Klein | d719740 | 2023-04-05 13:05:29 -0600 | [diff] [blame] | 394 | "When set, (re)starts goma, builds packages, and then stops goma.", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 395 | deprecation_note, |
| 396 | alternate_name="run-goma", |
| 397 | ) |
| 398 | # This option is for building chrome remotely. |
| 399 | # 1) starts reproxy 2) builds chrome with reproxy and 3) stops reproxy so |
| 400 | # logs/stats can be collected. |
| 401 | # Note: RECLIENT_DIR and REPROXY_CFG env var will be deprecated July, 2022. |
| 402 | # Use --reclient-dir and --reproxy-cfg input options instead. |
| 403 | build_shell_bool_style_args( |
| 404 | group, |
| 405 | "run_remoteexec", |
| 406 | False, |
| 407 | "If set to true, starts RBE reproxy, builds packages, and then stops " |
| 408 | "reproxy.", |
| 409 | deprecation_note, |
| 410 | ) |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 411 | |
Ryo Hashimoto | 0845ebf | 2023-06-06 19:21:13 +0900 | [diff] [blame^] | 412 | build_shell_bool_style_args( |
| 413 | group, |
| 414 | "bazel", |
| 415 | False, |
| 416 | "Use Bazel to build packages.", |
| 417 | deprecation_note, |
| 418 | ) |
| 419 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 420 | parser.add_argument("packages", nargs="*", help="Packages to build.") |
| 421 | return parser |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 422 | |
| 423 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 424 | def parse_args( |
| 425 | argv: List[str], |
| 426 | ) -> Tuple[commandline.ArgumentParser, commandline.ArgumentNamespace]: |
| 427 | """Parse and validate CLI arguments. |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 428 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 429 | Args: |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 430 | argv: Arguments passed via CLI. |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 431 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 432 | Returns: |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 433 | Tuple having the below two, |
| 434 | Argument Parser |
| 435 | Validated argument namespace. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 436 | """ |
| 437 | parser = get_parser() |
| 438 | opts = parser.parse_args(argv) |
Ram Chandrasekar | bdec0f0 | 2022-02-24 01:20:17 +0000 | [diff] [blame] | 439 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 440 | if opts.chrome: |
| 441 | opts.internal_chrome = True |
| 442 | opts.use_any_chrome = False |
Ram Chandrasekar | bdec0f0 | 2022-02-24 01:20:17 +0000 | [diff] [blame] | 443 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 444 | opts.setup_board_run_config = sysroot.SetupBoardRunConfig( |
| 445 | force=opts.cleanbuild, |
| 446 | usepkg=opts.usepkg, |
| 447 | jobs=opts.jobs, |
| 448 | quiet=True, |
| 449 | update_toolchain=not opts.skip_toolchain_update, |
| 450 | upgrade_chroot=not opts.skip_chroot_upgrade, |
| 451 | local_build=opts.reuse_pkgs_from_local_boards, |
| 452 | expanded_binhost_inheritance=opts.expandedbinhosts, |
Cindy Lin | adc610a | 2023-05-23 18:46:12 +0000 | [diff] [blame] | 453 | use_cq_prebuilts=opts.usepkg, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 454 | backtrack=opts.backtrack, |
| 455 | ) |
| 456 | opts.build_run_config = sysroot.BuildPackagesRunConfig( |
| 457 | usepkg=opts.usepkg, |
| 458 | install_debug_symbols=opts.withdebugsymbols, |
| 459 | packages=opts.packages, |
| 460 | use_goma=opts.run_goma, |
| 461 | use_remoteexec=opts.run_remoteexec, |
| 462 | incremental_build=opts.withrevdeps, |
| 463 | dryrun=opts.pretend, |
| 464 | usepkgonly=opts.usepkgonly, |
| 465 | workon=opts.workon, |
| 466 | install_auto_test=opts.withautotest, |
| 467 | autosetgov=opts.autosetgov, |
| 468 | autosetgov_sticky=opts.autosetgov_sticky, |
| 469 | use_any_chrome=opts.use_any_chrome, |
| 470 | internal_chrome=opts.internal, |
| 471 | clean_build=opts.cleanbuild, |
| 472 | eclean=opts.eclean, |
| 473 | rebuild_dep=opts.rebuild, |
| 474 | jobs=opts.jobs, |
| 475 | local_pkg=opts.reuse_pkgs_from_local_boards, |
| 476 | dev_image=opts.withdev, |
| 477 | factory_image=opts.withfactory, |
| 478 | test_image=opts.withtest, |
| 479 | debug_version=opts.withdebug, |
| 480 | backtrack=opts.backtrack, |
Ryo Hashimoto | 0845ebf | 2023-06-06 19:21:13 +0900 | [diff] [blame^] | 481 | bazel=opts.bazel, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 482 | ) |
| 483 | opts.Freeze() |
| 484 | return parser, opts |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 485 | |
| 486 | |
Lizzy Presland | 453237c | 2023-05-31 00:16:15 +0000 | [diff] [blame] | 487 | tracer = trace.get_tracer(__name__) |
| 488 | |
| 489 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 490 | @timer.timed("Elapsed time (build_packages)") |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 491 | def main(argv: Optional[List[str]] = None) -> Optional[int]: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 492 | commandline.RunInsideChroot() |
| 493 | parser, opts = parse_args(argv) |
Ram Chandrasekar | baa8963 | 2022-02-11 23:22:09 +0000 | [diff] [blame] | 494 | |
Lizzy Presland | 453237c | 2023-05-31 00:16:15 +0000 | [diff] [blame] | 495 | build_packages(parser, opts) |
| 496 | |
| 497 | |
| 498 | @tracer.start_as_current_span("scripts.build_packages.build_packages") |
| 499 | def build_packages( |
| 500 | parser: commandline.ArgumentParser, opts: commandline.ArgumentNamespace |
| 501 | ): |
Anuj Jamwal | 50db51b | 2023-06-12 17:03:53 +0000 | [diff] [blame] | 502 | span = trace.get_current_span() |
| 503 | |
Alex Klein | d719740 | 2023-04-05 13:05:29 -0600 | [diff] [blame] | 504 | # If the opts.board is not set, then it means user hasn't specified a |
| 505 | # default board in 'src/scripts/.default_board' and didn't specify it as |
| 506 | # input argument. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 507 | if not opts.board: |
Anuj Jamwal | 50db51b | 2023-06-12 17:03:53 +0000 | [diff] [blame] | 508 | span.add_event( |
| 509 | "exception", |
| 510 | attributes={ |
| 511 | "exception.type": "ArgumentMissingError", |
| 512 | "exception.message": "--board is required", |
| 513 | }, |
| 514 | ) |
| 515 | span.set_status(status.StatusCode.ERROR) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 516 | parser.error("--board is required") |
Ram Chandrasekar | bdec0f0 | 2022-02-24 01:20:17 +0000 | [diff] [blame] | 517 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 518 | build_target = build_target_lib.BuildTarget( |
| 519 | opts.board, build_root=opts.sysroot |
| 520 | ) |
Anuj Jamwal | 50db51b | 2023-06-12 17:03:53 +0000 | [diff] [blame] | 521 | span.set_attributes( |
| 522 | { |
| 523 | "board": build_target.name, |
| 524 | "workon_packages": workon_helper.WorkonHelper( |
| 525 | build_target.root |
| 526 | ).ListAtoms(), |
| 527 | } |
| 528 | ) |
| 529 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 530 | board_root = sysroot_lib.Sysroot(build_target.root) |
Cindy Lin | 7487daa | 2022-02-23 04:14:10 +0000 | [diff] [blame] | 531 | |
Alex Klein | fba23ba | 2022-02-03 11:58:48 -0700 | [diff] [blame] | 532 | try: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 533 | # TODO(xcl): Update run_configs to have a common base set of configs for |
| 534 | # setup_board and build_packages. |
| 535 | if not opts.skip_setup_board: |
| 536 | sysroot.SetupBoard( |
| 537 | build_target, |
| 538 | accept_licenses=opts.accept_licenses, |
| 539 | run_configs=opts.setup_board_run_config, |
| 540 | ) |
Sergey Frolov | f988de7 | 2023-03-06 20:05:21 -0700 | [diff] [blame] | 541 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 542 | sysroot.BuildPackages(build_target, board_root, opts.build_run_config) |
| 543 | except sysroot_lib.PackageInstallError as e: |
| 544 | try: |
| 545 | with urllib.request.urlopen( |
| 546 | "https://chromiumos-status.appspot.com/current?format=raw" |
Sergey Frolov | 7cf6c0b | 2022-06-06 16:47:44 -0600 | [diff] [blame] | 547 | ) as request: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 548 | logging.notice("Tree Status: %s", request.read().decode()) |
| 549 | except urllib.error.HTTPError: |
| 550 | pass |
Anuj Jamwal | 50db51b | 2023-06-12 17:03:53 +0000 | [diff] [blame] | 551 | span.record_exception(e) |
| 552 | span.set_status(status.StatusCode.ERROR, str(e)) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 553 | cros_build_lib.Die(e) |
Anuj Jamwal | 50db51b | 2023-06-12 17:03:53 +0000 | [diff] [blame] | 554 | except KeyboardInterrupt as e: |
| 555 | span.record_exception(e) |
| 556 | span.set_status(status.StatusCode.OK) |
| 557 | raise |