VirtualBox

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

Last change on this file since 106782 was 106778, checked in by vboxsync, 3 months ago

tools/envSub.vbs: Add the win.amd64 bin directories to the Path on arm64 as we're still missing bits and pieces. jiraref:VBP-1253

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