170 lines
8.4 KiB
Python

import typer
import os
from project_man import findProjectFile, getLibraryInProject, initProjectFile, addLibraryInProject, removeLibraryInProject, getLibrariesPresent
import elab as elaborate
import build_env
from typing import List, Optional
from typing_extensions import Annotated
import subprocess
gantry_install_path = "/home/thesis1/exjobb-public/scripts"
app = typer.Typer()
project = typer.Typer()
software = typer.Typer()
hardware = typer.Typer()
app.add_typer(project, name="project", help="Project management to ease working with the tool")
app.add_typer(software, name="sim", help="GHDL simulator command group")
app.add_typer(hardware, name="syn", help="Synthesis and deployment command group")
def autoDetectLibrary(lib: str) -> str:
if lib == "":
[exists, presentLibs] = getLibrariesPresent()
if not exists:
print("No libs found.")
return "work"
if len(presentLibs.keys()) == 1:
return presentLibs.popitem()[0]
else:
libs = list(map(lambda x: x[0], presentLibs.items()))
libsText = list(map(lambda i: f"{i}" + ": \"" + libs[i] + "\"", [x for x in range(len(libs))]))
selectionIndex = 0
while True:
try:
selectionIndex = int(input("More than one library present, please choose: \n" + "\n".join(libsText) + "\n"))
if selectionIndex < 0 or selectionIndex >=len(libs):
raise ValueError
else:
break
except ValueError:
print("Invalid input!"),
return libs[selectionIndex]
else:
return lib
def autoDetectStd(library: str, std: str) -> str:
if std == "" or std not in complete_vhdl_ver():
(exists, libDict) = getLibraryInProject(library)
if not exists:
return "93"
else:
return libDict["vhdl-version"]
else:
return std
def complete_vhdl_ver():
return ["87", "93", "93c", "00", "02", "08"]
@project.command(help="Creates a project file which allows for further project configuration")
def create(
name: Annotated[str, typer.Argument(help="Name of the project. Has no functional impact")]
):
print(f"Creating a project at {os.getcwd()}/gantry.toml")
initProjectFile(name)
@project.command(name="add-lib", help="Initializes a library in the project (non destructive)")
def addLib(
libname: Annotated[str, typer.Argument(help="Name of the library. This is what is used for imports in VHDL.")],
libpath: Annotated[str, typer.Argument(help="Relative path to the library. This tells simulators where to import form.")],
std: Annotated[str, typer.Option(help="Which VHDL standard to use. 87, 93, 93c, 00, 02 or 08", autocompletion=complete_vhdl_ver)] = "93"
):
print(f"Adding library {libname} to gantry.toml")
addLibraryInProject(libname, libpath, std)
@project.command(name="remove-lib", help="Removes a library in the project (non destructive)")
def removeLib(
libname: Annotated[str, typer.Argument(help="Name of the library.")]
):
print(f"Removing library {libname} from gantry.toml")
removeLibraryInProject(libname)
@software.command(help="Adds files to a library. Automatically updates gantry project file and creates GHDL project files")
def add(
filenames: Annotated[List[str], typer.Argument(help="Which files to add to the library. May be more than one.")],
std: Annotated[str, typer.Option(help="Which VHDL standard to use. 87, 93, 93c, 00, 02 or 08", autocompletion=complete_vhdl_ver)] = "93",
library: Annotated[str, typer.Option("--library", "-l", help="Library to compile to.")] = ""
):
library = autoDetectLibrary(library)
std = autoDetectStd(library, std)
(exists, _) = findProjectFile()
if exists:
addLibraryInProject(library, ".", std)
return build_env.addVHDLFiles(filenames, std, library)
@software.command(help="Removes files from a library. Automatically updates gantry project file and creates GHDL project files")
def remove(
filenames: Annotated[List[str], typer.Argument(help="Which files to add to the library. May be more than one.")],
std: Annotated[str, typer.Option(help="Which VHDL standard to use. 87, 93, 93c, 00, 02 or 08", autocompletion=complete_vhdl_ver)] = "93",
library: Annotated[str, typer.Option("--library", "-l", help="Library to compile to.")] = ""
):
library = autoDetectLibrary(library)
std = autoDetectStd(library, std)
return build_env.removeVHDLFiles(filenames, std, library)
@software.command(help="Runs analysis and elaboration on the provided top definition and architecture using GHDL. Automatically adds new files not present in the project")
def elab(
topdef: Annotated[str, typer.Argument(help="Top Definition entity to synthesize")] = "",
arch: Annotated[str, typer.Argument(help="Architecture to synthesize within the top definition provided")] = "",
library: Annotated[str, typer.Option("--library", "-l", help="Library to compile to")] = "",
includes: Annotated[Optional[List[str]], typer.Option("--include", "-i", help="Which libraries to include in compile")] = None,
std: Annotated[str, typer.Option(help="Which VHDL standard to use. 87, 93, 93c, 00, 02 or 08", autocompletion=complete_vhdl_ver)] = "93"
):
library = autoDetectLibrary(library)
std = autoDetectStd(library, std)
print(f"Elaborating {topdef} with arch {arch} in library {library}. VHDL {std}.")
if includes is not None:
print(f"Including libraries: {includes}")
else:
includes = []
return elaborate.elabDesign(topdef, arch, library, std, includes)
@software.command(help="Simulates elaborated design in GHDL and views waves in gtkwave. Automatically runs `gantry elab` on the same top def and arch.")
def run(
topdef: Annotated[str, typer.Argument(help="Top Definition entity to synthesize")] = "",
arch: Annotated[str, typer.Argument(help="Architecture to synthesize within the top definition provided")] = "",
library: Annotated[str, typer.Option("--library", "-l", help="Library to compile to")] = "",
includes: Annotated[Optional[List[str]], typer.Option("--include", "-i", help="Which libraries to include in compile")] = None,
std: Annotated[str, typer.Option(help="Which VHDL standard to use. 87, 93, 93c, 00, 02 or 08", autocompletion=complete_vhdl_ver)] = "93"
):
library = autoDetectLibrary(library)
std = autoDetectStd(library, std)
print(f"Running (and synthesizing if needed) {topdef} with arch {arch} in library {library}. VHDL {std}")
if includes is not None:
print(f"Including libraries: {includes}")
else:
includes = []
return elaborate.runDesign(topdef, arch, library, std, includes)
@hardware.command(help="Synthesizes the provided top level design using NXPython. Make sure you run this with NXPython.")
def synth(
topdef: Annotated[str, typer.Argument(help="Top Definition entity to synthesize")] = "",
library: Annotated[str, typer.Option("--library", "-l", help="Library to compile todefaults to \"\"")] = ""
):
proc = subprocess.run(["source_and_run.sh", f"{gantry_install_path}/nxp_script.py", "synthDesign", library, topdef])
@hardware.command(help="Places the provided top level design using NXPython. Make sure you run this with NXPython.")
def place(
topdef: Annotated[str, typer.Argument(help="Top Definition entity to synthesize")] = "",
library: Annotated[str, typer.Option("--library", "-l", help="Library to compile to. If current directory contains libraries, it will be automatically detected")] = ""
):
proc = subprocess.run(["source_and_run.sh", f"{gantry_install_path}/nxp_script.py", "placeDesign", library, topdef])
@hardware.command(help="Routes the provided top level design using NXPython. Make sure you run this with NXPython.")
def route(
topdef: Annotated[str, typer.Argument(help="Top Definition entity to synthesize")] = "",
library: Annotated[str, typer.Option("--library", "-l", help="Library to compile to. If current directory contains libraries, it will be automatically detected")] = ""
):
proc = subprocess.run(["source_and_run.sh", f"{gantry_install_path}/nxp_script.py", "routeDesign", library, topdef])
@hardware.command(help="")
def build():
print("Build!")
if __name__ == "__main__":
app()