Skip to content

Instantly share code, notes, and snippets.

@mxmauro
Created April 14, 2020 15:05
Show Gist options
  • Save mxmauro/4d392a8c830b8f84b650bacdf164b02e to your computer and use it in GitHub Desktop.
Save mxmauro/4d392a8c830b8f84b650bacdf164b02e to your computer and use it in GitHub Desktop.
Easily download and compile V8 engine on Windows
'Copyright 2020, Mauro H. Leggieri
'
'Permission is hereby granted, free of charge, to any person obtaining a copy of
'this software and associated documentation files (the "Software"), to deal in
'the Software without restriction, including without limitation the rights to
'use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
'of the Software, and to permit persons to whom the Software is furnished to do
'so, subject to the following conditions:
'
'The above copyright notice and this permission notice shall be included in all
'copies or substantial portions of the Software.
'
'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
'IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
'FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
'AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
'LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
'OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
'SOFTWARE.
Option Explicit
Const DontShowWindow = 0
Const DoShowWindow = 1
Const WaitUntilFinished = True
Const ForReading = 1
Const TristateFalse = 0
Const WshRunning = 0
Const StreamBinary = 1
Dim oFso, oShell, oWshSysEnv
Dim S, szScriptPath, szVisualStudioPath, szOption
Dim nIdx, nErr
Dim bRunAction, bForceDownload, bStaticLibrary, bEnableI18N, bUseSnapshot, bExtStartupData, bCompileProjects
'Set script path
szScriptPath = Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - Len(WScript.ScriptName))
Set oShell = CreateObject("WScript.Shell")
Set oFso = CreateObject("Scripting.FileSystemObject")
If oShell Is Nothing Or IsObject(oShell) = False Or oFso Is Nothing Or IsObject(oFso) = False Then
WScript.Echo "Error: Unable to create Shell object."
WScript.Quit 1
End If
'Set current directory to the location of the script
oShell.CurrentDirectory = szScriptPath
'-------------------------------------------------------------------------------
'Parse command line arguments
'-------------------------------------------------------------------------------
bForceDownload = False
bStaticLibrary = False
bEnableI18N = False
bUseSnapshot = False
bExtStartupData = False
bCompileProjects = True
For nIdx = 1 To WScript.Arguments.Count
S = WScript.Arguments(nIdx - 1)
szOption = ""
If Left(S, 1) = "/" Then
szOption = LCase(Mid(S, 2))
ElseIf Left(S, 2) = "--" Then
szOption = LCase(Mid(S, 3))
End If
If szOption = "help" Then
WScript.Echo "Use: CSCRIPT.EXE prepare_v8.vbs [options...]"
WScript.Echo ""
WScript.Echo "Where " & Chr(34) & "options" & Chr(34) & " can be:"
WScript.Echo " /force-download: Force a download of source code and tools."
WScript.Echo " /static-library: Compiles a non-static library."
WScript.Echo " /enable-i18n: Enables I18N support."
WScript.Echo " /use-snapshot: Enable the use of snapshots."
WScript.Echo " /external-startup-data: Enables the use of external startup data."
WScript.Echo " /skip-compile: Skips compilation step."
WScript.Quit 1
ElseIf szOption = "force-download" Then
bForceDownload = True
ElseIf szOption = "static-library" Then
bStaticLibrary = True
ElseIf szOption = "enable-i18n" Then
bEnableI18N = True
ElseIf szOption = "use-snapshot" Then
bUseSnapshot = True
ElseIf szOption = "external-startup-data" Then
bExtStartupData = True
ElseIf szOption = "skip-compile" Then
bCompileProjects = False
Else
WScript.Echo "Error: Invalid option " & Chr(34) & S & Chr(34) & "."
WScript.Quit 1
End If
Next
'-------------------------------------------------------------------------------
'Check required dependencies
'-------------------------------------------------------------------------------
If CheckDependencies() = False Then
WScript.Quit 1
End If
'-------------------------------------------------------------------------------
'Prepare Depot Tools
'-------------------------------------------------------------------------------
bRunAction = True
If bForceDownload = False Then
If oFso.FileExists(szScriptPath & "depot_tools\gclient.bat") Then bRunAction = False
End If
If bRunAction <> False Then
WScript.Echo "Downloading Depot Tools..."
'Create target directory
Call oShell.Run("CMD.EXE /C RD /S /Q depot_tools", DontShowWindow, WaitUntilFinished)
nErr = oShell.Run("CMD.EXE /C MD depot_tools", DontShowWindow, WaitUntilFinished)
If nErr <> 0 Then
WScript.Echo "Error: Cannot create directory " & Chr(34) & "depot_tools" & Chr(34) & ". [Code: " & CStr(nErr) & "]"
WScript.Quit 1
End If
'Download tools
nErr = DownloadFile("https://storage.googleapis.com/chrome-infra/depot_tools.zip", szScriptPath & "depot_tools\depot_tools.zip")
If nErr <> 0 Then
WScript.Echo "Error: Unable to download tools. [Code: " & CStr(nErr) & "]"
WScript.Quit 1
End If
'Decompress
WScript.Echo "Decompressing Depot Tools..."
nErr = ExtractZip(szScriptPath & "depot_tools\depot_tools.zip", szScriptPath & "depot_tools\")
If nErr <> 0 Then
WScript.Echo "Error: Cannot decompress tools. [Code: " & CStr(nErr) & "]"
WScript.Quit 1
End If
'Delete tools zip file
On Error Resume Next
oFso.DeleteFile szScriptPath & "depot_tools\depot_tools.zip"
On Error Goto 0
End If
'-------------------------------------------------------------------------------
'Setup environment
'-------------------------------------------------------------------------------
WScript.Echo "Locating Microsoft Visual Studio 2019..."
szVisualStudioPath = FindVisualStudio2019()
If Len(szVisualStudioPath) = 0 Then
WScript.Echo "Error: Unable to find Microsoft Visual Studio 2019."
WScript.Quit 1
End If
WScript.Echo " Found at " & szVisualStudioPath
WScript.Echo "Setting up environment variables..."
Set oWshSysEnv = oShell.Environment("PROCESS")
oWshSysEnv("DEPOT_TOOLS_WIN_TOOLCHAIN") = "0"
oWshSysEnv("GYP_MSVS_VERSION") = "2019"
oWshSysEnv("GYP_MSVS_OVERRIDE_PATH") = szVisualStudioPath
oWshSysEnv("PATH") = szScriptPath & "depot_tools;" & oShell.ExpandEnvironmentStrings("%PATH%")
'-------------------------------------------------------------------------------
'Prepare source code
'-------------------------------------------------------------------------------
bRunAction = True
If bForceDownload = False Then
If oFso.FileExists(szScriptPath & "src\v8\LICENSE") Then bRunAction = False
End If
If bRunAction <> False Then
WScript.Echo "Downloading V8 source code..."
'Create target directory
Call oShell.Run("CMD.EXE /C RD /S /Q src", DontShowWindow, WaitUntilFinished)
nErr = oShell.Run("CMD.EXE /C MD src", DontShowWindow, WaitUntilFinished)
If nErr <> 0 Then
WScript.Echo "Error: Cannot create directory " & Chr(34) & "src" & Chr(34) & ". [Code: " & CStr(nErr) & "]"
WScript.Quit 1
End If
'Download source code
oShell.CurrentDirectory = szScriptPath & "src\"
nErr = oShell.Run("fetch v8", DontShowWindow, WaitUntilFinished)
If nErr <> 0 Then
WScript.Echo "Error: Unable to download source code. [Code: " & CStr(nErr) & "]"
WScript.Quit 1
End If
'Execute hooks
WScript.Echo "Running GClient Hooks..."
oShell.CurrentDirectory = szScriptPath & "src\v8\"
nErr = oShell.Run("gclient runhooks", DontShowWindow, WaitUntilFinished)
If nErr <> 0 Then
WScript.Echo "Error: GClient process failed. [Code: " & CStr(nErr) & "]"
WScript.Quit 1
End If
End If
'-------------------------------------------------------------------------------
'Generate projects
'-------------------------------------------------------------------------------
oShell.CurrentDirectory = szScriptPath & "src\v8\"
Call oShell.Run("CMD.EXE /C RD /S /Q out.gn", DontShowWindow, WaitUntilFinished)
szOption = " -- is_component_build=false v8_static_library="
If bStaticLibrary <> False Then
szOption = szOption & "true"
Else
szOption = szOption & "false"
End If
szOption = szOption & " v8_enable_i18n_support="
If bEnableI18N <> False Then
szOption = szOption & "true"
Else
szOption = szOption & "false"
End If
szOption = szOption & " v8_use_snapshot="
If bUseSnapshot <> False Then
szOption = szOption & "true"
Else
szOption = szOption & "false"
End If
szOption = szOption & " v8_use_external_startup_data="
If bExtStartupData <> False Then
szOption = szOption & "true"
Else
szOption = szOption & "false"
End If
S = "python tools/dev/v8gen.py "
WScript.Echo "Generating Win32 Debug project..."
nErr = oShell.Run(S & "ia32.debug" & szOption, DontShowWindow, WaitUntilFinished)
If nErr = 0 Then
WScript.Echo "Generating Win32 Release project..."
nErr = oShell.Run(S & "ia32.release" & szOption, DontShowWindow, WaitUntilFinished)
End If
If nErr = 0 Then
WScript.Echo "Generating x64 Debug project..."
nErr = oShell.Run(S & "x64.debug" & szOption, DontShowWindow, WaitUntilFinished)
End If
If nErr = 0 Then
WScript.Echo "Generating x64 Release project..."
nErr = oShell.Run(S & "x64.release" & szOption, DontShowWindow, WaitUntilFinished)
End If
If nErr <> 0 Then
WScript.Echo "Error: Cannot complete project generation. [Code: " & CStr(nErr) & "]"
WScript.Quit 1
End If
'-------------------------------------------------------------------------------
'Compile projects
'-------------------------------------------------------------------------------
If bCompileProjects <> False Then
oShell.CurrentDirectory = szScriptPath & "src\v8\"
S = "ninja -C out.gn/"
WScript.Echo "Compiling Win32 Debug project..."
nErr = oShell.Run(S & "ia32.debug", DontShowWindow, WaitUntilFinished)
If nErr = 0 Then
WScript.Echo "Compiling Win32 Release project..."
nErr = oShell.Run(S & "ia32.release", DontShowWindow, WaitUntilFinished)
End If
If nErr = 0 Then
WScript.Echo "Compiling x64 Debug project..."
nErr = oShell.Run(S & "x64.debug", DontShowWindow, WaitUntilFinished)
End If
If nErr = 0 Then
WScript.Echo "Compiling x64 Release project..."
nErr = oShell.Run(S & "x64.release", DontShowWindow, WaitUntilFinished)
End If
If nErr <> 0 Then
WScript.Echo "Error: Cannot complete project compilation. [Code: " & CStr(nErr) & "]"
WScript.Quit 1
End If
End If
'-------------------------------------------------------------------------------
'Done
'-------------------------------------------------------------------------------
WScript.Echo "Done!"
WScript.Quit 0
'-------------------------------------------------------------------------------
Function FindVisualStudio2019()
Dim oApp
Dim S, szApp, szPath
szApp = szScriptPath & "deps\vswhere.exe -version [16.0,17.0) -property installationPath"
Set oApp = oShell.Exec(szApp)
If oApp Is Nothing Or IsObject(oApp) = False Then
FindVisualStudio2019 = ""
Set oApp = Nothing
Exit Function
End If
szPath = ""
Do While Not oApp.StdOut.AtEndOfStream
S = oApp.StdOut.ReadLine()
If Len(S) > 0 Then
szPath = S
Exit Do
End If
Loop
Set oApp = Nothing
If Len(szPath) > 0 And Right(szPath, 1) <> "\" Then szPath = szPath & "\"
FindVisualStudio2019 = szPath
Set oApp = Nothing
End Function
'-------------------------------------------------------------------------------
Function DownloadFile(szUrl, szFileName)
Dim oXmlHttp, oStream
Set oXmlHttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
If oXmlHttp Is Nothing Or IsObject(oXmlHttp) = False Then
Set oXmlHttp = CreateObject("Microsoft.XMLHTTP")
End If
Set oStream = CreateObject("Adodb.Stream")
If oXmlHttp Is Nothing Or IsObject(oXmlHttp) = False Or oStream Is Nothing Or IsObject(oStream) = False Then
DownloadFile = 2
Set oStream = Nothing
Set oXmlHttp = Nothing
Exit Function
End If
On Error Resume Next
Err.Clear
oXmlHttp.Open "GET", szUrl, False
If Err.Number = 0 Then
oXmlHttp.Send
End If
If Err.Number = 0 Then
oStream.Type = StreamBinary
oStream.Open
End If
If Err.Number = 0 Then
oStream.Write oXmlHttp.responseBody
End If
If Err.Number = 0 Then
oStream.SaveToFile szFileName, 2 '//overwrite
End If
DownloadFile = Err.Number
On Error Goto 0
Set oStream = Nothing
Set oXmlHttp = Nothing
End Function
Function DownloadAsString(szUrl)
Dim oXmlHttp
Set oXmlHttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
If oXmlHttp Is Nothing Or IsObject(oXmlHttp) = False Then
Set oXmlHttp = CreateObject("Microsoft.XMLHTTP")
End If
If oXmlHttp Is Nothing Or IsObject(oXmlHttp) = False Then
DownloadAsString = ""
Set oXmlHttp = Nothing
Exit Function
End If
On Error Resume Next
Err.Clear
oXmlHttp.Open "GET", szUrl, False
If Err.Number = 0 Then
oXmlHttp.Send
End If
If Err.Number = 0 Then
DownloadAsString = oXmlHttp.responseText
Else
DownloadAsString = ""
End If
On Error Goto 0
Set oXmlHttp = Nothing
End Function
'-------------------------------------------------------------------------------
Function ExtractZip(szFileName, szFolder)
Dim oShell, oFilesInZip
Set oShell = CreateObject("Shell.Application")
Set oFilesInZip = oShell.NameSpace(szFileName).Items
If oShell Is Nothing Or oFilesInZip Is Nothing Or IsObject(oShell) = False Or IsObject(oFilesInZip) = False Then
ExtractZip = 1
Set oFilesInZip = Nothing
Set oShell = Nothing
Exit Function
End If
On Error Resume Next
Err.Clear
Call oShell.NameSpace(szFolder).CopyHere(oFilesInZip, 4 Or 16 Or 512 Or 1024)
ExtractZip = Err.Number
On Error Goto 0
Set oFilesInZip = Nothing
Set oShell = Nothing
End Function
'-------------------------------------------------------------------------------
Function CheckDependencies()
Dim szContent, szUrl
Dim oRegex, oMatches
If oFso.FileExists(szScriptPath & "deps\vswhere.exe") = False Then
Call oShell.Run("CMD.EXE /C MD deps", DontShowWindow, WaitUntilFinished)
WScript.Echo "Downloading Microsoft Visual Studio Locator (VSWhere) utility..."
szContent = DownloadAsString("https://api.github.com/repos/microsoft/vswhere/releases/latest")
If Len(szContent) = 0 Then
WScript.Echo "Error: Unable to download Microsoft Visual Studio Locator repository information."
CheckDependencies = False
Exit Function
End If
Set oRegex = New RegExp
oRegex.Pattern = Chr(34) & "assets" & Chr(34) & "(?:.|\r|\n)*" & Chr(34) & "name" & Chr(34) & _
"\s*:\s*" & Chr(34) & "vswhere\.exe" & Chr(34) & "(?:.|\r|\n)*" & Chr(34) & _
"browser_download_url" & Chr(34) & "\s*:\s*" & Chr(34) & "([^" & _
Chr(34) & "]*)" & Chr(34)
oRegex.IgnoreCase = True
oRegex.Global = False
Set oMatches = oRegex.Execute(szContent)
If oMatches.Count = 0 Then
WScript.Echo "Error: Cannot locate latest release of Microsoft Visual Studio Locator utility."
CheckDependencies = False
Exit Function
End If
szUrl = oMatches.Item(0).SubMatches.Item(0)
WScript.Echo " Fount at " & szUrl
nErr = DownloadFile(szUrl, szScriptPath & "deps\vswhere.exe")
If nErr <> 0Then
WScript.Echo "Error: Unable to download Microsoft Visual Studio Locator utility. [Code: " & CStr(nErr) & "]"
CheckDependencies = False
Exit Function
End If
End If
CheckDependencies = True
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment