Coverage for tsfpga/examples/build_fpga.py: 0%
32 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-01 20:51 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-01 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# --------------------------------------------------------------------------------------------------
9import sys
10from pathlib import Path
11from shutil import copy2, make_archive
12from typing import TYPE_CHECKING
14# Do PYTHONPATH insert() instead of append() to prefer any local repo checkout over any pip install.
15REPO_ROOT = Path(__file__).parent.parent.parent.resolve()
16sys.path.insert(0, str(REPO_ROOT))
18# Import before others since it modifies PYTHONPATH.
19import tsfpga.examples.example_pythonpath # noqa: F401
21from tsfpga.build_project_list import BuildProjectList
22from tsfpga.examples.build_fpga_utils import arguments, generate_register_artifacts, setup_and_run
23from tsfpga.examples.example_env import TSFPGA_EXAMPLES_TEMP_DIR, get_tsfpga_example_modules
24from tsfpga.system_utils import create_directory, delete
26if TYPE_CHECKING:
27 from tsfpga.vivado.project import VivadoProject
30def main() -> None:
31 """
32 Main function for building FPGA projects. If you are setting up a new build flow from scratch,
33 you probably want to copy and modify this function, and reuse the others.
34 """
35 args = arguments(default_temp_dir=TSFPGA_EXAMPLES_TEMP_DIR)
36 modules = get_tsfpga_example_modules()
37 projects = BuildProjectList(
38 modules=modules,
39 project_filters=args.project_filters,
40 include_netlist_not_top_builds=args.netlist_builds,
41 no_color=args.no_color,
42 )
44 sys.exit(
45 setup_and_run(
46 modules=modules,
47 projects=projects,
48 args=args,
49 collect_artifacts_function=collect_artifacts,
50 )
51 )
54def collect_artifacts(project: "VivadoProject", output_path: Path) -> bool:
55 """
56 Example of a method to collect build artifacts. Will create a zip file with the bitstream,
57 hardware definition (.xsa) and register documentation.
59 Arguments:
60 project: Project object that has been built, and who's artifacts shall now be collected.
61 output_path: Path to the build output. Artifact zip will be placed here as well.
63 Return:
64 True if everything went well.
65 """
66 version = "0.0.0"
67 release_dir = create_directory(output_path / f"{project.name}-{version}", empty=True)
68 print(f"Creating release in {release_dir.resolve()}.zip")
70 generate_register_artifacts(modules=project.modules, output_path=release_dir / "registers")
71 copy2(output_path / f"{project.name}.bit", release_dir)
72 copy2(output_path / f"{project.name}.bin", release_dir)
73 if (output_path / f"{project.name}.xsa").exists():
74 copy2(output_path / f"{project.name}.xsa", release_dir)
76 make_archive(str(release_dir), "zip", release_dir)
78 # Remove folder so that only zip remains
79 delete(release_dir)
81 return True
84if __name__ == "__main__":
85 main()