Coverage for tsfpga/constraint.py: 94%
16 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 pathlib import Path
10from typing import TYPE_CHECKING, Literal
12if TYPE_CHECKING:
13 from .hdl_file import HdlFile
16class Constraint:
17 """
18 Class for handling a constraint file.
20 Can handle the regular global constraint files as well as scoped constraints.
21 For the latter to work the constraint file name must be the same as the .vhd file name,
22 which must be the same as the entity name.
23 """
25 def __init__(
26 self,
27 file: Path,
28 used_in: Literal["all", "synth", "impl"] = "all",
29 scoped_constraint: bool = False,
30 processing_order: Literal["early", "normal", "late"] = "normal",
31 ) -> None:
32 """
33 Arguments:
34 file: Path to the constraint file. Typically ends in .xdc or .tcl.
35 used_in: Optionally the constraint can be enabled only for "synth" or "impl".
36 scoped_constraint: If enabled the constraint file will be loaded with the "-ref"
37 argument in Vivado. An entity with the same name must exist.
38 processing_order: Optionally the processing order can be changed to "early" or "late".
39 """
40 self.file = file
41 self.used_in = used_in
42 self.ref = file.stem if scoped_constraint else None
43 self.processing_order = processing_order.lower()
45 def validate_scoped_entity(self, source_files: list["HdlFile"]) -> bool:
46 """
47 Make sure that a matching entity file exists in case this is a scoped constraint.
48 The list of source files should be the synthesis files for the module that this
49 constraint belongs to.
50 """
51 if self.ref is not None and not any(
52 [source_file.path.stem == self.ref] for source_file in source_files
53 ):
54 raise FileNotFoundError(
55 f"Could not find a matching entity file for scoped constraint file {self.file}"
56 )
58 return True
60 def __str__(self) -> str:
61 return str(self.file)