Coverage for tsfpga/examples/simulate.py: 0%

48 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-10 20:51 +0000

1# -------------------------------------------------------------------------------------------------- 

2# Copyright (c) Lukas Vik. All rights reserved. 

3# 

4# This file is part of the tsfpga project, a project platform for modern FPGA development. 

5# https://tsfpga.com 

6# https://github.com/tsfpga/tsfpga 

7# -------------------------------------------------------------------------------------------------- 

8 

9# Standard libraries 

10import argparse 

11import sys 

12from pathlib import Path 

13from typing import TYPE_CHECKING, Any, Optional 

14 

15if TYPE_CHECKING: 

16 # First party libraries 

17 from tsfpga.module_list import ModuleList 

18 

19# Do PYTHONPATH insert() instead of append() to prefer any local repo checkout over any pip install 

20REPO_ROOT = Path(__file__).parent.parent.parent.resolve() 

21sys.path.insert(0, str(REPO_ROOT)) 

22 

23# Import before others since it modifies PYTHONPATH. pylint: disable=unused-import 

24import tsfpga.examples.example_pythonpath # noqa: F401 

25 

26# First party libraries 

27import tsfpga 

28import tsfpga.create_vhdl_ls_config 

29from tsfpga.create_ghdl_ls_config import create_ghdl_ls_configuration 

30from tsfpga.examples.example_env import ( 

31 TSFPGA_EXAMPLES_TEMP_DIR, 

32 get_hdl_modules, 

33 get_tsfpga_example_modules, 

34) 

35from tsfpga.examples.simulation_utils import ( 

36 SimulationProject, 

37 create_vhdl_ls_configuration, 

38 get_arguments_cli, 

39) 

40from tsfpga.git_simulation_subset import GitSimulationSubset 

41 

42 

43def main() -> None: 

44 """ 

45 Main function for the simulation flow. If you are setting up a new simulation environment 

46 you probably want to copy and modify this function. The other functions and classes 

47 should be reusable in most cases. 

48 """ 

49 cli = get_arguments_cli(default_output_path=TSFPGA_EXAMPLES_TEMP_DIR) 

50 args = cli.parse_args() 

51 

52 modules = get_tsfpga_example_modules() 

53 

54 # Avoid the module that depends on Xilinx unisim library 

55 module_names_avoid = set(["hard_fifo"]) if args.vivado_skip else None 

56 modules_no_sim = get_hdl_modules(names_avoid=module_names_avoid) 

57 

58 if args.vcs_minimal: 

59 if args.test_patterns != "*": 

60 sys.exit( 

61 "Can not specify a test pattern when using the --vcs-minimal flag." 

62 f" Got {args.test_patterns}", 

63 ) 

64 

65 test_filters = find_git_test_filters( 

66 args=args, 

67 repo_root=tsfpga.REPO_ROOT, 

68 modules=modules, 

69 modules_no_sim=modules_no_sim, 

70 reference_branch="origin/main", 

71 ) 

72 if not test_filters: 

73 print("Nothing to run. Appears to be no VHDL-related git diff.") 

74 return 

75 

76 # Override the test pattern argument to VUnit 

77 args.test_patterns = test_filters 

78 print(f"Running VUnit with test pattern {args.test_patterns}") 

79 

80 # Enable minimal compilation in VUnit 

81 args.minimal = True 

82 

83 simulation_project = SimulationProject(args=args) 

84 simulation_project.add_modules(args=args, modules=modules, modules_no_sim=modules_no_sim) 

85 simlib = simulation_project.add_vivado_simlib(args=args) 

86 ip_core_vivado_project_directory = simulation_project.add_vivado_ip_cores( 

87 args=args, modules=modules + modules_no_sim 

88 ) 

89 

90 create_vhdl_ls_configuration( 

91 output_path=tsfpga.REPO_ROOT, 

92 temp_files_path=TSFPGA_EXAMPLES_TEMP_DIR, 

93 modules=modules + modules_no_sim, 

94 ip_core_vivado_project_directory=ip_core_vivado_project_directory, 

95 ) 

96 

97 create_ghdl_ls_configuration( 

98 output_path=tsfpga.REPO_ROOT, 

99 modules=modules + modules_no_sim, 

100 vunit_proj=simulation_project.vunit_proj, 

101 simlib=simlib, 

102 ) 

103 

104 simulation_project.vunit_proj.main() 

105 

106 

107def find_git_test_filters( 

108 args: argparse.Namespace, 

109 repo_root: Path, 

110 modules: "ModuleList", 

111 modules_no_sim: Optional["ModuleList"] = None, 

112 reference_branch: str = "origin/master", 

113 **setup_vunit_kwargs: Any, 

114) -> list[str]: 

115 """ 

116 Construct a VUnit test filter that will run all test cases that are affected by git changes. 

117 The current git state is compared to a reference branch, and differences are derived. 

118 See :class:`.GitSimulationSubset` for details. 

119 

120 Arguments: 

121 args: Command line argument namespace. 

122 repo_root: Path to the repository root. Git commands will be run here. 

123 modules: Will be passed on to :meth:`.SimulationProject.add_modules`. 

124 modules_no_sim: Will be passed on to :meth:`.SimulationProject.add_modules`. 

125 reference_branch (str): The name of the reference branch that is used to collect a diff. 

126 setup_vunit_kwargs : Will be passed on to :meth:`.SimulationProject.add_modules`. 

127 

128 Return: 

129 A list of VUnit test case filters. 

130 """ 

131 # Set up a dummy VUnit project that will be used for dependency scanning. 

132 # Note that sources are added identical to the "real" project above. 

133 simulation_project = SimulationProject(args=args) 

134 simulation_project.add_modules( 

135 args=args, modules=modules, modules_no_sim=modules_no_sim, **setup_vunit_kwargs 

136 ) 

137 

138 testbenches_to_run = GitSimulationSubset( 

139 repo_root=repo_root, 

140 reference_branch=reference_branch, 

141 vunit_proj=simulation_project.vunit_proj, 

142 ).find_subset() 

143 

144 test_filters = [] 

145 for testbench_file_name, library_name in testbenches_to_run: 

146 test_filters.append(f"{library_name}.{testbench_file_name}.*") 

147 

148 return test_filters 

149 

150 

151if __name__ == "__main__": 

152 main()