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