Skip to content

Instantly share code, notes, and snippets.

@m5knt
Last active March 28, 2023 01:50
Show Gist options
  • Save m5knt/430dfc0f66b12c3739b1915342ccfdac to your computer and use it in GitHub Desktop.
Save m5knt/430dfc0f66b12c3739b1915342ccfdac to your computer and use it in GitHub Desktop.
Automatically finds and executes cmake
@echo off
call :MAIN %* <nul
exit /b
:MAIN
if not exist "%BAT_CMAKE_BAT%" call :AUTO_DETECTOR
if not exist "%BAT_CMAKE_BAT%" (
echo cmake not found
echo please install "cmake"
echo see https://cmake.org/
exit /b 1
)
"%BAT_CMAKE_BAT%" %*
exit /b
::
::
::
:AUTO_DETECTOR
set BAT_CMAKE_BAT=
call :SEARCH_PATH
if defined BAT_CMAKE_BAT exit /b
if "%VisualStudioVersion%" == "17.0" call :SEARCH_VS2022
if "%VisualStudioVersion%" == "16.0" call :SEARCH_VS2019
if "%VisualStudioVersion%" == "15.0" call :SEARCH_VS2017
if defined BAT_CMAKE_BAT exit /b
rem call :SEARCH_VS2022
rem if defined BAT_CMAKE_BAT exit /b
rem call :SEARCH_VS2019
rem if defined BAT_CMAKE_BAT exit /b
call :SEARCH_VS2017
if defined BAT_CMAKE_BAT exit /b
exit /b
:SEARCH_PATH
where /q cmake.exe
if "%ERRORLEVEL%" == "0" (
for /f "delims=" %%a in ('where cmake.exe') do (
set "BAT_CMAKE_BAT=%%a"
exit /b
)
)
exit /b
:SEARCH_CMAKE
rem tipical location "C:\Program Files\CMake"
call :SEARCH "HKLM\SOFTWARE\Kitware\CMake" "InstallDir" ^
"bin\cmake.exe"
exit /b
:SEARCH_VS2017
rem tipical location "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional"
rem vs15.9.53/cmake version 3.12.18081601-MSVC_2
call :SEARCH "HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\47121d9d" "InstallLocation" ^
"Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe"
exit /b
:SEARCH_VS2019
rem tipical location "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional"
rem vs16.11.25/cmake version 3.20.21032501-MSVC_2
call :SEARCH "HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\53313fde" "InstallLocation" ^
"Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe"
exit /b
:SEARCH_VS2022
rem tipical location "C:\Program Files\Microsoft Visual Studio\2022\Professional"
rem vs17.5.3/cmake version 3.25.1-msvc1
call :SEARCH "HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\900f21fc" "InstallLocation" ^
"Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe"
exit /b
:SEARCH
:: %1 registory path
:: %2 registory key
:: %3 relative to base
:: return %BAT_CMAKE_BAT%
setlocal
call :REG_QUERY %*
if "%RETVAL%" == "" exit /b
for /f "delims=" %%a in ("%RETVAL%") do (
endlocal
set "BAT_CMAKE_BAT=%%a"
exit /b
)
:REG_QUERY
set RETVAL=
reg query %1 /v %2 >nul 2>nul
if not "%ERRORLEVEL%" == "0" exit /b
for /f "tokens=1,2,*" %%a in ('reg query %1 /v %2') do set "RETVAL=%%c"
if not "%~3" == "" (
if "%RETVAL:~-1%" == "\" ( set "RETVAL=%RETVAL%%~3" ) else ( set "RETVAL=%RETVAL%\%~3" )
)
if exist "%RETVAL%" exit /b
set RETVAL=
exit /b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment