Ryan Beltran | 1f2dd08 | 2022-04-25 18:42:32 +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 | """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 |
| 12 | import sys |
| 13 | from typing import List |
| 14 | |
| 15 | from chromite.lib import build_target_lib |
| 16 | from chromite.lib import commandline |
| 17 | from chromite.lib import cros_build_lib |
| 18 | from chromite.lib.parser import package_info |
| 19 | from chromite.service import toolchain |
| 20 | from chromite.utils import file_util |
| 21 | |
| 22 | |
| 23 | def get_arg_parser() -> commandline.ArgumentParser: |
| 24 | """Creates an argument parser for this script.""" |
| 25 | default_board = cros_build_lib.GetDefaultBoard() |
| 26 | parser = commandline.ArgumentParser(description=__doc__) |
| 27 | parser.add_argument( |
| 28 | '--differential', |
| 29 | action='store_true', |
| 30 | help='only lint lines touched by the last commit') |
| 31 | parser.add_argument( |
| 32 | '-b', |
| 33 | '--board', |
| 34 | '--build-target', |
| 35 | dest='board', |
| 36 | default=default_board, |
| 37 | required=not default_board, |
| 38 | help='The board to emerge packages for') |
| 39 | parser.add_argument( |
| 40 | '-o', |
| 41 | '--output', |
| 42 | default=sys.stdout, |
| 43 | help='File to use instead of stdout.') |
| 44 | parser.add_argument( |
| 45 | '--no-clippy', |
| 46 | dest='clippy', |
| 47 | action='store_false', |
| 48 | help='Disable cargo clippy linter.') |
| 49 | parser.add_argument( |
| 50 | '--no-tidy', |
| 51 | dest='tidy', |
| 52 | action='store_false', |
| 53 | help='Disable clang tidy linter.') |
| 54 | parser.add_argument( |
| 55 | 'packages', nargs='+', help='package(s) to emerge and retrieve lints for') |
| 56 | return parser |
| 57 | |
| 58 | |
| 59 | def parse_args(argv: List[str]): |
| 60 | """Parses arguments in argv and returns the options.""" |
| 61 | parser = get_arg_parser() |
| 62 | opts = parser.parse_args(argv) |
| 63 | opts.Freeze() |
| 64 | return opts |
| 65 | |
| 66 | |
| 67 | def main(argv: List[str]) -> None: |
| 68 | cros_build_lib.AssertInsideChroot() |
| 69 | opts = parse_args(argv) |
| 70 | |
| 71 | packages = [package_info.parse(x) for x in opts.packages] |
| 72 | sysroot = build_target_lib.get_default_sysroot_path(opts.board) |
| 73 | |
| 74 | build_linter = toolchain.BuildLinter(packages, sysroot, opts.differential) |
| 75 | lints = build_linter.emerge_with_linting( |
| 76 | use_clippy=opts.clippy, use_tidy=opts.tidy) |
| 77 | |
| 78 | with file_util.Open(opts.output, 'w') as output_file: |
| 79 | json.dump(lints, output_file) |