Skip to content

Instantly share code, notes, and snippets.

@oktal3700
Created August 31, 2020 13:31
Show Gist options
  • Save oktal3700/2ee62ece299ba52ff13bb745720bc6fe to your computer and use it in GitHub Desktop.
Save oktal3700/2ee62ece299ba52ff13bb745720bc6fe to your computer and use it in GitHub Desktop.
:: https://github.com/swift-project/pilotclient
:: Generate VS solution file for swift pilot client.
:: Easily choose compiler version, Qt version, and 32/64-bit.
:: Last updated 2019-09-29
@echo off
setlocal
echo:
echo - - - - - - - - - - - - - - - - - - - - - - - - - - -
echo Generating VS solution file for swift pilot client...
echo - - - - - - - - - - - - - - - - - - - - - - - - - - -
echo:
call :find_qt QT_ROOT
if not defined QT_ROOT if not defined QTDIR ( echo>&2 error: Could not find Qt & goto error )
set OLD_DIR=%CD%
set SRC_DIR=%~dp0
echo %~d0^> cd "%SRC_DIR%"
cd "%SRC_DIR%"
echo:
call :ask "Enter build directory" OUT_DIR .\build
if not exist "%OUT_DIR%" ( echo>&2 error: Build directory must exist & goto error )
if exist "%OUT_DIR%\swift.sln" set EXISTS=1
if exist "%OUT_DIR%\Makefile" set EXISTS=1
if defined EXISTS (
choice /c yn /m "Detected an existing build in that directory. Continue"
if errorlevel 2 exit /b
echo:
)
call :menu_start "Choose Qt version"
if defined QT_ROOT for /f "delims=" %%g in ('dir /b "%QT_ROOT%\?.*"') do (
call :menu_item "%%g" "%QT_ROOT%\%%g"
)
if defined QTDIR for /f "delims=" %%g in ("%QTDIR%") do (
if not ["%QT_ROOT%\"]==["%%~dpg"] call :menu_item "%%~nxg (QTDIR)" "%QTDIR%"
)
call :menu_end QT_PATH %MENU_COUNT%
call :menu_start "Choose VS installation"
set VS_WHERE=%programfiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe
set VC_COMPONENT=Microsoft.VisualStudio.Component.VC.Tools.x86.x64
set CMD="%VS_WHERE%" -all -prerelease -sort -products * -property installationPath -requires %VC_COMPONENT%
for /f "delims=" %%g in ('%CMD%') do (call :menu_item "%%g" "%%g")
call :menu_end VS_PATH 1
call :menu_start "Choose VC toolset version"
for /f "delims=. tokens=1,2" %%g in ('dir /b "%VS_PATH%\VC\Tools\MSVC"') do (call :menu_item "%%g.%%h" "%%g.%%h")
call :menu_end VC_VER %MENU_COUNT%
call :menu_start "Choose project type"
call :menu_item "VS Solution" "-tp vc"
call :menu_item "Makefile" ""
call :menu_end QT_TP 1
if not defined QT_TP (
call :menu_start "Choose compiler"
call :menu_item cl win32-msvc
call :menu_item clang-cl win32-clang-msvc
call :menu_end QT_SPEC 1
) else set QT_SPEC=win32-msvc
call :menu_start "Choose platform architecture"
call :menu_item 32-bit 32
call :menu_item 64-bit 64
call :menu_end EXT_ARCH 2
if [%EXT_ARCH%]==[32] ( set "VC_ARCH=amd64_x86" & set "QT_ARCH=msvc2019" )
if [%EXT_ARCH%]==[64] ( set "VC_ARCH=amd64" & set "QT_ARCH=msvc2019_64" )
call :ask "Any additional compiler flags?" CXX_FLAGS ""
set SRC_DIR
set OUT_DIR
set QT_PATH
set QT_ARCH
set QT_SPEC
set QT_TP
set VS_PATH
set VC_VER
set VC_ARCH
set EXT_ARCH
echo:
call "%VS_PATH%\VC\Auxiliary\Build\vcvarsall.bat" %VC_ARCH% -vcvars_ver=%VC_VER%
prompt $n:$g$s
echo on
cd "%OUT_DIR%"
"%QT_PATH%\%QT_ARCH%\bin\qmake" -Wlogic -r %QT_TP% -spec %QT_SPEC% "QMAKE_CXXFLAGS+=%CXX_FLAGS%" "%SRC_DIR%\swift.pro"
copy "%SRC_DIR%\externals\win32-msvc\%EXT_ARCH%\lib\*.dll" out\debug\bin
copy "%SRC_DIR%\externals\win32-msvc\%EXT_ARCH%\lib\*.dll" out\release\bin
@echo off
prompt $p$g
echo:
if defined QT_TP call :sxs_toolset
cd "%OLD_DIR%"
pause
exit /b
:error
echo:
pause
exit /b 1
:find_qt
setlocal
set KEY=HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall
for /f "delims=" %%g in ('reg query "%KEY%" /s /v DisplayName ^| find "HKEY_CURRENT_USER"') do (
for /f "tokens=3" %%h in ('reg query "%%g" /v DisplayName ^| find "REG_SZ"') do (
if ["%%h"]==["Qt"] (
for /f "delims=" %%i in ('reg query "%%g" /v InstallLocation ^| find "REG_SZ"') do (
set _TEMP=%%i
)
)
)
)
endlocal & if not ["%_TEMP%"]==[""] set "%~1=%_TEMP:*REG_SZ =%"
exit /b
:ask
setlocal
set /p "_TEMP=%~1: [%~3] "
endlocal & if ["%_TEMP%"]==[""] ( set "%~2=%~3" & if not ["%~3"]==[""] echo %~3 ) else ( set "%~2=%_TEMP%" )
echo:
exit /b
:menu_start
echo %~1:
set MENU_COUNT=0
exit /b
:menu_item
set /a "MENU_COUNT+=1"
echo %MENU_COUNT%. %~1
set MENU_OPT[%MENU_COUNT%]=%~2
exit /b
:menu_end
set "MENU_OPT="
if [%MENU_COUNT%]==[1] ( set "MENU_OPT=1" & echo [1] ) else ( set /p "MENU_OPT=[%~2] " )
if ["%MENU_OPT%"]==[""] set "MENU_OPT=%~2"
call set %~1=%%MENU_OPT[%MENU_OPT%]%%
echo:
exit /b
:sxs_toolset
set PROPS_FILE=Microsoft.VCToolsVersion.%VC_VER%.props
set PROPS_PATH=%VCInstallDir%\Auxiliary\Build\%VC_VER%\%PROPS_FILE%
if not exist "%PROPS_PATH%" set PROPS_PATH=%VCInstallDir%\Auxiliary\Build.%VC_VER%\%PROPS_FILE%
if exist "%PROPS_PATH%" (
set "XML_PATH=%SRC_DIR%;%PATH%"
where /q $XML_PATH:xml || goto missing_xml
set "XML=%SRC_DIR%\xml"
where /q xml || goto local_xml
set "XML=xml"
:local_xml
echo XML=%XML%
echo:
"%XML%" --help | find /i "xmlstarlet" >nul || goto missing_xml
echo Importing %PROPS_FILE%
for /r %%g in (*.vcxproj) do (
rem This part is based on the info here:
rem https://blogs.msdn.microsoft.com/vcblog/2017/11/15/side-by-side-minor-version-msvc-toolsets-in-visual-studio-2017/
"%XML%" ed -S -L -i "/_:Project/_:Import[contains(@Project,\"Microsoft.Cpp.Default.props\")]" -t elem -n Import %%g
"%XML%" ed -S -L -s "/_:Project/_:Import[not(@Project)]" -t attr -n Project -v "%PROPS_PATH%" %%g
)
) else echo Using latest VC toolset
echo:
exit /b
:missing_xml
echo>&2 error: This script requires xmlstarlet to select a side-by-side VS toolset:
echo>&2 http://xmlstar.sourceforge.net/
echo:
exit /b 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment