Coverage for apio/commands/apio_lint.py: 100%
26 statements
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-23 03:53 +0000
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-23 03:53 +0000
1# -*- coding: utf-8 -*-
2# -- This file is part of the Apio project
3# -- (C) 2016-2024 FPGAwars
4# -- Authors
5# -- * Jesús Arroyo (2016-2019)
6# -- * Juan Gonzalez (obijuan) (2019-2024)
7# -- License GPLv2
8"""Implementation of 'apio lint' command"""
9import sys
10from pathlib import Path
11import click
12from apio.managers.scons_manager import SConsManager
13from apio.utils import cmd_util
14from apio.commands import options
15from apio.apio_context import (
16 ApioContext,
17 PackagesPolicy,
18 ProjectPolicy,
19 RemoteConfigPolicy,
20)
21from apio.common.proto.apio_scons_pb2 import LintParams
24# ------- apio lint
26nosynth_option = click.option(
27 "nosynth", # Var name
28 "--nosynth",
29 is_flag=True,
30 help="Do not define the SYNTHESIS macro.",
31 cls=cmd_util.ApioOption,
32)
34novlt_option = click.option(
35 "novlt", # Var name
36 "--novlt",
37 is_flag=True,
38 help="Disable warning suppression .vlt file.",
39 cls=cmd_util.ApioOption,
40)
43# -- Text in the rich-text format of the python rich library.
44APIO_LINT_HELP = """
45The command 'apio lint' scans the project's source files and reports errors, \
46inconsistencies, and style violations. The command uses the Verilator tool, \
47which is included with the standard Apio installation.
49If specified files are not specified, the top module of the project and \
50its dependencies are linted.
52Examples:[code]
53 apio lint
54 apio lint -t my_module
55 apio lint file1.v file2.v
56 apio lint --nosynth
57 apio lint --novlt[/code]
59By default, 'apio lint' injects the 'SYNTHESIS' macro to lint the \
60synthesizable portion of the design. To lint code that is hidden by \
61'SYNTHESIS', use the '--nosynth' option.
63To customize the behavior of the 'verilator' linter, add the option \
64'verilator-extra-option' in the project file 'apio.ini' with the extra \
65options you would like to use.
66"""
69@click.command(
70 name="lint",
71 cls=cmd_util.ApioCommand,
72 short_help="Lint the source code.",
73 help=APIO_LINT_HELP,
74)
75@click.pass_context
76@click.argument("files", nargs=-1, required=False)
77@nosynth_option
78@novlt_option
79@options.top_module_option_gen(
80 short_help="Restrict linting to this module and its dependencies."
81)
82@options.env_option_gen()
83@options.project_dir_option
84def cli(
85 _: click.Context,
86 *,
87 # Args
88 files,
89 # Options
90 nosynth: bool,
91 novlt: bool,
92 top_module: str,
93 env: str | None,
94 project_dir: Path | None,
95):
96 """Lint the source code."""
98 # pylint: disable=too-many-arguments
100 # -- Create the apio context.
101 apio_ctx = ApioContext(
102 project_policy=ProjectPolicy.PROJECT_REQUIRED,
103 remote_config_policy=RemoteConfigPolicy.CACHED_OK,
104 packages_policy=PackagesPolicy.ENSURE_PACKAGES,
105 project_dir_arg=project_dir,
106 env_arg=env,
107 )
109 # -- Create the scons manager.
110 scons = SConsManager(apio_ctx)
112 # -- Create the lint params
113 lint_params = LintParams(
114 top_module=top_module if top_module else None,
115 nosynth=nosynth,
116 novlt=novlt,
117 file_names=files,
118 )
120 assert lint_params.IsInitialized(), lint_params
122 # -- Lint the project with the given parameters
123 exit_code = scons.lint(lint_params)
124 sys.exit(exit_code)