Coverage for tsfpga/create_ghdl_ls_config.py: 0%

33 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-02-21 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 

9from __future__ import annotations 

10 

11import json 

12from pathlib import Path 

13from typing import TYPE_CHECKING 

14 

15from .system_utils import create_file, path_relative_to 

16 

17if TYPE_CHECKING: 

18 from vunit.ui import VUnit 

19 

20 from .module_list import ModuleList 

21 from .vivado.simlib_common import VivadoSimlibCommon 

22 

23 

24def create_ghdl_ls_configuration( # noqa: C901 

25 output_path: Path, 

26 modules: ModuleList, 

27 vunit_proj: VUnit, 

28 simlib: VivadoSimlibCommon | None = None, 

29) -> None: 

30 """ 

31 Create a configuration file (hdl-prj.json) for the vhdl-lsp VHDL Language Server 

32 (https://github.com/ghdl/ghdl-language-server). 

33 

34 Can be used with modules and an "empty" VUnit project, or with a complete VUnit 

35 project with all user files added. 

36 

37 Execution of this function takes roughly 12 ms for a large project (62 modules and a 

38 VUnit project). 

39 

40 Arguments: 

41 output_path: Output folder. 

42 modules: Source files from these modules will be included. 

43 vunit_proj: A VUnit project. 

44 simlib: Source from this Vivado simlib project will be added. 

45 """ 

46 

47 def get_relative_path(path: Path) -> Path: 

48 return path_relative_to(path=path, other=output_path) 

49 

50 data = {"options": {"ghdl_analysis": []}, "files": []} 

51 

52 def add_compiled_library(path: Path) -> None: 

53 relative_path = get_relative_path(path) 

54 data["options"]["ghdl_analysis"].append(f"-P{relative_path}") 

55 

56 data["options"]["ghdl_analysis"] += ["--std=08"] 

57 

58 compiled_vunit_libraries_path = ( 

59 Path(vunit_proj._output_path) / "ghdl" / "libraries" # noqa: SLF001 

60 ) 

61 for compiled_library_path in compiled_vunit_libraries_path.glob("*"): 

62 add_compiled_library(compiled_library_path) 

63 

64 if simlib is not None: 

65 for library_name in simlib.library_names: 

66 add_compiled_library(simlib.output_path / library_name) 

67 

68 # The same file might be present in both module file list as well as VUnit project. 

69 # However the opposite might also be true in many cases. 

70 # E.g. files that depend on IP cores that are not included in the simulation project. 

71 # For this reason, add all files to a set first to avoid duplicates. 

72 files = set() 

73 

74 if modules is not None: 

75 for module in modules: 

76 for hdl_file in module.get_simulation_files( 

77 include_ip_cores=False, 

78 include_verilog_files=False, 

79 include_systemverilog_files=False, 

80 ): 

81 files.add(hdl_file.path) 

82 

83 for source_file in vunit_proj.get_compile_order(): 

84 files.add(Path(source_file.name).resolve()) 

85 

86 for file_path in files: 

87 data["files"].append({"file": str(get_relative_path(file_path)), "language": "vhdl"}) 

88 

89 create_file(output_path / "hdl-prj.json", json.dumps(data))