Commands.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. import subprocess
  2. import colorama
  3. from colorama import init,Fore, Back, Style
  4. import argparse
  5. import os
  6. import os.path
  7. from contextlib import contextmanager
  8. import shutil
  9. import glob
  10. from pathlib import Path
  11. DEBUGMODE = False
  12. NOTESTFAILED = 0
  13. MAKEFAILED = 1
  14. TESTFAILED = 2
  15. FLOWFAILURE = 3
  16. CALLFAILURE = 4
  17. def joinit(iterable, delimiter):
  18. it = iter(iterable)
  19. yield next(it)
  20. for x in it:
  21. yield delimiter
  22. yield x
  23. class TestFlowFailure(Exception):
  24. def __init__(self,completed):
  25. self._errorcode = completed.returncode
  26. def errorCode(self):
  27. return(self._errorcode)
  28. class CallFailure(Exception):
  29. pass
  30. def check(n):
  31. #print(n)
  32. if n is not None:
  33. if n.returncode != 0:
  34. raise TestFlowFailure(n)
  35. else:
  36. raise CallFailure()
  37. def msg(t):
  38. print(Fore.CYAN + t + Style.RESET_ALL)
  39. def errorMsg(t):
  40. print(Fore.RED + t + Style.RESET_ALL)
  41. def fullTestFolder(rootFolder):
  42. return(os.path.join(rootFolder,"CMSIS","DSP","Testing","fulltests"))
  43. class BuildConfig:
  44. def __init__(self,toUnset,rootFolder,buildFolder,compiler,toolchain,core,cmake):
  45. self._toUnset = toUnset
  46. self._buildFolder = buildFolder
  47. self._rootFolder = os.path.abspath(rootFolder)
  48. self._dspFolder = os.path.join(self._rootFolder,"CMSIS","DSP")
  49. self._testingFolder = os.path.join(self._dspFolder,"Testing")
  50. self._fullTests = os.path.join(self._testingFolder,"fulltests")
  51. self._compiler = compiler
  52. self._toolchain = toolchain
  53. self._core = core
  54. self._cmake = cmake
  55. self._savedEnv = {}
  56. def compiler(self):
  57. return(self._compiler)
  58. def toolChainFile(self):
  59. return(self._toolchain)
  60. def core(self):
  61. return(self._core)
  62. def path(self):
  63. return(os.path.join(self._fullTests,self._buildFolder))
  64. def archivePath(self):
  65. return(os.path.join(self._fullTests,"archive",self._buildFolder))
  66. def archiveResultPath(self):
  67. return(os.path.join(self._fullTests,"archive",self._buildFolder,"results"))
  68. def archiveLogPath(self):
  69. return(os.path.join(self._fullTests,"archive",self._buildFolder,"logs"))
  70. def archiveErrorPath(self):
  71. return(os.path.join(self._fullTests,"archive",self._buildFolder,"errors"))
  72. def toolChainPath(self):
  73. return(self._dspFolder)
  74. def cmakeFilePath(self):
  75. return(self._testingFolder)
  76. def buildFolderName(self):
  77. return(self._buildFolder)
  78. def saveEnv(self):
  79. if self._toUnset is not None:
  80. for v in self._toUnset:
  81. self._savedEnv[v] = os.environ[v]
  82. del os.environ[v]
  83. def restoreEnv(self):
  84. if self._toUnset is not None:
  85. for v in self._toUnset:
  86. os.environ[v] = self._savedEnv[v]
  87. self._savedEnv = {}
  88. # Build for a folder
  89. # We need to be able to detect failed build
  90. def build(self,test):
  91. completed=None
  92. # Save and unset some environment variables
  93. self.saveEnv()
  94. with self.buildFolder() as b:
  95. msg(" Build %s\n" % self.buildFolderName())
  96. with open(os.path.join(self.archiveLogPath(),"makelog_%s.txt" % test),"w") as makelog:
  97. with open(os.path.join(self.archiveErrorPath(),"makeerror_%s.txt" % test),"w") as makeerr:
  98. if DEBUGMODE:
  99. completed=subprocess.run(["make","-j8","VERBOSE=1"],timeout=3600)
  100. else:
  101. completed=subprocess.run(["make","-j8","VERBOSE=1"],stdout=makelog,stderr=makeerr,timeout=3600)
  102. # Restore environment variables
  103. self.restoreEnv()
  104. check(completed)
  105. def getTest(self,test):
  106. return(Test(self,test))
  107. # Launch cmake command.
  108. def createCMake(self,flags):
  109. with self.buildFolder() as b:
  110. self.saveEnv()
  111. msg("Create cmake for %s\n" % self.buildFolderName())
  112. toolchainCmake = os.path.join(self.toolChainPath(),self.toolChainFile())
  113. cmd = [self._cmake]
  114. cmd += ["-DCMAKE_PREFIX_PATH=%s" % self.compiler(),
  115. "-DCMAKE_TOOLCHAIN_FILE=%s" % toolchainCmake,
  116. "-DARM_CPU=%s" % self.core(),
  117. "-DPLATFORM=FVP"
  118. ]
  119. cmd += flags
  120. cmd += ["-DBENCHMARK=OFF",
  121. "-DFULLYCONNECTED=OFF",
  122. "-DCONVOLUTION=OFF",
  123. "-DACTIVATION=OFF",
  124. "-DPOOLING=OFF",
  125. "-DSOFTMAX=OFF",
  126. "-DNNSUPPORT=OFF",
  127. "-DBASICMATHSNN=OFF",
  128. "-DRESHAPE=OFF",
  129. "-DCONCATENATION=OFF",
  130. "-DWRAPPER=OFF",
  131. "-DCONFIGTABLE=OFF",
  132. "-DROOT=%s" % self._rootFolder,
  133. "-DCMAKE_BUILD_TYPE=Release",
  134. "-G", "Unix Makefiles" ,"%s" % self.cmakeFilePath()]
  135. with open(os.path.join(self.archiveLogPath(),"cmakecmd.txt"),"w") as cmakecmd:
  136. cmakecmd.write("".join(joinit(cmd," ")))
  137. with open(os.path.join(self.archiveLogPath(),"cmakelog.txt"),"w") as cmakelog:
  138. with open(os.path.join(self.archiveErrorPath(),"cmakeerror.txt"),"w") as cmakeerr:
  139. completed=subprocess.run(cmd, stdout=cmakelog,stderr=cmakeerr, timeout=3600)
  140. self.restoreEnv()
  141. check(completed)
  142. # Create the build folder if missing
  143. def createFolder(self):
  144. os.makedirs(self.path(),exist_ok=True)
  145. def createArchive(self, flags):
  146. os.makedirs(self.archivePath(),exist_ok=True)
  147. os.makedirs(self.archiveResultPath(),exist_ok=True)
  148. os.makedirs(self.archiveErrorPath(),exist_ok=True)
  149. os.makedirs(self.archiveLogPath(),exist_ok=True)
  150. with open(os.path.join(self.archivePath(),"flags.txt"),"w") as f:
  151. for flag in flags:
  152. f.write(flag)
  153. f.write("\n")
  154. # Delete the build folder
  155. def cleanFolder(self):
  156. print("Delete %s\n" % self.path())
  157. #DEBUG
  158. if not DEBUGMODE:
  159. shutil.rmtree(self.path())
  160. # Archive results and currentConfig.csv to another folder
  161. def archiveResults(self):
  162. results=glob.glob(os.path.join(self.path(),"results_*"))
  163. for result in results:
  164. dst=os.path.join(self.archiveResultPath(),os.path.basename(result))
  165. shutil.copy(result,dst)
  166. src = os.path.join(self.path(),"currentConfig.csv")
  167. dst = os.path.join(self.archiveResultPath(),os.path.basename(src))
  168. shutil.copy(src,dst)
  169. @contextmanager
  170. def buildFolder(self):
  171. current=os.getcwd()
  172. try:
  173. os.chdir(self.path() )
  174. yield self.path()
  175. finally:
  176. os.chdir(current)
  177. @contextmanager
  178. def archiveFolder(self):
  179. current=os.getcwd()
  180. try:
  181. os.chdir(self.archivePath() )
  182. yield self.archivePath()
  183. finally:
  184. os.chdir(current)
  185. @contextmanager
  186. def resultFolder(self):
  187. current=os.getcwd()
  188. try:
  189. os.chdir(self.archiveResultPath())
  190. yield self.archiveResultPath()
  191. finally:
  192. os.chdir(current)
  193. @contextmanager
  194. def logFolder(self):
  195. current=os.getcwd()
  196. try:
  197. os.chdir(self.archiveLogPath())
  198. yield self.archiveLogPath()
  199. finally:
  200. os.chdir(current)
  201. @contextmanager
  202. def errorFolder(self):
  203. current=os.getcwd()
  204. try:
  205. os.chdir(self.archiveErrorPath())
  206. yield self.archiveErrorPath()
  207. finally:
  208. os.chdir(current)
  209. class Test:
  210. def __init__(self,build,test):
  211. self._test = test
  212. self._buildConfig = build
  213. def buildConfig(self):
  214. return(self._buildConfig)
  215. def testName(self):
  216. return(self._test)
  217. # Process a test from the test description file
  218. def processTest(self):
  219. completed=subprocess.run(["python","processTests.py","-e",self.testName()],timeout=3600)
  220. check(completed)
  221. def getResultPath(self):
  222. return(os.path.join(self.buildConfig().path() ,self.resultName()))
  223. def resultName(self):
  224. return("results_%s.txt" % self.testName())
  225. # Run a specific test in the current folder
  226. # A specific results.txt file is created in
  227. # the build folder for this test
  228. #
  229. # We need a timeout and detect failed run
  230. def run(self,fvp):
  231. completed = None
  232. with self.buildConfig().buildFolder() as b:
  233. msg(" Run %s\n" % self.testName() )
  234. with open(self.resultName(),"w") as results:
  235. completed=subprocess.run(fvp.split(),stdout=results,timeout=3600)
  236. check(completed)
  237. # Process results of the given tests
  238. # in given build folder
  239. # We need to detect failed tests
  240. def processResult(self):
  241. msg(" Parse result for %s\n" % self.testName())
  242. with open(os.path.join(self.buildConfig().archiveResultPath(),"processedResult_%s.txt" % self.testName()),"w") as presult:
  243. completed=subprocess.run(["python","processResult.py","-e","-r",self.getResultPath()],stdout=presult,timeout=3600)
  244. # When a test fail, the regression is continuing but we
  245. # track that a test has failed
  246. if completed.returncode==0:
  247. return(NOTESTFAILED)
  248. else:
  249. return(TESTFAILED)
  250. def runAndProcess(self,compiler,fvp):
  251. # If we can't parse test description we fail all tests
  252. self.processTest()
  253. # Otherwise if only building or those tests are failing, we continue
  254. # with other tests
  255. try:
  256. self.buildConfig().build(self.testName())
  257. except:
  258. return(MAKEFAILED)
  259. # We run tests only for AC6
  260. # For other compilers only build is tests
  261. # Since full build is no more possible because of huge pattersn,
  262. # build is done per test suite.
  263. if compiler == "AC6":
  264. if fvp is not None:
  265. self.run(fvp)
  266. return(self.processResult())
  267. else:
  268. msg("No FVP available")
  269. return(NOTESTFAILED)
  270. else:
  271. return(NOTESTFAILED)
  272. # Preprocess the test description
  273. def preprocess():
  274. msg("Process test description file\n")
  275. completed = subprocess.run(["python", "preprocess.py","-f","desc.txt"],timeout=3600)
  276. check(completed)
  277. # Generate all missing C code by using all classes in the
  278. # test description file
  279. def generateAllCCode():
  280. msg("Generate all missing C files\n")
  281. completed = subprocess.run(["python","processTests.py", "-e"],timeout=3600)
  282. check(completed)