Coverage for tsfpga/create_vhdl_ls_config.py: 0%
34 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 20:52 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 20:52 +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# --------------------------------------------------------------------------------------------------
9# Standard libraries
10from pathlib import Path
11from typing import TYPE_CHECKING, Any, Optional
13# Third party libraries
14import rtoml
16# First party libraries
17from tsfpga.vivado.ip_cores import VivadoIpCores
19if TYPE_CHECKING:
20 # Third party libraries
21 from vunit.ui import VUnit
23 # Local folder libraries
24 from .module_list import ModuleList
27def create_configuration(
28 output_path: Path,
29 modules: Optional["ModuleList"] = None,
30 vunit_proj: Optional["VUnit"] = None,
31 files: Optional[list[tuple[Path, str]]] = None,
32 vivado_location: Optional[Path] = None,
33 ip_core_vivado_project_directory: Optional[Path] = None,
34) -> None:
35 """
36 Create a configuration file (``vhdl_ls.toml``) for the rust_hdl VHDL Language Server
37 (https://github.com/VHDL-LS/rust_hdl).
39 Can be used with modules and an "empty" VUnit project, or with a complete VUnit
40 project with all user files added.
41 Files can also be added manually with the ``files`` argument.
43 Execution of this function takes roughly 12 ms for a large project (62 modules and a
44 VUnit project).
46 Arguments:
47 output_path: vhdl_ls.toml file will be placed in this folder.
48 modules: All files from these modules will be added.
49 vunit_proj: All files in this VUnit project will be added.
50 This includes the files from VUnit itself, and any user files.
52 .. warning::
53 Using a VUnit project with user files and location/check preprocessing enabled is
54 dangerous, since it introduces the risk of editing a generated file.
55 files: All files listed here will be added.
56 Can be used to add additional files outside of the modules or the VUnit project.
57 The list shall contain tuples: ``(Path, "library name")``.
58 vivado_location: Vivado binary path.
59 The ``unisim`` from this Vivado installation will be added.
60 ip_core_vivado_project_directory: Path to a Vivado project that contains
61 generated "simulation" and "synthesis" files of IP cores
62 (the "generate_target" TCL command).
63 See :py:mod:`.examples.simulate.py` for an example of using this.
64 """
65 toml_data: dict[str, dict[str, Any]] = dict(libraries={})
67 def add_file(file_path: Path, library_name: str) -> None:
68 """
69 Note that 'file_path' may contain wildcards.
70 """
71 if library_name not in toml_data["libraries"]:
72 toml_data["libraries"][library_name] = dict(files=[])
74 if library_name in ["vunit_lib", "osvvm", "unisim", "xil_defaultlib"]:
75 toml_data["libraries"][library_name]["is_third_party"] = True
77 toml_data["libraries"][library_name]["files"].append(str(file_path.resolve()))
79 if modules is not None:
80 for module in modules:
81 add_file(file_path=module.path / "**" / "*.vhd", library_name=module.library_name)
83 if vunit_proj is not None:
84 for source_file in vunit_proj.get_compile_order():
85 add_file(file_path=Path(source_file.name), library_name=source_file.library.name)
87 if files is not None:
88 for file_path, library_name in files:
89 add_file(file_path=file_path, library_name=library_name)
91 if vivado_location is not None:
92 vcomponents_package = (
93 vivado_location.parent.parent
94 / "data"
95 / "vhdl"
96 / "src"
97 / "unisims"
98 / "unisim_retarget_VCOMP.vhd"
99 )
100 if not vcomponents_package.exists():
101 raise FileNotFoundError(f"Could not find unisim file: {vcomponents_package}")
103 add_file(file_path=vcomponents_package, library_name="unisim")
105 if ip_core_vivado_project_directory is not None:
106 # Vivado 2020.2+ (?) seems to place the files in "gen"
107 ip_gen_dir = (
108 ip_core_vivado_project_directory / f"{VivadoIpCores.project_name}.gen" / "sources_1"
109 )
110 if ip_gen_dir.exists():
111 add_file(file_path=ip_gen_dir / "ip" / "**" / "*.vhd", library_name="xil_defaultlib")
113 rtoml.dump(obj=toml_data, file=output_path / "vhdl_ls.toml", pretty=True)