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

27 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-25 02:31 +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 --all 

58 apio lint --nosynth 

59 apio lint --novlt[/code] 

60 

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

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

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

64 

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

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

67options you would like to use. 

68""" 

69 

70 

71@click.command( 

72 name="lint", 

73 cls=cmd_util.ApioCommand, 

74 short_help="Lint the source code.", 

75 help=APIO_LINT_HELP, 

76) 

77@click.pass_context 

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

79@nosynth_option 

80@novlt_option 

81@options.top_module_option_gen( 

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

83) 

84@options.env_option_gen() 

85@options.project_dir_option 

86def cli( 

87 _: click.Context, 

88 *, 

89 # Args 

90 files, 

91 # Options 

92 nosynth: bool, 

93 novlt: bool, 

94 top_module: str, 

95 env: Optional[str], 

96 project_dir: Optional[Path], 

97): 

98 """Lint the source code.""" 

99 

100 # pylint: disable=too-many-arguments 

101 

102 # -- Create the apio context. 

103 apio_ctx = ApioContext( 

104 project_policy=ProjectPolicy.PROJECT_REQUIRED, 

105 remote_config_policy=RemoteConfigPolicy.CACHED_OK, 

106 packages_policy=PackagesPolicy.ENSURE_PACKAGES, 

107 project_dir_arg=project_dir, 

108 env_arg=env, 

109 ) 

110 

111 # -- Create the scons manager. 

112 scons = SConsManager(apio_ctx) 

113 

114 # -- Create the lint params 

115 lint_params = LintParams( 

116 top_module=top_module if top_module else None, 

117 nosynth=nosynth, 

118 novlt=novlt, 

119 file_names=files, 

120 ) 

121 

122 assert lint_params.IsInitialized(), lint_params 

123 

124 # -- Lint the project with the given parameters 

125 exit_code = scons.lint(lint_params) 

126 sys.exit(exit_code)