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 | |
| 11 | import json |
Ryan Beltran | 378934c | 2022-11-23 00:44:26 +0000 | [diff] [blame] | 12 | import os |
| 13 | from pathlib import Path |
Ryan Beltran | 1f2dd08 | 2022-04-25 18:42:32 +0000 | [diff] [blame] | 14 | import sys |
Ryan Beltran | ce85d0f | 2022-08-09 21:36:39 +0000 | [diff] [blame] | 15 | from typing import List, Text |
Ryan Beltran | 1f2dd08 | 2022-04-25 18:42:32 +0000 | [diff] [blame] | 16 | |
| 17 | from chromite.lib import build_target_lib |
| 18 | from chromite.lib import commandline |
| 19 | from chromite.lib import cros_build_lib |
Ryan Beltran | b217586 | 2022-04-28 19:55:57 +0000 | [diff] [blame] | 20 | from chromite.lib import portage_util |
Ryan Beltran | ce85d0f | 2022-08-09 21:36:39 +0000 | [diff] [blame] | 21 | from chromite.lib import terminal |
Ryan Beltran | 5514eab | 2022-04-28 21:40:24 +0000 | [diff] [blame] | 22 | from chromite.lib import workon_helper |
Ryan Beltran | 1f2dd08 | 2022-04-25 18:42:32 +0000 | [diff] [blame] | 23 | from chromite.lib.parser import package_info |
| 24 | from chromite.service import toolchain |
| 25 | from chromite.utils import file_util |
| 26 | |
| 27 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 28 | def parse_packages( |
| 29 | build_target: build_target_lib.BuildTarget, packages: List[str] |
| 30 | ) -> List[package_info.PackageInfo]: |
| 31 | """Parse packages and insert the category if none is given. |
Ryan Beltran | b217586 | 2022-04-28 19:55:57 +0000 | [diff] [blame] | 32 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 33 | Args: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 34 | build_target: build_target to find ebuild for |
| 35 | packages: user input package names to parse |
Ryan Beltran | b217586 | 2022-04-28 19:55:57 +0000 | [diff] [blame] | 36 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 37 | Returns: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 38 | A list of parsed PackageInfo objects |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 39 | """ |
| 40 | package_infos: List[package_info.PackageInfo] = [] |
| 41 | for package in packages: |
| 42 | parsed = package_info.parse(package) |
| 43 | if not parsed.category: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 44 | # If a category is not specified, get it from the ebuild path. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 45 | if build_target.is_host(): |
| 46 | ebuild_path = portage_util.FindEbuildForPackage( |
| 47 | package, build_target.root |
| 48 | ) |
| 49 | else: |
| 50 | ebuild_path = portage_util.FindEbuildForBoardPackage( |
| 51 | package, build_target.name, build_target.root |
| 52 | ) |
| 53 | ebuild_data = portage_util.EBuild(ebuild_path) |
| 54 | parsed = package_info.parse(ebuild_data.package) |
| 55 | package_infos.append(parsed) |
| 56 | return package_infos |
Ryan Beltran | b217586 | 2022-04-28 19:55:57 +0000 | [diff] [blame] | 57 | |
| 58 | |
Ryan Beltran | ce85d0f | 2022-08-09 21:36:39 +0000 | [diff] [blame] | 59 | def format_lint(lint: toolchain.LinterFinding) -> Text: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 60 | """Formats a lint for human-readable printing. |
Ryan Beltran | ce85d0f | 2022-08-09 21:36:39 +0000 | [diff] [blame] | 61 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 62 | Example output: |
| 63 | [ClangTidy] In 'path/to/file.c' on line 36: |
| 64 | Also in 'path/to/file.c' on line 40: |
| 65 | Also in 'path/to/file.c' on lines 50-53: |
| 66 | You did something bad, don't do it. |
Ryan Beltran | ce85d0f | 2022-08-09 21:36:39 +0000 | [diff] [blame] | 67 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 68 | Args: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 69 | lint: A linter finding from the toolchain service. |
Ryan Beltran | ce85d0f | 2022-08-09 21:36:39 +0000 | [diff] [blame] | 70 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 71 | Returns: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 72 | A correctly formatted string ready to be displayed to the user. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 73 | """ |
Ryan Beltran | ce85d0f | 2022-08-09 21:36:39 +0000 | [diff] [blame] | 74 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 75 | color = terminal.Color(True) |
| 76 | lines = [] |
| 77 | linter_prefix = color.Color( |
| 78 | terminal.Color.YELLOW, |
| 79 | f"[{lint.linter}]", |
| 80 | background_color=terminal.Color.BLACK, |
| 81 | ) |
| 82 | for loc in lint.locations: |
| 83 | if not lines: |
| 84 | location_prefix = f"\n{linter_prefix} In" |
| 85 | else: |
| 86 | location_prefix = " and in" |
| 87 | if loc.line_start != loc.line_end: |
| 88 | lines.append( |
| 89 | f"{location_prefix} '{loc.filepath}' " |
| 90 | f"lines {loc.line_start}-{loc.line_end}:" |
| 91 | ) |
| 92 | else: |
| 93 | lines.append( |
| 94 | f"{location_prefix} '{loc.filepath}' line {loc.line_start}:" |
| 95 | ) |
| 96 | message_lines = lint.message.split("\n") |
| 97 | for line in message_lines: |
| 98 | lines.append(f" {line}") |
| 99 | lines.append("") |
| 100 | return "\n".join(lines) |
Ryan Beltran | ce85d0f | 2022-08-09 21:36:39 +0000 | [diff] [blame] | 101 | |
| 102 | |
Ryan Beltran | a32a1a1 | 2022-09-28 06:03:45 +0000 | [diff] [blame] | 103 | def json_format_lint(lint: toolchain.LinterFinding) -> Text: |
| 104 | """Formats a lint in json for machine parsing. |
| 105 | |
| 106 | Args: |
| 107 | lint: A linter finding from the toolchain service. |
| 108 | |
| 109 | Returns: |
| 110 | A correctly formatted json string ready to be displayed to the user. |
| 111 | """ |
| 112 | |
| 113 | def _dictify(original): |
| 114 | """Turns namedtuple's to dictionaries recursively.""" |
| 115 | # Handle namedtuples |
| 116 | if isinstance(original, tuple) and hasattr(original, "_asdict"): |
| 117 | return _dictify(original._asdict()) |
| 118 | # Handle collection types |
| 119 | elif hasattr(original, "__iter__"): |
| 120 | # Handle strings |
| 121 | if isinstance(original, (str, bytes)): |
| 122 | return original |
| 123 | # Handle dictionaries |
| 124 | elif isinstance(original, dict): |
| 125 | return {k: _dictify(v) for k, v in original.items()} |
| 126 | # Handle lists, sets, etc. |
| 127 | else: |
| 128 | return [_dictify(x) for x in original] |
Ryan Beltran | c37fb39 | 2023-05-11 18:24:40 +0000 | [diff] [blame] | 129 | # Handle PackageInfo objects |
| 130 | elif isinstance(original, package_info.PackageInfo): |
| 131 | return original.atom |
Ryan Beltran | a32a1a1 | 2022-09-28 06:03:45 +0000 | [diff] [blame] | 132 | # Handle everything else |
| 133 | return original |
| 134 | |
| 135 | return json.dumps(_dictify(lint)) |
| 136 | |
| 137 | |
Ryan Beltran | 378934c | 2022-11-23 00:44:26 +0000 | [diff] [blame] | 138 | def get_all_sysroots() -> List[Text]: |
| 139 | """Gets all available sysroots for both host and boards.""" |
| 140 | host_root = Path(build_target_lib.BuildTarget(None).root) |
| 141 | roots = [str(host_root)] |
| 142 | build_dir = host_root / "build" |
| 143 | for board in os.listdir(build_dir): |
| 144 | if board != "bin": |
| 145 | board_root = build_dir / board |
| 146 | if board_root.is_dir(): |
| 147 | roots.append(str(board_root)) |
| 148 | return roots |
| 149 | |
| 150 | |
Ryan Beltran | 1f2dd08 | 2022-04-25 18:42:32 +0000 | [diff] [blame] | 151 | def get_arg_parser() -> commandline.ArgumentParser: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 152 | """Creates an argument parser for this script.""" |
| 153 | default_board = cros_build_lib.GetDefaultBoard() |
| 154 | parser = commandline.ArgumentParser(description=__doc__) |
Ryan Beltran | dbd7b81 | 2022-06-08 23:36:16 +0000 | [diff] [blame] | 155 | |
Ryan Beltran | 378934c | 2022-11-23 00:44:26 +0000 | [diff] [blame] | 156 | board_group = parser.add_mutually_exclusive_group() |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 157 | board_group.add_argument( |
| 158 | "-b", |
| 159 | "--board", |
| 160 | "--build-target", |
| 161 | dest="board", |
| 162 | default=default_board, |
| 163 | help="The board to emerge packages for", |
| 164 | ) |
| 165 | board_group.add_argument( |
| 166 | "--host", action="store_true", help="emerge for host instead of board." |
| 167 | ) |
Ryan Beltran | 378934c | 2022-11-23 00:44:26 +0000 | [diff] [blame] | 168 | parser.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 169 | "--fetch-only", |
| 170 | action="store_true", |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 171 | help="Fetch lints from previous run without resetting or calling " |
| 172 | "emerge.", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 173 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 174 | parser.add_argument( |
| 175 | "--differential", |
| 176 | action="store_true", |
| 177 | help="only lint lines touched by the last commit", |
| 178 | ) |
| 179 | parser.add_argument( |
| 180 | "-o", |
| 181 | "--output", |
| 182 | default=sys.stdout, |
| 183 | help="File to use instead of stdout.", |
| 184 | ) |
| 185 | parser.add_argument( |
| 186 | "--json", action="store_true", help="Output lints in JSON format." |
| 187 | ) |
| 188 | parser.add_argument( |
| 189 | "--no-clippy", |
| 190 | dest="clippy", |
| 191 | action="store_false", |
| 192 | help="Disable cargo clippy linter.", |
| 193 | ) |
| 194 | parser.add_argument( |
| 195 | "--no-tidy", |
| 196 | dest="tidy", |
| 197 | action="store_false", |
| 198 | help="Disable clang tidy linter.", |
| 199 | ) |
| 200 | parser.add_argument( |
| 201 | "--no-golint", |
| 202 | dest="golint", |
| 203 | action="store_false", |
| 204 | help="Disable golint linter.", |
| 205 | ) |
| 206 | parser.add_argument( |
Ryan Beltran | 378934c | 2022-11-23 00:44:26 +0000 | [diff] [blame] | 207 | "--iwyu", |
| 208 | action="store_true", |
| 209 | help="Enable include-what-you-use linter.", |
| 210 | ) |
| 211 | parser.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 212 | "packages", |
| 213 | nargs="*", |
| 214 | help="package(s) to emerge and retrieve lints for", |
| 215 | ) |
| 216 | return parser |
Ryan Beltran | 1f2dd08 | 2022-04-25 18:42:32 +0000 | [diff] [blame] | 217 | |
| 218 | |
| 219 | def parse_args(argv: List[str]): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 220 | """Parses arguments in argv and returns the options.""" |
| 221 | parser = get_arg_parser() |
| 222 | opts = parser.parse_args(argv) |
| 223 | opts.Freeze() |
Ryan Beltran | dbd7b81 | 2022-06-08 23:36:16 +0000 | [diff] [blame] | 224 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 225 | # A package must be specified unless we are in fetch-only mode |
| 226 | if not (opts.fetch_only or opts.packages): |
Ryan Beltran | 378934c | 2022-11-23 00:44:26 +0000 | [diff] [blame] | 227 | parser.error("Emerge mode requires specified package(s).") |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 228 | if opts.fetch_only and opts.packages: |
| 229 | parser.error("Cannot specify packages for fetch-only mode.") |
Ryan Beltran | dbd7b81 | 2022-06-08 23:36:16 +0000 | [diff] [blame] | 230 | |
Ryan Beltran | 378934c | 2022-11-23 00:44:26 +0000 | [diff] [blame] | 231 | # A board must be specified unless we are in fetch-only mode |
| 232 | if not (opts.fetch_only or opts.board or opts.host): |
| 233 | parser.error("Emerge mode requires either --board or --host.") |
| 234 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 235 | return opts |
Ryan Beltran | 1f2dd08 | 2022-04-25 18:42:32 +0000 | [diff] [blame] | 236 | |
| 237 | |
| 238 | def main(argv: List[str]) -> None: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 239 | cros_build_lib.AssertInsideChroot() |
| 240 | opts = parse_args(argv) |
Ryan Beltran | 1f2dd08 | 2022-04-25 18:42:32 +0000 | [diff] [blame] | 241 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 242 | if opts.host: |
| 243 | # BuildTarget interprets None as host target |
| 244 | build_target = build_target_lib.BuildTarget(None) |
Ryan Beltran | dbd7b81 | 2022-06-08 23:36:16 +0000 | [diff] [blame] | 245 | else: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 246 | build_target = build_target_lib.BuildTarget(opts.board) |
| 247 | packages = parse_packages(build_target, opts.packages) |
| 248 | package_atoms = [x.atom for x in packages] |
Ryan Beltran | 1f2dd08 | 2022-04-25 18:42:32 +0000 | [diff] [blame] | 249 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 250 | with workon_helper.WorkonScope(build_target, package_atoms): |
| 251 | build_linter = toolchain.BuildLinter( |
| 252 | packages, build_target.root, opts.differential |
| 253 | ) |
| 254 | if opts.fetch_only: |
Ryan Beltran | 378934c | 2022-11-23 00:44:26 +0000 | [diff] [blame] | 255 | if opts.host or opts.board: |
| 256 | roots = [build_target.root] |
| 257 | else: |
| 258 | roots = get_all_sysroots() |
| 259 | lints = [] |
| 260 | for root in roots: |
| 261 | build_linter.sysroot = root |
| 262 | lints.extend( |
| 263 | build_linter.fetch_findings( |
| 264 | use_clippy=opts.clippy, |
| 265 | use_tidy=opts.tidy, |
| 266 | use_golint=opts.golint, |
| 267 | use_iwyu=opts.iwyu, |
| 268 | ) |
| 269 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 270 | else: |
| 271 | lints = build_linter.emerge_with_linting( |
| 272 | use_clippy=opts.clippy, |
| 273 | use_tidy=opts.tidy, |
| 274 | use_golint=opts.golint, |
Ryan Beltran | 378934c | 2022-11-23 00:44:26 +0000 | [diff] [blame] | 275 | use_iwyu=opts.iwyu, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 276 | ) |
Ryan Beltran | ce85d0f | 2022-08-09 21:36:39 +0000 | [diff] [blame] | 277 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 278 | if opts.json: |
Ryan Beltran | a32a1a1 | 2022-09-28 06:03:45 +0000 | [diff] [blame] | 279 | formatted_output_inner = ",\n".join(json_format_lint(l) for l in lints) |
| 280 | formatted_output = f"[{formatted_output_inner}]" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 281 | else: |
Ryan Beltran | a32a1a1 | 2022-09-28 06:03:45 +0000 | [diff] [blame] | 282 | formatted_output = "\n".join(format_lint(l) for l in lints) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 283 | |
| 284 | with file_util.Open(opts.output, "w") as output_file: |
| 285 | output_file.write(formatted_output) |
| 286 | if not opts.json: |
| 287 | output_file.write(f"\nFound {len(lints)} lints.") |
| 288 | output_file.write("\n") |