Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2022 The ChromiumOS Authors |
Ryan Beltran | 1f2dd08 | 2022-04-25 18:42:32 +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 | |
| 5 | """This script emerges packages and retrieves their lints. |
| 6 | |
| 7 | Currently support is provided for both general and differential linting of C++ |
| 8 | with Clang Tidy and Rust with Cargo Clippy for all packages within platform2. |
| 9 | """ |
| 10 | |
Ryan Beltran | 179d6bb | 2023-09-14 23:37:33 +0000 | [diff] [blame] | 11 | import collections |
Ryan Beltran | 1f2dd08 | 2022-04-25 18:42:32 +0000 | [diff] [blame] | 12 | import json |
Ryan Beltran | 179d6bb | 2023-09-14 23:37:33 +0000 | [diff] [blame] | 13 | import logging |
Ryan Beltran | 378934c | 2022-11-23 00:44:26 +0000 | [diff] [blame] | 14 | import os |
| 15 | from pathlib import Path |
Ryan Beltran | 1f2dd08 | 2022-04-25 18:42:32 +0000 | [diff] [blame] | 16 | import sys |
Ryan Beltran | 179d6bb | 2023-09-14 23:37:33 +0000 | [diff] [blame] | 17 | from typing import DefaultDict, Dict, Iterable, List, Optional, Text, Tuple |
Ryan Beltran | 1f2dd08 | 2022-04-25 18:42:32 +0000 | [diff] [blame] | 18 | |
| 19 | from chromite.lib import build_target_lib |
| 20 | from chromite.lib import commandline |
Ryan Beltran | 4706f34 | 2023-08-29 01:16:22 +0000 | [diff] [blame] | 21 | from chromite.lib import constants |
Ryan Beltran | 1f2dd08 | 2022-04-25 18:42:32 +0000 | [diff] [blame] | 22 | from chromite.lib import cros_build_lib |
Ryan Beltran | b217586 | 2022-04-28 19:55:57 +0000 | [diff] [blame] | 23 | from chromite.lib import portage_util |
Ryan Beltran | ce85d0f | 2022-08-09 21:36:39 +0000 | [diff] [blame] | 24 | from chromite.lib import terminal |
Ryan Beltran | 5514eab | 2022-04-28 21:40:24 +0000 | [diff] [blame] | 25 | from chromite.lib import workon_helper |
Ryan Beltran | 1f2dd08 | 2022-04-25 18:42:32 +0000 | [diff] [blame] | 26 | from chromite.lib.parser import package_info |
| 27 | from chromite.service import toolchain |
| 28 | from chromite.utils import file_util |
| 29 | |
| 30 | |
Ryan Beltran | 179d6bb | 2023-09-14 23:37:33 +0000 | [diff] [blame] | 31 | PLATFORM2_PATH = constants.CHROOT_SOURCE_ROOT / "src/platform2" |
| 32 | |
| 33 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 34 | def parse_packages( |
| 35 | build_target: build_target_lib.BuildTarget, packages: List[str] |
| 36 | ) -> List[package_info.PackageInfo]: |
| 37 | """Parse packages and insert the category if none is given. |
Ryan Beltran | b217586 | 2022-04-28 19:55:57 +0000 | [diff] [blame] | 38 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 39 | Args: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 40 | build_target: build_target to find ebuild for |
| 41 | packages: user input package names to parse |
Ryan Beltran | b217586 | 2022-04-28 19:55:57 +0000 | [diff] [blame] | 42 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 43 | Returns: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 44 | A list of parsed PackageInfo objects |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 45 | """ |
| 46 | package_infos: List[package_info.PackageInfo] = [] |
| 47 | for package in packages: |
| 48 | parsed = package_info.parse(package) |
| 49 | if not parsed.category: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 50 | # If a category is not specified, get it from the ebuild path. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 51 | if build_target.is_host(): |
| 52 | ebuild_path = portage_util.FindEbuildForPackage( |
| 53 | package, build_target.root |
| 54 | ) |
| 55 | else: |
| 56 | ebuild_path = portage_util.FindEbuildForBoardPackage( |
| 57 | package, build_target.name, build_target.root |
| 58 | ) |
| 59 | ebuild_data = portage_util.EBuild(ebuild_path) |
| 60 | parsed = package_info.parse(ebuild_data.package) |
| 61 | package_infos.append(parsed) |
| 62 | return package_infos |
Ryan Beltran | b217586 | 2022-04-28 19:55:57 +0000 | [diff] [blame] | 63 | |
| 64 | |
Ryan Beltran | 4706f34 | 2023-08-29 01:16:22 +0000 | [diff] [blame] | 65 | def make_relative_to_cros(file_path: str) -> Path: |
| 66 | """removes /mnt/host/source from file_paths if present.""" |
| 67 | path = Path(file_path) |
| 68 | try: |
| 69 | return path.relative_to(constants.CHROOT_SOURCE_ROOT) |
| 70 | except ValueError: |
| 71 | return path |
| 72 | |
| 73 | |
Ryan Beltran | 179d6bb | 2023-09-14 23:37:33 +0000 | [diff] [blame] | 74 | def process_fixes_by_file( |
| 75 | lint: toolchain.LinterFinding, file_lengths: Dict[Path, int] |
| 76 | ) -> Optional[DefaultDict[Path, List[toolchain.SuggestedFix]]]: |
| 77 | """Get fixes grouped by file if all the fixes apply to valid files. |
| 78 | |
| 79 | If any fixes modify invalid files this returns None. |
Ryan Beltran | f83a397 | 2023-09-21 00:22:25 +0000 | [diff] [blame^] | 80 | |
| 81 | Args: |
| 82 | lint: LinterFinding to get fixes from |
| 83 | file_lengths: dictionary of previously determined file lengths which |
| 84 | may be modified with additional entries |
Ryan Beltran | 179d6bb | 2023-09-14 23:37:33 +0000 | [diff] [blame] | 85 | """ |
| 86 | if not lint.suggested_fixes: |
| 87 | return None |
| 88 | |
| 89 | new_fixes_by_file: DefaultDict[ |
| 90 | Path, List[toolchain.SuggestedFix] |
| 91 | ] = collections.defaultdict(list) |
| 92 | for fix in lint.suggested_fixes: |
| 93 | filepath = Path(fix.location.filepath) |
| 94 | # These are files that we locate, and are usually generated files. |
| 95 | if filepath.is_absolute(): |
| 96 | logging.warning( |
| 97 | "Skipped applying fix due to invalid path: %s", filepath |
| 98 | ) |
| 99 | return None |
| 100 | # Make sure this file exists in platform2 |
| 101 | file_in_platform2 = PLATFORM2_PATH / filepath |
| 102 | if not file_in_platform2.exists(): |
| 103 | logging.warning( |
| 104 | "Skipped applying fix due to invalid path: %s", filepath |
| 105 | ) |
| 106 | return None |
| 107 | if file_in_platform2 not in file_lengths: |
| 108 | file_lengths[file_in_platform2] = len( |
| 109 | file_in_platform2.read_text(encoding="utf-8") |
| 110 | ) |
| 111 | if fix.location.end_offset > file_lengths[file_in_platform2]: |
| 112 | logging.warning( |
| 113 | "Skipped applying fix due to out of bounds change to: %s", |
| 114 | filepath, |
| 115 | ) |
| 116 | return None |
| 117 | new_fixes_by_file[file_in_platform2].append(fix) |
| 118 | |
| 119 | return new_fixes_by_file |
| 120 | |
| 121 | |
| 122 | def get_noconflict_fixes( |
| 123 | lints: List[toolchain.LinterFinding], |
| 124 | ) -> Tuple[ |
| 125 | DefaultDict[Path, List[toolchain.SuggestedFix]], |
| 126 | List[toolchain.LinterFinding], |
| 127 | ]: |
| 128 | """Get a conflict free set of replacements to apply for each file. |
| 129 | |
| 130 | Fixes will not be included in results if they: |
| 131 | A) include a replacement to a path which does not exist |
| 132 | B) include a replacement to a path outside of platform2 |
| 133 | C) include a replacement to file location that exceeds the file size |
| 134 | D) overlap a previous replacement. |
| 135 | |
| 136 | Args: |
| 137 | lints: List of lints to aggregate suggested fixes from. |
| 138 | |
| 139 | Returns: |
| 140 | A tuple including: |
| 141 | 0) the mapping of paths to a list of their suggested fixes |
| 142 | 1) the list of lints which were fixed |
| 143 | """ |
| 144 | fixes_by_file: DefaultDict[ |
| 145 | Path, List[toolchain.SuggestedFix] |
| 146 | ] = collections.defaultdict(list) |
| 147 | lints_fixed = [] |
| 148 | file_lengths = {} |
| 149 | for lint in lints: |
| 150 | new_fixes_by_file = process_fixes_by_file(lint, file_lengths) |
| 151 | if not new_fixes_by_file: |
| 152 | continue |
| 153 | files_with_overlap = set( |
| 154 | filepath |
| 155 | for filepath, new_fixes in new_fixes_by_file.items() |
| 156 | if has_overlap(fixes_by_file[filepath], new_fixes) |
| 157 | ) |
| 158 | if files_with_overlap: |
| 159 | logging.warning( |
| 160 | "Skipped applying fix for %s due to conflicts in:\n\t%s.", |
| 161 | lint.name, |
| 162 | "\n\t".join(files_with_overlap), |
| 163 | ) |
| 164 | else: |
| 165 | for filepath, new_fixes in new_fixes_by_file.items(): |
| 166 | fixes_by_file[filepath].extend(new_fixes) |
| 167 | lints_fixed.append(lint) |
| 168 | |
| 169 | return fixes_by_file, lints_fixed |
| 170 | |
| 171 | |
| 172 | def has_overlap( |
| 173 | prior_fixes: List[toolchain.SuggestedFix], |
| 174 | new_fixes: List[toolchain.SuggestedFix], |
| 175 | ) -> bool: |
| 176 | """Check if new fixes have overlapping ranges with a prior replacement. |
| 177 | |
| 178 | Note: this implementation is n^2, but the amount of lints in a single file |
| 179 | is experimentally pretty small, so optimizing this is probably not a large |
| 180 | concern. |
| 181 | """ |
| 182 | for new in new_fixes: |
| 183 | for old in prior_fixes: |
| 184 | if ( |
| 185 | (old.location.start_offset <= new.location.start_offset) |
| 186 | and (new.location.start_offset <= old.location.end_offset) |
| 187 | ) or ( |
| 188 | (old.location.start_offset <= new.location.end_offset) |
| 189 | and (new.location.end_offset <= old.location.end_offset) |
| 190 | ): |
| 191 | return True |
| 192 | return False |
| 193 | |
| 194 | |
| 195 | def apply_edits(content: Text, fixes: List[toolchain.SuggestedFix]) -> Text: |
| 196 | """Modify a file by applying a list of fixes.""" |
| 197 | |
| 198 | # We need to be able to apply fixes in reverse order within a file to |
| 199 | # preserve code locations. |
| 200 | def fix_sort_key(fix: toolchain.SuggestedFix) -> int: |
| 201 | return fix.location.start_offset |
| 202 | |
| 203 | pieces = [] |
| 204 | content_end = len(content) |
| 205 | for fix in sorted(fixes, key=fix_sort_key, reverse=True): |
| 206 | pieces += [ |
| 207 | content[fix.location.end_offset : content_end], |
| 208 | fix.replacement, |
| 209 | ] |
| 210 | content_end = fix.location.start_offset |
| 211 | pieces.append(content[:content_end]) |
| 212 | |
| 213 | return "".join(reversed(pieces)) |
| 214 | |
| 215 | |
| 216 | def apply_fixes( |
| 217 | lints: List[toolchain.LinterFinding], |
| 218 | ) -> Tuple[List[toolchain.LinterFinding], Iterable[Path]]: |
| 219 | """Modify files in Platform2 to apply suggested fixes from linter findings. |
| 220 | |
| 221 | Some fixes which cannot be applied cleanly will be discarded (see the |
| 222 | `get_noconflict_fixes_by_file` description for more details). |
| 223 | |
| 224 | Args: |
| 225 | lints: LinterFindings to apply potential fixes from. |
| 226 | |
| 227 | Returns: |
| 228 | A tuple including: |
| 229 | 0) The list of lints which were fixed |
| 230 | 1) The list of files which were modified |
| 231 | """ |
| 232 | |
| 233 | fixes_by_file, lints_fixed = get_noconflict_fixes(lints) |
| 234 | |
| 235 | for filepath, fixes in fixes_by_file.items(): |
| 236 | file_content = filepath.read_text(encoding="utf-8") |
| 237 | rewrite = apply_edits(file_content, fixes) |
| 238 | filepath.write_text(rewrite, encoding="utf-8") |
| 239 | |
| 240 | return lints_fixed, fixes_by_file.keys() |
| 241 | |
| 242 | |
Ryan Beltran | ce85d0f | 2022-08-09 21:36:39 +0000 | [diff] [blame] | 243 | def format_lint(lint: toolchain.LinterFinding) -> Text: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 244 | """Formats a lint for human-readable printing. |
Ryan Beltran | ce85d0f | 2022-08-09 21:36:39 +0000 | [diff] [blame] | 245 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 246 | Example output: |
| 247 | [ClangTidy] In 'path/to/file.c' on line 36: |
| 248 | Also in 'path/to/file.c' on line 40: |
| 249 | Also in 'path/to/file.c' on lines 50-53: |
| 250 | You did something bad, don't do it. |
Ryan Beltran | ce85d0f | 2022-08-09 21:36:39 +0000 | [diff] [blame] | 251 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 252 | Args: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 253 | lint: A linter finding from the toolchain service. |
Ryan Beltran | ce85d0f | 2022-08-09 21:36:39 +0000 | [diff] [blame] | 254 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 255 | Returns: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 256 | A correctly formatted string ready to be displayed to the user. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 257 | """ |
Ryan Beltran | ce85d0f | 2022-08-09 21:36:39 +0000 | [diff] [blame] | 258 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 259 | color = terminal.Color(True) |
| 260 | lines = [] |
| 261 | linter_prefix = color.Color( |
| 262 | terminal.Color.YELLOW, |
| 263 | f"[{lint.linter}]", |
| 264 | background_color=terminal.Color.BLACK, |
| 265 | ) |
| 266 | for loc in lint.locations: |
Ryan Beltran | 4706f34 | 2023-08-29 01:16:22 +0000 | [diff] [blame] | 267 | filepath = make_relative_to_cros(loc.filepath) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 268 | if not lines: |
| 269 | location_prefix = f"\n{linter_prefix} In" |
| 270 | else: |
| 271 | location_prefix = " and in" |
| 272 | if loc.line_start != loc.line_end: |
| 273 | lines.append( |
Ryan Beltran | 4706f34 | 2023-08-29 01:16:22 +0000 | [diff] [blame] | 274 | f"{location_prefix} '{filepath}' " |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 275 | f"lines {loc.line_start}-{loc.line_end}:" |
| 276 | ) |
| 277 | else: |
| 278 | lines.append( |
Ryan Beltran | 4706f34 | 2023-08-29 01:16:22 +0000 | [diff] [blame] | 279 | f"{location_prefix} '{filepath}' line {loc.line_start}:" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 280 | ) |
| 281 | message_lines = lint.message.split("\n") |
| 282 | for line in message_lines: |
| 283 | lines.append(f" {line}") |
| 284 | lines.append("") |
| 285 | return "\n".join(lines) |
Ryan Beltran | ce85d0f | 2022-08-09 21:36:39 +0000 | [diff] [blame] | 286 | |
| 287 | |
Ryan Beltran | a32a1a1 | 2022-09-28 06:03:45 +0000 | [diff] [blame] | 288 | def json_format_lint(lint: toolchain.LinterFinding) -> Text: |
| 289 | """Formats a lint in json for machine parsing. |
| 290 | |
| 291 | Args: |
| 292 | lint: A linter finding from the toolchain service. |
| 293 | |
| 294 | Returns: |
| 295 | A correctly formatted json string ready to be displayed to the user. |
| 296 | """ |
| 297 | |
| 298 | def _dictify(original): |
| 299 | """Turns namedtuple's to dictionaries recursively.""" |
| 300 | # Handle namedtuples |
| 301 | if isinstance(original, tuple) and hasattr(original, "_asdict"): |
| 302 | return _dictify(original._asdict()) |
| 303 | # Handle collection types |
| 304 | elif hasattr(original, "__iter__"): |
| 305 | # Handle strings |
| 306 | if isinstance(original, (str, bytes)): |
| 307 | return original |
| 308 | # Handle dictionaries |
| 309 | elif isinstance(original, dict): |
| 310 | return {k: _dictify(v) for k, v in original.items()} |
| 311 | # Handle lists, sets, etc. |
| 312 | else: |
| 313 | return [_dictify(x) for x in original] |
Ryan Beltran | c37fb39 | 2023-05-11 18:24:40 +0000 | [diff] [blame] | 314 | # Handle PackageInfo objects |
| 315 | elif isinstance(original, package_info.PackageInfo): |
| 316 | return original.atom |
Ryan Beltran | a32a1a1 | 2022-09-28 06:03:45 +0000 | [diff] [blame] | 317 | # Handle everything else |
| 318 | return original |
| 319 | |
| 320 | return json.dumps(_dictify(lint)) |
| 321 | |
| 322 | |
Ryan Beltran | 378934c | 2022-11-23 00:44:26 +0000 | [diff] [blame] | 323 | def get_all_sysroots() -> List[Text]: |
| 324 | """Gets all available sysroots for both host and boards.""" |
| 325 | host_root = Path(build_target_lib.BuildTarget(None).root) |
| 326 | roots = [str(host_root)] |
| 327 | build_dir = host_root / "build" |
| 328 | for board in os.listdir(build_dir): |
| 329 | if board != "bin": |
| 330 | board_root = build_dir / board |
| 331 | if board_root.is_dir(): |
| 332 | roots.append(str(board_root)) |
| 333 | return roots |
| 334 | |
| 335 | |
Ryan Beltran | 1f2dd08 | 2022-04-25 18:42:32 +0000 | [diff] [blame] | 336 | def get_arg_parser() -> commandline.ArgumentParser: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 337 | """Creates an argument parser for this script.""" |
| 338 | default_board = cros_build_lib.GetDefaultBoard() |
| 339 | parser = commandline.ArgumentParser(description=__doc__) |
Ryan Beltran | dbd7b81 | 2022-06-08 23:36:16 +0000 | [diff] [blame] | 340 | |
Ryan Beltran | 378934c | 2022-11-23 00:44:26 +0000 | [diff] [blame] | 341 | board_group = parser.add_mutually_exclusive_group() |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 342 | board_group.add_argument( |
| 343 | "-b", |
| 344 | "--board", |
| 345 | "--build-target", |
| 346 | dest="board", |
| 347 | default=default_board, |
| 348 | help="The board to emerge packages for", |
| 349 | ) |
| 350 | board_group.add_argument( |
| 351 | "--host", action="store_true", help="emerge for host instead of board." |
| 352 | ) |
Ryan Beltran | 378934c | 2022-11-23 00:44:26 +0000 | [diff] [blame] | 353 | parser.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 354 | "--fetch-only", |
| 355 | action="store_true", |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 356 | help="Fetch lints from previous run without resetting or calling " |
| 357 | "emerge.", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 358 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 359 | parser.add_argument( |
Ryan Beltran | 179d6bb | 2023-09-14 23:37:33 +0000 | [diff] [blame] | 360 | "--apply-fixes", |
| 361 | action="store_true", |
| 362 | help="Apply suggested fixes from linters.", |
| 363 | ) |
| 364 | parser.add_argument( |
Ryan Beltran | f83a397 | 2023-09-21 00:22:25 +0000 | [diff] [blame^] | 365 | "--filter-names", |
| 366 | help="Only keep lints if the name contains one of the provided filters", |
| 367 | action="append", |
| 368 | ) |
| 369 | parser.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 370 | "--differential", |
| 371 | action="store_true", |
| 372 | help="only lint lines touched by the last commit", |
| 373 | ) |
| 374 | parser.add_argument( |
| 375 | "-o", |
| 376 | "--output", |
| 377 | default=sys.stdout, |
| 378 | help="File to use instead of stdout.", |
| 379 | ) |
| 380 | parser.add_argument( |
| 381 | "--json", action="store_true", help="Output lints in JSON format." |
| 382 | ) |
| 383 | parser.add_argument( |
| 384 | "--no-clippy", |
| 385 | dest="clippy", |
| 386 | action="store_false", |
| 387 | help="Disable cargo clippy linter.", |
| 388 | ) |
| 389 | parser.add_argument( |
| 390 | "--no-tidy", |
| 391 | dest="tidy", |
| 392 | action="store_false", |
| 393 | help="Disable clang tidy linter.", |
| 394 | ) |
| 395 | parser.add_argument( |
| 396 | "--no-golint", |
| 397 | dest="golint", |
| 398 | action="store_false", |
| 399 | help="Disable golint linter.", |
| 400 | ) |
| 401 | parser.add_argument( |
Ryan Beltran | 378934c | 2022-11-23 00:44:26 +0000 | [diff] [blame] | 402 | "--iwyu", |
| 403 | action="store_true", |
| 404 | help="Enable include-what-you-use linter.", |
| 405 | ) |
| 406 | parser.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 407 | "packages", |
| 408 | nargs="*", |
| 409 | help="package(s) to emerge and retrieve lints for", |
| 410 | ) |
| 411 | return parser |
Ryan Beltran | 1f2dd08 | 2022-04-25 18:42:32 +0000 | [diff] [blame] | 412 | |
| 413 | |
| 414 | def parse_args(argv: List[str]): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 415 | """Parses arguments in argv and returns the options.""" |
| 416 | parser = get_arg_parser() |
| 417 | opts = parser.parse_args(argv) |
| 418 | opts.Freeze() |
Ryan Beltran | dbd7b81 | 2022-06-08 23:36:16 +0000 | [diff] [blame] | 419 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 420 | # A package must be specified unless we are in fetch-only mode |
| 421 | if not (opts.fetch_only or opts.packages): |
Ryan Beltran | 378934c | 2022-11-23 00:44:26 +0000 | [diff] [blame] | 422 | parser.error("Emerge mode requires specified package(s).") |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 423 | if opts.fetch_only and opts.packages: |
| 424 | parser.error("Cannot specify packages for fetch-only mode.") |
Ryan Beltran | dbd7b81 | 2022-06-08 23:36:16 +0000 | [diff] [blame] | 425 | |
Ryan Beltran | 378934c | 2022-11-23 00:44:26 +0000 | [diff] [blame] | 426 | # A board must be specified unless we are in fetch-only mode |
| 427 | if not (opts.fetch_only or opts.board or opts.host): |
| 428 | parser.error("Emerge mode requires either --board or --host.") |
| 429 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 430 | return opts |
Ryan Beltran | 1f2dd08 | 2022-04-25 18:42:32 +0000 | [diff] [blame] | 431 | |
| 432 | |
Ryan Beltran | f83a397 | 2023-09-21 00:22:25 +0000 | [diff] [blame^] | 433 | def filter_lints( |
| 434 | lints: List[toolchain.LinterFinding], names_filters: List[Text] |
| 435 | ) -> List[toolchain.LinterFinding]: |
| 436 | """Filter linter finding by name.""" |
| 437 | return [l for l in lints if any(f in l.name for f in names_filters)] |
| 438 | |
| 439 | |
Ryan Beltran | 1f2dd08 | 2022-04-25 18:42:32 +0000 | [diff] [blame] | 440 | def main(argv: List[str]) -> None: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 441 | cros_build_lib.AssertInsideChroot() |
| 442 | opts = parse_args(argv) |
Ryan Beltran | 1f2dd08 | 2022-04-25 18:42:32 +0000 | [diff] [blame] | 443 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 444 | if opts.host: |
| 445 | # BuildTarget interprets None as host target |
| 446 | build_target = build_target_lib.BuildTarget(None) |
Ryan Beltran | dbd7b81 | 2022-06-08 23:36:16 +0000 | [diff] [blame] | 447 | else: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 448 | build_target = build_target_lib.BuildTarget(opts.board) |
| 449 | packages = parse_packages(build_target, opts.packages) |
| 450 | package_atoms = [x.atom for x in packages] |
Ryan Beltran | 1f2dd08 | 2022-04-25 18:42:32 +0000 | [diff] [blame] | 451 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 452 | with workon_helper.WorkonScope(build_target, package_atoms): |
| 453 | build_linter = toolchain.BuildLinter( |
| 454 | packages, build_target.root, opts.differential |
| 455 | ) |
| 456 | if opts.fetch_only: |
Ryan Beltran | 179d6bb | 2023-09-14 23:37:33 +0000 | [diff] [blame] | 457 | if opts.apply_fixes: |
| 458 | logging.warning( |
| 459 | "Apply fixes with fetch_only may lead to fixes being" |
| 460 | " applied incorrectly if source files have changed!" |
| 461 | ) |
Ryan Beltran | 378934c | 2022-11-23 00:44:26 +0000 | [diff] [blame] | 462 | if opts.host or opts.board: |
| 463 | roots = [build_target.root] |
| 464 | else: |
| 465 | roots = get_all_sysroots() |
| 466 | lints = [] |
| 467 | for root in roots: |
| 468 | build_linter.sysroot = root |
| 469 | lints.extend( |
| 470 | build_linter.fetch_findings( |
| 471 | use_clippy=opts.clippy, |
| 472 | use_tidy=opts.tidy, |
| 473 | use_golint=opts.golint, |
| 474 | use_iwyu=opts.iwyu, |
| 475 | ) |
| 476 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 477 | else: |
| 478 | lints = build_linter.emerge_with_linting( |
| 479 | use_clippy=opts.clippy, |
| 480 | use_tidy=opts.tidy, |
| 481 | use_golint=opts.golint, |
Ryan Beltran | 378934c | 2022-11-23 00:44:26 +0000 | [diff] [blame] | 482 | use_iwyu=opts.iwyu, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 483 | ) |
Ryan Beltran | ce85d0f | 2022-08-09 21:36:39 +0000 | [diff] [blame] | 484 | |
Ryan Beltran | f83a397 | 2023-09-21 00:22:25 +0000 | [diff] [blame^] | 485 | if opts.filter_names: |
| 486 | lints = filter_lints(lints, opts.filter_names) |
| 487 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 488 | if opts.json: |
Ryan Beltran | a32a1a1 | 2022-09-28 06:03:45 +0000 | [diff] [blame] | 489 | formatted_output_inner = ",\n".join(json_format_lint(l) for l in lints) |
| 490 | formatted_output = f"[{formatted_output_inner}]" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 491 | else: |
Ryan Beltran | a32a1a1 | 2022-09-28 06:03:45 +0000 | [diff] [blame] | 492 | formatted_output = "\n".join(format_lint(l) for l in lints) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 493 | |
Ryan Beltran | 179d6bb | 2023-09-14 23:37:33 +0000 | [diff] [blame] | 494 | if opts.apply_fixes: |
| 495 | fixed_lints, modified_files = apply_fixes(lints) |
| 496 | if opts.json: |
| 497 | formatted_fixes_inner = ",\n".join( |
| 498 | json_format_lint(l) for l in lints |
| 499 | ) |
| 500 | formatted_fixes = f"[{formatted_fixes_inner}]" |
| 501 | else: |
| 502 | formatted_fixes = "\n".join(format_lint(l) for l in fixed_lints) |
| 503 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 504 | with file_util.Open(opts.output, "w") as output_file: |
| 505 | output_file.write(formatted_output) |
| 506 | if not opts.json: |
| 507 | output_file.write(f"\nFound {len(lints)} lints.") |
Ryan Beltran | 179d6bb | 2023-09-14 23:37:33 +0000 | [diff] [blame] | 508 | if opts.apply_fixes: |
| 509 | output_file.write("\n\n\n--------- Fixed Problems ---------\n\n") |
| 510 | output_file.write(formatted_fixes) |
| 511 | if not opts.json: |
| 512 | output_file.write( |
| 513 | f"\nFixed {len(fixed_lints)}/{len(lints)} lints." |
| 514 | ) |
| 515 | output_file.write("\n\n\n--------- Modified Files ---------\n\n") |
Ryan Beltran | f83a397 | 2023-09-21 00:22:25 +0000 | [diff] [blame^] | 516 | output_file.write("\n".join(str(f) for f in sorted(modified_files))) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 517 | output_file.write("\n") |