From 325cbbff89adb4a0d3676d7ddb392f6623bda931 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20=C3=96rtenberg?= Date: Wed, 5 Mar 2025 16:26:21 +0100 Subject: [PATCH] added initial support for including libraries using flag -i and a relative path --- scripts/build_env.py | 36 ++++++++++++++++++++---------------- scripts/elab.py | 24 ++++++++++++++++-------- scripts/gantry.py | 33 ++++++++++++++++++++++----------- 3 files changed, 58 insertions(+), 35 deletions(-) diff --git a/scripts/build_env.py b/scripts/build_env.py index 4adefce..b6fceff 100644 --- a/scripts/build_env.py +++ b/scripts/build_env.py @@ -5,57 +5,61 @@ 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, relativePath="work"): +def ghdlEnvExists(std, lib): ## Check if work exists try: - os.lstat(relativePath) + os.lstat("work") except: return False ## Check that work is writable - if not os.access(relativePath, os.W_OK): - print(f"{relativePath} is write-protected, please acquire correct permissions") + if not os.access("work", os.W_OK): + print("work is write-protected, please acquire correct permissions") return False cfFileExists = False - filesInWork = os.listdir(relativePath) + filesInWork = os.listdir("work") cfFileId = getCfFileId(std) for file in filesInWork: - if ".cf" in file and cfFileId in file: + 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): - if ghdlEnvExists(std=std): +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, init=True) + addAllVHDLFiles(std=std, lib=lib, init=True) return 0 -def addAllVHDLFiles(std: str, init=False): +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) and not init: + if not ghdlEnvExists(std=std, lib=lib) and not init: return -1 vhdlFiles = [] currentlyAdded = [] + absWorkDir = os.path.join(os.getcwd(), "work") cfFileId = getCfFileId(std) ## Find already present files if not init: - cfFileName = list(filter(lambda x: ".cf" in x and cfFileId in x, os.listdir("work")))[0] - cfFilePath = os.path.join(os.getcwd(),f"work/{cfFileName}") + 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(): + print(file) + print(currentlyAdded, file, file not in currentlyAdded) if ".vhd" in file and file not in currentlyAdded: - vhdlFiles.append(file) + vhdlFiles.append(os.path.join(os.getcwd(), file)) if len(vhdlFiles) > 0: print(f"Detected new files. Adding {vhdlFiles}") - command = ["ghdl", "-i", "--workdir=work", "--work=work", f"--std={std}"] + vhdlFiles + command = ["ghdl", "-i", "--workdir=work", f"--work={lib}", f"--std={std}"] + vhdlFiles subprocess.run(command) return 0 @@ -64,5 +68,5 @@ def getCurrentlyAddedFiles(cfFilePath:str): lines = f.readlines() f.close() fileLines = filter(lambda x: "file" in x, lines) - files = map(lambda x: split("\" \"",x)[1], fileLines) + files = map(lambda x: split("\"",x)[1], fileLines) return list(files) diff --git a/scripts/elab.py b/scripts/elab.py index 39b0029..1d5a040 100644 --- a/scripts/elab.py +++ b/scripts/elab.py @@ -1,27 +1,35 @@ import os import subprocess import build_env +from typing import List -def elabDesign(topDef: str, arch: str, lib: str, std: str): + +def generateIncludesForGHDL(includes: List[str]): + cmd = [] + for inc in includes: + cmd.append(f"-P{os.path.join(os.getcwd(), f"{inc}/work")}") + 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) == -1: + 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", f"--workdir={lib}", f"--work={lib}", f"--std={std}", - "-o", f"{lib}/{topDef}-{arch}", f"{lib}.{topDef}", f"{arch}"] + "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): +def runDesign(topDef: str, arch: str, lib: str, std: str, includes): ## elaborate first, then run - if elabDesign(topDef, arch, lib, std) == -1: + 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={lib}", f"--work={lib}", f"--std={std}", - "-o", f"{lib}/{topDef}-{arch}", f"{topDef}", f"{arch}", + "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=