import typer import os from project_man import findProjectFile, initProjectFile, addLibraryInProject, projectFileExists, 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 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)] = "93c" ): 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="Initializes the GHDL build environment in a library named \"work\". Adds all files ending in \".vhd\" to the project") def init( std: Annotated[str, typer.Option(help="Which VHDL standard to use. 87, 93, 93c, 00, 02 or 08", autocompletion=complete_vhdl_ver)] = "93c", library: Annotated[str, typer.Option("--library", "-l", help="Library to compile to. Defaults to \"work\"")] = "work", ): (exists, _) = findProjectFile() if exists: addLibraryInProject(library, ".", std) return build_env.createBuildEnv(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)] = "93c" ): if library == "": [exists, presentLibs] = getLibrariesPresent() if not exists: print("No libs found.") return if len(presentLibs.keys()) == 1: library = presentLibs.popitem()[0] else: libs = list(map(lambda x: "\"" + x[0] + "\"", presentLibs.items())) print("More than one library present, please specify one by adding the flag -l with one of: " + " ".join(libs)) return 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)] = "93c" ): 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()