import os from re import split import subprocess def ghdlEnvExists(relativePath="work"): ## Check if work exists try: os.lstat(relativePath) 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") return False cfFileExists = False filesInWork = os.listdir(relativePath) for file in filesInWork: if ".cf" in file: cfFileExists = True if not cfFileExists: return False ## Nothing bad, continue return True def createBuildEnv(): if ghdlEnvExists(): 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 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 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 and file not in currentlyAdded: vhdlFiles.append(file) 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__": getCurrentlyAddedFiles("work/work-obj93.cf")