Added project files to automate library imports

This commit is contained in:
Erik Örtenberg 2025-03-13 15:14:27 +01:00
parent 1668e6df6c
commit 420d0df2e2
4 changed files with 398 additions and 0 deletions

70
scripts/build_env.py Normal file
View File

@ -0,0 +1,70 @@
import os
from re import split
import subprocess
def getCfFileId(std: str):
return "08" if std == "08" else "93" ## Weird behaviour from GHDL, but all vhdl versions besides 08 have [...]93.cf
def ghdlEnvExists(std, lib):
## Check if work exists
try:
os.lstat("work")
except:
return False
## Check that work is writable
if not os.access("work", os.W_OK):
print("work is write-protected, please acquire correct permissions")
return False
cfFileExists = False
filesInWork = os.listdir("work")
cfFileId = getCfFileId(std)
for file in filesInWork:
if ".cf" in file and lib in file and cfFileId in file:
cfFileExists = True
if not cfFileExists:
return False
## Nothing bad, continue
return True
def createBuildEnv(std: str, lib: str):
if ghdlEnvExists(std=std, lib=lib):
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(std=std, lib=lib, init=True)
return 0
def addAllVHDLFiles(std: str, lib: str, init=False):
## Ensure everything is ready for adding files
## (init exception to avoid one if-case in ghdlEnvExists)
if not ghdlEnvExists(std=std, lib=lib) and not init:
return -1
vhdlFiles = []
currentlyAdded = []
absWorkDir = "work"
cfFileId = getCfFileId(std)
## Find already present files
if not init:
cfFileName = list(filter(lambda x: ".cf" in x and lib in x and cfFileId in x, os.listdir(absWorkDir)))[0]
cfFilePath = os.path.join(absWorkDir,cfFileName)
currentlyAdded = getCurrentlyAddedFiles(cfFilePath)
print(currentlyAdded)
## Add files not added
for file in os.listdir():
if ".vhd" in file and file not in currentlyAdded:
vhdlFiles.append(file)
if len(vhdlFiles) > 0:
print(f"Detected new files. Adding {vhdlFiles}")
command = ["ghdl", "-i", "--workdir=work", f"--work={lib}", f"--std={std}"] + 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)

46
scripts/elab.py Normal file
View File

@ -0,0 +1,46 @@
import os
import subprocess
import build_env
from typing import List
from project_man import getLibrariesPresent, getLibrariesInProject
def generateIncludesForGHDL(includes: List[str]):
cmd = []
[exists, projectLibs] = getLibrariesInProject()
if not exists:
return []
for lib in projectLibs.keys():
includeString = f"{projectLibs[lib]['path']}/work"
cmd.append(f"-P{includeString}")
for inc in includes:
includeString = f"{inc}/work"
cmd.append(f"-P{includeString}")
return cmd
def elabDesign(topDef: str, arch: str, lib: str, std: str, includes: List[str]):
## Add all source files present in pwd
if build_env.addAllVHDLFiles(std, lib) == -1:
print("Adding files failed. GHDL Build environment may be broken...")
return -1
incs = generateIncludesForGHDL(includes)
command = [
"ghdl", "-m", "--workdir=work", f"--work={lib}", f"--std={std}"] + incs + ["-o", f"work/{topDef}-{arch}", f"work.{topDef}", f"{arch}"]
subprocess.run(command)
def runDesign(topDef: str, arch: str, lib: str, std: str, includes):
## elaborate first, then run
if elabDesign(topDef, arch, lib, std, includes) == -1:
print("Elaboration failed...")
return -1
os.makedirs("wave",exist_ok=True)
wavePath = os.path.join(os.getcwd(), "wave")
incs = generateIncludesForGHDL(includes)
command = [ ## may add -v for verbose
"ghdl", "--elab-run", f"--workdir=work", f"--work={lib}", f"--std={std}"] + incs + ["-o", f"work/{topDef}-{arch}", f"{topDef}", f"{arch}",
f"--wave=wave/{topDef}-{arch}.ghw" ##, "--read-wave-opt=<See"
]
subprocess.run(command)
command = [
"gtkwave", f"{topDef}-{arch}.ghw", "--rcvar",
"do_initial_zoom_fit yes"]
subprocess.run(command, cwd=wavePath)

130
scripts/gantry.py Normal file
View File

@ -0,0 +1,130 @@
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()

152
scripts/project_man.py Normal file
View File

@ -0,0 +1,152 @@
import os
from typing import Any
import toml
import datetime
def findProjectRoot() -> "tuple[bool, str]":
[exists, projectFile] = findProjectFile()
if not exists:
return (False, "")
return (True, "/".join(projectFile.split("/")[0:-1]))
def getRelativePathToRoot(path: str) -> str:
(exists, projectRoot) = findProjectRoot()
if not exists:
return ""
return os.path.relpath(path, projectRoot)
def findProjectFile() -> "tuple[bool, str]":
cwd = os.getcwd().split("/")
for i in range(len(cwd) - 2):
searchPath = "/".join(cwd[0:len(cwd)-i])
[exists, pathToProjectFile] = projectFileExists(searchPath)
if exists:
return (True, pathToProjectFile)
return (False, "")
def projectFileExists(path: str) -> "tuple[bool, str]":
files = os.listdir(path)
for file in files:
if "gantry.toml" in file:
return (True, os.path.join(path, file))
return (False,"")
def initProjectFile(projectName: str) -> "tuple[bool, str]":
cwd = os.getcwd()
projectPath = os.path.join(cwd, "gantry.toml")
[exists, existingProjectPath] = projectFileExists(cwd)
if exists:
existingProjectName = existingProjectPath.split("/")[-1]
return (False, f"Project {existingProjectName} already exists, amend it to fit your intention or delete it to create a new project")
try:
with open(projectPath, "w") as f:
parsedTOML = toml.loads(createProjectFileTemplate(projectName))
toml.dump(parsedTOML, f)
except:
return (False, "Creation of file failed, permissions may be set wrong")
return (True, projectPath)
def loadProjectFile() -> "tuple[bool, str | dict[str, Any]]" :
[exists, path] = findProjectFile()
if not exists:
return (False, "")
try:
with open(path, "r") as f:
toml.load
parsedTOML = toml.load(f)
return (True, parsedTOML)
except:
return (False, "Reading Project file failed, permissions may be set wrong")
def writeProjectFile(projectDict: "dict[str, Any]") -> "tuple[bool, str]":
[exists, path] = findProjectFile()
if not exists:
return (False, "")
try:
with open(path, "w") as f:
toml.dump(projectDict, f)
return (True, "")
except:
return (False, "Reading Project file failed, permissions may be set wrong")
def getProjectDict() -> "tuple[bool, dict[str, Any]]":
[exists, output] = loadProjectFile()
if not exists:
print(output)
return (False, {})
if isinstance(output, dict):
return (True, output)
else:
print(output)
return (False, {})
def removeLibraryInProject(lib: str) -> "tuple[bool, str]":
[exists, projectDict] = getProjectDict()
if not exists:
return (False, "Found no project dictionary")
if "libraries" not in projectDict.keys():
return (False, "No libraries are declared in this project.")
if lib in projectDict["libraries"].keys():
projectDict["libraries"].pop(lib)
[wentWell, _] = writeProjectFile(projectDict)
return (wentWell, "")
return (False, "Library with this name is not declared")
def addLibraryInProject(lib: str, relPath: str, std: str) -> "tuple[bool, str]":
(exists, projectDict) = getProjectDict()
if not exists:
return (False, "Project doesn't exist.")
if "libraries" not in projectDict.keys():
projectDict["libraries"] = {}
if lib not in projectDict["libraries"].keys():
libDir = getRelativePathToRoot(os.path.join(os.getcwd(), relPath))
projectDict["libraries"][lib] = {}
projectDict["libraries"][lib]["vhdl-version"] = std
projectDict["libraries"][lib]["path"] = libDir
os.makedirs(name=relPath,exist_ok=True)
print(libDir)
[wentWell, _] = writeProjectFile(projectDict)
return (wentWell, "")
return (False, "Library with this name is already declared")
def getLibrariesInProject() -> "tuple[bool, dict[str, Any]]":
(exists, projectDict) = getProjectDict()
if not exists:
return (False, {})
libs = {}
if "libraries" not in projectDict.keys():
## Successful read, no libs found -> empty return
return (True, {})
(_, projectRoot) = findProjectRoot()
for lib in projectDict["libraries"].keys():
libs[lib] = projectDict["libraries"][lib]
absPath = os.path.join(projectRoot, libs[lib]["path"])
libs[lib]["path"] = os.path.relpath(absPath, os.getcwd())
return (True, libs)
def getLibrariesPresent() -> "tuple[bool, dict[str, Any]]":
cwd = os.getcwd()
(exists, libs) = getLibrariesInProject()
if not exists:
return (False, {})
libsInCwd = {}
for lib in libs.keys():
if os.path.samefile(libs[lib]["path"], cwd):
libsInCwd[lib] = libs[lib]
return (True, libsInCwd)
def createProjectFileTemplate(projectName: str) -> str:
return f"""
title = "{projectName}"
createdAt = "{datetime.date.today()}"
maintainer = ""
email = ""
version = "0.0.1"
"""