diff --git a/scripts/build_env.py b/scripts/build_env.py index 7479baa..688041f 100644 --- a/scripts/build_env.py +++ b/scripts/build_env.py @@ -1,4 +1,5 @@ import os +from re import split import subprocess def ghdlEnvExists(relativePath="work"): @@ -23,8 +24,10 @@ def ghdlEnvExists(relativePath="work"): def createBuildEnv(): if ghdlEnvExists(): - return -1 + print("Build environment already exists, exiting...") + return -1 ## Create build env + print("Initializing GHDL project in current directory...") os.makedirs("work",exist_ok=True) addAllVHDLFiles(init=True) return 0 @@ -34,15 +37,31 @@ def addAllVHDLFiles(init=False): ## (init exception to avoid one if-case in ghdlEnvExists) if not ghdlEnvExists() and not init: return -1 - ## Add files vhdlFiles = [] + currentlyAdded = [] + ## Find already present files + if not init: + cfFileName = list(filter(lambda x: ".cf" in x, os.listdir("work")))[0] + cfFilePath = os.path.join(os.getcwd(),f"work/{cfFileName}") + currentlyAdded = getCurrentlyAddedFiles(cfFilePath) + ## Add files not added for file in os.listdir(): - if ".vhd" in file: + if ".vhd" in file and file not in currentlyAdded: vhdlFiles.append(file) - command = ["ghdl", "-i", "--workdir=work"] + vhdlFiles - subprocess.run(command) + if len(vhdlFiles) > 0: + print(f"Detected new files. Adding {vhdlFiles}") + command = ["ghdl", "-i", "--workdir=work"] + vhdlFiles + subprocess.run(command) return 0 +def getCurrentlyAddedFiles(cfFilePath:str): + f = open(cfFilePath,"r") + lines = f.readlines() + f.close() + fileLines = filter(lambda x: "file" in x, lines) + files = map(lambda x: split("\" \"",x)[1], fileLines) + return list(files) + if __name__ == "__main__": - createBuildEnv() + getCurrentlyAddedFiles("work/work-obj93.cf") diff --git a/scripts/synth.py b/scripts/elab.py similarity index 78% rename from scripts/synth.py rename to scripts/elab.py index 85e5a13..db3d4f1 100644 --- a/scripts/synth.py +++ b/scripts/elab.py @@ -2,10 +2,10 @@ import os import subprocess import build_env -def synthDesign(topDef: str, arch: str, lib: str, std: str): +def elabDesign(topDef: str, arch: str, lib: str, std: str): ## Add all source files present in pwd if build_env.addAllVHDLFiles() == -1: - print("build env not working") + print("Adding files failed. GHDL Build environment may be broken...") return -1 command = [ "ghdl", "-m", f"--workdir={lib}", f"--work={lib}", f"--std={std}", @@ -13,9 +13,9 @@ def synthDesign(topDef: str, arch: str, lib: str, std: str): subprocess.run(command) def runDesign(topDef: str, arch: str, lib: str, std: str): - ## synth first, then run - if synthDesign(topDef, arch, lib, std) == -1: - print("synth failed") + ## elaborate first, then run + if elabDesign(topDef, arch, lib, std) == -1: + print("Elaboration failed...") return -1 os.makedirs("wave",exist_ok=True) libPath = os.path.join(os.getcwd(), lib) diff --git a/scripts/gantry.py b/scripts/gantry.py index 0bd06ee..8e17038 100644 --- a/scripts/gantry.py +++ b/scripts/gantry.py @@ -1,33 +1,36 @@ import typer -import synth as syn +import elab import build_env from typing_extensions import Annotated - app = typer.Typer() +software = typer.Typer() +hardware = typer.Typer() -@app.command() +app.add_typer(software, name="sim", help="GHDL simulator command group") +app.add_typer(hardware, name="syn", help="Synthesis and deployment command group") + + +@software.command(help="Initializes the GHDL build environment in a library named \"work\". Adds all files ending in \".vhd\" to the project") def init(): - print("Initializing GHDL project in current directory...") - build_env.createBuildEnv() - + return build_env.createBuildEnv() def complete_vhdl_ver(): return ["87", "93", "93c", "00", "02", "08"] -@app.command() -def synth( +@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 from")] = "work", 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"Synthesizing {topdef} with arch {arch} in library {library}. VHDL {std}") - syn.synthDesign(topdef, arch, library, std) + print(f"Elaborating {topdef} with arch {arch} in library {library}. VHDL {std}") + return elab.elabDesign(topdef, arch, library, std) -@app.command() +@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")] = "", @@ -35,9 +38,9 @@ def run( 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}") - syn.runDesign(topdef, arch, library, std) + return elab.runDesign(topdef, arch, library, std) -@app.command() +@hardware.command() def build(): print("Build!")