ISEWrap.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //
  2. // Vivado(TM)
  3. // ISEWrap.js: Vivado Runs Script for WSH 5.1/5.6
  4. // Copyright 1986-2022 Xilinx, Inc. All Rights Reserved.
  5. // Copyright 2022-2023 Advanced Micro Devices, Inc. All Rights Reserved.
  6. //
  7. // GLOBAL VARIABLES
  8. var ISEShell = new ActiveXObject( "WScript.Shell" );
  9. var ISEFileSys = new ActiveXObject( "Scripting.FileSystemObject" );
  10. var ISERunDir = "";
  11. var ISELogFile = "runme.log";
  12. var ISELogFileStr = null;
  13. var ISELogEcho = true;
  14. var ISEOldVersionWSH = false;
  15. // BOOTSTRAP
  16. ISEInit();
  17. //
  18. // ISE FUNCTIONS
  19. //
  20. function ISEInit() {
  21. // 1. RUN DIR setup
  22. var ISEScrFP = WScript.ScriptFullName;
  23. var ISEScrN = WScript.ScriptName;
  24. ISERunDir =
  25. ISEScrFP.substr( 0, ISEScrFP.length - ISEScrN.length - 1 );
  26. // 2. LOG file setup
  27. ISELogFileStr = ISEOpenFile( ISELogFile );
  28. // 3. LOG echo?
  29. var ISEScriptArgs = WScript.Arguments;
  30. for ( var loopi=0; loopi<ISEScriptArgs.length; loopi++ ) {
  31. if ( ISEScriptArgs(loopi) == "-quiet" ) {
  32. ISELogEcho = false;
  33. break;
  34. }
  35. }
  36. // 4. WSH version check
  37. var ISEOptimalVersionWSH = 5.6;
  38. var ISECurrentVersionWSH = WScript.Version;
  39. if ( ISECurrentVersionWSH < ISEOptimalVersionWSH ) {
  40. ISEStdErr( "" );
  41. ISEStdErr( "Warning: ExploreAhead works best with Microsoft WSH " +
  42. ISEOptimalVersionWSH + " or higher. Downloads" );
  43. ISEStdErr( " for upgrading your Windows Scripting Host can be found here: " );
  44. ISEStdErr( " http://msdn.microsoft.com/downloads/list/webdev.asp" );
  45. ISEStdErr( "" );
  46. ISEOldVersionWSH = true;
  47. }
  48. }
  49. function ISEStep( ISEProg, ISEArgs ) {
  50. // CHECK for a STOP FILE
  51. if ( ISEFileSys.FileExists(ISERunDir + "/.stop.rst") ) {
  52. ISEStdErr( "" );
  53. ISEStdErr( "*** Halting run - EA reset detected ***" );
  54. ISEStdErr( "" );
  55. WScript.Quit( 1 );
  56. }
  57. // WRITE STEP HEADER to LOG
  58. ISEStdOut( "" );
  59. ISEStdOut( "*** Running " + ISEProg );
  60. ISEStdOut( " with args " + ISEArgs );
  61. ISEStdOut( "" );
  62. // LAUNCH!
  63. var ISEExitCode = ISEExec( ISEProg, ISEArgs );
  64. if ( ISEExitCode != 0 ) {
  65. WScript.Quit( ISEExitCode );
  66. }
  67. }
  68. function ISEExec( ISEProg, ISEArgs ) {
  69. var ISEStep = ISEProg;
  70. if (ISEProg == "realTimeFpga" || ISEProg == "planAhead" || ISEProg == "vivado") {
  71. ISEProg += ".bat";
  72. }
  73. var ISECmdLine = ISEProg + " " + ISEArgs;
  74. var ISEExitCode = 1;
  75. if ( ISEOldVersionWSH ) { // WSH 5.1
  76. // BEGIN file creation
  77. ISETouchFile( ISEStep, "begin" );
  78. // LAUNCH!
  79. ISELogFileStr.Close();
  80. ISECmdLine =
  81. "%comspec% /c " + ISECmdLine + " >> " + ISELogFile + " 2>&1";
  82. ISEExitCode = ISEShell.Run( ISECmdLine, 0, true );
  83. ISELogFileStr = ISEOpenFile( ISELogFile );
  84. } else { // WSH 5.6
  85. // LAUNCH!
  86. ISEShell.CurrentDirectory = ISERunDir;
  87. // Redirect STDERR to STDOUT
  88. ISECmdLine = "%comspec% /c " + ISECmdLine + " 2>&1";
  89. var ISEProcess = ISEShell.Exec( ISECmdLine );
  90. // BEGIN file creation
  91. var wbemFlagReturnImmediately = 0x10;
  92. var wbemFlagForwardOnly = 0x20;
  93. var objWMIService = GetObject ("winmgmts:{impersonationLevel=impersonate, (Systemtime)}!//./root/cimv2");
  94. var processor = objWMIService.ExecQuery("SELECT * FROM Win32_Processor", "WQL",wbemFlagReturnImmediately | wbemFlagForwardOnly);
  95. var computerSystem = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", wbemFlagReturnImmediately | wbemFlagForwardOnly);
  96. var NOC = 0;
  97. var NOLP = 0;
  98. var TPM = 0;
  99. var cpuInfos = new Enumerator(processor);
  100. for(;!cpuInfos.atEnd(); cpuInfos.moveNext()) {
  101. var cpuInfo = cpuInfos.item();
  102. NOC += cpuInfo.NumberOfCores;
  103. NOLP += cpuInfo.NumberOfLogicalProcessors;
  104. }
  105. var csInfos = new Enumerator(computerSystem);
  106. for(;!csInfos.atEnd(); csInfos.moveNext()) {
  107. var csInfo = csInfos.item();
  108. TPM += csInfo.TotalPhysicalMemory;
  109. }
  110. var ISEHOSTCORE = NOLP
  111. var ISEMEMTOTAL = TPM
  112. var ISENetwork = WScript.CreateObject( "WScript.Network" );
  113. var ISEHost = ISENetwork.ComputerName;
  114. var ISEUser = ISENetwork.UserName;
  115. var ISEPid = ISEProcess.ProcessID;
  116. var ISEBeginFile = ISEOpenFile( "." + ISEStep + ".begin.rst" );
  117. ISEBeginFile.WriteLine( "<?xml version=\"1.0\"?>" );
  118. ISEBeginFile.WriteLine( "<ProcessHandle Version=\"1\" Minor=\"0\">" );
  119. ISEBeginFile.WriteLine( " <Process Command=\"" + ISEProg +
  120. "\" Owner=\"" + ISEUser +
  121. "\" Host=\"" + ISEHost +
  122. "\" Pid=\"" + ISEPid +
  123. "\" HostCore=\"" + ISEHOSTCORE +
  124. "\" HostMemory=\"" + ISEMEMTOTAL +
  125. "\">" );
  126. ISEBeginFile.WriteLine( " </Process>" );
  127. ISEBeginFile.WriteLine( "</ProcessHandle>" );
  128. ISEBeginFile.Close();
  129. var ISEOutStr = ISEProcess.StdOut;
  130. var ISEErrStr = ISEProcess.StdErr;
  131. // WAIT for ISEStep to finish
  132. while ( ISEProcess.Status == 0 ) {
  133. // dump stdout then stderr - feels a little arbitrary
  134. while ( !ISEOutStr.AtEndOfStream ) {
  135. ISEStdOut( ISEOutStr.ReadLine() );
  136. }
  137. WScript.Sleep( 100 );
  138. }
  139. ISEExitCode = ISEProcess.ExitCode;
  140. }
  141. ISELogFileStr.Close();
  142. // END/ERROR file creation
  143. if ( ISEExitCode != 0 ) {
  144. ISETouchFile( ISEStep, "error" );
  145. } else {
  146. ISETouchFile( ISEStep, "end" );
  147. }
  148. return ISEExitCode;
  149. }
  150. //
  151. // UTILITIES
  152. //
  153. function ISEStdOut( ISELine ) {
  154. ISELogFileStr.WriteLine( ISELine );
  155. if ( ISELogEcho ) {
  156. WScript.StdOut.WriteLine( ISELine );
  157. }
  158. }
  159. function ISEStdErr( ISELine ) {
  160. ISELogFileStr.WriteLine( ISELine );
  161. if ( ISELogEcho ) {
  162. WScript.StdErr.WriteLine( ISELine );
  163. }
  164. }
  165. function ISETouchFile( ISERoot, ISEStatus ) {
  166. var ISETFile =
  167. ISEOpenFile( "." + ISERoot + "." + ISEStatus + ".rst" );
  168. ISETFile.Close();
  169. }
  170. function ISEOpenFile( ISEFilename ) {
  171. // This function has been updated to deal with a problem seen in CR #870871.
  172. // In that case the user runs a script that runs impl_1, and then turns around
  173. // and runs impl_1 -to_step write_bitstream. That second run takes place in
  174. // the same directory, which means we may hit some of the same files, and in
  175. // particular, we will open the runme.log file. Even though this script closes
  176. // the file (now), we see cases where a subsequent attempt to open the file
  177. // fails. Perhaps the OS is slow to release the lock, or the disk comes into
  178. // play? In any case, we try to work around this by first waiting if the file
  179. // is already there for an arbitrary 5 seconds. Then we use a try-catch block
  180. // and try to open the file 10 times with a one second delay after each attempt.
  181. // Again, 10 is arbitrary. But these seem to stop the hang in CR #870871.
  182. // If there is an unrecognized exception when trying to open the file, we output
  183. // an error message and write details to an exception.log file.
  184. var ISEFullPath = ISERunDir + "/" + ISEFilename;
  185. if (ISEFileSys.FileExists(ISEFullPath)) {
  186. // File is already there. This could be a problem. Wait in case it is still in use.
  187. WScript.Sleep(5000);
  188. }
  189. var i;
  190. for (i = 0; i < 10; ++i) {
  191. try {
  192. return ISEFileSys.OpenTextFile(ISEFullPath, 8, true);
  193. } catch (exception) {
  194. var error_code = exception.number & 0xFFFF; // The other bits are a facility code.
  195. if (error_code == 52) { // 52 is bad file name or number.
  196. // Wait a second and try again.
  197. WScript.Sleep(1000);
  198. continue;
  199. } else {
  200. WScript.StdErr.WriteLine("ERROR: Exception caught trying to open file " + ISEFullPath);
  201. var exceptionFilePath = ISERunDir + "/exception.log";
  202. if (!ISEFileSys.FileExists(exceptionFilePath)) {
  203. WScript.StdErr.WriteLine("See file " + exceptionFilePath + " for details.");
  204. var exceptionFile = ISEFileSys.OpenTextFile(exceptionFilePath, 8, true);
  205. exceptionFile.WriteLine("ERROR: Exception caught trying to open file " + ISEFullPath);
  206. exceptionFile.WriteLine("\tException name: " + exception.name);
  207. exceptionFile.WriteLine("\tException error code: " + error_code);
  208. exceptionFile.WriteLine("\tException message: " + exception.message);
  209. exceptionFile.Close();
  210. }
  211. throw exception;
  212. }
  213. }
  214. }
  215. // If we reached this point, we failed to open the file after 10 attempts.
  216. // We need to error out.
  217. WScript.StdErr.WriteLine("ERROR: Failed to open file " + ISEFullPath);
  218. WScript.Quit(1);
  219. }