Fix whitespace representation for dgit
authorTimo Röhling <roehling@debian.org>
Sun, 28 Jan 2024 14:08:52 +0000 (15:08 +0100)
committerTimo Röhling <roehling@debian.org>
Sun, 28 Jan 2024 14:08:52 +0000 (15:08 +0100)
Gbp-Dch: ignore

third_party/civetweb/build.cmd
third_party/civetweb/mingw.cmd

index 8ccf0e4bc4a3291dba73abbf592e75209980ff9f..6c1df1609a8c1efd73c44649726ef58648575064 100644 (file)
-:: Make sure the extensions are enabled
-@verify other 2>nul
-@setlocal EnableDelayedExpansion
-@if errorlevel 1 (
-  call :print_usage "Failed to enable extensions"
-  exit /b 1
-)
-
-::Change the code page to unicode
-@chcp 65001 1>nul 2>nul
-@if errorlevel 1 (
-  call :print_usage "Failed to change the code page to unicode"
-  exit /b 1
-)
-
-:: Set up some global variables
-@set project=civetweb
-@set "script_name=%~nx0"
-@set "script_folder=%~dp0"
-@set "script_folder=%script_folder:~0,-1%"
-@set "output_path=%script_folder%\output"
-@set "build_path=%output_path%\build"
-@set "install_path=%output_path%\install"
-@set build_shared=OFF
-@set build_type=Release
-@set dependency_path=%TEMP%\%project%-build-dependencies
-
-:: Check the command line parameters
-@set logging_level=1
-@set "options=%* "
-@if not "!options!"=="!options:/? =!" set usage="Convenience script to build %project% with CMake"
-@for %%a in (%options%) do @(
-  @set arg=%%~a
-  @set arg=!arg: =!
-  @set one=!arg:~0,1!
-  @set two=!arg:~0,2!
-  @if /i [!arg!] == [/q] set quiet=true
-  @if /i [!two!] == [/v] call :verbosity "!arg!"
-  @if /i [!arg!] == [/s] set build_shared=ON
-  @if /i [!arg!] == [/d] set build_type=Debug
-  @if /i not [!one!] == [/] (
-    if not defined generator (
-      set generator=!arg!
-    ) else (
-      set usage="Too many generators: !method! !arg!" ^
-                "There should only be one generator parameter"
-    )
-  )
-)
-@if defined quiet (
-  set logging_level=0
-)
-@if not defined generator (
-  set generator=MSVC
-)
-@if /i not [%generator%] == [MinGW] (
-  if /i not [%generator%] == [MSVC] (
-    call :print_usage "Invalid argument: %generator%"
-    exit /b 1
-  )
-)
-
-:: Set up the logging
-@set log_folder=%output_path%\logs
-@call :iso8601 timestamp
-@set log_path=%log_folder%\%timestamp%.log
-@set log_keep=10
-
-:: Only keep a certain amount of logs
-@set /a "log_keep=log_keep-1"
-@if not exist %log_folder% @mkdir %log_folder%
-@for /f "skip=%log_keep%" %%f in ('dir /b /o-D /tc %log_folder%') do @(
-  call :log 4 "Removing old log file %log_folder%\%%f"
-  del %log_folder%\%%f
-)
-
-:: Set up some more global variables
-@call :architecture arch
-@call :windows_version win_ver win_ver_major win_ver_minor win_ver_rev
-@call :script_source script_source
-@if [%script_source%] == [explorer] (
-  set /a "logging_level=logging_level+1"
-)
-
-:: Print the usage or start the script
-@set exit_code=0
-@if defined usage (
-  call :print_usage %usage%
-) else (
-  call :main
-  @if errorlevel 1 (
-    @call :log 0 "Failed to build the %project% project"
-    @set exit_code=1
-  )
-)
-
-:: Tell the user where the built files are
-@call :log 5
-@call :log 0 "The built files are available in %install_path%"
-
-:: Stop the script if the user double clicked
-@if [%script_source%] == [explorer] (
-  pause
-)
-
-@exit /b %exit_code%
-@endlocal
-@goto :eof
-
-:: -------------------------- Functions start here ----------------------------
-
-:main - Main function that performs the build
-@setlocal
-@call :log 6
-@call :log 2 "Welcome to the %project% build script"
-@call :log 6 "------------------------------------"
-@call :log 6
-@call :log 2 "This script builds the project using CMake"
-@call :log 6
-@call :log 2 "Generating %generator%..."
-@call :log 6
-@set methods=dependencies ^
-             generate ^
-             build ^
-             install
-@for %%m in (%methods%) do @(
-  call :log 3 "Excuting the '%%m' method"
-  call :log 8
-  call :%%~m
-  if errorlevel 1 (
-    call :log 0 "Failed to complete the '%%~m' dependency routine"
-    call :log 0 "View the log at %log_path%"
-    exit /b 1
-  )
-)
-@call :log 6 "------------------------------------"
-@call :log 2 "Build complete"
-@call :log 6
-@endlocal
-@goto :eof
-
-:print_usage - Prints the usage of the script
-:: %* - message to print, each argument on it's own line
-@setlocal
-@for %%a in (%*) do @echo.%%~a
-@echo.
-@echo.build [/?][/v[v...]^|/q][MinGW^|MSVC]
-@echo.
-@echo.  [MinGW^|(MSVC)]
-@echo.              Builds the library with one of the compilers
-@echo.  /s          Builds shared libraries
-@echo.  /d          Builds a debug variant of the project
-@echo.  /v          Sets the output to be more verbose
-@echo.  /v[v...]    Extra verbosity, /vv, /vvv, etc
-@echo.  /q          Quiets the output
-@echo.  /?          Shows this usage message
-@echo.
-@endlocal
-@goto :eof
-
-:dependencies - Installs any prerequisites for the build
-@setlocal EnableDelayedExpansion
-@if errorlevel 1 (
-  call :log 0 "Failed to enable extensions"
-  exit /b 1
-)
-@call :log 5
-@call :log 0 "Installing dependencies for %generator%"
-@if /i [%generator%] == [MinGW] (
-  call :mingw compiler_path
-  @if errorlevel 1 (
-    @call :log 5
-    @call :log 0 "Failed to find MinGW"
-    @exit /b 1
-  )
-  set "PATH=!compiler_path!;%PATH%"
-  @call :find_in_path gcc_executable gcc.exe
-  @if errorlevel 1 (
-    @call :log 5
-    @call :log 0 "Failed to find gcc.exe"
-    @exit /b 1
-  )
-)
-@if [%reboot_required%] equ [1] call :reboot
-@endlocal & set "PATH=%PATH%"
-@goto :eof
-
-:generate - Uses CMake to generate the build files
-@setlocal EnableDelayedExpansion
-@if errorlevel 1 (
-  call :log 0 "Failed to enable extensions"
-  exit /b 1
-)
-@call :log 5
-@call :log 0 "Generating CMake files for %generator%"
-@call :cmake cmake_executable
-@if errorlevel 1 (
-  @call :log 5
-  @call :log 0 "Need CMake to create the build files"
-  @exit /b 1
-)
-@if /i [%generator%] == [MinGW] @(
-  @set "generator_var=-G "MinGW Makefiles^""
-)
-@if /i [%generator%] == [MSVC] @(
-  rem We could figure out the correct MSVS generator here
-)
-@call :iso8601 iso8601
-@set output=%temp%\cmake-%iso8601%.log
-@if not exist %build_path% mkdir %build_path%
-@cd %build_path%
-@"%cmake_executable%" ^
-  !generator_var! ^
-  -DCMAKE_BUILD_TYPE=!build_type! ^
-  -DBUILD_SHARED_LIBS=!build_shared! ^
-  "%script_folder%" > "%output%"
-@if errorlevel 1 (
-  @call :log 5
-  @call :log 0 "Failed to generate build files with CMake"
-  @call :log_append "%output%"
-  @cd %script_folder%
-  @exit /b 1
-)
-@cd %script_folder%
-@endlocal
-@goto :eof
-
-:build - Builds the library
-@setlocal EnableDelayedExpansion
-@if errorlevel 1 (
-  call :log 0 "Failed to enable extensions"
-  exit /b 1
-)
-@call :log 5
-@call :log 0 "Building %project% with %generator%"
-@if /i [%generator%] == [MinGW] @(
-  @call :find_in_path mingw32_make_executable mingw32-make.exe
-  @if errorlevel 1 (
-    @call :log 5
-    @call :log 0 "Failed to find mingw32-make"
-    @exit /b 1
-  )
-  @set "build_command=^"!mingw32_make_executable!^" all test"
-)
-@if /i [%generator%] == [MSVC] @(
-  @call :msbuild msbuild_executable
-  @if errorlevel 1 (
-    @call :log 5
-    @call :log 0 "Failed to find MSBuild"
-    @exit /b 1
-  )
-  @set "build_command=^"!msbuild_executable!^" /m:4 /p:Configuration=%build_type% %project%.sln"
-)
-@if not defined build_command (
-  @call :log 5
-  @call :log 0 "No build command for %generator%"
-  @exit /b 1
-)
-@cd %build_path%
-@call :iso8601 iso8601
-@set output=%temp%\build-%iso8601%.log
-@call :log 7
-@call :log 2 "Build command: %build_command:"=%"
-@%build_command% > "%output%"
-@if errorlevel 1 (
-  @call :log_append "%output%"
-  @call :log 5
-  @call :log 0 "Failed to complete the build"
-  @exit /b 1
-)
-@call :log_append "%output%"
-@cd %script_folder%
-@endlocal
-@goto :eof
-
-:install - Installs the built files
-@setlocal
-@call :log 5
-@call :log 0 "Installing built files"
-@call :cmake cmake_executable
-@if errorlevel 1 (
-  @call :log 5
-  @call :log 0 "Need CMake to install the built files"
-  @exit /b 1
-)
-@call :iso8601 iso8601
-@set output=%temp%\install-%iso8601%.log
-@"%cmake_executable%" ^
-  "-DCMAKE_INSTALL_PREFIX=%install_path%" ^
-  -P "%build_path%/cmake_install.cmake" ^
-  > "%output%"
-@if errorlevel 1 (
-  @call :log_append "%output%"
-  @call :log 5
-  @call :log 0 "Failed to install the files"
-  @exit /b 1
-)
-@call :log_append "%output%"
-@endlocal
-@goto :eof
-
-:script_source - Determines if the script was ran from the cli or explorer
-:: %1 - The return variable [cli|explorer]
-@verify other 2>nul
-@setlocal EnableDelayedExpansion
-@if errorlevel 1 (
-  call :log 0 "Failed to enable extensions"
-  exit /b 1
-)
-@call :log 3 "Attempting to detect the script source"
-@echo "The invocation command was: '%cmdcmdline%'" >> %log_path%
-@for /f "tokens=1-3,*" %%a in ("%cmdcmdline%") do @(
-  set cmd=%%~a
-  set arg1=%%~b
-  set arg2=%%~c
-  set rest=%%~d
-)
-@set quote="
-@if "!arg2:~0,1!" equ "!quote!" (
-  if "!arg2:~-1!" neq "!quote!" (
-    set "arg2=!arg2:~1!"
-  )
-)
-@call :log 4 "cmd  = %cmd%"
-@call :log 4 "arg1 = %arg1%"
-@call :log 4 "arg2 = %arg2%"
-@call :log 4 "rest = %rest%"
-@call :log 4 "src  = %~f0"
-@if /i "%arg2%" == "call" (
-  set script_source=cli
-) else (
-  @if /i "%arg1%" == "/c" (
-    set script_source=explorer
-  ) else (
-    set script_source=cli
-  )
-)
-@call :log 3 "The script was invoked from %script_source%"
-@endlocal & set "%~1=%script_source%"
-@goto :eof
-
-:architecture - Finds the system architecture
-:: %1 - The return variable [x86|x86_64]
-@setlocal
-@call :log 3 "Determining the processor architecture"
-@set "key=HKLM\System\CurrentControlSet\Control\Session Manager\Environment"
-@set "var=PROCESSOR_ARCHITECTURE"
-@for /f "skip=2 tokens=2,*" %%a in ('reg query "%key%" /v "%var%"') do @set "arch=%%b"
-@if "%arch%" == "AMD64" set arch=x86_64
-@call :log 4 "arch = %arch%"
-@endlocal & set "%~1=%arch%"
-@goto :eof
-
-:md5 - Gets the MD5 checksum for a file
-:: %1 - The hash
-:: %2 - The file path
-@setlocal
-@set var=%~1
-@set file_path=%~2
-@if [%var%] == [] exit /b 1
-@if "%file_path%" == "" exit /b 1
-@if not exist "%file_path%" exit /b 1
-@for /f "skip=3 tokens=1,*" %%a in ('powershell Get-FileHash -Algorithm MD5 "'%file_path%'"') do @set hash=%%b
-@if not defined hash (
-  call :log 6
-  call :log 0 "Failed to get MD5 hash for %file_path%"
-  exit /b 1
-)
-@endlocal & set "%var%=%hash: =%"
-@goto :eof
-
-:windows_version - Checks the windows version
-:: %1 - The windows version
-:: %2 - The major version number return variable
-:: %3 - The minor version number return variable
-:: %4 - The revision version number return variable
-@setlocal
-@call :log 3 "Retrieving the Windows version"
-@for /f "tokens=2 delims=[]" %%x in ('ver') do @set win_ver=%%x
-@set win_ver=%win_ver:Version =%
-@set win_ver_major=%win_ver:~0,1%
-@set win_ver_minor=%win_ver:~2,1%
-@set win_ver_rev=%win_ver:~4%
-@call :log 4 "win_ver = %win_ver%"
-@endlocal & set "%~1=%win_ver%" ^
-          & set "%~2=%win_ver_major%" ^
-          & set "%~3=%win_ver_minor%" ^
-          & set "%~4=%win_ver_rev%"
-@goto :eof
-
-:find_in_path - Finds a program of file in the PATH
-@setlocal
-@set var=%~1
-@set file=%~2
-@if [%var%] == [] exit /b 1
-@if [%file%] == [] exit /b 1
-@call :log 3 "Searching PATH for %file%"
-@for %%x in ("%file%") do @set "file_path=%%~f$PATH:x"
-@if not defined file_path exit /b 1
-@endlocal & set "%var%=%file_path%"
-@goto :eof
-
-:administrator_check - Checks for administrator priviledges
-@setlocal
-@call :log 2 "Checking for administrator priviledges"
-@set "key=HKLM\Software\VCA\Tool Chain\Admin Check"
-@reg add "%key%" /v Elevated /t REG_DWORD /d 1 /f > nul 2>&1
-@if errorlevel 1 exit /b 1
-@reg delete "%key%" /va /f > nul 2>&1
-@endlocal
-@goto :eof
-
-:log_append - Appends another file into the current logging file
-:: %1 - the file_path to the file to concatenate
-@setlocal
-@set "file_path=%~1"
-@if [%file_path%] == [] exit /b 1
-@call :log 3 "Appending to log: %file_path%"
-@call :iso8601 iso8601
-@set "temp_log=%temp%\append-%iso8601%.log"
-@call :log 4 "Using temp file %temp_log%"
-@type "%log_path%" "%file_path%" > "%temp_log%" 2>nul
-@move /y "%temp_log%" "%log_path%" 1>nul
-@del "%file_path%" 2>nul
-@del "%temp_log%" 2>nul
-@endlocal
-@goto :eof
-
-:iso8601 - Returns the current time in ISO8601 format
-:: %1 - the return variable
-:: %2 - format [extended|basic*]
-:: iso8601 - contains the resulting timestamp
-@setlocal
-@wmic Alias /? >NUL 2>&1 || @exit /b 1
-@set "var=%~1"
-@if "%var%" == "" @exit /b 1
-@set "format=%~2"
-@if "%format%" == "" set format=basic
-@for /F "skip=1 tokens=1-6" %%g IN ('wmic Path Win32_UTCTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') do @(
-  @if "%%~l"=="" goto :iso8601_done
-  @set "yyyy=%%l"
-  @set "mm=00%%j"
-  @set "dd=00%%g"
-  @set "hour=00%%h"
-  @set "minute=00%%i"
-  @set "seconds=00%%k"
-)
-:iso8601_done
-@set mm=%mm:~-2%
-@set dd=%dd:~-2%
-@set hour=%hour:~-2%
-@set minute=%minute:~-2%
-@set seconds=%seconds:~-2%
-@if /i [%format%] == [extended] (
-  set iso8601=%yyyy%-%mm%-%dd%T%hour%:%minute%:%seconds%Z
-) else (
-  if /i [%format%] == [basic] (
-    set iso8601=%yyyy%%mm%%dd%T%hour%%minute%%seconds%Z
-  ) else (
-    @exit /b 1
-  )
-)
-@set iso8601=%iso8601: =0%
-@endlocal & set %var%=%iso8601%
-@goto :eof
-
-:verbosity - Processes the verbosity parameter '/v[v...]
-:: %1 - verbosity given on the command line
-:: logging_level - set to the number of v's
-@setlocal
-@set logging_level=0
-@set verbosity=%~1
-:verbosity_loop
-@set verbosity=%verbosity:~1%
-@if not [%verbosity%] == [] @(
-  set /a "logging_level=logging_level+1"
-  goto verbosity_loop
-)
-@endlocal & set logging_level=%logging_level%
-@goto :eof
-
-:log - Logs a message, depending on verbosity
-:: %1 - level
-::       [0-4] for CLI logging
-::       [5-9] for GUI logging
-:: %2 - message to print
-@setlocal
-@set "level=%~1"
-@set "msg=%~2"
-@if "%log_folder%" == "" (
-  echo Logging was used to early in the script, log_folder isn't set yet
-  goto :eof
-)
-@if "%log_path%" == "" (
-  echo Logging was used to early in the script, log_path isn't set yet
-  goto :eof
-)
-@if not exist "%log_folder%" mkdir "%log_folder%"
-@if not exist "%log_path%" echo. 1>nul 2>"%log_path%"
-@echo.%msg% >> "%log_path%"
-@if %level% geq 5 (
-  @if [%script_source%] == [explorer] (
-    set /a "level=level-5"
-  ) else (
-    @goto :eof
-  )
-)
-@if "%logging_level%" == "" (
-  echo Logging was used to early in the script, logging_level isn't set yet
-  goto :eof
-)
-@if %logging_level% geq %level% echo.%msg% 1>&2
-@endlocal
-@goto :eof
-
-
-:start_browser - Opens the default browser to a URL
-:: %1 - the url to open
-@setlocal
-@set url=%~1
-@call :log 4 "Opening default browser: %url%"
-@start %url%
-@endlocal
-@goto :eof
-
-:find_cmake - Finds cmake on the command line or in the registry
-:: %1 - the cmake file path
-@setlocal
-@set var=%~1
-@if [%var%] == [] exit /b 1
-@call :log 6
-@call :log 6 "Finding CMake"
-@call :log 6 "--------------"
-@call :find_in_path cmake_executable cmake.exe
-@if not errorlevel 1 goto found_cmake
-@for /l %%i in (5,-1,0) do @(
-@for /l %%j in (9,-1,0) do @(
-@for /l %%k in (9,-1,0) do @(
-@for %%l in (HKCU HKLM) do @(
-@for %%m in (SOFTWARE SOFTWARE\Wow6432Node) do @(
-  @reg query "%%l\%%m\Kitware\CMake %%i.%%j.%%k" /ve > nul 2>nul
-  @if not errorlevel 1 (
-    @for /f "skip=2 tokens=2,*" %%a in ('reg query "%%l\%%m\Kitware\CMake %%i.%%j.%%k" /ve') do @(
-      @if exist "%%b\bin\cmake.exe" (
-        @set "cmake_executable=%%b\bin\cmake.exe"
-        goto found_cmake
-      )
-    )
-  )
-)))))
-@call :log 5
-@call :log 0 "Failed to find cmake"
-@exit /b 1
-:found_cmake
-@endlocal & set "%var%=%cmake_executable%"
-@goto :eof
-
-:cmake - Finds cmake and installs it if necessary
-:: %1 - the cmake file path
-@setlocal
-@set var=%~1
-@if [%var%] == [] exit /b 1
-@call :log 6
-@call :log 6 "Checking for CMake"
-@call :log 6 "------------------"
-@call :find_cmake cmake_executable cmake.exe
-@if not errorlevel 1 goto got_cmake
-@set checksum=C00267A3D3D9619A7A2E8FA4F46D7698
-@set version=3.2.2
-@call :install_nsis cmake http://www.cmake.org/files/v%version:~0,3%/cmake-%version%-win32-x86.exe %checksum%
-@if errorlevel 1 (
-  call :log 5
-  call :log 0 "Failed to install cmake"
-  @exit /b 1
-)
-@call :find_cmake cmake_executable cmake.exe
-@if not errorlevel 1 goto got_cmake
-@call :log 5
-@call :log 0 "Failed to check for cmake"
-@exit /b 1
-:got_cmake
-@endlocal & set "%var%=%cmake_executable%"
-@goto :eof
-
-:mingw - Finds MinGW, installing it if needed
-:: %1 - the compiler path that should be added to PATH
-@setlocal EnableDelayedExpansion
-@if errorlevel 1 (
-  @call :log 5
-  @call :log 0 "Failed to enable extensions"
-  @exit /b 1
-)
-@set var=%~1
-@if [%var%] == [] exit /b 1
-@call :log 6
-@call :log 6 "Checking for MinGW"
-@call :log 6 "------------------"
-@call :find_in_path gcc_executable gcc.exe
-@if not errorlevel 1 (
-  @for %%a in ("%gcc_executable%") do @set "compiler_path=%%~dpa"
-  goto got_mingw
-)
-@call :log 7
-@call :log 2 "Downloading MinGW"
-@if %logging_level% leq 1 set "logging=/q"
-@if %logging_level% gtr 1 set "logging=/v"
-@set output_path=
-@for /f %%a in ('call
-    "%script_folder%\mingw.cmd"
-    %logging%
-    /arch "%arch%"
-    "%dependency_path%"'
-) do @set "compiler_path=%%a\"
-@if not defined compiler_path (
-  @call :log_append "%output%"
-  @call :log 5
-  @call :log 0 "Failed to download MinGW"
-  @exit /b 1
-)
-:got_mingw
-@call :log 5
-@call :log 0 "Found MinGW: %compiler_path%gcc.exe"
-@endlocal & set "%var%=%compiler_path%"
-@goto :eof
-
-:msbuild - Finds MSBuild
-:: %1 - the path to MSBuild executable
-@setlocal
-@set var=%~1
-@if [%var%] == [] exit /b 1
-@call :find_in_path msbuild_executable msbuild.exe
-@if not errorlevel 1 goto got_msbuild
-@for /l %%i in (20,-1,4) do @(
-@for /l %%j in (9,-1,0) do @(
-@for %%k in (HKCU HKLM) do @(
-@for %%l in (SOFTWARE SOFTWARE\Wow6432Node) do @(
-  @reg query "%%k\%%l\Microsoft\MSBuild\%%i.%%j" /v MSBuildOverrideTasksPath > nul 2>nul
-  @if not errorlevel 1 (
-    @for /f "skip=2 tokens=2,*" %%a in ('reg query "%%k\%%l\Microsoft\MSBuild\%%i.%%j" /v MSBuildOverrideTasksPath') do @(
-      @if exist "%%bmsbuild.exe" (
-        @set "msbuild_executable=%%bmsbuild.exe"
-        goto got_msbuild
-      )
-    )
-  )
-))))
-@call :log 5
-@call :log 0 "Failed to check for MSBuild"
-@exit /b 1
-:got_msbuild
-@endlocal & set "%var%=%msbuild_executable%"
-@goto :eof
-
-:download - Downloads a file from the internet
-:: %1 - the url of the file to download
-:: %2 - the file to download to
-:: %3 - the MD5 checksum of the file (optional)
-@setlocal EnableDelayedExpansion
-@if errorlevel 1 (
-  call :print_usage "Failed to enable extensions"
-  exit /b 1
-)
-@set url=%~1
-@set file_path=%~2
-@set checksum=%~3
-@for %%a in (%file_path%) do @set dir_path=%%~dpa
-@for %%a in (%file_path%) do @set file_name=%%~nxa
-@if [%url%] == [] exit /b 1
-@if [%file_path%] == [] exit /b 1
-@if [%dir_path%] == [] exit /b 1
-@if [%file_name%] == [] exit /b 1
-@if not exist "%dir_path%" mkdir "%dir_path%"
-@call :log 1 "Downloading %url%"
-@call :iso8601 iso8601
-@set temp_path=%temp%\download-%iso8601%-%file_name%
-@call :log 3 "Using temp file %temp_path%"
-@powershell Invoke-WebRequest "%url%" -OutFile %temp_path%
-@if errorlevel 1 (
-  call :log 0 "Failed to download %url%"
-  exit /b 1
-)
-@if [%checksum%] neq [] (
-  @call :log 4 "Checking %checksum% against %temp_path%"
-  @call :md5 hash "%temp_path%"
-  if "!hash!" neq "%checksum%" (
-    call :log 0 "Failed to match checksum: %temp_path%"
-    call :log 0 "Hash    : !hash!"
-    call :log 0 "Checksum: %checksum%"
-    exit /b 1
-  ) else (
-    call :log 3 "Checksum matched: %temp_path%"
-    call :log 3 "Hash    : !hash!"
-    call :log 3 "Checksum: %checksum%"
-  )
-)
-@call :log 4 "Renaming %temp_path% to %file_path%"
-@move /y "%temp_path%" "%file_path%" 1>nul
-@endlocal
-@goto :eof
-
-:install_msi - Installs a dependency from an Microsoft Installer package (.msi)
-:: %1 - [string] name of the project to install
-:: %2 - The location of the .msi, a url must start with 'http://' or file_path
-:: %3 - The checksum of the msi (optional)
-@setlocal
-@set name=%~1
-@set file_path=%~2
-@set checksum=%~3
-@set msi=%~nx2
-@set msi_path=%dependency_path%\%msi%
-@if [%name%] == [] exit /b 1
-@if [%file_path%] == [] exit /b 1
-@if [%msi%] == [] exit /b 1
-@if [%msi_path%] == [] exit /b 1
-@for %%x in (msiexec.exe) do @set "msiexec_path=%%~f$PATH:x"
-@if "msiexec_path" == "" (
-  call :log 0 "Failed to find the Microsoft package installer (msiexec.exe)"
-  call :log 6
-  call :log 0 "Please install it from the Microsoft Download center"
-  call :log 6
-  choice /C YN /T 60 /D N /M "Would you like to go there now?"
-  if !errorlevel! equ 1 call :start_browser ^
-    "http://search.microsoft.com/DownloadResults.aspx?q=Windows+Installer"
-  exit /b 1
-)
-@call :log 6
-@call :log 1 "Installing the '%name%' dependency"
-@call :log 6 "-------------------------------------"
-@call :administrator_check
-@if errorlevel 1 (
-  call :log 0 "You must run %~nx0 in elevated mode to install '%name%'"
-  call :log 5 "Right-Click and select 'Run as Administrator'
-  call :log 0 "Install the dependency manually by running %file_path%"
-  @exit /b 740
-)
-@if [%file_path:~0,4%] == [http] (
-  if not exist "%msi_path%" (
-    call :download "%file_path%" "%msi_path%" %checksum%
-    if errorlevel 1 (
-      call :log 0 "Failed to download the %name% dependency"
-      exit /b 1
-    )
-  )
-) else (
-  call :log 2 "Copying MSI %file_path% to %msi_path%"
-  call :log 7
-  if not exist "%msi_path%" (
-    xcopy /q /y /z "%file_path%" "%msi_path%" 1>nul
-    if errorlevel 1 (
-      call :log 0 "Failed to copy the Microsoft Installer"
-      exit /b 1
-    )
-  )
-)
-@call :log 1 "Running the %msi%"
-@call :log 6
-@set msi_log=%temp%\msiexec-%timestamp%.log
-@call :log 3 "Logging to: %msi_log%"
-@msiexec /i "%msi_path%" /passive /log "%msi_log%" ALLUSERS=1
-@set msi_errorlevel=%errorlevel%
-@call :log_append "%msi_log%"
-@if %msi_errorlevel% equ 0 goto install_msi_success
-@if %msi_errorlevel% equ 3010 goto install_msi_success_reboot
-@if %msi_errorlevel% equ 1641 goto install_msi_success_reboot
-@if %msi_errorlevel% equ 3015 goto install_msi_in_progress_reboot
-@if %msi_errorlevel% equ 1615 goto install_msi_in_progress_reboot
-@call :log 0 "Microsoft Installer failed: %msi_errorlevel%"
-@call :log 0 "Install the dependency manually by running %msi_path%"
-@exit /b 1
-:install_msi_in_progress_reboot
-@call :log 0 "The installation requires a reboot to continue"
-@call :log 5
-@call :reboot
-@exit /b 1
-:install_msi_success_reboot
-@call :log 3 "The installation requires a reboot to be fully functional"
-@set reboot_required=1
-:install_msi_success
-@call :log 2 "Successfully installed %name%"
-@call :log 7
-@endlocal & set reboot_required=%reboot_required%
-@goto :eof
-
-:install_nsis - Installs a dependency from an Nullsoft Installer package (.exe)
-:: %1 - [string] name of the project to install
-:: %2 - The location of the .exe, a url must start with 'http://' or file_path
-:: %3 - The checksum of the exe (optional)
-@setlocal
-@set name=%~1
-@set file_path=%~2
-@set checksum=%~3
-@set exe=%~nx2
-@set exe_path=%dependency_path%\%exe%
-@if [%name%] == [] exit /b 1
-@if [%file_path%] == [] exit /b 1
-@if [%exe%] == [] exit /b 1
-@if [%exe_path%] == [] exit /b 1
-@call :log 6
-@call :log 1 "Installing the '%name%' dependency"
-@call :log 6 "-------------------------------------"
-@call :administrator_check
-@if errorlevel 1 (
-  call :log 0 "You must run %~nx0 in elevated mode to install '%name%'"
-  call :log 5 "Right-Click and select 'Run as Administrator'
-  call :log 0 "Install the dependency manually by running %file_path%"
-  @exit /b 740
-)
-@if [%file_path:~0,4%] == [http] (
-  if not exist "%exe_path%" (
-    call :download "%file_path%" "%exe_path%" %checksum%
-    if errorlevel 1 (
-      call :log 0 "Failed to download the %name% dependency"
-      exit /b 1
-    )
-  )
-) else (
-  call :log 2 "Copying installer %file_path% to %exe_path%"
-  call :log 7
-  if not exist "%exe_path%" (
-    xcopy /q /y /z "%file_path%" "%exe_path%" 1>nul
-    if errorlevel 1 (
-      call :log 0 "Failed to copy the Nullsoft Installer"
-      exit /b 1
-    )
-  )
-)
-@call :log 1 "Running the %exe%"
-@call :log 6
-@"%exe_path%" /S
-@set nsis_errorlevel=%errorlevel%
-@if %nsis_errorlevel% equ 0 goto install_nsis_success
-@if %nsis_errorlevel% equ 3010 goto install_nsis_success_reboot
-@if %nsis_errorlevel% equ 1641 goto install_nsis_success_reboot
-@if %nsis_errorlevel% equ 3015 goto install_nsis_in_progress_reboot
-@if %nsis_errorlevel% equ 1615 goto install_nsis_in_progress_reboot
-@call :log 0 "Nullsoft Installer failed: %nsis_errorlevel%"
-@call :log 0 "Install the dependency manually by running %exe_path%"
-@exit /b 1
-:install_nsis_in_progress_reboot
-@call :log 0 "The installation requires a reboot to continue"
-@call :log 5
-@call :reboot
-@exit /b 1
-:install_nsis_success_reboot
-@call :log 3 "The installation requires a reboot to be fully functional"
-@set reboot_required=1
-:install_nsis_success
-@call :log 2 "Successfully installed %name%"
-@call :log 7
-@endlocal & set reboot_required=%reboot_required%
-@goto :eof
-
-:reboot - Asks the user if they would like to reboot then stops the script
-@setlocal
-@call :log 6 "-------------------------------------------"
-@choice /C YN /T 60 /D N /M "The %method% requires a reboot, reboot now?"
-@set ret=%errorlevel%
-@call :log 6
-@if %ret% equ 1 (
-  @shutdown /r
-) else (
-  @call :log 0 "You will need to reboot to complete the %method%"
-  @call :log 5
-)
-@endlocal
-@goto :eof
+:: Make sure the extensions are enabled\r
+@verify other 2>nul\r
+@setlocal EnableDelayedExpansion\r
+@if errorlevel 1 (\r
+  call :print_usage "Failed to enable extensions"\r
+  exit /b 1\r
+)\r
+\r
+::Change the code page to unicode\r
+@chcp 65001 1>nul 2>nul\r
+@if errorlevel 1 (\r
+  call :print_usage "Failed to change the code page to unicode"\r
+  exit /b 1\r
+)\r
+\r
+:: Set up some global variables\r
+@set project=civetweb\r
+@set "script_name=%~nx0"\r
+@set "script_folder=%~dp0"\r
+@set "script_folder=%script_folder:~0,-1%"\r
+@set "output_path=%script_folder%\output"\r
+@set "build_path=%output_path%\build"\r
+@set "install_path=%output_path%\install"\r
+@set build_shared=OFF\r
+@set build_type=Release\r
+@set dependency_path=%TEMP%\%project%-build-dependencies\r
+\r
+:: Check the command line parameters\r
+@set logging_level=1\r
+@set "options=%* "\r
+@if not "!options!"=="!options:/? =!" set usage="Convenience script to build %project% with CMake"\r
+@for %%a in (%options%) do @(\r
+  @set arg=%%~a\r
+  @set arg=!arg: =!\r
+  @set one=!arg:~0,1!\r
+  @set two=!arg:~0,2!\r
+  @if /i [!arg!] == [/q] set quiet=true\r
+  @if /i [!two!] == [/v] call :verbosity "!arg!"\r
+  @if /i [!arg!] == [/s] set build_shared=ON\r
+  @if /i [!arg!] == [/d] set build_type=Debug\r
+  @if /i not [!one!] == [/] (\r
+    if not defined generator (\r
+      set generator=!arg!\r
+    ) else (\r
+      set usage="Too many generators: !method! !arg!" ^\r
+                "There should only be one generator parameter"\r
+    )\r
+  )\r
+)\r
+@if defined quiet (\r
+  set logging_level=0\r
+)\r
+@if not defined generator (\r
+  set generator=MSVC\r
+)\r
+@if /i not [%generator%] == [MinGW] (\r
+  if /i not [%generator%] == [MSVC] (\r
+    call :print_usage "Invalid argument: %generator%"\r
+    exit /b 1\r
+  )\r
+)\r
+\r
+:: Set up the logging\r
+@set log_folder=%output_path%\logs\r
+@call :iso8601 timestamp\r
+@set log_path=%log_folder%\%timestamp%.log\r
+@set log_keep=10\r
+\r
+:: Only keep a certain amount of logs\r
+@set /a "log_keep=log_keep-1"\r
+@if not exist %log_folder% @mkdir %log_folder%\r
+@for /f "skip=%log_keep%" %%f in ('dir /b /o-D /tc %log_folder%') do @(\r
+  call :log 4 "Removing old log file %log_folder%\%%f"\r
+  del %log_folder%\%%f\r
+)\r
+\r
+:: Set up some more global variables\r
+@call :architecture arch\r
+@call :windows_version win_ver win_ver_major win_ver_minor win_ver_rev\r
+@call :script_source script_source\r
+@if [%script_source%] == [explorer] (\r
+  set /a "logging_level=logging_level+1"\r
+)\r
+\r
+:: Print the usage or start the script\r
+@set exit_code=0\r
+@if defined usage (\r
+  call :print_usage %usage%\r
+) else (\r
+  call :main\r
+  @if errorlevel 1 (\r
+    @call :log 0 "Failed to build the %project% project"\r
+    @set exit_code=1\r
+  )\r
+)\r
+\r
+:: Tell the user where the built files are\r
+@call :log 5\r
+@call :log 0 "The built files are available in %install_path%"\r
+\r
+:: Stop the script if the user double clicked\r
+@if [%script_source%] == [explorer] (\r
+  pause\r
+)\r
+\r
+@exit /b %exit_code%\r
+@endlocal\r
+@goto :eof\r
+\r
+:: -------------------------- Functions start here ----------------------------\r
+\r
+:main - Main function that performs the build\r
+@setlocal\r
+@call :log 6\r
+@call :log 2 "Welcome to the %project% build script"\r
+@call :log 6 "------------------------------------"\r
+@call :log 6\r
+@call :log 2 "This script builds the project using CMake"\r
+@call :log 6\r
+@call :log 2 "Generating %generator%..."\r
+@call :log 6\r
+@set methods=dependencies ^\r
+             generate ^\r
+             build ^\r
+             install\r
+@for %%m in (%methods%) do @(\r
+  call :log 3 "Excuting the '%%m' method"\r
+  call :log 8\r
+  call :%%~m\r
+  if errorlevel 1 (\r
+    call :log 0 "Failed to complete the '%%~m' dependency routine"\r
+    call :log 0 "View the log at %log_path%"\r
+    exit /b 1\r
+  )\r
+)\r
+@call :log 6 "------------------------------------"\r
+@call :log 2 "Build complete"\r
+@call :log 6\r
+@endlocal\r
+@goto :eof\r
+\r
+:print_usage - Prints the usage of the script\r
+:: %* - message to print, each argument on it's own line\r
+@setlocal\r
+@for %%a in (%*) do @echo.%%~a\r
+@echo.\r
+@echo.build [/?][/v[v...]^|/q][MinGW^|MSVC]\r
+@echo.\r
+@echo.  [MinGW^|(MSVC)]\r
+@echo.              Builds the library with one of the compilers\r
+@echo.  /s          Builds shared libraries\r
+@echo.  /d          Builds a debug variant of the project\r
+@echo.  /v          Sets the output to be more verbose\r
+@echo.  /v[v...]    Extra verbosity, /vv, /vvv, etc\r
+@echo.  /q          Quiets the output\r
+@echo.  /?          Shows this usage message\r
+@echo.\r
+@endlocal\r
+@goto :eof\r
+\r
+:dependencies - Installs any prerequisites for the build\r
+@setlocal EnableDelayedExpansion\r
+@if errorlevel 1 (\r
+  call :log 0 "Failed to enable extensions"\r
+  exit /b 1\r
+)\r
+@call :log 5\r
+@call :log 0 "Installing dependencies for %generator%"\r
+@if /i [%generator%] == [MinGW] (\r
+  call :mingw compiler_path\r
+  @if errorlevel 1 (\r
+    @call :log 5\r
+    @call :log 0 "Failed to find MinGW"\r
+    @exit /b 1\r
+  )\r
+  set "PATH=!compiler_path!;%PATH%"\r
+  @call :find_in_path gcc_executable gcc.exe\r
+  @if errorlevel 1 (\r
+    @call :log 5\r
+    @call :log 0 "Failed to find gcc.exe"\r
+    @exit /b 1\r
+  )\r
+)\r
+@if [%reboot_required%] equ [1] call :reboot\r
+@endlocal & set "PATH=%PATH%"\r
+@goto :eof\r
+\r
+:generate - Uses CMake to generate the build files\r
+@setlocal EnableDelayedExpansion\r
+@if errorlevel 1 (\r
+  call :log 0 "Failed to enable extensions"\r
+  exit /b 1\r
+)\r
+@call :log 5\r
+@call :log 0 "Generating CMake files for %generator%"\r
+@call :cmake cmake_executable\r
+@if errorlevel 1 (\r
+  @call :log 5\r
+  @call :log 0 "Need CMake to create the build files"\r
+  @exit /b 1\r
+)\r
+@if /i [%generator%] == [MinGW] @(\r
+  @set "generator_var=-G "MinGW Makefiles^""\r
+)\r
+@if /i [%generator%] == [MSVC] @(\r
+  rem We could figure out the correct MSVS generator here\r
+)\r
+@call :iso8601 iso8601\r
+@set output=%temp%\cmake-%iso8601%.log\r
+@if not exist %build_path% mkdir %build_path%\r
+@cd %build_path%\r
+@"%cmake_executable%" ^\r
+  !generator_var! ^\r
+  -DCMAKE_BUILD_TYPE=!build_type! ^\r
+  -DBUILD_SHARED_LIBS=!build_shared! ^\r
+  "%script_folder%" > "%output%"\r
+@if errorlevel 1 (\r
+  @call :log 5\r
+  @call :log 0 "Failed to generate build files with CMake"\r
+  @call :log_append "%output%"\r
+  @cd %script_folder%\r
+  @exit /b 1\r
+)\r
+@cd %script_folder%\r
+@endlocal\r
+@goto :eof\r
+\r
+:build - Builds the library\r
+@setlocal EnableDelayedExpansion\r
+@if errorlevel 1 (\r
+  call :log 0 "Failed to enable extensions"\r
+  exit /b 1\r
+)\r
+@call :log 5\r
+@call :log 0 "Building %project% with %generator%"\r
+@if /i [%generator%] == [MinGW] @(\r
+  @call :find_in_path mingw32_make_executable mingw32-make.exe\r
+  @if errorlevel 1 (\r
+    @call :log 5\r
+    @call :log 0 "Failed to find mingw32-make"\r
+    @exit /b 1\r
+  )\r
+  @set "build_command=^"!mingw32_make_executable!^" all test"\r
+)\r
+@if /i [%generator%] == [MSVC] @(\r
+  @call :msbuild msbuild_executable\r
+  @if errorlevel 1 (\r
+    @call :log 5\r
+    @call :log 0 "Failed to find MSBuild"\r
+    @exit /b 1\r
+  )\r
+  @set "build_command=^"!msbuild_executable!^" /m:4 /p:Configuration=%build_type% %project%.sln"\r
+)\r
+@if not defined build_command (\r
+  @call :log 5\r
+  @call :log 0 "No build command for %generator%"\r
+  @exit /b 1\r
+)\r
+@cd %build_path%\r
+@call :iso8601 iso8601\r
+@set output=%temp%\build-%iso8601%.log\r
+@call :log 7\r
+@call :log 2 "Build command: %build_command:"=%"\r
+@%build_command% > "%output%"\r
+@if errorlevel 1 (\r
+  @call :log_append "%output%"\r
+  @call :log 5\r
+  @call :log 0 "Failed to complete the build"\r
+  @exit /b 1\r
+)\r
+@call :log_append "%output%"\r
+@cd %script_folder%\r
+@endlocal\r
+@goto :eof\r
+\r
+:install - Installs the built files\r
+@setlocal\r
+@call :log 5\r
+@call :log 0 "Installing built files"\r
+@call :cmake cmake_executable\r
+@if errorlevel 1 (\r
+  @call :log 5\r
+  @call :log 0 "Need CMake to install the built files"\r
+  @exit /b 1\r
+)\r
+@call :iso8601 iso8601\r
+@set output=%temp%\install-%iso8601%.log\r
+@"%cmake_executable%" ^\r
+  "-DCMAKE_INSTALL_PREFIX=%install_path%" ^\r
+  -P "%build_path%/cmake_install.cmake" ^\r
+  > "%output%"\r
+@if errorlevel 1 (\r
+  @call :log_append "%output%"\r
+  @call :log 5\r
+  @call :log 0 "Failed to install the files"\r
+  @exit /b 1\r
+)\r
+@call :log_append "%output%"\r
+@endlocal\r
+@goto :eof\r
+\r
+:script_source - Determines if the script was ran from the cli or explorer\r
+:: %1 - The return variable [cli|explorer]\r
+@verify other 2>nul\r
+@setlocal EnableDelayedExpansion\r
+@if errorlevel 1 (\r
+  call :log 0 "Failed to enable extensions"\r
+  exit /b 1\r
+)\r
+@call :log 3 "Attempting to detect the script source"\r
+@echo "The invocation command was: '%cmdcmdline%'" >> %log_path%\r
+@for /f "tokens=1-3,*" %%a in ("%cmdcmdline%") do @(\r
+  set cmd=%%~a\r
+  set arg1=%%~b\r
+  set arg2=%%~c\r
+  set rest=%%~d\r
+)\r
+@set quote="\r
+@if "!arg2:~0,1!" equ "!quote!" (\r
+  if "!arg2:~-1!" neq "!quote!" (\r
+    set "arg2=!arg2:~1!"\r
+  )\r
+)\r
+@call :log 4 "cmd  = %cmd%"\r
+@call :log 4 "arg1 = %arg1%"\r
+@call :log 4 "arg2 = %arg2%"\r
+@call :log 4 "rest = %rest%"\r
+@call :log 4 "src  = %~f0"\r
+@if /i "%arg2%" == "call" (\r
+  set script_source=cli\r
+) else (\r
+  @if /i "%arg1%" == "/c" (\r
+    set script_source=explorer\r
+  ) else (\r
+    set script_source=cli\r
+  )\r
+)\r
+@call :log 3 "The script was invoked from %script_source%"\r
+@endlocal & set "%~1=%script_source%"\r
+@goto :eof\r
+\r
+:architecture - Finds the system architecture\r
+:: %1 - The return variable [x86|x86_64]\r
+@setlocal\r
+@call :log 3 "Determining the processor architecture"\r
+@set "key=HKLM\System\CurrentControlSet\Control\Session Manager\Environment"\r
+@set "var=PROCESSOR_ARCHITECTURE"\r
+@for /f "skip=2 tokens=2,*" %%a in ('reg query "%key%" /v "%var%"') do @set "arch=%%b"\r
+@if "%arch%" == "AMD64" set arch=x86_64\r
+@call :log 4 "arch = %arch%"\r
+@endlocal & set "%~1=%arch%"\r
+@goto :eof\r
+\r
+:md5 - Gets the MD5 checksum for a file\r
+:: %1 - The hash\r
+:: %2 - The file path\r
+@setlocal\r
+@set var=%~1\r
+@set file_path=%~2\r
+@if [%var%] == [] exit /b 1\r
+@if "%file_path%" == "" exit /b 1\r
+@if not exist "%file_path%" exit /b 1\r
+@for /f "skip=3 tokens=1,*" %%a in ('powershell Get-FileHash -Algorithm MD5 "'%file_path%'"') do @set hash=%%b\r
+@if not defined hash (\r
+  call :log 6\r
+  call :log 0 "Failed to get MD5 hash for %file_path%"\r
+  exit /b 1\r
+)\r
+@endlocal & set "%var%=%hash: =%"\r
+@goto :eof\r
+\r
+:windows_version - Checks the windows version\r
+:: %1 - The windows version\r
+:: %2 - The major version number return variable\r
+:: %3 - The minor version number return variable\r
+:: %4 - The revision version number return variable\r
+@setlocal\r
+@call :log 3 "Retrieving the Windows version"\r
+@for /f "tokens=2 delims=[]" %%x in ('ver') do @set win_ver=%%x\r
+@set win_ver=%win_ver:Version =%\r
+@set win_ver_major=%win_ver:~0,1%\r
+@set win_ver_minor=%win_ver:~2,1%\r
+@set win_ver_rev=%win_ver:~4%\r
+@call :log 4 "win_ver = %win_ver%"\r
+@endlocal & set "%~1=%win_ver%" ^\r
+          & set "%~2=%win_ver_major%" ^\r
+          & set "%~3=%win_ver_minor%" ^\r
+          & set "%~4=%win_ver_rev%"\r
+@goto :eof\r
+\r
+:find_in_path - Finds a program of file in the PATH\r
+@setlocal\r
+@set var=%~1\r
+@set file=%~2\r
+@if [%var%] == [] exit /b 1\r
+@if [%file%] == [] exit /b 1\r
+@call :log 3 "Searching PATH for %file%"\r
+@for %%x in ("%file%") do @set "file_path=%%~f$PATH:x"\r
+@if not defined file_path exit /b 1\r
+@endlocal & set "%var%=%file_path%"\r
+@goto :eof\r
+\r
+:administrator_check - Checks for administrator priviledges\r
+@setlocal\r
+@call :log 2 "Checking for administrator priviledges"\r
+@set "key=HKLM\Software\VCA\Tool Chain\Admin Check"\r
+@reg add "%key%" /v Elevated /t REG_DWORD /d 1 /f > nul 2>&1\r
+@if errorlevel 1 exit /b 1\r
+@reg delete "%key%" /va /f > nul 2>&1\r
+@endlocal\r
+@goto :eof\r
+\r
+:log_append - Appends another file into the current logging file\r
+:: %1 - the file_path to the file to concatenate\r
+@setlocal\r
+@set "file_path=%~1"\r
+@if [%file_path%] == [] exit /b 1\r
+@call :log 3 "Appending to log: %file_path%"\r
+@call :iso8601 iso8601\r
+@set "temp_log=%temp%\append-%iso8601%.log"\r
+@call :log 4 "Using temp file %temp_log%"\r
+@type "%log_path%" "%file_path%" > "%temp_log%" 2>nul\r
+@move /y "%temp_log%" "%log_path%" 1>nul\r
+@del "%file_path%" 2>nul\r
+@del "%temp_log%" 2>nul\r
+@endlocal\r
+@goto :eof\r
+\r
+:iso8601 - Returns the current time in ISO8601 format\r
+:: %1 - the return variable\r
+:: %2 - format [extended|basic*]\r
+:: iso8601 - contains the resulting timestamp\r
+@setlocal\r
+@wmic Alias /? >NUL 2>&1 || @exit /b 1\r
+@set "var=%~1"\r
+@if "%var%" == "" @exit /b 1\r
+@set "format=%~2"\r
+@if "%format%" == "" set format=basic\r
+@for /F "skip=1 tokens=1-6" %%g IN ('wmic Path Win32_UTCTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') do @(\r
+  @if "%%~l"=="" goto :iso8601_done\r
+  @set "yyyy=%%l"\r
+  @set "mm=00%%j"\r
+  @set "dd=00%%g"\r
+  @set "hour=00%%h"\r
+  @set "minute=00%%i"\r
+  @set "seconds=00%%k"\r
+)\r
+:iso8601_done\r
+@set mm=%mm:~-2%\r
+@set dd=%dd:~-2%\r
+@set hour=%hour:~-2%\r
+@set minute=%minute:~-2%\r
+@set seconds=%seconds:~-2%\r
+@if /i [%format%] == [extended] (\r
+  set iso8601=%yyyy%-%mm%-%dd%T%hour%:%minute%:%seconds%Z\r
+) else (\r
+  if /i [%format%] == [basic] (\r
+    set iso8601=%yyyy%%mm%%dd%T%hour%%minute%%seconds%Z\r
+  ) else (\r
+    @exit /b 1\r
+  )\r
+)\r
+@set iso8601=%iso8601: =0%\r
+@endlocal & set %var%=%iso8601%\r
+@goto :eof\r
+\r
+:verbosity - Processes the verbosity parameter '/v[v...]\r
+:: %1 - verbosity given on the command line\r
+:: logging_level - set to the number of v's\r
+@setlocal\r
+@set logging_level=0\r
+@set verbosity=%~1\r
+:verbosity_loop\r
+@set verbosity=%verbosity:~1%\r
+@if not [%verbosity%] == [] @(\r
+  set /a "logging_level=logging_level+1"\r
+  goto verbosity_loop\r
+)\r
+@endlocal & set logging_level=%logging_level%\r
+@goto :eof\r
+\r
+:log - Logs a message, depending on verbosity\r
+:: %1 - level\r
+::       [0-4] for CLI logging\r
+::       [5-9] for GUI logging\r
+:: %2 - message to print\r
+@setlocal\r
+@set "level=%~1"\r
+@set "msg=%~2"\r
+@if "%log_folder%" == "" (\r
+  echo Logging was used to early in the script, log_folder isn't set yet\r
+  goto :eof\r
+)\r
+@if "%log_path%" == "" (\r
+  echo Logging was used to early in the script, log_path isn't set yet\r
+  goto :eof\r
+)\r
+@if not exist "%log_folder%" mkdir "%log_folder%"\r
+@if not exist "%log_path%" echo. 1>nul 2>"%log_path%"\r
+@echo.%msg% >> "%log_path%"\r
+@if %level% geq 5 (\r
+  @if [%script_source%] == [explorer] (\r
+    set /a "level=level-5"\r
+  ) else (\r
+    @goto :eof\r
+  )\r
+)\r
+@if "%logging_level%" == "" (\r
+  echo Logging was used to early in the script, logging_level isn't set yet\r
+  goto :eof\r
+)\r
+@if %logging_level% geq %level% echo.%msg% 1>&2\r
+@endlocal\r
+@goto :eof\r
+\r
+\r
+:start_browser - Opens the default browser to a URL\r
+:: %1 - the url to open\r
+@setlocal\r
+@set url=%~1\r
+@call :log 4 "Opening default browser: %url%"\r
+@start %url%\r
+@endlocal\r
+@goto :eof\r
+\r
+:find_cmake - Finds cmake on the command line or in the registry\r
+:: %1 - the cmake file path\r
+@setlocal\r
+@set var=%~1\r
+@if [%var%] == [] exit /b 1\r
+@call :log 6\r
+@call :log 6 "Finding CMake"\r
+@call :log 6 "--------------"\r
+@call :find_in_path cmake_executable cmake.exe\r
+@if not errorlevel 1 goto found_cmake\r
+@for /l %%i in (5,-1,0) do @(\r
+@for /l %%j in (9,-1,0) do @(\r
+@for /l %%k in (9,-1,0) do @(\r
+@for %%l in (HKCU HKLM) do @(\r
+@for %%m in (SOFTWARE SOFTWARE\Wow6432Node) do @(\r
+  @reg query "%%l\%%m\Kitware\CMake %%i.%%j.%%k" /ve > nul 2>nul\r
+  @if not errorlevel 1 (\r
+    @for /f "skip=2 tokens=2,*" %%a in ('reg query "%%l\%%m\Kitware\CMake %%i.%%j.%%k" /ve') do @(\r
+      @if exist "%%b\bin\cmake.exe" (\r
+        @set "cmake_executable=%%b\bin\cmake.exe"\r
+        goto found_cmake\r
+      )\r
+    )\r
+  )\r
+)))))\r
+@call :log 5\r
+@call :log 0 "Failed to find cmake"\r
+@exit /b 1\r
+:found_cmake\r
+@endlocal & set "%var%=%cmake_executable%"\r
+@goto :eof\r
+\r
+:cmake - Finds cmake and installs it if necessary\r
+:: %1 - the cmake file path\r
+@setlocal\r
+@set var=%~1\r
+@if [%var%] == [] exit /b 1\r
+@call :log 6\r
+@call :log 6 "Checking for CMake"\r
+@call :log 6 "------------------"\r
+@call :find_cmake cmake_executable cmake.exe\r
+@if not errorlevel 1 goto got_cmake\r
+@set checksum=C00267A3D3D9619A7A2E8FA4F46D7698\r
+@set version=3.2.2\r
+@call :install_nsis cmake http://www.cmake.org/files/v%version:~0,3%/cmake-%version%-win32-x86.exe %checksum%\r
+@if errorlevel 1 (\r
+  call :log 5\r
+  call :log 0 "Failed to install cmake"\r
+  @exit /b 1\r
+)\r
+@call :find_cmake cmake_executable cmake.exe\r
+@if not errorlevel 1 goto got_cmake\r
+@call :log 5\r
+@call :log 0 "Failed to check for cmake"\r
+@exit /b 1\r
+:got_cmake\r
+@endlocal & set "%var%=%cmake_executable%"\r
+@goto :eof\r
+\r
+:mingw - Finds MinGW, installing it if needed\r
+:: %1 - the compiler path that should be added to PATH\r
+@setlocal EnableDelayedExpansion\r
+@if errorlevel 1 (\r
+  @call :log 5\r
+  @call :log 0 "Failed to enable extensions"\r
+  @exit /b 1\r
+)\r
+@set var=%~1\r
+@if [%var%] == [] exit /b 1\r
+@call :log 6\r
+@call :log 6 "Checking for MinGW"\r
+@call :log 6 "------------------"\r
+@call :find_in_path gcc_executable gcc.exe\r
+@if not errorlevel 1 (\r
+  @for %%a in ("%gcc_executable%") do @set "compiler_path=%%~dpa"\r
+  goto got_mingw\r
+)\r
+@call :log 7\r
+@call :log 2 "Downloading MinGW"\r
+@if %logging_level% leq 1 set "logging=/q"\r
+@if %logging_level% gtr 1 set "logging=/v"\r
+@set output_path=\r
+@for /f %%a in ('call\r
+    "%script_folder%\mingw.cmd"\r
+    %logging%\r
+    /arch "%arch%"\r
+    "%dependency_path%"'\r
+) do @set "compiler_path=%%a\"\r
+@if not defined compiler_path (\r
+  @call :log_append "%output%"\r
+  @call :log 5\r
+  @call :log 0 "Failed to download MinGW"\r
+  @exit /b 1\r
+)\r
+:got_mingw\r
+@call :log 5\r
+@call :log 0 "Found MinGW: %compiler_path%gcc.exe"\r
+@endlocal & set "%var%=%compiler_path%"\r
+@goto :eof\r
+\r
+:msbuild - Finds MSBuild\r
+:: %1 - the path to MSBuild executable\r
+@setlocal\r
+@set var=%~1\r
+@if [%var%] == [] exit /b 1\r
+@call :find_in_path msbuild_executable msbuild.exe\r
+@if not errorlevel 1 goto got_msbuild\r
+@for /l %%i in (20,-1,4) do @(\r
+@for /l %%j in (9,-1,0) do @(\r
+@for %%k in (HKCU HKLM) do @(\r
+@for %%l in (SOFTWARE SOFTWARE\Wow6432Node) do @(\r
+  @reg query "%%k\%%l\Microsoft\MSBuild\%%i.%%j" /v MSBuildOverrideTasksPath > nul 2>nul\r
+  @if not errorlevel 1 (\r
+    @for /f "skip=2 tokens=2,*" %%a in ('reg query "%%k\%%l\Microsoft\MSBuild\%%i.%%j" /v MSBuildOverrideTasksPath') do @(\r
+      @if exist "%%bmsbuild.exe" (\r
+        @set "msbuild_executable=%%bmsbuild.exe"\r
+        goto got_msbuild\r
+      )\r
+    )\r
+  )\r
+))))\r
+@call :log 5\r
+@call :log 0 "Failed to check for MSBuild"\r
+@exit /b 1\r
+:got_msbuild\r
+@endlocal & set "%var%=%msbuild_executable%"\r
+@goto :eof\r
+\r
+:download - Downloads a file from the internet\r
+:: %1 - the url of the file to download\r
+:: %2 - the file to download to\r
+:: %3 - the MD5 checksum of the file (optional)\r
+@setlocal EnableDelayedExpansion\r
+@if errorlevel 1 (\r
+  call :print_usage "Failed to enable extensions"\r
+  exit /b 1\r
+)\r
+@set url=%~1\r
+@set file_path=%~2\r
+@set checksum=%~3\r
+@for %%a in (%file_path%) do @set dir_path=%%~dpa\r
+@for %%a in (%file_path%) do @set file_name=%%~nxa\r
+@if [%url%] == [] exit /b 1\r
+@if [%file_path%] == [] exit /b 1\r
+@if [%dir_path%] == [] exit /b 1\r
+@if [%file_name%] == [] exit /b 1\r
+@if not exist "%dir_path%" mkdir "%dir_path%"\r
+@call :log 1 "Downloading %url%"\r
+@call :iso8601 iso8601\r
+@set temp_path=%temp%\download-%iso8601%-%file_name%\r
+@call :log 3 "Using temp file %temp_path%"\r
+@powershell Invoke-WebRequest "%url%" -OutFile %temp_path%\r
+@if errorlevel 1 (\r
+  call :log 0 "Failed to download %url%"\r
+  exit /b 1\r
+)\r
+@if [%checksum%] neq [] (\r
+  @call :log 4 "Checking %checksum% against %temp_path%"\r
+  @call :md5 hash "%temp_path%"\r
+  if "!hash!" neq "%checksum%" (\r
+    call :log 0 "Failed to match checksum: %temp_path%"\r
+    call :log 0 "Hash    : !hash!"\r
+    call :log 0 "Checksum: %checksum%"\r
+    exit /b 1\r
+  ) else (\r
+    call :log 3 "Checksum matched: %temp_path%"\r
+    call :log 3 "Hash    : !hash!"\r
+    call :log 3 "Checksum: %checksum%"\r
+  )\r
+)\r
+@call :log 4 "Renaming %temp_path% to %file_path%"\r
+@move /y "%temp_path%" "%file_path%" 1>nul\r
+@endlocal\r
+@goto :eof\r
+\r
+:install_msi - Installs a dependency from an Microsoft Installer package (.msi)\r
+:: %1 - [string] name of the project to install\r
+:: %2 - The location of the .msi, a url must start with 'http://' or file_path\r
+:: %3 - The checksum of the msi (optional)\r
+@setlocal\r
+@set name=%~1\r
+@set file_path=%~2\r
+@set checksum=%~3\r
+@set msi=%~nx2\r
+@set msi_path=%dependency_path%\%msi%\r
+@if [%name%] == [] exit /b 1\r
+@if [%file_path%] == [] exit /b 1\r
+@if [%msi%] == [] exit /b 1\r
+@if [%msi_path%] == [] exit /b 1\r
+@for %%x in (msiexec.exe) do @set "msiexec_path=%%~f$PATH:x"\r
+@if "msiexec_path" == "" (\r
+  call :log 0 "Failed to find the Microsoft package installer (msiexec.exe)"\r
+  call :log 6\r
+  call :log 0 "Please install it from the Microsoft Download center"\r
+  call :log 6\r
+  choice /C YN /T 60 /D N /M "Would you like to go there now?"\r
+  if !errorlevel! equ 1 call :start_browser ^\r
+    "http://search.microsoft.com/DownloadResults.aspx?q=Windows+Installer"\r
+  exit /b 1\r
+)\r
+@call :log 6\r
+@call :log 1 "Installing the '%name%' dependency"\r
+@call :log 6 "-------------------------------------"\r
+@call :administrator_check\r
+@if errorlevel 1 (\r
+  call :log 0 "You must run %~nx0 in elevated mode to install '%name%'"\r
+  call :log 5 "Right-Click and select 'Run as Administrator'\r
+  call :log 0 "Install the dependency manually by running %file_path%"\r
+  @exit /b 740\r
+)\r
+@if [%file_path:~0,4%] == [http] (\r
+  if not exist "%msi_path%" (\r
+    call :download "%file_path%" "%msi_path%" %checksum%\r
+    if errorlevel 1 (\r
+      call :log 0 "Failed to download the %name% dependency"\r
+      exit /b 1\r
+    )\r
+  )\r
+) else (\r
+  call :log 2 "Copying MSI %file_path% to %msi_path%"\r
+  call :log 7\r
+  if not exist "%msi_path%" (\r
+    xcopy /q /y /z "%file_path%" "%msi_path%" 1>nul\r
+    if errorlevel 1 (\r
+      call :log 0 "Failed to copy the Microsoft Installer"\r
+      exit /b 1\r
+    )\r
+  )\r
+)\r
+@call :log 1 "Running the %msi%"\r
+@call :log 6\r
+@set msi_log=%temp%\msiexec-%timestamp%.log\r
+@call :log 3 "Logging to: %msi_log%"\r
+@msiexec /i "%msi_path%" /passive /log "%msi_log%" ALLUSERS=1\r
+@set msi_errorlevel=%errorlevel%\r
+@call :log_append "%msi_log%"\r
+@if %msi_errorlevel% equ 0 goto install_msi_success\r
+@if %msi_errorlevel% equ 3010 goto install_msi_success_reboot\r
+@if %msi_errorlevel% equ 1641 goto install_msi_success_reboot\r
+@if %msi_errorlevel% equ 3015 goto install_msi_in_progress_reboot\r
+@if %msi_errorlevel% equ 1615 goto install_msi_in_progress_reboot\r
+@call :log 0 "Microsoft Installer failed: %msi_errorlevel%"\r
+@call :log 0 "Install the dependency manually by running %msi_path%"\r
+@exit /b 1\r
+:install_msi_in_progress_reboot\r
+@call :log 0 "The installation requires a reboot to continue"\r
+@call :log 5\r
+@call :reboot\r
+@exit /b 1\r
+:install_msi_success_reboot\r
+@call :log 3 "The installation requires a reboot to be fully functional"\r
+@set reboot_required=1\r
+:install_msi_success\r
+@call :log 2 "Successfully installed %name%"\r
+@call :log 7\r
+@endlocal & set reboot_required=%reboot_required%\r
+@goto :eof\r
+\r
+:install_nsis - Installs a dependency from an Nullsoft Installer package (.exe)\r
+:: %1 - [string] name of the project to install\r
+:: %2 - The location of the .exe, a url must start with 'http://' or file_path\r
+:: %3 - The checksum of the exe (optional)\r
+@setlocal\r
+@set name=%~1\r
+@set file_path=%~2\r
+@set checksum=%~3\r
+@set exe=%~nx2\r
+@set exe_path=%dependency_path%\%exe%\r
+@if [%name%] == [] exit /b 1\r
+@if [%file_path%] == [] exit /b 1\r
+@if [%exe%] == [] exit /b 1\r
+@if [%exe_path%] == [] exit /b 1\r
+@call :log 6\r
+@call :log 1 "Installing the '%name%' dependency"\r
+@call :log 6 "-------------------------------------"\r
+@call :administrator_check\r
+@if errorlevel 1 (\r
+  call :log 0 "You must run %~nx0 in elevated mode to install '%name%'"\r
+  call :log 5 "Right-Click and select 'Run as Administrator'\r
+  call :log 0 "Install the dependency manually by running %file_path%"\r
+  @exit /b 740\r
+)\r
+@if [%file_path:~0,4%] == [http] (\r
+  if not exist "%exe_path%" (\r
+    call :download "%file_path%" "%exe_path%" %checksum%\r
+    if errorlevel 1 (\r
+      call :log 0 "Failed to download the %name% dependency"\r
+      exit /b 1\r
+    )\r
+  )\r
+) else (\r
+  call :log 2 "Copying installer %file_path% to %exe_path%"\r
+  call :log 7\r
+  if not exist "%exe_path%" (\r
+    xcopy /q /y /z "%file_path%" "%exe_path%" 1>nul\r
+    if errorlevel 1 (\r
+      call :log 0 "Failed to copy the Nullsoft Installer"\r
+      exit /b 1\r
+    )\r
+  )\r
+)\r
+@call :log 1 "Running the %exe%"\r
+@call :log 6\r
+@"%exe_path%" /S\r
+@set nsis_errorlevel=%errorlevel%\r
+@if %nsis_errorlevel% equ 0 goto install_nsis_success\r
+@if %nsis_errorlevel% equ 3010 goto install_nsis_success_reboot\r
+@if %nsis_errorlevel% equ 1641 goto install_nsis_success_reboot\r
+@if %nsis_errorlevel% equ 3015 goto install_nsis_in_progress_reboot\r
+@if %nsis_errorlevel% equ 1615 goto install_nsis_in_progress_reboot\r
+@call :log 0 "Nullsoft Installer failed: %nsis_errorlevel%"\r
+@call :log 0 "Install the dependency manually by running %exe_path%"\r
+@exit /b 1\r
+:install_nsis_in_progress_reboot\r
+@call :log 0 "The installation requires a reboot to continue"\r
+@call :log 5\r
+@call :reboot\r
+@exit /b 1\r
+:install_nsis_success_reboot\r
+@call :log 3 "The installation requires a reboot to be fully functional"\r
+@set reboot_required=1\r
+:install_nsis_success\r
+@call :log 2 "Successfully installed %name%"\r
+@call :log 7\r
+@endlocal & set reboot_required=%reboot_required%\r
+@goto :eof\r
+\r
+:reboot - Asks the user if they would like to reboot then stops the script\r
+@setlocal\r
+@call :log 6 "-------------------------------------------"\r
+@choice /C YN /T 60 /D N /M "The %method% requires a reboot, reboot now?"\r
+@set ret=%errorlevel%\r
+@call :log 6\r
+@if %ret% equ 1 (\r
+  @shutdown /r\r
+) else (\r
+  @call :log 0 "You will need to reboot to complete the %method%"\r
+  @call :log 5\r
+)\r
+@endlocal\r
+@goto :eof\r
index 4b26215f1ee4819a785a04334e593dab0fca5a81..57437334853e535a3cbc20e2d364b25222df99f2 100644 (file)
-:: Make sure the extensions are enabled
-@verify other 2>nul
-@setlocal EnableExtensions EnableDelayedExpansion
-@if errorlevel 1 (
-  @call :print_usage "Failed to enable extensions"
-  @exit /b 1
-)
-
-::Change the code page to unicode
-@chcp 65001 1>nul 2>nul
-@if errorlevel 1 (
-  @call :print_usage "Failed to change the code page to unicode"
-  @exit /b 1
-)
-
-:: Set up some global variables
-@set "script_name=%~nx0"
-@set "script_folder=%~dp0"
-@set "script_folder=%script_folder:~0,-1%"
-@set "dependency_path=%TEMP%\mingw-build-dependencies"
-
-:: Check the command line parameters
-@set logging_level=1
-:options_loop
-@if [%1] == [] goto :options_parsed
-@set "arg=%~1"
-@set one=%arg:~0,1%
-@set two=%arg:~0,2%
-@set three=%arg:~0,3%
-@if /i [%arg%] == [/?] (
-  @call :print_usage "Downloads a specific version of MinGW"
-  @exit /b 0
-)
-@if /i [%arg%] == [/q] set quiet=true
-@if /i [%two%] == [/v] @if /i not [%three%] == [/ve] @call :verbosity "!arg!"
-@if /i [%arg%] == [/version] set "version=%~2" & shift
-@if /i [%arg%] == [/arch] set "arch=%~2" & shift
-@if /i [%arg%] == [/exceptions] set "exceptions=%~2" & shift
-@if /i [%arg%] == [/threading] set "threading=%~2" & shift
-@if /i [%arg%] == [/revision] set "revision=%~2" & shift
-@if /i not [!one!] == [/] (
-  if not defined output_path (
-    set output_path=!arg!
-  ) else (
-    @call :print_usage "Too many output locations: !output_path! !arg!" ^
-                       "There should only be one output location"
-    @exit /b 1
-  )
-)
-@shift
-@goto :options_loop
-:options_parsed
-@if defined quiet set logging_level=0
-@if not defined output_path set "output_path=%script_folder%\mingw-builds"
-@set "output_path=%output_path:/=\%"
-
-:: Set up the logging
-@set "log_folder=%output_path%\logs"
-@call :iso8601 timestamp
-@set "log_path=%log_folder%\%timestamp%.log"
-@set log_keep=10
-
-:: Get default architecture
-@if not defined arch @call :architecture arch
-
-:: Only keep a certain amount of logs
-@set /a "log_keep=log_keep-1"
-@if not exist %log_folder% @mkdir %log_folder%
-@for /f "skip=%log_keep%" %%f in ('dir /b /o-D /tc %log_folder%') do @(
-  @call :log 4 "Removing old log file %log_folder%\%%f"
-  del %log_folder%\%%f
-)
-
-:: Set up some more global variables
-@call :windows_version win_ver win_ver_major win_ver_minor win_ver_rev
-@call :script_source script_source
-@if [%script_source%] == [explorer] (
-  set /a "logging_level=logging_level+1"
-)
-
-:: Execute the main function
-@call :main "%arch%" "%version%" "%threading%" "%exceptions%" "%revision%"
-@if errorlevel 1 (
-  @call :log 0 "Failed to download MinGW"
-  @call :log 0 "View the log at %log_path%"
-  @exit /b 1
-)
-
-:: Stop the script if the user double clicked
-@if [%script_source%] == [explorer] (
-  pause
-)
-
-@endlocal
-@goto :eof
-
-:: -------------------------- Functions start here ----------------------------
-
-:main - Main function that performs the download
-:: %1 - Target architecture
-:: %2 - Version of MinGW to get [optional]
-:: %3 - Threading model [optional]
-:: %4 - Exception model [optional]
-:: %5 - Package revision [optional]
-@setlocal
-@call :log 6
-@call :log 2 "Welcome to the MinGW download script"
-@call :log 6 "------------------------------------"
-@call :log 6
-@call :log 2 "This script downloads a specific version of MinGW"
-@set "arch=%~1"
-@if "%arch%" == "" @exit /b 1
-@set "version=%~2"
-@set "threading=%~3"
-@set "exceptions=%~4"
-@set "revision=%~5"
-@call :log 3 "arch       = %arch%"
-@call :log 3 "version    = %version%"
-@call :log 3 "exceptions = %exceptions%"
-@call :log 3 "threading  = %threading%"
-@call :log 3 "revision   = %revision%"
-@call :repository repo
-@if errorlevel 1 (
-  @call :log 0 "Failed to get the MinGW-builds repository information"
-  @exit /b 1
-)
-@call :resolve slug url "%repo%" "%arch%" "%version%" "%threading%" "%exceptions%" "%revision%"
-@if errorlevel 1 (
-  @call :log 0 "Failed to resolve the correct URL of MinGW"
-  @exit /b 1
-)
-@call :unpack compiler_path "%url%" "%output_path%\mingw\%slug%"
-@if errorlevel 1 (
-  @call :log 0 "Failed to unpack the MinGW archive"
-  @exit /b 1
-)
-@rmdir /s /q "%dependency_path%"
-@echo.%compiler_path%
-@endlocal
-@goto :eof
-
-:repository - Gets the MinGW-builds repository
-:: %1 - The return variable for the repository file path
-@verify other 2>nul
-@setlocal EnableDelayedExpansion
-@if errorlevel 1 (
-  @call :log 0 "Failed to enable extensions"
-  @exit /b 1
-)
-@set "var=%~1"
-@if "%var%" == "" @exit /b 1
-@call :log 7
-@call :log 2 "Getting MinGW repository information"
-@set "url=http://downloads.sourceforge.net/project/mingw-w64/Toolchains targetting Win32/Personal Builds/mingw-builds/installer/repository.txt"
-@call :log 6
-@call :log 1 "Downloading MinGW repository"
-@set "file_path=%dependency_path%\mingw-repository.txt"
-@call :download "%url%" "%file_path%"
-@if errorlevel 1 (
-  @call :log 0 "Failed to download the MinGW repository information"
-  @exit /b 1
-)
-@set "repository_path=%dependency_path%\repository.txt"
-@del "%repository_path%" 2>nul
-@for /f "delims=| tokens=1-6,*" %%a in (%file_path%) do @(
-  @set "version=%%~a"
-  @set "version=!version: =!"
-  @set "arch=%%~b"
-  @set "arch=!arch: =!"
-  @set "threading=%%~c"
-  @set "threading=!threading: =!"
-  @set "exceptions=%%~d"
-  @set "exceptions=!exceptions: =!"
-  @set "revision=%%~e"
-  @set "revision=!revision: =!"
-  @set "revision=!revision:rev=!"
-  @set "url=%%~f"
-  @set "url=!url:%%20= !"
-  @for /l %%a in (1,1,32) do @if "!url:~-1!" == " " set url=!url:~0,-1!
-  @echo !arch!^|!version!^|!threading!^|!exceptions!^|!revision!^|!url!>> "%repository_path%"
-)
-@del "%file_path%" 2>nul
-@endlocal & set "%var%=%repository_path%"
-@goto :eof
-
-:resolve - Gets the MinGW-builds repository
-:: %1 - The return variable for the MinGW slug
-:: %2 - The return variable for the MinGW URL
-:: %3 - The repository information to use
-:: %4 - Target architecture
-:: %5 - Version of MinGW to get [optional]
-:: %6 - Threading model [optional]
-:: %7 - Exception model [optional]
-:: %8 - Package revision [optional]
-@setlocal
-@set "slug_var=%~1"
-@if "%slug_var%" == "" @exit /b 1
-@set "url_var=%~2"
-@if "%url_var%" == "" @exit /b 1
-@set "repository=%~3"
-@if "%repository%" == "" @exit /b 1
-@set "arch=%~4"
-@if "%arch%" == "" @exit /b 1
-@call :resolve_version version "%repository%" "%arch%" "%~5"
-@if errorlevel 1 @exit /b 1
-@call :resolve_threading threading "%repository%" "%arch%" "%version%" "%~6"
-@if errorlevel 1 @exit /b 1
-@call :resolve_exceptions exceptions "%repository%" "%arch%" "%version%" "%threading%" "%~7"
-@if errorlevel 1 @exit /b 1
-@call :resolve_revision revision "%repository%" "%arch%" "%version%" "%threading%" "%exceptions%" "%~8"
-@if errorlevel 1 @exit /b 1
-@call :log 3 "Finding URL"
-@for /f "delims=| tokens=1-6" %%a in (%repository%) do @(
-  @if "%arch%" == "%%a" (
-    @if "%version%" == "%%b" (
-      @if "%threading%" == "%%c" (
-        @if "%exceptions%" == "%%d" (
-          @if "%revision%" == "%%e" (
-            @set "url=%%f"
-) ) ) ) ) )
-@if "%url%" == "" (
-  @call :log 0 "Failed to resolve URL"
-  @exit /b 1
-)
-@set slug=gcc-%version%-%arch%-%threading%-%exceptions%-rev%revision%
-@call :log 2 "Resolved slug: %slug%"
-@call :log 2 "Resolved url: %url%"
-@endlocal & set "%slug_var%=%slug%" & set "%url_var%=%url%"
-@goto :eof
-
-:unpack - Unpacks the MinGW archive
-:: %1 - The return variable name for the compiler path
-:: %2 - The filepath or URL of the archive
-:: %3 - The folder to unpack to
-@verify other 2>nul
-@setlocal EnableDelayedExpansion
-@if errorlevel 1 (
-  @call :log 0 "Failed to enable extensions"
-  @exit /b 1
-)
-@set "var=%~1"
-@if "%var%" == "" @exit /b 1
-@set "archive_path=%~2"
-@if "%archive_path%" == "" @exit /b 1
-@set "folder_path=%~3"
-@if "%folder_path%" == "" @exit /b 1
-@set "compiler_path=%folder_path%\bin"
-@if exist "%compiler_path%" goto :unpack_done
-@call :log 7
-@call :log 2 "Unpacking MinGW archive"
-@set "http=%archive_path:~0,4%"
-@if "%http%" == "http" (
-  @set "url=%archive_path%"
-  @for /f %%a in ("!url: =-!") do @set "file_name=%%~na"
-  @for /f %%a in ("!url: =-!") do @set "file_ext=%%~xa"
-  @set "archive_path=%dependency_path%\!file_name!!file_ext!"
-  @if not exist "!archive_path!" (
-    @call :log 6
-    @call :log 1 "Downloading MinGW archive"
-    @call :download "!url!" "!archive_path!"
-    @if errorlevel 1 (
-      @del "!archive_path!" 2>nul
-      @call :log 0 "Failed to download: !file_name!!file_ext!"
-      @exit /b 1
-    )
-  )
-)
-@if not exist "%archive_path%" (
-  @call :log 0 "The archive did not exist to unpack: %archive_path%"
-  @exit /b 1
-)
-@for /f %%a in ("%archive_path: =-%") do @set "file_name=%%~na"
-@for /f %%a in ("%archive_path: =-%") do @set "file_ext=%%~xa"
-@call :log 6
-@call :log 1 "Unpacking MinGW %file_name%%file_ext%"
-@call :find_sevenzip sevenzip_executable
-@if errorlevel 1 (
-  @call :log 0 "Need 7zip to unpack the MinGW archive"
-  @exit /b 1
-)
-@call :iso8601 iso8601
-@for /f %%a in ("%folder_path%") do @set "tmp_path=%%~dpatmp-%iso8601%"
-@"%sevenzip_executable%" x -y "-o%tmp_path%" "%archive_path%" > nul
-@if errorlevel 1 (
-  @rmdir /s /q "%folder_path%"
-  @call :log 0 "Failed to unpack the MinGW archive"
-  @exit /b 1
-)
-@set "expected_path=%tmp_path%\mingw64"
-@if not exist "%expected_path%" (
-  @set "expected_path=%tmp_path%\mingw32"
-)
-@move /y "%expected_path%" "%folder_path%" > nul
-@if errorlevel 1 (
-  @rmdir /s /q "%tmp_path%" 2>nul
-  @call :log 0 "Failed to move MinGW folder"
-  @call :log 0 "%expected_path%"
-  @call :log 0 "%folder_path%"
-  @exit /b 1
-)
-@rmdir /s /q %tmp_path%
-@set "compiler_path=%folder_path%\bin"
-:unpack_done
-@if not exist "%compiler_path%\gcc.exe" (
-  @call :log 0 "Failed to find gcc: %compiler_path%"
-  @exit /b 1
-)
-@endlocal & set "%var%=%compiler_path%"
-@goto :eof
-
-:find_sevenzip - Finds (or downloads) the 7zip executable
-:: %1 - The return variable for the 7zip executable path
-@setlocal
-@set "var=%~1"
-@if "%var%" == "" @exit /b 1
-@call :log 2 "Finding 7zip"
-@call :find_in_path sevenzip_executable 7z.exe
-@if not errorlevel 1 goto :find_sevenzip_done
-@call :find_in_path sevenzip_executable 7za.exe
-@if not errorlevel 1 goto :find_sevenzip_done
-@set checksum=2FAC454A90AE96021F4FFC607D4C00F8
-@set "url=http://7-zip.org/a/7za920.zip"
-@for /f %%a in ("%url: =-%") do @set "file_name=%%~na"
-@for /f %%a in ("%url: =-%") do @set "file_ext=%%~xa"
-@set "archive_path=%dependency_path%\%file_name%%file_ext%"
-@if not exist "%archive_path%" (
-  @call :log 6
-  @call :log 1 "Downloading 7zip archive"
-  @call :download "%url%" "%archive_path%" %checksum%
-  @if errorlevel 1 (
-    @del "%archive_path%" 2>nul
-    @call :log 0 "Failed to download: %file_name%%file_ext%"
-    @exit /b 1
-  )
-)
-@set "sevenzip_path=%dependency_path%\sevenzip"
-@if not exist "%sevenzip_path%" (
-  @call :unzip "%archive_path%" "%sevenzip_path%"
-  @if errorlevel 1 (
-    @call :log 0 "Failed to unzip the7zip archive"
-    @exit /b 1
-  )
-)
-@set "sevenzip_executable=%sevenzip_path%\7za.exe"
-@if not exist "%sevenzip_executable%" (
-  @call :log 0 "Failed to find unpacked 7zip: %sevenzip_executable%"
-  @exit /b 1
-)
-:find_sevenzip_done
-@call :log 2 "Found 7zip: %sevenzip_executable%"
-@endlocal & set "%var%=%sevenzip_executable%"
-@goto :eof
-
-:unzip - Unzips a .zip archive
-:: %1 - The archive to unzip
-:: %2 - The location to unzip to
-@setlocal
-@set "archive_path=%~1"
-@if "%archive_path%" == "" @exit /b 1
-@set "folder_path=%~2"
-@if "%folder_path%" == "" @exit /b 1
-@for /f %%a in ("%archive_path: =-%") do @set "file_name=%%~na"
-@for /f %%a in ("%archive_path: =-%") do @set "file_ext=%%~xa"
-@call :log 2 "Unzipping: %file_name%%file_ext%"
-@call :iso8601 iso8601
-@set "log_path=%temp%\unzip-%iso8601%-%file_name%.log"
-@powershell ^
-  Add-Type -assembly "system.io.compression.filesystem"; ^
-  [io.compression.zipfile]::ExtractToDirectory(^
-    '%archive_path%', '%folder_path%') 2>"%log_path%"
-@if errorlevel 1 (
-  @call :log 0 "Failed to unzip: %file_name%%file_ext%"
-  @call :log_append "%log_path%"
-  @exit /b 1
-)
-@endlocal
-@goto :eof
-
-:resolve_version - Gets the version of the MinGW compiler
-:: %1 - The return variable for the version
-:: %2 - The repository information to use
-:: %3 - The architecture of the compiler
-:: %4 - Version of MinGW to get [optional]
-@verify other 2>nul
-@setlocal EnableDelayedExpansion
-@if errorlevel 1 (
-  @call :log 0 "Failed to enable extensions"
-  @exit /b 1
-)
-@set "var=%~1"
-@if "%var%" == "" @exit /b 1
-@set "repository=%~2"
-@if "%repository%" == "" @exit /b 1
-@set "arch=%~3"
-@if "%arch%" == "" @exit /b 1
-@set "version=%~4"
-@if not "%version%" == "" goto :resolve_version_done
-:: Find the latest version
-@call :log 3 "Finding latest version"
-@set version=0.0.0
-@for /f "delims=| tokens=1-6" %%a in (%repository%) do @(
-  @if "%arch%" == "%%a" (
-    @call :version_compare result "%version%" "%%b"
-    @if errorlevel 1 (
-      @call :log 0 "Failed to compare versions: %version% %%a"
-      @exit /b 1
-    )
-    @if !result! lss 0 set version=%%b
-  )
-)
-:resolve_version_done
-@if "%version%" == "" (
-  @call :log 0 "Failed to resolve latest version number"
-  @exit /b 1
-)
-@call :log 2 "Resolved version: %version%"
-@endlocal & set "%var%=%version%"
-@goto :eof
-
-:resolve_threading - Gets the threading model of the MinGW compiler
-:: %1 - The return variable for the threading model
-:: %2 - The repository information to use
-:: %3 - The architecture of the compiler
-:: %4 - The version of the compiler
-:: %5 - threading model of MinGW to use [optional]
-@verify other 2>nul
-@setlocal EnableDelayedExpansion
-@if errorlevel 1 (
-  @call :log 0 "Failed to enable extensions"
-  @exit /b 1
-)
-@set "var=%~1"
-@if "%var%" == "" @exit /b 1
-@set "repository=%~2"
-@if "%repository%" == "" @exit /b 1
-@set "arch=%~3"
-@if "%arch%" == "" @exit /b 1
-@set "version=%~4"
-@if "%version%" == "" @exit /b 1
-@set "threading=%~5"
-@if not "%threading%" == "" goto :resolve_threading_done
-@call :log 3 "Finding best threading model"
-@for /f "delims=| tokens=1-6" %%a in (%repository%) do @(
-  @if "%arch%" == "%%a" (
-    @if "%version%" == "%%b" (
-      @if not defined threading (
-        @set "threading=%%c"
-      )
-      @if "%%c" == "posix" (
-        @set "threading=%%c"
-) ) ) )
-:resolve_threading_done
-@if "%threading%" == "" (
-  @call :log 0 "Failed to resolve the best threading model"
-  @exit /b 1
-)
-@call :log 2 "Resolved threading model: %threading%"
-@endlocal & set "%var%=%threading%"
-@goto :eof
-
-:resolve_exceptions - Gets the exception model of the MinGW compiler
-:: %1 - The return variable for the exception model
-:: %2 - The repository information to use
-:: %3 - The architecture of the compiler
-:: %4 - The version of the compiler
-:: %4 - The threading model of the compiler
-:: %5 - exception model of MinGW to use [optional]
-@verify other 2>nul
-@setlocal EnableDelayedExpansion
-@if errorlevel 1 (
-  @call :log 0 "Failed to enable extensions"
-  @exit /b 1
-)
-@set "var=%~1"
-@if "%var%" == "" @exit /b 1
-@set "repository=%~2"
-@if "%repository%" == "" @exit /b 1
-@set "arch=%~3"
-@if "%arch%" == "" @exit /b 1
-@set "version=%~4"
-@if "%version%" == "" @exit /b 1
-@set "threading=%~5"
-@if "%threading%" == "" @exit /b 1
-@set "exceptions=%~6"
-@if not "%exceptions%" == "" goto :resolve_exceptions_done
-@call :log 3 "Finding best exception model"
-@for /f "delims=| tokens=1-6" %%a in (%repository%) do @(
-  @if "%arch%" == "%%a" (
-    @if "%version%" == "%%b" (
-      @if "%threading%" == "%%c" (
-        @if not defined exceptions (
-          @set "exceptions=%%d"
-        )
-        @if "%%d" == "dwarf" (
-          @set "exceptions=%%d"
-        )
-        @if "%%d" == "seh" (
-          @set "exceptions=%%d"
-) ) ) ) )
-:resolve_exceptions_done
-@if "%exceptions%" == "" (
-  @call :log 0 "Failed to resolve the best exception model"
-  @exit /b 1
-)
-@call :log 2 "Resolved exception model: %exceptions%"
-@endlocal & set "%var%=%exceptions%"
-@goto :eof
-
-:resolve_revision - Gets the revision of the MinGW compiler
-:: %1 - The return variable for the revision
-:: %2 - The repository information to use
-:: %3 - The architecture of the compiler
-:: %4 - The version of the compiler
-:: %4 - The threading model of the compiler
-:: %4 - The exception model of the compiler
-:: %5 - revision of the MinGW package to use [optional]
-@verify other 2>nul
-@setlocal EnableDelayedExpansion
-@if errorlevel 1 (
-  @call :log 0 "Failed to enable extensions"
-  @exit /b 1
-)
-@set "var=%~1"
-@if "%var%" == "" @exit /b 1
-@set "repository=%~2"
-@if "%repository%" == "" @exit /b 1
-@set "arch=%~3"
-@if "%arch%" == "" @exit /b 1
-@set "version=%~4"
-@if "%version%" == "" @exit /b 1
-@set "threading=%~5"
-@if "%threading%" == "" @exit /b 1
-@set "exceptions=%~6"
-@if "%exceptions%" == "" @exit /b 1
-@set "revision=%~7"
-@if not "%revision%" == "" goto :resolve_revision_done
-@call :log 3 "Finding latest revision"
-@for /f "delims=| tokens=1-6" %%a in (%repository%) do @(
-  @if "%arch%" == "%%a" (
-    @if "%version%" == "%%b" (
-      @if "%threading%" == "%%c" (
-        @if "%exceptions%" == "%%d" (
-          @if "%%e" gtr "%revision%" (
-            @set "revision=%%e"
-) ) ) ) ) )
-:resolve_revision_done
-@if "%revision%" == "" (
-  @call :log 0 "Failed to resolve latest revision"
-  @exit /b 1
-)
-@call :log 2 "Resolved revision: %revision%"
-@endlocal & set "%var%=%revision%"
-@goto :eof
-
-:version_compare - Compares two semantic version numbers
-:: %1 - The return variable:
-::        - < 0 : if %2 < %3
-::        -   0 : if %2 == %3
-::        - > 0 : if %2 > %3
-:: %2 - The first version to compare
-:: %3 - The second version to compare
-@setlocal
-@set "var=%~1"
-@if "%var%" == "" @exit /b 1
-@set "lhs=%~2"
-@if "%lhs%" == "" @exit /b 1
-@set "rhs=%~3"
-@if "%lhs%" == "" @exit /b 1
-@set result=0
-@for /f "delims=. tokens=1-6" %%a in ("%lhs%.%rhs%") do @(
-  @if %%a lss %%d (
-    set result=-1
-    goto :version_compare_done
-  ) else (
-    @if %%a gtr %%d (
-      set result=1
-      goto :version_compare_done
-    ) else (
-      @if %%b lss %%e (
-        set result=-1
-        goto :version_compare_done
-      ) else (
-        @if %%b gtr %%e (
-          set result=1
-          goto :version_compare_done
-        ) else (
-          @if %%c lss %%f (
-            set result=-1
-            goto :version_compare_done
-          ) else (
-            @if %%c gtr %%f (
-              set result=1
-              goto :version_compare_done
-            )
-          )
-        )
-      )
-    )
-  )
-)
-:version_compare_done
-@endlocal & set "%var%=%result%"
-@goto :eof
-
-:print_usage - Prints the usage of the script
-:: %* - message to print, each argument on it's own line
-@setlocal
-@for %%a in (%*) do @echo.%%~a
-@echo.
-@echo.build [/?][/v[v...]^|/q][/version][/arch a][/threading t]
-@echo.      [/exceptions e][/revision r] location
-@echo.
-@echo.  /version v  The version of MinGW to download
-@echo.  /arch a     The target architecture [i686^|x86_64]
-@echo.  /threading t
-@echo.              Threading model to use [posix^|win32]
-@echo.  /exceptions e
-@echo.              Exception model to use [sjlj^|seh^|dwarf]
-@echo.  /revision e Revision of the release to use
-@echo.  /v          Sets the output to be more verbose
-@echo.  /v[v...]    Extra verbosity, /vv, /vvv, etc
-@echo.  /q          Quiets the output
-@echo.  /?          Shows this usage message
-@echo.
-@endlocal
-@goto :eof
-
-:script_source - Determines if the script was ran from the cli or explorer
-:: %1 - The return variable [cli|explorer]
-@verify other 2>nul
-@setlocal EnableDelayedExpansion
-@if errorlevel 1 (
-  @call :log 0 "Failed to enable extensions"
-  @exit /b 1
-)
-@call :log 3 "Attempting to detect the script source"
-@echo "The invocation command was: '%cmdcmdline%'" >> %log_path%
-@for /f "tokens=1-3,*" %%a in ("%cmdcmdline%") do @(
-  set cmd=%%~a
-  set arg1=%%~b
-  set arg2=%%~c
-  set rest=%%~d
-)
-@set quote="
-@if "!arg2:~0,1!" equ "!quote!" (
-  if "!arg2:~-1!" neq "!quote!" (
-    set "arg2=!arg2:~1!"
-  )
-)
-@call :log 4 "cmd  = %cmd%"
-@call :log 4 "arg1 = %arg1%"
-@call :log 4 "arg2 = %arg2%"
-@call :log 4 "rest = %rest%"
-@call :log 4 "src  = %~f0"
-@if /i "%arg2%" == "call" (
-  set script_source=cli
-) else (
-  @if /i "%arg1%" == "/c" (
-    set script_source=explorer
-  ) else (
-    set script_source=cli
-  )
-)
-@call :log 3 "The script was invoked from %script_source%"
-@endlocal & set "%~1=%script_source%"
-@goto :eof
-
-:architecture - Finds the system architecture
-:: %1 - The return variable [i686|x86_64]
-@setlocal
-@call :log 3 "Determining the processor architecture"
-@set "key=HKLM\System\CurrentControlSet\Control\Session Manager\Environment"
-@set "var=PROCESSOR_ARCHITECTURE"
-@for /f "skip=2 tokens=2,*" %%a in ('reg query "%key%" /v "%var%"') do @set "arch=%%b"
-@if "%arch%" == "AMD64" set arch=x86_64
-@if "%arch%" == "x64" set arch=i686
-@call :log 4 "arch = %arch%"
-@endlocal & set "%~1=%arch%"
-@goto :eof
-
-:md5 - Gets the MD5 checksum for a file
-:: %1 - The hash
-:: %2 - The file path
-@setlocal
-@set "var=%~1"
-@set "file_path=%~2"
-@if "%var%" == "" @exit /b 1
-@if "%file_path%" == "" @exit /b 1
-@if not exist "%file_path%" @exit /b 1
-@for /f "skip=3 tokens=1,*" %%a in ('powershell Get-FileHash -Algorithm MD5 "'%file_path%'"') do @set hash=%%b
-@if not defined hash (
-  @call :log 6
-  @call :log 0 "Failed to get MD5 hash for %file_path%"
-  @exit /b 1
-)
-@endlocal & set "%var%=%hash: =%"
-@goto :eof
-
-:windows_version - Checks the windows version
-:: %1 - The windows version
-:: %2 - The major version number return variable
-:: %3 - The minor version number return variable
-:: %4 - The revision version number return variable
-@setlocal
-@call :log 3 "Retrieving the Windows version"
-@for /f "tokens=2 delims=[]" %%x in ('ver') do @set win_ver=%%x
-@set win_ver=%win_ver:Version =%
-@set win_ver_major=%win_ver:~0,1%
-@set win_ver_minor=%win_ver:~2,1%
-@set win_ver_rev=%win_ver:~4%
-@call :log 4 "win_ver = %win_ver%"
-@endlocal & set "%~1=%win_ver%" ^
-          & set "%~2=%win_ver_major%" ^
-          & set "%~3=%win_ver_minor%" ^
-          & set "%~4=%win_ver_rev%"
-@goto :eof
-
-:find_in_path - Finds a program of file in the PATH
-:: %1 - return variable of the file path
-@setlocal
-@set "var=%~1"
-@if "%var%" == "" @exit /b 1
-@set "file=%~2"
-@if "%file%" == "" @exit /b 1
-@call :log 3 "Searching PATH for %file%"
-@for %%x in ("%file%") do @set "file_path=%%~f$PATH:x"
-@if not defined file_path @exit /b 1
-@endlocal & set "%var%=%file_path%"
-@goto :eof
-
-:log_append - Appends another file into the current logging file
-:: %1 - the file_path to the file to concatenate
-@setlocal
-@set "file_path=%~1"
-@if "%file_path%" == "" @exit /b 1
-@call :log 3 "Appending to log: %file_path%"
-@call :iso8601 iso8601
-@set temp_log=%temp%\append-%iso8601%.log
-@call :log 4 "Using temp file %temp_log%"
-@type "%log_path%" "%file_path%" > "%temp_log%" 2>nul
-@move /y "%temp_log%" "%log_path%" 1>nul
-@del "%file_path% 2>nul
-@del "%temp_log% 2>nul
-@endlocal
-@goto :eof
-
-:iso8601 - Returns the current time in ISO8601 format
-:: %1 - the return variable
-:: %2 - format [extended|basic*]
-:: iso8601 - contains the resulting timestamp
-@setlocal
-@wmic Alias /? >NUL 2>&1 || @exit /b 1
-@set "var=%~1"
-@if "%var%" == "" @exit /b 1
-@set "format=%~2"
-@if "%format%" == "" set format=basic
-@for /F "skip=1 tokens=1-6" %%g IN ('wmic Path Win32_UTCTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') do @(
-  @if "%%~l"=="" goto :iso8601_done
-  @set "yyyy=%%l"
-  @set "mm=00%%j"
-  @set "dd=00%%g"
-  @set "hour=00%%h"
-  @set "minute=00%%i"
-  @set "seconds=00%%k"
-)
-:iso8601_done
-@set mm=%mm:~-2%
-@set dd=%dd:~-2%
-@set hour=%hour:~-2%
-@set minute=%minute:~-2%
-@set seconds=%seconds:~-2%
-@if /i [%format%] == [extended] (
-  set iso8601=%yyyy%-%mm%-%dd%T%hour%:%minute%:%seconds%Z
-) else (
-  if /i [%format%] == [basic] (
-    set iso8601=%yyyy%%mm%%dd%T%hour%%minute%%seconds%Z
-  ) else (
-    @exit /b 1
-  )
-)
-@set iso8601=%iso8601: =0%
-@endlocal & set %var%=%iso8601%
-@goto :eof
-
-:verbosity - Processes the verbosity parameter '/v[v...]
-:: %1 - verbosity given on the command line
-:: logging_level - set to the number of v's
-@setlocal
-@set logging_level=0
-@set verbosity=%~1
-:verbosity_loop
-@set verbosity=%verbosity:~1%
-@if not [%verbosity%] == [] @(
-  set /a "logging_level=logging_level+1"
-  goto verbosity_loop
-)
-@endlocal & set logging_level=%logging_level%
-@goto :eof
-
-:log - Logs a message, depending on verbosity
-:: %1 - level
-::       [0-4] for CLI logging
-::       [5-9] for GUI logging
-:: %2 - message to print
-@setlocal
-@set "level=%~1"
-@set "msg=%~2"
-@if "%log_folder%" == "" (
-  echo Logging was used to early in the script, log_folder isn't set yet
-  goto :eof
-)
-@if "%log_path%" == "" (
-  echo Logging was used to early in the script, log_path isn't set yet
-  goto :eof
-)
-@if not exist "%log_folder%" mkdir "%log_folder%"
-@if not exist "%log_path%" echo. 1>nul 2>"%log_path%"
-@echo.%msg% >> "%log_path%"
-@if %level% geq 5 (
-  @if [%script_source%] == [explorer] (
-    set /a "level=level-5"
-  ) else (
-    @goto :eof
-  )
-)
-@if "%logging_level%" == "" (
-  echo Logging was used to early in the script, logging_level isn't set yet
-  goto :eof
-)
-@if %logging_level% geq %level% echo.%msg% 1>&2
-@endlocal
-@goto :eof
-
-:download - Downloads a file from the internet
-:: %1 - the url of the file to download
-:: %2 - the file to download to
-:: %3 - the MD5 checksum of the file (optional)
-@setlocal EnableDelayedExpansion
-@if errorlevel 1 (
-  @call :print_usage "Failed to enable extensions"
-  @exit /b 1
-)
-@set "url=%~1"
-@set "file_path=%~2"
-@set "checksum=%~3"
-@for %%a in (%file_path%) do @set dir_path=%%~dpa
-@for %%a in (%file_path%) do @set file_name=%%~nxa
-@if "%url%" == "" @exit /b 1
-@if "%file_path%" == "" @exit /b 1
-@if "%dir_path%" == "" @exit /b 1
-@if "%file_name%" == "" @exit /b 1
-@if not exist "%dir_path%" mkdir "%dir_path%"
-@call :log 2 "Downloading %url%"
-@call :iso8601 iso8601
-@set "temp_path=%temp%\download-%iso8601%-%file_name%"
-@set "log_path=%temp%\download-%iso8601%-log-%file_name%"
-@call :log 4 "Using temp file %temp_path%"
-@powershell Invoke-WebRequest "'%url%'" ^
-  -OutFile "'%temp_path%'" ^
-  -UserAgent [Microsoft.PowerShell.Commands.PSUserAgent]::IE ^
-  1>nul 2>"%log_path%"
-@if errorlevel 1 (
-  @call :log 0 "Failed to download %url%"
-  @call :log_append "%log_path%"
-  @exit /b 1
-)
-@if [%checksum%] neq [] (
-  @call :log 4 "Checking %checksum% against %temp_path%"
-  @call :md5 hash "%temp_path%"
-  if "!hash!" neq "%checksum%" (
-    @call :log 0 "Failed to match checksum: %temp_path%"
-    @call :log 0 "Hash    : !hash!"
-    @call :log 0 "Checksum: %checksum%"
-    @exit /b 1
-  ) else (
-    @call :log 3 "Checksum matched: %temp_path%"
-    @call :log 3 "Hash    : !hash!"
-    @call :log 3 "Checksum: %checksum%"
-  )
-)
-@call :log 4 "Renaming %temp_path% to %file_path%"
-@move /y "%temp_path%" "%file_path%" 1>nul
-@endlocal
-@goto :eof
+:: Make sure the extensions are enabled\r
+@verify other 2>nul\r
+@setlocal EnableExtensions EnableDelayedExpansion\r
+@if errorlevel 1 (\r
+  @call :print_usage "Failed to enable extensions"\r
+  @exit /b 1\r
+)\r
+\r
+::Change the code page to unicode\r
+@chcp 65001 1>nul 2>nul\r
+@if errorlevel 1 (\r
+  @call :print_usage "Failed to change the code page to unicode"\r
+  @exit /b 1\r
+)\r
+\r
+:: Set up some global variables\r
+@set "script_name=%~nx0"\r
+@set "script_folder=%~dp0"\r
+@set "script_folder=%script_folder:~0,-1%"\r
+@set "dependency_path=%TEMP%\mingw-build-dependencies"\r
+\r
+:: Check the command line parameters\r
+@set logging_level=1\r
+:options_loop\r
+@if [%1] == [] goto :options_parsed\r
+@set "arg=%~1"\r
+@set one=%arg:~0,1%\r
+@set two=%arg:~0,2%\r
+@set three=%arg:~0,3%\r
+@if /i [%arg%] == [/?] (\r
+  @call :print_usage "Downloads a specific version of MinGW"\r
+  @exit /b 0\r
+)\r
+@if /i [%arg%] == [/q] set quiet=true\r
+@if /i [%two%] == [/v] @if /i not [%three%] == [/ve] @call :verbosity "!arg!"\r
+@if /i [%arg%] == [/version] set "version=%~2" & shift\r
+@if /i [%arg%] == [/arch] set "arch=%~2" & shift\r
+@if /i [%arg%] == [/exceptions] set "exceptions=%~2" & shift\r
+@if /i [%arg%] == [/threading] set "threading=%~2" & shift\r
+@if /i [%arg%] == [/revision] set "revision=%~2" & shift\r
+@if /i not [!one!] == [/] (\r
+  if not defined output_path (\r
+    set output_path=!arg!\r
+  ) else (\r
+    @call :print_usage "Too many output locations: !output_path! !arg!" ^\r
+                       "There should only be one output location"\r
+    @exit /b 1\r
+  )\r
+)\r
+@shift\r
+@goto :options_loop\r
+:options_parsed\r
+@if defined quiet set logging_level=0\r
+@if not defined output_path set "output_path=%script_folder%\mingw-builds"\r
+@set "output_path=%output_path:/=\%"\r
+\r
+:: Set up the logging\r
+@set "log_folder=%output_path%\logs"\r
+@call :iso8601 timestamp\r
+@set "log_path=%log_folder%\%timestamp%.log"\r
+@set log_keep=10\r
+\r
+:: Get default architecture\r
+@if not defined arch @call :architecture arch\r
+\r
+:: Only keep a certain amount of logs\r
+@set /a "log_keep=log_keep-1"\r
+@if not exist %log_folder% @mkdir %log_folder%\r
+@for /f "skip=%log_keep%" %%f in ('dir /b /o-D /tc %log_folder%') do @(\r
+  @call :log 4 "Removing old log file %log_folder%\%%f"\r
+  del %log_folder%\%%f\r
+)\r
+\r
+:: Set up some more global variables\r
+@call :windows_version win_ver win_ver_major win_ver_minor win_ver_rev\r
+@call :script_source script_source\r
+@if [%script_source%] == [explorer] (\r
+  set /a "logging_level=logging_level+1"\r
+)\r
+\r
+:: Execute the main function\r
+@call :main "%arch%" "%version%" "%threading%" "%exceptions%" "%revision%"\r
+@if errorlevel 1 (\r
+  @call :log 0 "Failed to download MinGW"\r
+  @call :log 0 "View the log at %log_path%"\r
+  @exit /b 1\r
+)\r
+\r
+:: Stop the script if the user double clicked\r
+@if [%script_source%] == [explorer] (\r
+  pause\r
+)\r
+\r
+@endlocal\r
+@goto :eof\r
+\r
+:: -------------------------- Functions start here ----------------------------\r
+\r
+:main - Main function that performs the download\r
+:: %1 - Target architecture\r
+:: %2 - Version of MinGW to get [optional]\r
+:: %3 - Threading model [optional]\r
+:: %4 - Exception model [optional]\r
+:: %5 - Package revision [optional]\r
+@setlocal\r
+@call :log 6\r
+@call :log 2 "Welcome to the MinGW download script"\r
+@call :log 6 "------------------------------------"\r
+@call :log 6\r
+@call :log 2 "This script downloads a specific version of MinGW"\r
+@set "arch=%~1"\r
+@if "%arch%" == "" @exit /b 1\r
+@set "version=%~2"\r
+@set "threading=%~3"\r
+@set "exceptions=%~4"\r
+@set "revision=%~5"\r
+@call :log 3 "arch       = %arch%"\r
+@call :log 3 "version    = %version%"\r
+@call :log 3 "exceptions = %exceptions%"\r
+@call :log 3 "threading  = %threading%"\r
+@call :log 3 "revision   = %revision%"\r
+@call :repository repo\r
+@if errorlevel 1 (\r
+  @call :log 0 "Failed to get the MinGW-builds repository information"\r
+  @exit /b 1\r
+)\r
+@call :resolve slug url "%repo%" "%arch%" "%version%" "%threading%" "%exceptions%" "%revision%"\r
+@if errorlevel 1 (\r
+  @call :log 0 "Failed to resolve the correct URL of MinGW"\r
+  @exit /b 1\r
+)\r
+@call :unpack compiler_path "%url%" "%output_path%\mingw\%slug%"\r
+@if errorlevel 1 (\r
+  @call :log 0 "Failed to unpack the MinGW archive"\r
+  @exit /b 1\r
+)\r
+@rmdir /s /q "%dependency_path%"\r
+@echo.%compiler_path%\r
+@endlocal\r
+@goto :eof\r
+\r
+:repository - Gets the MinGW-builds repository\r
+:: %1 - The return variable for the repository file path\r
+@verify other 2>nul\r
+@setlocal EnableDelayedExpansion\r
+@if errorlevel 1 (\r
+  @call :log 0 "Failed to enable extensions"\r
+  @exit /b 1\r
+)\r
+@set "var=%~1"\r
+@if "%var%" == "" @exit /b 1\r
+@call :log 7\r
+@call :log 2 "Getting MinGW repository information"\r
+@set "url=http://downloads.sourceforge.net/project/mingw-w64/Toolchains targetting Win32/Personal Builds/mingw-builds/installer/repository.txt"\r
+@call :log 6\r
+@call :log 1 "Downloading MinGW repository"\r
+@set "file_path=%dependency_path%\mingw-repository.txt"\r
+@call :download "%url%" "%file_path%"\r
+@if errorlevel 1 (\r
+  @call :log 0 "Failed to download the MinGW repository information"\r
+  @exit /b 1\r
+)\r
+@set "repository_path=%dependency_path%\repository.txt"\r
+@del "%repository_path%" 2>nul\r
+@for /f "delims=| tokens=1-6,*" %%a in (%file_path%) do @(\r
+  @set "version=%%~a"\r
+  @set "version=!version: =!"\r
+  @set "arch=%%~b"\r
+  @set "arch=!arch: =!"\r
+  @set "threading=%%~c"\r
+  @set "threading=!threading: =!"\r
+  @set "exceptions=%%~d"\r
+  @set "exceptions=!exceptions: =!"\r
+  @set "revision=%%~e"\r
+  @set "revision=!revision: =!"\r
+  @set "revision=!revision:rev=!"\r
+  @set "url=%%~f"\r
+  @set "url=!url:%%20= !"\r
+  @for /l %%a in (1,1,32) do @if "!url:~-1!" == " " set url=!url:~0,-1!\r
+  @echo !arch!^|!version!^|!threading!^|!exceptions!^|!revision!^|!url!>> "%repository_path%"\r
+)\r
+@del "%file_path%" 2>nul\r
+@endlocal & set "%var%=%repository_path%"\r
+@goto :eof\r
+\r
+:resolve - Gets the MinGW-builds repository\r
+:: %1 - The return variable for the MinGW slug\r
+:: %2 - The return variable for the MinGW URL\r
+:: %3 - The repository information to use\r
+:: %4 - Target architecture\r
+:: %5 - Version of MinGW to get [optional]\r
+:: %6 - Threading model [optional]\r
+:: %7 - Exception model [optional]\r
+:: %8 - Package revision [optional]\r
+@setlocal\r
+@set "slug_var=%~1"\r
+@if "%slug_var%" == "" @exit /b 1\r
+@set "url_var=%~2"\r
+@if "%url_var%" == "" @exit /b 1\r
+@set "repository=%~3"\r
+@if "%repository%" == "" @exit /b 1\r
+@set "arch=%~4"\r
+@if "%arch%" == "" @exit /b 1\r
+@call :resolve_version version "%repository%" "%arch%" "%~5"\r
+@if errorlevel 1 @exit /b 1\r
+@call :resolve_threading threading "%repository%" "%arch%" "%version%" "%~6"\r
+@if errorlevel 1 @exit /b 1\r
+@call :resolve_exceptions exceptions "%repository%" "%arch%" "%version%" "%threading%" "%~7"\r
+@if errorlevel 1 @exit /b 1\r
+@call :resolve_revision revision "%repository%" "%arch%" "%version%" "%threading%" "%exceptions%" "%~8"\r
+@if errorlevel 1 @exit /b 1\r
+@call :log 3 "Finding URL"\r
+@for /f "delims=| tokens=1-6" %%a in (%repository%) do @(\r
+  @if "%arch%" == "%%a" (\r
+    @if "%version%" == "%%b" (\r
+      @if "%threading%" == "%%c" (\r
+        @if "%exceptions%" == "%%d" (\r
+          @if "%revision%" == "%%e" (\r
+            @set "url=%%f"\r
+) ) ) ) ) )\r
+@if "%url%" == "" (\r
+  @call :log 0 "Failed to resolve URL"\r
+  @exit /b 1\r
+)\r
+@set slug=gcc-%version%-%arch%-%threading%-%exceptions%-rev%revision%\r
+@call :log 2 "Resolved slug: %slug%"\r
+@call :log 2 "Resolved url: %url%"\r
+@endlocal & set "%slug_var%=%slug%" & set "%url_var%=%url%"\r
+@goto :eof\r
+\r
+:unpack - Unpacks the MinGW archive\r
+:: %1 - The return variable name for the compiler path\r
+:: %2 - The filepath or URL of the archive\r
+:: %3 - The folder to unpack to\r
+@verify other 2>nul\r
+@setlocal EnableDelayedExpansion\r
+@if errorlevel 1 (\r
+  @call :log 0 "Failed to enable extensions"\r
+  @exit /b 1\r
+)\r
+@set "var=%~1"\r
+@if "%var%" == "" @exit /b 1\r
+@set "archive_path=%~2"\r
+@if "%archive_path%" == "" @exit /b 1\r
+@set "folder_path=%~3"\r
+@if "%folder_path%" == "" @exit /b 1\r
+@set "compiler_path=%folder_path%\bin"\r
+@if exist "%compiler_path%" goto :unpack_done\r
+@call :log 7\r
+@call :log 2 "Unpacking MinGW archive"\r
+@set "http=%archive_path:~0,4%"\r
+@if "%http%" == "http" (\r
+  @set "url=%archive_path%"\r
+  @for /f %%a in ("!url: =-!") do @set "file_name=%%~na"\r
+  @for /f %%a in ("!url: =-!") do @set "file_ext=%%~xa"\r
+  @set "archive_path=%dependency_path%\!file_name!!file_ext!"\r
+  @if not exist "!archive_path!" (\r
+    @call :log 6\r
+    @call :log 1 "Downloading MinGW archive"\r
+    @call :download "!url!" "!archive_path!"\r
+    @if errorlevel 1 (\r
+      @del "!archive_path!" 2>nul\r
+      @call :log 0 "Failed to download: !file_name!!file_ext!"\r
+      @exit /b 1\r
+    )\r
+  )\r
+)\r
+@if not exist "%archive_path%" (\r
+  @call :log 0 "The archive did not exist to unpack: %archive_path%"\r
+  @exit /b 1\r
+)\r
+@for /f %%a in ("%archive_path: =-%") do @set "file_name=%%~na"\r
+@for /f %%a in ("%archive_path: =-%") do @set "file_ext=%%~xa"\r
+@call :log 6\r
+@call :log 1 "Unpacking MinGW %file_name%%file_ext%"\r
+@call :find_sevenzip sevenzip_executable\r
+@if errorlevel 1 (\r
+  @call :log 0 "Need 7zip to unpack the MinGW archive"\r
+  @exit /b 1\r
+)\r
+@call :iso8601 iso8601\r
+@for /f %%a in ("%folder_path%") do @set "tmp_path=%%~dpatmp-%iso8601%"\r
+@"%sevenzip_executable%" x -y "-o%tmp_path%" "%archive_path%" > nul\r
+@if errorlevel 1 (\r
+  @rmdir /s /q "%folder_path%"\r
+  @call :log 0 "Failed to unpack the MinGW archive"\r
+  @exit /b 1\r
+)\r
+@set "expected_path=%tmp_path%\mingw64"\r
+@if not exist "%expected_path%" (\r
+  @set "expected_path=%tmp_path%\mingw32"\r
+)\r
+@move /y "%expected_path%" "%folder_path%" > nul\r
+@if errorlevel 1 (\r
+  @rmdir /s /q "%tmp_path%" 2>nul\r
+  @call :log 0 "Failed to move MinGW folder"\r
+  @call :log 0 "%expected_path%"\r
+  @call :log 0 "%folder_path%"\r
+  @exit /b 1\r
+)\r
+@rmdir /s /q %tmp_path%\r
+@set "compiler_path=%folder_path%\bin"\r
+:unpack_done\r
+@if not exist "%compiler_path%\gcc.exe" (\r
+  @call :log 0 "Failed to find gcc: %compiler_path%"\r
+  @exit /b 1\r
+)\r
+@endlocal & set "%var%=%compiler_path%"\r
+@goto :eof\r
+\r
+:find_sevenzip - Finds (or downloads) the 7zip executable\r
+:: %1 - The return variable for the 7zip executable path\r
+@setlocal\r
+@set "var=%~1"\r
+@if "%var%" == "" @exit /b 1\r
+@call :log 2 "Finding 7zip"\r
+@call :find_in_path sevenzip_executable 7z.exe\r
+@if not errorlevel 1 goto :find_sevenzip_done\r
+@call :find_in_path sevenzip_executable 7za.exe\r
+@if not errorlevel 1 goto :find_sevenzip_done\r
+@set checksum=2FAC454A90AE96021F4FFC607D4C00F8\r
+@set "url=http://7-zip.org/a/7za920.zip"\r
+@for /f %%a in ("%url: =-%") do @set "file_name=%%~na"\r
+@for /f %%a in ("%url: =-%") do @set "file_ext=%%~xa"\r
+@set "archive_path=%dependency_path%\%file_name%%file_ext%"\r
+@if not exist "%archive_path%" (\r
+  @call :log 6\r
+  @call :log 1 "Downloading 7zip archive"\r
+  @call :download "%url%" "%archive_path%" %checksum%\r
+  @if errorlevel 1 (\r
+    @del "%archive_path%" 2>nul\r
+    @call :log 0 "Failed to download: %file_name%%file_ext%"\r
+    @exit /b 1\r
+  )\r
+)\r
+@set "sevenzip_path=%dependency_path%\sevenzip"\r
+@if not exist "%sevenzip_path%" (\r
+  @call :unzip "%archive_path%" "%sevenzip_path%"\r
+  @if errorlevel 1 (\r
+    @call :log 0 "Failed to unzip the7zip archive"\r
+    @exit /b 1\r
+  )\r
+)\r
+@set "sevenzip_executable=%sevenzip_path%\7za.exe"\r
+@if not exist "%sevenzip_executable%" (\r
+  @call :log 0 "Failed to find unpacked 7zip: %sevenzip_executable%"\r
+  @exit /b 1\r
+)\r
+:find_sevenzip_done\r
+@call :log 2 "Found 7zip: %sevenzip_executable%"\r
+@endlocal & set "%var%=%sevenzip_executable%"\r
+@goto :eof\r
+\r
+:unzip - Unzips a .zip archive\r
+:: %1 - The archive to unzip\r
+:: %2 - The location to unzip to\r
+@setlocal\r
+@set "archive_path=%~1"\r
+@if "%archive_path%" == "" @exit /b 1\r
+@set "folder_path=%~2"\r
+@if "%folder_path%" == "" @exit /b 1\r
+@for /f %%a in ("%archive_path: =-%") do @set "file_name=%%~na"\r
+@for /f %%a in ("%archive_path: =-%") do @set "file_ext=%%~xa"\r
+@call :log 2 "Unzipping: %file_name%%file_ext%"\r
+@call :iso8601 iso8601\r
+@set "log_path=%temp%\unzip-%iso8601%-%file_name%.log"\r
+@powershell ^\r
+  Add-Type -assembly "system.io.compression.filesystem"; ^\r
+  [io.compression.zipfile]::ExtractToDirectory(^\r
+    '%archive_path%', '%folder_path%') 2>"%log_path%"\r
+@if errorlevel 1 (\r
+  @call :log 0 "Failed to unzip: %file_name%%file_ext%"\r
+  @call :log_append "%log_path%"\r
+  @exit /b 1\r
+)\r
+@endlocal\r
+@goto :eof\r
+\r
+:resolve_version - Gets the version of the MinGW compiler\r
+:: %1 - The return variable for the version\r
+:: %2 - The repository information to use\r
+:: %3 - The architecture of the compiler\r
+:: %4 - Version of MinGW to get [optional]\r
+@verify other 2>nul\r
+@setlocal EnableDelayedExpansion\r
+@if errorlevel 1 (\r
+  @call :log 0 "Failed to enable extensions"\r
+  @exit /b 1\r
+)\r
+@set "var=%~1"\r
+@if "%var%" == "" @exit /b 1\r
+@set "repository=%~2"\r
+@if "%repository%" == "" @exit /b 1\r
+@set "arch=%~3"\r
+@if "%arch%" == "" @exit /b 1\r
+@set "version=%~4"\r
+@if not "%version%" == "" goto :resolve_version_done\r
+:: Find the latest version\r
+@call :log 3 "Finding latest version"\r
+@set version=0.0.0\r
+@for /f "delims=| tokens=1-6" %%a in (%repository%) do @(\r
+  @if "%arch%" == "%%a" (\r
+    @call :version_compare result "%version%" "%%b"\r
+    @if errorlevel 1 (\r
+      @call :log 0 "Failed to compare versions: %version% %%a"\r
+      @exit /b 1\r
+    )\r
+    @if !result! lss 0 set version=%%b\r
+  )\r
+)\r
+:resolve_version_done\r
+@if "%version%" == "" (\r
+  @call :log 0 "Failed to resolve latest version number"\r
+  @exit /b 1\r
+)\r
+@call :log 2 "Resolved version: %version%"\r
+@endlocal & set "%var%=%version%"\r
+@goto :eof\r
+\r
+:resolve_threading - Gets the threading model of the MinGW compiler\r
+:: %1 - The return variable for the threading model\r
+:: %2 - The repository information to use\r
+:: %3 - The architecture of the compiler\r
+:: %4 - The version of the compiler\r
+:: %5 - threading model of MinGW to use [optional]\r
+@verify other 2>nul\r
+@setlocal EnableDelayedExpansion\r
+@if errorlevel 1 (\r
+  @call :log 0 "Failed to enable extensions"\r
+  @exit /b 1\r
+)\r
+@set "var=%~1"\r
+@if "%var%" == "" @exit /b 1\r
+@set "repository=%~2"\r
+@if "%repository%" == "" @exit /b 1\r
+@set "arch=%~3"\r
+@if "%arch%" == "" @exit /b 1\r
+@set "version=%~4"\r
+@if "%version%" == "" @exit /b 1\r
+@set "threading=%~5"\r
+@if not "%threading%" == "" goto :resolve_threading_done\r
+@call :log 3 "Finding best threading model"\r
+@for /f "delims=| tokens=1-6" %%a in (%repository%) do @(\r
+  @if "%arch%" == "%%a" (\r
+    @if "%version%" == "%%b" (\r
+      @if not defined threading (\r
+        @set "threading=%%c"\r
+      )\r
+      @if "%%c" == "posix" (\r
+        @set "threading=%%c"\r
+) ) ) )\r
+:resolve_threading_done\r
+@if "%threading%" == "" (\r
+  @call :log 0 "Failed to resolve the best threading model"\r
+  @exit /b 1\r
+)\r
+@call :log 2 "Resolved threading model: %threading%"\r
+@endlocal & set "%var%=%threading%"\r
+@goto :eof\r
+\r
+:resolve_exceptions - Gets the exception model of the MinGW compiler\r
+:: %1 - The return variable for the exception model\r
+:: %2 - The repository information to use\r
+:: %3 - The architecture of the compiler\r
+:: %4 - The version of the compiler\r
+:: %4 - The threading model of the compiler\r
+:: %5 - exception model of MinGW to use [optional]\r
+@verify other 2>nul\r
+@setlocal EnableDelayedExpansion\r
+@if errorlevel 1 (\r
+  @call :log 0 "Failed to enable extensions"\r
+  @exit /b 1\r
+)\r
+@set "var=%~1"\r
+@if "%var%" == "" @exit /b 1\r
+@set "repository=%~2"\r
+@if "%repository%" == "" @exit /b 1\r
+@set "arch=%~3"\r
+@if "%arch%" == "" @exit /b 1\r
+@set "version=%~4"\r
+@if "%version%" == "" @exit /b 1\r
+@set "threading=%~5"\r
+@if "%threading%" == "" @exit /b 1\r
+@set "exceptions=%~6"\r
+@if not "%exceptions%" == "" goto :resolve_exceptions_done\r
+@call :log 3 "Finding best exception model"\r
+@for /f "delims=| tokens=1-6" %%a in (%repository%) do @(\r
+  @if "%arch%" == "%%a" (\r
+    @if "%version%" == "%%b" (\r
+      @if "%threading%" == "%%c" (\r
+        @if not defined exceptions (\r
+          @set "exceptions=%%d"\r
+        )\r
+        @if "%%d" == "dwarf" (\r
+          @set "exceptions=%%d"\r
+        )\r
+        @if "%%d" == "seh" (\r
+          @set "exceptions=%%d"\r
+) ) ) ) )\r
+:resolve_exceptions_done\r
+@if "%exceptions%" == "" (\r
+  @call :log 0 "Failed to resolve the best exception model"\r
+  @exit /b 1\r
+)\r
+@call :log 2 "Resolved exception model: %exceptions%"\r
+@endlocal & set "%var%=%exceptions%"\r
+@goto :eof\r
+\r
+:resolve_revision - Gets the revision of the MinGW compiler\r
+:: %1 - The return variable for the revision\r
+:: %2 - The repository information to use\r
+:: %3 - The architecture of the compiler\r
+:: %4 - The version of the compiler\r
+:: %4 - The threading model of the compiler\r
+:: %4 - The exception model of the compiler\r
+:: %5 - revision of the MinGW package to use [optional]\r
+@verify other 2>nul\r
+@setlocal EnableDelayedExpansion\r
+@if errorlevel 1 (\r
+  @call :log 0 "Failed to enable extensions"\r
+  @exit /b 1\r
+)\r
+@set "var=%~1"\r
+@if "%var%" == "" @exit /b 1\r
+@set "repository=%~2"\r
+@if "%repository%" == "" @exit /b 1\r
+@set "arch=%~3"\r
+@if "%arch%" == "" @exit /b 1\r
+@set "version=%~4"\r
+@if "%version%" == "" @exit /b 1\r
+@set "threading=%~5"\r
+@if "%threading%" == "" @exit /b 1\r
+@set "exceptions=%~6"\r
+@if "%exceptions%" == "" @exit /b 1\r
+@set "revision=%~7"\r
+@if not "%revision%" == "" goto :resolve_revision_done\r
+@call :log 3 "Finding latest revision"\r
+@for /f "delims=| tokens=1-6" %%a in (%repository%) do @(\r
+  @if "%arch%" == "%%a" (\r
+    @if "%version%" == "%%b" (\r
+      @if "%threading%" == "%%c" (\r
+        @if "%exceptions%" == "%%d" (\r
+          @if "%%e" gtr "%revision%" (\r
+            @set "revision=%%e"\r
+) ) ) ) ) )\r
+:resolve_revision_done\r
+@if "%revision%" == "" (\r
+  @call :log 0 "Failed to resolve latest revision"\r
+  @exit /b 1\r
+)\r
+@call :log 2 "Resolved revision: %revision%"\r
+@endlocal & set "%var%=%revision%"\r
+@goto :eof\r
+\r
+:version_compare - Compares two semantic version numbers\r
+:: %1 - The return variable:\r
+::        - < 0 : if %2 < %3\r
+::        -   0 : if %2 == %3\r
+::        - > 0 : if %2 > %3\r
+:: %2 - The first version to compare\r
+:: %3 - The second version to compare\r
+@setlocal\r
+@set "var=%~1"\r
+@if "%var%" == "" @exit /b 1\r
+@set "lhs=%~2"\r
+@if "%lhs%" == "" @exit /b 1\r
+@set "rhs=%~3"\r
+@if "%lhs%" == "" @exit /b 1\r
+@set result=0\r
+@for /f "delims=. tokens=1-6" %%a in ("%lhs%.%rhs%") do @(\r
+  @if %%a lss %%d (\r
+    set result=-1\r
+    goto :version_compare_done\r
+  ) else (\r
+    @if %%a gtr %%d (\r
+      set result=1\r
+      goto :version_compare_done\r
+    ) else (\r
+      @if %%b lss %%e (\r
+        set result=-1\r
+        goto :version_compare_done\r
+      ) else (\r
+        @if %%b gtr %%e (\r
+          set result=1\r
+          goto :version_compare_done\r
+        ) else (\r
+          @if %%c lss %%f (\r
+            set result=-1\r
+            goto :version_compare_done\r
+          ) else (\r
+            @if %%c gtr %%f (\r
+              set result=1\r
+              goto :version_compare_done\r
+            )\r
+          )\r
+        )\r
+      )\r
+    )\r
+  )\r
+)\r
+:version_compare_done\r
+@endlocal & set "%var%=%result%"\r
+@goto :eof\r
+\r
+:print_usage - Prints the usage of the script\r
+:: %* - message to print, each argument on it's own line\r
+@setlocal\r
+@for %%a in (%*) do @echo.%%~a\r
+@echo.\r
+@echo.build [/?][/v[v...]^|/q][/version][/arch a][/threading t]\r
+@echo.      [/exceptions e][/revision r] location\r
+@echo.\r
+@echo.  /version v  The version of MinGW to download\r
+@echo.  /arch a     The target architecture [i686^|x86_64]\r
+@echo.  /threading t\r
+@echo.              Threading model to use [posix^|win32]\r
+@echo.  /exceptions e\r
+@echo.              Exception model to use [sjlj^|seh^|dwarf]\r
+@echo.  /revision e Revision of the release to use\r
+@echo.  /v          Sets the output to be more verbose\r
+@echo.  /v[v...]    Extra verbosity, /vv, /vvv, etc\r
+@echo.  /q          Quiets the output\r
+@echo.  /?          Shows this usage message\r
+@echo.\r
+@endlocal\r
+@goto :eof\r
+\r
+:script_source - Determines if the script was ran from the cli or explorer\r
+:: %1 - The return variable [cli|explorer]\r
+@verify other 2>nul\r
+@setlocal EnableDelayedExpansion\r
+@if errorlevel 1 (\r
+  @call :log 0 "Failed to enable extensions"\r
+  @exit /b 1\r
+)\r
+@call :log 3 "Attempting to detect the script source"\r
+@echo "The invocation command was: '%cmdcmdline%'" >> %log_path%\r
+@for /f "tokens=1-3,*" %%a in ("%cmdcmdline%") do @(\r
+  set cmd=%%~a\r
+  set arg1=%%~b\r
+  set arg2=%%~c\r
+  set rest=%%~d\r
+)\r
+@set quote="\r
+@if "!arg2:~0,1!" equ "!quote!" (\r
+  if "!arg2:~-1!" neq "!quote!" (\r
+    set "arg2=!arg2:~1!"\r
+  )\r
+)\r
+@call :log 4 "cmd  = %cmd%"\r
+@call :log 4 "arg1 = %arg1%"\r
+@call :log 4 "arg2 = %arg2%"\r
+@call :log 4 "rest = %rest%"\r
+@call :log 4 "src  = %~f0"\r
+@if /i "%arg2%" == "call" (\r
+  set script_source=cli\r
+) else (\r
+  @if /i "%arg1%" == "/c" (\r
+    set script_source=explorer\r
+  ) else (\r
+    set script_source=cli\r
+  )\r
+)\r
+@call :log 3 "The script was invoked from %script_source%"\r
+@endlocal & set "%~1=%script_source%"\r
+@goto :eof\r
+\r
+:architecture - Finds the system architecture\r
+:: %1 - The return variable [i686|x86_64]\r
+@setlocal\r
+@call :log 3 "Determining the processor architecture"\r
+@set "key=HKLM\System\CurrentControlSet\Control\Session Manager\Environment"\r
+@set "var=PROCESSOR_ARCHITECTURE"\r
+@for /f "skip=2 tokens=2,*" %%a in ('reg query "%key%" /v "%var%"') do @set "arch=%%b"\r
+@if "%arch%" == "AMD64" set arch=x86_64\r
+@if "%arch%" == "x64" set arch=i686\r
+@call :log 4 "arch = %arch%"\r
+@endlocal & set "%~1=%arch%"\r
+@goto :eof\r
+\r
+:md5 - Gets the MD5 checksum for a file\r
+:: %1 - The hash\r
+:: %2 - The file path\r
+@setlocal\r
+@set "var=%~1"\r
+@set "file_path=%~2"\r
+@if "%var%" == "" @exit /b 1\r
+@if "%file_path%" == "" @exit /b 1\r
+@if not exist "%file_path%" @exit /b 1\r
+@for /f "skip=3 tokens=1,*" %%a in ('powershell Get-FileHash -Algorithm MD5 "'%file_path%'"') do @set hash=%%b\r
+@if not defined hash (\r
+  @call :log 6\r
+  @call :log 0 "Failed to get MD5 hash for %file_path%"\r
+  @exit /b 1\r
+)\r
+@endlocal & set "%var%=%hash: =%"\r
+@goto :eof\r
+\r
+:windows_version - Checks the windows version\r
+:: %1 - The windows version\r
+:: %2 - The major version number return variable\r
+:: %3 - The minor version number return variable\r
+:: %4 - The revision version number return variable\r
+@setlocal\r
+@call :log 3 "Retrieving the Windows version"\r
+@for /f "tokens=2 delims=[]" %%x in ('ver') do @set win_ver=%%x\r
+@set win_ver=%win_ver:Version =%\r
+@set win_ver_major=%win_ver:~0,1%\r
+@set win_ver_minor=%win_ver:~2,1%\r
+@set win_ver_rev=%win_ver:~4%\r
+@call :log 4 "win_ver = %win_ver%"\r
+@endlocal & set "%~1=%win_ver%" ^\r
+          & set "%~2=%win_ver_major%" ^\r
+          & set "%~3=%win_ver_minor%" ^\r
+          & set "%~4=%win_ver_rev%"\r
+@goto :eof\r
+\r
+:find_in_path - Finds a program of file in the PATH\r
+:: %1 - return variable of the file path\r
+@setlocal\r
+@set "var=%~1"\r
+@if "%var%" == "" @exit /b 1\r
+@set "file=%~2"\r
+@if "%file%" == "" @exit /b 1\r
+@call :log 3 "Searching PATH for %file%"\r
+@for %%x in ("%file%") do @set "file_path=%%~f$PATH:x"\r
+@if not defined file_path @exit /b 1\r
+@endlocal & set "%var%=%file_path%"\r
+@goto :eof\r
+\r
+:log_append - Appends another file into the current logging file\r
+:: %1 - the file_path to the file to concatenate\r
+@setlocal\r
+@set "file_path=%~1"\r
+@if "%file_path%" == "" @exit /b 1\r
+@call :log 3 "Appending to log: %file_path%"\r
+@call :iso8601 iso8601\r
+@set temp_log=%temp%\append-%iso8601%.log\r
+@call :log 4 "Using temp file %temp_log%"\r
+@type "%log_path%" "%file_path%" > "%temp_log%" 2>nul\r
+@move /y "%temp_log%" "%log_path%" 1>nul\r
+@del "%file_path% 2>nul\r
+@del "%temp_log% 2>nul\r
+@endlocal\r
+@goto :eof\r
+\r
+:iso8601 - Returns the current time in ISO8601 format\r
+:: %1 - the return variable\r
+:: %2 - format [extended|basic*]\r
+:: iso8601 - contains the resulting timestamp\r
+@setlocal\r
+@wmic Alias /? >NUL 2>&1 || @exit /b 1\r
+@set "var=%~1"\r
+@if "%var%" == "" @exit /b 1\r
+@set "format=%~2"\r
+@if "%format%" == "" set format=basic\r
+@for /F "skip=1 tokens=1-6" %%g IN ('wmic Path Win32_UTCTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') do @(\r
+  @if "%%~l"=="" goto :iso8601_done\r
+  @set "yyyy=%%l"\r
+  @set "mm=00%%j"\r
+  @set "dd=00%%g"\r
+  @set "hour=00%%h"\r
+  @set "minute=00%%i"\r
+  @set "seconds=00%%k"\r
+)\r
+:iso8601_done\r
+@set mm=%mm:~-2%\r
+@set dd=%dd:~-2%\r
+@set hour=%hour:~-2%\r
+@set minute=%minute:~-2%\r
+@set seconds=%seconds:~-2%\r
+@if /i [%format%] == [extended] (\r
+  set iso8601=%yyyy%-%mm%-%dd%T%hour%:%minute%:%seconds%Z\r
+) else (\r
+  if /i [%format%] == [basic] (\r
+    set iso8601=%yyyy%%mm%%dd%T%hour%%minute%%seconds%Z\r
+  ) else (\r
+    @exit /b 1\r
+  )\r
+)\r
+@set iso8601=%iso8601: =0%\r
+@endlocal & set %var%=%iso8601%\r
+@goto :eof\r
+\r
+:verbosity - Processes the verbosity parameter '/v[v...]\r
+:: %1 - verbosity given on the command line\r
+:: logging_level - set to the number of v's\r
+@setlocal\r
+@set logging_level=0\r
+@set verbosity=%~1\r
+:verbosity_loop\r
+@set verbosity=%verbosity:~1%\r
+@if not [%verbosity%] == [] @(\r
+  set /a "logging_level=logging_level+1"\r
+  goto verbosity_loop\r
+)\r
+@endlocal & set logging_level=%logging_level%\r
+@goto :eof\r
+\r
+:log - Logs a message, depending on verbosity\r
+:: %1 - level\r
+::       [0-4] for CLI logging\r
+::       [5-9] for GUI logging\r
+:: %2 - message to print\r
+@setlocal\r
+@set "level=%~1"\r
+@set "msg=%~2"\r
+@if "%log_folder%" == "" (\r
+  echo Logging was used to early in the script, log_folder isn't set yet\r
+  goto :eof\r
+)\r
+@if "%log_path%" == "" (\r
+  echo Logging was used to early in the script, log_path isn't set yet\r
+  goto :eof\r
+)\r
+@if not exist "%log_folder%" mkdir "%log_folder%"\r
+@if not exist "%log_path%" echo. 1>nul 2>"%log_path%"\r
+@echo.%msg% >> "%log_path%"\r
+@if %level% geq 5 (\r
+  @if [%script_source%] == [explorer] (\r
+    set /a "level=level-5"\r
+  ) else (\r
+    @goto :eof\r
+  )\r
+)\r
+@if "%logging_level%" == "" (\r
+  echo Logging was used to early in the script, logging_level isn't set yet\r
+  goto :eof\r
+)\r
+@if %logging_level% geq %level% echo.%msg% 1>&2\r
+@endlocal\r
+@goto :eof\r
+\r
+:download - Downloads a file from the internet\r
+:: %1 - the url of the file to download\r
+:: %2 - the file to download to\r
+:: %3 - the MD5 checksum of the file (optional)\r
+@setlocal EnableDelayedExpansion\r
+@if errorlevel 1 (\r
+  @call :print_usage "Failed to enable extensions"\r
+  @exit /b 1\r
+)\r
+@set "url=%~1"\r
+@set "file_path=%~2"\r
+@set "checksum=%~3"\r
+@for %%a in (%file_path%) do @set dir_path=%%~dpa\r
+@for %%a in (%file_path%) do @set file_name=%%~nxa\r
+@if "%url%" == "" @exit /b 1\r
+@if "%file_path%" == "" @exit /b 1\r
+@if "%dir_path%" == "" @exit /b 1\r
+@if "%file_name%" == "" @exit /b 1\r
+@if not exist "%dir_path%" mkdir "%dir_path%"\r
+@call :log 2 "Downloading %url%"\r
+@call :iso8601 iso8601\r
+@set "temp_path=%temp%\download-%iso8601%-%file_name%"\r
+@set "log_path=%temp%\download-%iso8601%-log-%file_name%"\r
+@call :log 4 "Using temp file %temp_path%"\r
+@powershell Invoke-WebRequest "'%url%'" ^\r
+  -OutFile "'%temp_path%'" ^\r
+  -UserAgent [Microsoft.PowerShell.Commands.PSUserAgent]::IE ^\r
+  1>nul 2>"%log_path%"\r
+@if errorlevel 1 (\r
+  @call :log 0 "Failed to download %url%"\r
+  @call :log_append "%log_path%"\r
+  @exit /b 1\r
+)\r
+@if [%checksum%] neq [] (\r
+  @call :log 4 "Checking %checksum% against %temp_path%"\r
+  @call :md5 hash "%temp_path%"\r
+  if "!hash!" neq "%checksum%" (\r
+    @call :log 0 "Failed to match checksum: %temp_path%"\r
+    @call :log 0 "Hash    : !hash!"\r
+    @call :log 0 "Checksum: %checksum%"\r
+    @exit /b 1\r
+  ) else (\r
+    @call :log 3 "Checksum matched: %temp_path%"\r
+    @call :log 3 "Hash    : !hash!"\r
+    @call :log 3 "Checksum: %checksum%"\r
+  )\r
+)\r
+@call :log 4 "Renaming %temp_path% to %file_path%"\r
+@move /y "%temp_path%" "%file_path%" 1>nul\r
+@endlocal\r
+@goto :eof\r