-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathllama_install_prerequisites_and_build.bat
More file actions
256 lines (210 loc) · 8.98 KB
/
llama_install_prerequisites_and_build.bat
File metadata and controls
256 lines (210 loc) · 8.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
@echo off
setlocal EnableExtensions
rem ==========================================================
rem Llama.cpp Community Build Script (Windows 10/11)
rem Checks prerequisites, installs if needed, and builds llama.cpp
rem developed by SAmedia / py-sandy / Sandra Weidmann
rem license: GPLv3
rem ==========================================================
pushd "%~dp0"
set "LLAMA_MARKER=%ProgramData%\llama_stage.txt"
rem --- Resume handling after reboot ---
if /i "%~1"=="--continue" (
if exist "%LLAMA_MARKER%" (
echo [INFO] Resuming setup after reboot...
del "%LLAMA_MARKER%" >nul 2>&1
goto :after_vs_install
)
)
rem --- make sure that winget is installed ---
where winget >nul 2>&1
if errorlevel 1 (
echo [INFO] winget not found – trying installation...
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
"Invoke-WebRequest -Uri https://aka.ms/getwinget -OutFile winget.msixbundle; ^
Add-AppxPackage .\winget.msixbundle"
if errorlevel 1 (
echo [ERROR] winget couldn't be installed.
pause
exit /b 1
)
)
rem --- Git Check ---
where git >nul 2>&1
if errorlevel 1 (
echo [INFO] Git not found – installing via winget...
winget install --id Git.Git -e --source winget || goto :err
)
rem --- CMake Check ---
where cmake >nul 2>&1
if errorlevel 1 (
echo [INFO] CMake not found – installing via winget...
winget install --id Kitware.CMake -e --source winget || goto :err
)
rem --- CUDA Toolkit installer if needed ---
call install_cuda.bat
rem --- Visual Studio Check (inkl. C++ Workload) ---
set "VSPATH="
if exist "%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" (
for /f "usebackq tokens=*" %%i in (`
"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" ^
-latest -products * ^
-requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 ^
-property installationPath
`) do set "VSPATH=%%i"
)
set "VSRC=0"
if "%VSPATH%"=="" (
echo [INFO] Visual Studio with C++ Tools not found – installing Community Edition with Workload...
winget install --id Microsoft.VisualStudio.2022.Community --override "--add Microsoft.VisualStudio.Workload.NativeDesktop --includeRecommended --passive --wait" || goto :err
set "VSRC=%ERRORLEVEL%"
echo [INFO] VS installer exit code: %VSRC%
)
rem --- 0, 3010 (reboot required), 1641 (reboot initiated) count as a success ---
if "%VSRC%"=="0" goto :ask_reboot
if "%VSRC%"=="3010" goto :ask_reboot
if "%VSRC%"=="1641" goto :ask_reboot
echo [ERROR] Visual Studio install failed (exit code %VSRC%).
goto :err
:ask_reboot
echo.
echo [INFO] Visual Studio 2022 installation completed.
choice /M "Do you want to reboot now to continue setup?"
if errorlevel 2 (
echo [INFO] You chose not to reboot now. Please restart manually later and run this script again.
goto :eof
) else (
echo [INFO] Preparing automatic resume after reboot...
rem marker in %ProgramData% - rights/UAC solid
echo stage2 > "%ProgramData%\llama_stage.txt"
rem start Batch via RunOnce via cmd.exe
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce" ^
/v LlamaInstaller /t REG_SZ ^
/d "%ComSpec% /d /c \"\"%~f0\" --continue\"" /f >nul 2>&1
echo [INFO] System will reboot in 10 seconds...
shutdown /r /t 10
exit /b
)
:after_vs_install
echo [INFO] Continuing setup after VS installation and reboot...
rem recalculate VS-path – new process after reboot means old variables are gone
set "VSPATH="
if exist "%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" (
for /f "usebackq tokens=*" %%i in (`
"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" ^
-latest -products * ^
-requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 ^
-property installationPath
`) do set "VSPATH=%%i"
)
echo [INFO] Visual Studio found at: %VSPATH%
rem --- trying first vcvars64.bat ---
if exist "%VSPATH%\VC\Auxiliary\Build\vcvars64.bat" (
call "%VSPATH%\VC\Auxiliary\Build\vcvars64.bat" || goto :err
) else (
rem --- then try VsDevCmd.bat ---
if exist "%VSPATH%\Common7\Tools\VsDevCmd.bat" (
call "%VSPATH%\Common7\Tools\VsDevCmd.bat" -arch=x64 -host_arch=x64 || goto :err
) else (
echo [ERROR] Even vcvars64.bat nor VsDevCmd.bat was found!
goto :err
)
)
rem --- change to script folder ---
pushd "%~dp0" || goto :err
rem --- clone llama.cpp repo ---
if not exist "llama.cpp\.git" (
git clone https://github.com/ggml-org/llama.cpp.git || goto :err
)
rem --- change to llama.cpp ---
pushd "llama.cpp" || goto :err
rem --- prepare vcpkg ---
set "VCPKG_ROOT=%CD%\vcpkg"
if not exist "%VCPKG_ROOT%\." (
git clone https://github.com/microsoft/vcpkg.git "%VCPKG_ROOT%" || goto :err
call "%VCPKG_ROOT%\bootstrap-vcpkg.bat" || goto :err
)
"%VCPKG_ROOT%\vcpkg.exe" install curl:x64-windows || goto :err
set "BUILD_DIR=%CD%\build"
set "LOGFILE=%CD%\build_log.txt"
echo [BUILD] %DATE% %TIME% :: Configure + Build (Ninja) > "%LOGFILE%"
echo [INFO] CMake configure is starting ...
rem --- CMake Build ---
cmake -S . -B "%BUILD_DIR%" -G Ninja ^
-DGGML_CUDA=ON ^
-DGGML_CUDA_GRAPHS=ON ^
-DGGML_CUDA_FA_ALL_QUANTS=ON ^
-DLLAMA_CURL=ON ^
-DCMAKE_CUDA_ARCHITECTURES=native ^
-DCMAKE_BUILD_TYPE=Release ^
-DCMAKE_TOOLCHAIN_FILE="%VCPKG_ROOT%\scripts\buildsystems\vcpkg.cmake" ^
-DVCPKG_TARGET_TRIPLET=x64-windows ^
-DCMAKE_CXX_FLAGS="/W0" >> "%LOGFILE%" 2>&1 || goto :err
echo [INFO] Build is starting...please wait...it can take some minutes...
call :START_NINJA_PROGRESS "Build (Ninja)"
cmake --build "%BUILD_DIR%" -j %NUMBER_OF_PROCESSORS% >> "%LOGFILE%" 2>&1 || goto :err
rem --- finished ---
popd
popd
echo.
echo [INFO] Build done successfully!
echo [LOG] Details: "%LOGFILE%"
pause
exit /b 0
:err
echo.
echo [ERROR] Error at build or dependencies. See log: "%LOGFILE%"
pause
exit /b 1
:START_NINJA_PROGRESS
setlocal EnableExtensions DisableDelayedExpansion
set "___P_TITLE=%~1"
if "%___P_TITLE%"=="" set "___P_TITLE=Ninja Build"
rem Fallback if LOGFILE is not set
set "___P_LOG=%LOGFILE%"
if not defined ___P_LOG set "___P_LOG=%CD%\build_log.txt"
rem Handover to PowerShell via environment variables (reliable, no argument parsing)
set "PS_NINJA_TITLE=%___P_TITLE%"
set "PS_NINJA_LOG=%___P_LOG%"
rem Background PowerShell: waits for ninja.exe, reads the log tail, shows % + bar on ONE line.
start "" /b powershell -NoProfile -ExecutionPolicy Bypass -Command ^
"$ErrorActionPreference='SilentlyContinue';" ^
"$title=$env:PS_NINJA_TITLE; $log=[Environment]::ExpandEnvironmentVariables($env:PS_NINJA_LOG);" ^
"$reNinja=[regex]'^\s*\[(\d+)/(\d+)\]'; $reCMake=[regex]'^\s*\[\s*(\d{1,3})%\s*\]';" ^
"$spin=@('|','/','-','\'); $i=0; $lastPct=$null; $done=$null; $total=$null;" ^
"Write-Host '';" ^
"$row=[Console]::CursorTop - 1;" ^
"for($w=0; $w -lt 240 -and -not (Get-Process -Name 'ninja' -ErrorAction SilentlyContinue); $w++){ Start-Sleep -Milliseconds 250 }" ^
"while(Get-Process -Name 'ninja' -ErrorAction SilentlyContinue){" ^
" $found=$false;" ^
" if (Test-Path $log){" ^
" try{ $tail = Get-Content -Path $log -Tail 1000 -ErrorAction Stop } catch { $tail=@() }" ^
" foreach($ln in $tail){" ^
" if($reNinja.IsMatch($ln)){" ^
" $m=$reNinja.Match($ln); $done=[int]$m.Groups[1].Value; $total=[int]$m.Groups[2].Value; if($total -gt 0){ $lastPct=[math]::Floor(100*$done/$total); $found=$true }" ^
" } elseif($reCMake.IsMatch($ln)){" ^
" $m=$reCMake.Match($ln); $lastPct=[int]$m.Groups[1].Value; $done=$null; $total=$null; $found=$true" ^
" }" ^
" }" ^
" }" ^
" $w=[Console]::WindowWidth;" ^
" if($w -lt 20){ $w=80 }" ^
" if($lastPct -ne $null){" ^
" $maxBar=[math]::Min(40,[math]::Max(10,$w-($title.Length+18))); " ^
" $fill=[math]::Floor($maxBar*$lastPct/100);" ^
" $bar=('#'*$fill).PadRight($maxBar,'-');" ^
" $ct= if($done -ne $null -and $total -ne $null) { ' ' + $done + '/' + $total } else { '' };" ^
" $txt='['+$lastPct+'%] '+$bar+$ct;" ^
" } else {" ^
" $txt='['+$spin[$i %% $spin.Length]+'] running...'; $i++" ^
" }" ^
" $line=$title+' '+$txt; if($line.Length -ge $w){ $line=$line.Substring(0,$w-1) }" ^
" [Console]::SetCursorPosition(0,$row);" ^
" [Console]::Write($line.PadRight($w-1));" ^
" [Console]::Title=$line;" ^
" Start-Sleep -Milliseconds 250" ^
"}" ^
"$w=[Console]::WindowWidth; if($w -lt 20){ $w=80 };" ^
"$doneLine=$title+' [done]'; if($doneLine.Length -ge $w){ $doneLine=$doneLine.Substring(0,$w-1) };" ^
"[Console]::SetCursorPosition(0,$row); [Console]::Write($doneLine.PadRight($w-1)); [Console]::Title=$title+' [done]';"
endlocal & exit /b 0