Coverage for apio / commands / apio_lint.py: 100%
36 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-08 02:47 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-08 02:47 +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 typing import Optional
11from pathlib import Path
12import click
13from apio.managers.scons_manager import SConsManager
14from apio.utils import util
15from apio.utils import cmd_util
16from apio.commands import options
17from apio.apio_context import (
18 ApioContext,
19 PackagesPolicy,
20 ProjectPolicy,
21 RemoteConfigPolicy,
22)
23from apio.common.proto.apio_pb2 import LintParams
26# ------- apio lint
28nosynth_option = click.option(
29 "nosynth", # Var name
30 "--nosynth",
31 is_flag=True,
32 help="Do not inject the SUNTHESIS macro.",
33 cls=cmd_util.ApioOption,
34)
36novlt_option = click.option(
37 "novlt", # Var name
38 "--novlt",
39 is_flag=True,
40 help="Disable warning suppression .vlt file.",
41 cls=cmd_util.ApioOption,
42)
44nostyle_option = click.option(
45 "nostyle", # Var name
46 "--nostyle",
47 is_flag=True,
48 help="Disable all style warnings.",
49 cls=cmd_util.ApioOption,
50)
53nowarn_option = click.option(
54 "nowarn", # Var name
55 "--nowarn",
56 type=str,
57 metavar="nowarn",
58 help="Disable specific warning(s).",
59 cls=cmd_util.ApioOption,
60)
62warn_option = click.option(
63 "warn", # Var name
64 "--warn",
65 type=str,
66 metavar="warn",
67 help="Enable specific warning(s).",
68 cls=cmd_util.ApioOption,
69)
72# -- Text in the rich-text format of the python rich library.
73APIO_LINT_HELP = """
74The command 'apio lint' scans the project's source files and reports errors, \
75inconsistencies, and style violations. The command uses the Verilator tool, \
76which is included with the standard Apio installation.
78Examples:[code]
79 apio lint
80 apio lint -t my_module
81 apio lint --all
82 apio lint --nosynth
83 apio lint --novlt[/code]
85By default, 'apio lint' injects the 'SYNTHESIS' macro to lint the \
86synthesizable portion of the design. To lint code that is hidden by \
87'SYNTHESIS', use the '--nosynth' option.
88"""
91@click.command(
92 name="lint",
93 cls=cmd_util.ApioCommand,
94 short_help="Lint the source code.",
95 help=APIO_LINT_HELP,
96)
97@click.pass_context
98@nosynth_option
99@novlt_option
100@nostyle_option
101@nowarn_option
102@warn_option
103@options.all_option_gen(
104 short_help="Enable all warnings, including code style warnings."
105)
106@options.top_module_option_gen(
107 short_help="Restrict linting to this module and its dependencies."
108)
109@options.env_option_gen()
110@options.project_dir_option
111def cli(
112 _: click.Context,
113 # Options
114 nosynth: bool,
115 novlt: bool,
116 nostyle: bool,
117 nowarn: str,
118 warn: str,
119 all_: bool,
120 top_module: str,
121 env: Optional[str],
122 project_dir: Optional[Path],
123):
124 """Lint the source code."""
126 # pylint: disable=too-many-arguments
127 # pylint: disable=too-many-positional-arguments
129 # -- Create the apio context.
130 apio_ctx = ApioContext(
131 project_policy=ProjectPolicy.PROJECT_REQUIRED,
132 remote_config_policy=RemoteConfigPolicy.CACHED_OK,
133 packages_policy=PackagesPolicy.ENSURE_PACKAGES,
134 project_dir_arg=project_dir,
135 env_arg=env,
136 )
138 # -- Create the scons manager.
139 scons = SConsManager(apio_ctx)
141 # -- Convert the comma separated args values to python lists
142 no_warns_list = util.split(nowarn, ",", strip=True, keep_empty=False)
143 warns_list = util.split(warn, ",", strip=True, keep_empty=False)
145 # -- Create the lint params
146 lint_params = LintParams(
147 top_module=top_module if top_module else None,
148 verilator_all=all_,
149 verilator_no_style=nostyle,
150 verilator_no_warns=no_warns_list,
151 verilator_warns=warns_list,
152 nosynth=nosynth,
153 novlt=novlt,
154 )
156 assert lint_params.IsInitialized(), lint_params
158 # -- Lint the project with the given parameters
159 exit_code = scons.lint(lint_params)
160 sys.exit(exit_code)