Coverage for apio/commands/apio_lint.py: 100%

27 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-09 01:55 +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 cmd_util 

15from apio.commands import options 

16from apio.apio_context import ( 

17 ApioContext, 

18 PackagesPolicy, 

19 ProjectPolicy, 

20 RemoteConfigPolicy, 

21) 

22from apio.common.proto.apio_pb2 import LintParams 

23 

24 

25# ------- apio lint 

26 

27nosynth_option = click.option( 

28 "nosynth", # Var name 

29 "--nosynth", 

30 is_flag=True, 

31 help="Do not define the SYNTHESIS macro.", 

32 cls=cmd_util.ApioOption, 

33) 

34 

35novlt_option = click.option( 

36 "novlt", # Var name 

37 "--novlt", 

38 is_flag=True, 

39 help="Disable warning suppression .vlt file.", 

40 cls=cmd_util.ApioOption, 

41) 

42 

43 

44# -- Text in the rich-text format of the python rich library. 

45APIO_LINT_HELP = """ 

46The command 'apio lint' scans the project's source files and reports errors, \ 

47inconsistencies, and style violations. The command uses the Verilator tool, \ 

48which is included with the standard Apio installation. 

49 

50If specified files are not specified, the top module of the project and \ 

51its dependencies are linted. 

52 

53Examples:[code] 

54 apio lint 

55 apio lint -t my_module 

56 apio lint file1.v file2.v 

57 apio lint --nosynth 

58 apio lint --novlt[/code] 

59 

60By default, 'apio lint' injects the 'SYNTHESIS' macro to lint the \ 

61synthesizable portion of the design. To lint code that is hidden by \ 

62'SYNTHESIS', use the '--nosynth' option. 

63 

64To customize the behavior of the 'verilator' linter, add the option \ 

65'verilator-extra-option' in the project file 'apio.ini' with the extra \ 

66options you would like to use. 

67""" 

68 

69 

70@click.command( 

71 name="lint", 

72 cls=cmd_util.ApioCommand, 

73 short_help="Lint the source code.", 

74 help=APIO_LINT_HELP, 

75) 

76@click.pass_context 

77@click.argument("files", nargs=-1, required=False) 

78@nosynth_option 

79@novlt_option 

80@options.top_module_option_gen( 

81 short_help="Restrict linting to this module and its dependencies." 

82) 

83@options.env_option_gen() 

84@options.project_dir_option 

85def cli( 

86 _: click.Context, 

87 *, 

88 # Args 

89 files, 

90 # Options 

91 nosynth: bool, 

92 novlt: bool, 

93 top_module: str, 

94 env: Optional[str], 

95 project_dir: Optional[Path], 

96): 

97 """Lint the source code.""" 

98 

99 # pylint: disable=too-many-arguments 

100 

101 # -- Create the apio context. 

102 apio_ctx = ApioContext( 

103 project_policy=ProjectPolicy.PROJECT_REQUIRED, 

104 remote_config_policy=RemoteConfigPolicy.CACHED_OK, 

105 packages_policy=PackagesPolicy.ENSURE_PACKAGES, 

106 project_dir_arg=project_dir, 

107 env_arg=env, 

108 ) 

109 

110 # -- Create the scons manager. 

111 scons = SConsManager(apio_ctx) 

112 

113 # -- Create the lint params 

114 lint_params = LintParams( 

115 top_module=top_module if top_module else None, 

116 nosynth=nosynth, 

117 novlt=novlt, 

118 file_names=files, 

119 ) 

120 

121 assert lint_params.IsInitialized(), lint_params 

122 

123 # -- Lint the project with the given parameters 

124 exit_code = scons.lint(lint_params) 

125 sys.exit(exit_code)