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