VirtualBox

source: vbox/trunk/tools/envSub.vbs@ 89804

Last change on this file since 89804 was 87406, checked in by vboxsync, 4 years ago

tools/envSub.vbs: Fixed code looking for windbg.exe.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 16.8 KB
Line 
1' $Id: envSub.vbs 87406 2021-01-24 16:52:50Z vboxsync $
2'' @file
3' VBScript worker for env.cmd
4'
5
6'
7' Copyright (C) 2006-2020 Oracle Corporation
8'
9' This file is part of VirtualBox Open Source Edition (OSE), as
10' available from http://www.virtualbox.org. This file is free software;
11' you can redistribute it and/or modify it under the terms of the GNU
12' General Public License (GPL) as published by the Free Software
13' Foundation, in version 2 as it comes in the "COPYING" file of the
14' VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15' hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16'
17
18
19''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
20' Header Files
21''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
22''
23' Includes a vbscript file relative to the script.
24sub IncludeFile(strFilename)
25 dim objFile, objFileSys
26 set objFileSys = WScript.CreateObject("Scripting.FileSystemObject")
27 dim strPath : strPath = objFileSys.BuildPath(objFileSys.GetParentFolderName(Wscript.ScriptFullName), strFilename)
28 set objFile = objFileSys.openTextFile(strPath)
29 executeglobal objFile.readAll()
30 objFile.close
31 set objFileSys = nothing
32end sub
33
34IncludeFile "win\vbscript\helpers.vbs"
35
36
37''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
38' Global Variables '
39''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
40dim g_cntVerbose
41g_cntVerbose = 1
42
43
44sub LogPrint(str)
45 if g_cntVerbose > 1 then
46 WScript.StdErr.WriteLine "debug: " & str
47 end if
48end sub
49
50sub DbgPrint(str)
51 if g_cntVerbose > 2 then
52 WScript.StdErr.WriteLine "debug: " & str
53 end if
54end sub
55
56
57''
58' The main() like function.
59'
60function Main()
61 Main = 1
62
63 '
64 ' check that we're not using wscript.
65 '
66 if UCase(Right(Wscript.FullName, 11)) = "WSCRIPT.EXE" then
67 Wscript.Echo "This script must be run under CScript."
68 exit function
69 end if
70 SelfTest
71
72 '
73 ' Get our bearings.
74 '
75 dim strScriptDir
76 strScriptDir = g_objFileSys.GetParentFolderName(Wscript.ScriptFullName)
77
78 dim strRootDir
79 strRootDir = g_objFileSys.GetParentFolderName(strScriptDir)
80
81 dim strRealArch
82 strRealArch = Trim(EnvGet("PROCESSOR_ARCHITEW6432"))
83 if strRealArch = "" then strRealArch = Trim(EnvGet("PROCESSOR_ARCHITECTURE"))
84 if strRealArch = "" then strRealArch = "amd64"
85 strRealArch = LCase(strRealArch)
86 if strRealArch <> "amd64" and strRealArch <> "x86" then
87 MsgError "Unsupported host architecture: " & strRealArch ' quits
88 end if
89
90 '
91 ' Guest the default configuration.
92 '
93 dim arrTypes : arrTypes = Array("debug", "release", "profile", "strict", "dbgopt")
94 dim arrTargetAndHosts : arrTargetAndHosts = Array("win", "linux", "solaris", "darwin", "os2", "freebsd")
95 dim arrArchitectures : arrArchitectures = Array("x86", "amd64", "arm32", "arm64", "sparc32", "sparc64")
96
97 dim strType
98 strType = EnvGetDefValid("KBUILD_TYPE", "debug", arrTypes)
99
100 dim strPathDevTools
101 strPathDevTools= EnvGetDef("KBUILD_DEVTOOLS", g_objFileSys.BuildPath(strRootDir, "tools"))
102
103 dim strPathkBuild
104 strPathkBuild = EnvGetDef("KBUILD_PATH", g_objFileSys.BuildPath(strRootDir, "kBuild"))
105
106 dim strTarget, strTargetArch
107 strTarget = EnvGetDefValid("KBUILD_TARGET", "win", arrTargetAndHosts)
108 strTargetArch = EnvGetDefValid("KBUILD_TARGET_ARCH", strRealArch, arrArchitectures)
109
110 dim strHost, strHostArch
111 strHost = EnvGetDefValid("KBUILD_HOST", "win", arrTargetAndHosts)
112 strHostArch = EnvGetDefValid("KBUILD_HOST_ARCH", strRealArch, arrArchitectures)
113
114 '
115 ' Parse arguments.
116 '
117 dim arrValueOpts : arrValueOpts = Array("--type", "--arch", "--tmp-script")
118 dim arrCmdToExec : arrCmdToExec = Array()
119 dim blnDashDash : blnDashDash = false
120 dim strChdirTo : strChdirTo = strRootDir
121 dim strTmpScript : strTmpScript = g_objFileSys.BuildPath(strScriptDir, "envtmp.cmd")
122 dim i : i = 0
123 do while i < Wscript.Arguments.Count
124 dim strArg, strValue, off
125 strArg = Wscript.Arguments.item(i)
126 i = i + 1
127 if blnDashDash <> true then
128 ' Does it have an embedded value? Does it take a value?
129 off = InStr(2, strArg, "=")
130 if off <= 0 then off = InStr(2, strArg, ":")
131 if off > 0 then
132 strValue = Mid(strArg, off + 1)
133 strArg = Left(strArg, off - 1)
134 if not ArrayContainsString(arrValueOpts, strArg) then
135 MsgSyntaxError "'" & strArg & "' does not take a value" ' quits
136 end if
137 elseif ArrayContainsString(arrValueOpts, strArg) then
138 if i >= Wscript.Arguments.Count then
139 MsgSyntaxError "'" & strArg & "' takes a value" ' quits
140 end if
141 strValue = Wscript.Arguments.item(i)
142 i = i + 1
143 end if
144
145 ' Process it.
146 select case strArg
147 ' Build types:
148 case "--type"
149 if not ArrayContainsString(arrTypes, strValue) then
150 MsgSyntaxError "Invalid build type '" & strValue & "'. Valid types: " & ArrayJoinString(arrTypes, ", ") ' quits
151 else
152 strType = strValue
153 end if
154 case "--release"
155 strType = "release"
156 case "--debug"
157 strType = "debug"
158 case "--strict"
159 strType = "strict"
160 case "--dbgopt"
161 strType = "dbgopt"
162
163 ' Target architecture:
164 case "--arch"
165 if not ArrayContainsString(arrArchitectures, strValue) then
166 MsgSyntaxError "Invalid target architecture'" & strValue & "'. Valid ones: " _
167 & ArrayJoinString(arrArchitectures, ", ") ' quits
168 else
169 strTargetArch = strValue
170 end if
171 case "--amd64"
172 strTargetArch = "amd64"
173 case "--x86"
174 strTargetArch = "amd64"
175 case "--arm32"
176 strTargetArch = "arm32"
177 case "--arm64"
178 strTargetArch = "amd64"
179
180 ' Verbosity, env.sh compatibility and stuff
181 case "--quiet"
182 g_cntVerbose = 0
183 case "--verbose"
184 g_cntVerbose = g_cntVerbose + 1
185 case "--chdir"
186 strChdirTo = strValue
187 case "--no-chdir"
188 strChdirTo = ""
189
190 ' Internal.
191 case "--tmp-script"
192 strTmpScript = strValue
193
194 ' Standard options
195 case "-h", "-?", "--help"
196 Print "Sets the VBox development shell environment on Windows."
197 Print "usage: env.cmd [--type <type> | --release | --debug | --strict | --dbgopt]"
198 Print " [--arch <arch> | --amd64 | --x86 | --arm32 | --arm64]"
199 Print " [--no-chdir | --chdir <dir>] [--quiet | --verbose]"
200 Print " [--] [prog] [args...]"
201 Print "usage: env.cmd [--help | -h | -?]"
202 Print "usage: env.cmd [--version | -V]"
203 Main = 0
204 exit function
205 case "-V", "--version"
206 Print "x.y"
207 Main = 0
208 exit function
209
210 case "--"
211 blnDashDash = True
212
213 case else
214 MsgSyntaxError "Unknown option: " & strArg
215 end select
216 else
217 ' cscript may eat quoting... So we should consider using some windows API to get the raw command line
218 ' and look for the dash-dash instead. Maybe.
219 arrCmdToExec = ArrayAppend(arrCmdToExec, strArg)
220 end if
221 loop
222
223 '
224 ' Set up the environment.
225 '
226 dim str1
227
228 EnvSet "KBUILD_PATH", UnixSlashes(strPathkBuild)
229 EnvSet "KBUILD_DEVTOOLS", UnixSlashes(strPathDevTools)
230 EnvSet "KBUILD_TYPE", strType
231 EnvSet "KBUILD_TARGET", strTarget
232 EnvSet "KBUILD_TARGET_ARCH", strTargetArch
233 EnvSet "KBUILD_HOST", strHost
234 EnvSet "KBUILD_HOST_ARCH", strHostArch
235
236 ' Remove legacy variables.
237 dim arrObsolete
238 arrObsolete = Array("BUILD_TYPE", "BUILD_TARGET", "BUILD_TARGET_ARCH", "BUILD_PLATFORM", "BUILD_PLATFORM_ARCH", _
239 "PATH_DEVTOOLS", "KBUILD_TARGET_CPU", "KBUILD_PLATFORM_CPU")
240 for each str1 in arrObsolete
241 EnvUnset str1
242 next
243
244 ' cleanup path before we start adding to it
245 for each str1 in arrArchitectures
246 EnvRemovePathItem "Path", DosSlashes(strPathkBuild & "\bin\win." & str1), ";"
247 EnvRemovePathItem "Path", DosSlashes(strPathDevTools & "\win." & str1) & "\bin", ";"
248 next
249
250 '
251 ' We skip the extra stuff like gnuwin32, windbg, cl.exe and mingw64 if
252 ' there is a command to execute.
253 '
254 if ArraySize(arrCmdToExec) = 0 then
255 ' Add some gnuwin32 tools to the end of the path.
256 EnvAppendPathItem "Path", DosSlashes(strPathDevTools & "\win.x86\gnuwin32\r1\bin"), ";"
257
258 ' Add the newest debugger we can find to the front of the path.
259 dim strDir, blnStop
260 bldExitLoop = false
261 for each str1 in arrArchitectures
262 strDir = strPathDevTools & "\win." & str1 & "\sdk"
263 for each strSubDir in GetSubdirsStartingWithRVerSorted(strDir, "v")
264 if FileExists(strDir & "\" & strSubDir & "\Debuggers\" & XlateArchitectureToWin(strHostArch) & "\windbg.exe") then
265 EnvPrependPathItem "Path", DosSlashes(strDir & "\" & strSubDir & "\Debuggers\" & XlateArchitectureToWin(strHostArch)), ";"
266 bldExitLoop = true
267 exit for
268 end if
269 next
270 if bldExitLoop then exit for
271 next
272
273 ' Add VCC to the end of the path.
274 dim str2, strDir2, arrVccOldBinDirs
275 arrVccOldBinDirs = Array("\bin\" & strHostArch & "_" & strTargetArch, "\bin\" & strTargetArch, "\bin")
276 bldExitLoop = false
277 for each str1 in Array("amd64", "x86")
278 for each strDir in GetSubdirsStartingWithRVerSorted(strPathDevTools & "\win." & str1 & "\vcc", "v")
279 strDir = strPathDevTools & "\win." & str1 & "\vcc\" & strDir
280 if DirExists(strDir & "\Tools\MSVC") then
281 for each strDir2 in GetSubdirsStartingWithRVerSorted(strDir & "\Tools\MSVC", "1")
282 strDir2 = strDir & "\Tools\MSVC\" & strDir2 & "\bin\Host" & XlateArchitectureToWin(strHostArch) _
283 & "\" & XlateArchitectureToWin(strTargetArch)
284 if FileExists(strDir2 & "\cl.exe") then
285 EnvAppendPathItem "Path", DosSlashes(strDir2), ";"
286 if strTargetArch <> strHostArch then
287 EnvAppendPathItem "Path", DosSlashes(PathStripFilename(strDir2) & "\" & XlateArchitectureToWin(strHostArch)), ";"
288 end if
289 bldExitLoop = true
290 exit for
291 end if
292 next
293 elseif DirExists(strDir & "\bin") then
294 for each str2 in arrVccOldBinDirs
295 if FileExists(strDir & str2 & "\cl.exe") then
296 EnvAppendPathItem "Path", DosSlashes(strDir & str2), ";"
297 if str2 <> "\bin" then EnvAppendPathItem "Path", DosSlashes(strDir & "bin"), ";"
298 bldExitLoop = true
299 exit for
300 end if
301 next
302 end if
303 if bldExitLoop then exit for
304 next
305 if bldExitLoop then exit for
306 next
307
308 ' Add mingw64 if it's still there.
309 if strHostArch = "amd64" or strTargetArch = "amd64" then
310 str1 = strPathDev & "win.amd64\mingw-64\r1\bin"
311 if DirExists(str1) then EnvAppendPathItem "Path", DosSlashes(str1), ";"
312 end if
313 end if
314
315 ' Add the output tools and bin directories to the fron of the path, taking PATH_OUT_BASE into account.
316 dim strOutDir
317 strOutDir = EnvGetDef("PATH_OUT_BASE", strRootDir & "\out")
318 strOutDir = strOutDir & "\" & strTarget & "." & strTargetArch & "\" & strType
319 EnvPrependPathItem "Path", DosSlashes(strOutDir & "\bin\tools"), ";"
320 EnvPrependPathItem "Path", DosSlashes(strOutDir & "\bin"), ";"
321
322 ' Add kbuild binary directory to the front the the path.
323 EnvPrependPathItem "Path", DosSlashes(strPathkBuild & "\bin\win." & strHostArch), ";"
324
325 ' Finally, add the relevant tools/**/bin directories to the front of the path.
326 EnvPrependPathItem "Path", DosSlashes(strPathDevTools & "\bin"), ";"
327 if strHostArch = "amd64" then EnvPrependPathItem "Path", DosSlashes(strPathDevTools & "\win.x86\bin"), ";"
328 EnvPrependPathItem "Path", DosSlashes(strPathDevTools & "\win." & strHostArch) & "\bin", ";"
329
330 '
331 ' Export if we are not executing a program.
332 '
333 Main = g_rcScript
334 if ArraySize(arrCmdToExec) = 0 then
335 dim objTmpScript
336 set objTmpScript = g_objFileSys.CreateTextFile(strTmpScript, true, false)
337 objTmpScript.WriteLine
338
339 for each str1 in Array("Path", "KBUILD_PATH", "KBUILD_DEVTOOLS", "KBUILD_TYPE", _
340 "KBUILD_TARGET", "KBUILD_TARGET_ARCH", "KBUILD_HOST", "KBUILD_HOST_ARCH")
341 objTmpScript.WriteLine "SET " & str1 & "=" & EnvGet(str1)
342 next
343 for each str1 in arrObsolete
344 if EnvExists(str1) then objTmpScript.WriteLine "SET " & str1 & "="
345 next
346
347 if strChdirTo <> "" then
348 objTmpScript.WriteLine "CD """ & strChdirTo & """"
349 if Mid(strChdirTo, 2, 1) = ":" then
350 objTmpScript.WriteLine Left(strChdirTo, 2)
351 end if
352 end if
353
354 objTmpScript.Close()
355 '
356 ' Run the specified program.
357 '
358 ' We must redirect stderr to stdout here, because vbscript doesn't seem to
359 ' have any way to reuse the same console/stdout/stererr as we use (Exec
360 ' creates two pipes, Run a new console), nor can vbscript service two
361 ' TextStream/pipe objects at the same time without the risk of deadlocking
362 ' with the child process (we read stdout, child waits for stderr space).
363 '
364 ' So, to make it work we use kmk_redirect.exe to stuff everything into stderr
365 ' and ignore stdout.
366 '
367 else
368 if strChdirTo <> "" then
369 g_objShell.CurrentDirectory = strChdirTo
370 end if
371
372 ' Prepate the command line.
373 dim strCmdLine, str
374 strCmdLine = """" & DosSlashes(strPathkBuild) & "\bin\win." & strHostArch & "\kmk_redirect.exe"" -d1=2 -c0 -- " _
375 & """" & arrCmdToExec(0) & """"
376 for i = 1 to UBound(arrCmdToExec)
377 str = arrCmdToExec(i)
378 if InStr(1, str, " ") > 0 then '' @todo There is more stuff that needs escaping
379 strCmdLine = strCmdLine & " """ & str & """"
380 else
381 strCmdLine = strCmdLine & " " & str
382 end if
383 next
384
385 ' Start it.
386 if g_cntVerbose > 0 then MsgInfo "Executing command: " & strCmdLine
387 dim objChild
388 set objChild = g_objShell.Exec(strCmdLine)
389
390 ' The fun output / wait. As mention above, we only need to bother with stderr here.
391 dim cMsSleepMin : cMsSleepMin = 8
392 dim cMsSleepMax : cMsSleepMax = 92
393 dim cMsSleep : cMsSleep = cMsSleepMin
394 do while objChild.Status = 0
395 if not objChild.StdErr.AtEndOfStream then ' Seems this bugger might do a 0x80
396 WScript.StdErr.WriteLine objChild.StdErr.ReadLine()
397 cMsSleep = cMsSleepMin
398 elseif objChild.Status = 0 then
399 Wscript.Sleep cMsSleep
400 ' We probably only end up here once stderr is closed/disconnected (i.e. never).
401 ' This was originally written with the idea that AtEndOfStream would use
402 ' PeekNamedPipe to see if there were anything to read, rather than block.
403 ' Let's keep it for now.
404 if cMsSleep < cMsSleepMax then cMsSleep = cMsSleep + 8
405 end if
406 loop
407
408 ' Flush any remaining output on the offchance that we could get out of the above loop with pending output.
409 WScript.StdErr.Write strStdErr & objChild.StdErr.ReadAll()
410 WScript.StdOut.Write strStdOut & objChild.StdOut.ReadAll()
411
412 ' Return the exit code to our parent.
413 if g_cntVerbose > 0 then MsgInfo "Exit code = " & objChild.ExitCode
414 Main = objChild.ExitCode
415 end if
416end function
417
418'
419' What crt0.o typically does:
420'
421WScript.Quit(Main())
422
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette