added some functions to verify and initialize ghdl project

This commit is contained in:
Erik Örtenberg 2025-02-11 00:37:05 +01:00
parent e0e4345a31
commit 92b6d6a12f

View File

@ -1,25 +1,48 @@
import os import os
import subprocess
def buildEnvExists(): def ghdlEnvExists(relativePath="work"):
## Check if work exists
try: try:
result = os.lstat("work") os.lstat(relativePath)
except FileNotFoundError: except:
result = "-1" return False
except e: ## Check that work is writable
result = "-2" if not os.access(relativePath, os.W_OK):
if result == "-2": print(f"{relativePath} is write-protected, please acquire correct permissions")
print(f"Encountered an unexpected error {e}") return False
print("exiting...") cfFileExists = False
return filesInWork = os.listdir(relativePath)
if result == "-1": for file in filesInWork:
print("Work doesn't exist, initializing build environment") if ".cf" in file:
return cfFileExists = True
print("Work exists, no filesystem things to do") if not cfFileExists:
if os.access("work", os.W_OK): return False
print("write access checked inside work, nothing to do") ## Nothing bad, continue
else: return True
print("work is not writable, change permissions of dir")
def createBuildEnv():
if ghdlEnvExists():
return -1
## Create build env
os.makedirs("work",exist_ok=True)
addAllVHDLFiles(init=False)
return 0
def addAllVHDLFiles(init=False):
## Ensure everything is ready for adding files
## (init exception to avoid one if-case in ghdlEnvExists)
if not ghdlEnvExists() and not init:
return -1
## Add files
vhdlFiles = []
for file in os.listdir():
if ".vhd" in file:
vhdlFiles.append(file)
command = ["ghdl", "-i", "--workdir=work"] + vhdlFiles
subprocess.run(command)
return 0
if __name__ == "__main__": if __name__ == "__main__":
buildEnvExists() createBuildEnv()