build_windows.ps1 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #!powershell
  2. #
  3. # powershell -ExecutionPolicy Bypass -File .\scripts\build_windows.ps1
  4. #
  5. # gcloud auth application-default login
  6. $ErrorActionPreference = "Stop"
  7. function checkEnv() {
  8. if ($null -ne $env:ARCH ) {
  9. $script:ARCH = $env:ARCH
  10. } else {
  11. $arch=([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)
  12. if ($null -ne $arch) {
  13. $script:ARCH = ($arch.ToString().ToLower()).Replace("x64", "amd64")
  14. } else {
  15. write-host "WARNING: old powershell detected, assuming amd64 architecture - set `$env:ARCH to override"
  16. $script:ARCH="amd64"
  17. }
  18. }
  19. $script:TARGET_ARCH=$script:ARCH
  20. Write-host "Building for ${script:TARGET_ARCH}"
  21. write-host "Locating required tools and paths"
  22. $script:SRC_DIR=$PWD
  23. if ($null -eq $env:VCToolsRedistDir) {
  24. $MSVC_INSTALL=(Get-CimInstance MSFT_VSInstance -Namespace root/cimv2/vs)[0].InstallLocation
  25. $env:VCToolsRedistDir=(get-item "${MSVC_INSTALL}\VC\Redist\MSVC\*")[0]
  26. }
  27. if (-Not (get-command -ErrorAction silent ninja)) {
  28. $script:NINJA_DIR=(gci -path (Get-CimInstance MSFT_VSInstance -Namespace root/cimv2/vs)[0].InstallLocation -r -fi ninja.exe) | split-path -parent
  29. }
  30. # Locate CUDA versions
  31. # Note: this assumes every version found will be built
  32. $cudaList=(get-item "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v*\bin\" -ea 'silentlycontinue')
  33. if ($cudaList.length -eq 0) {
  34. $d=(get-command -ea 'silentlycontinue' nvcc).path
  35. if ($null -ne $d) {
  36. $script:CUDA_DIRS=@($d| split-path -parent)
  37. }
  38. } else {
  39. $script:CUDA_DIRS=$cudaList
  40. }
  41. $inoSetup=(get-item "C:\Program Files*\Inno Setup*\")
  42. if ($inoSetup.length -gt 0) {
  43. $script:INNO_SETUP_DIR=$inoSetup[0]
  44. }
  45. $script:DIST_DIR="${script:SRC_DIR}\dist\windows-${script:TARGET_ARCH}"
  46. $env:CGO_ENABLED="1"
  47. Write-Output "Checking version"
  48. if (!$env:VERSION) {
  49. $data=(git describe --tags --first-parent --abbrev=7 --long --dirty --always)
  50. $pattern="v(.+)"
  51. if ($data -match $pattern) {
  52. $script:VERSION=$matches[1]
  53. }
  54. } else {
  55. $script:VERSION=$env:VERSION
  56. }
  57. $pattern = "(\d+[.]\d+[.]\d+).*"
  58. if ($script:VERSION -match $pattern) {
  59. $script:PKG_VERSION=$matches[1]
  60. } else {
  61. $script:PKG_VERSION="0.0.0"
  62. }
  63. write-host "Building Ollama $script:VERSION with package version $script:PKG_VERSION"
  64. # Note: Windows Kits 10 signtool crashes with GCP's plugin
  65. if ($null -eq $env:SIGN_TOOL) {
  66. ${script:SignTool}="C:\Program Files (x86)\Windows Kits\8.1\bin\x64\signtool.exe"
  67. } else {
  68. ${script:SignTool}=${env:SIGN_TOOL}
  69. }
  70. if ("${env:KEY_CONTAINER}") {
  71. ${script:OLLAMA_CERT}=$(resolve-path "${script:SRC_DIR}\ollama_inc.crt")
  72. Write-host "Code signing enabled"
  73. } else {
  74. write-host "Code signing disabled - please set KEY_CONTAINERS to sign and copy ollama_inc.crt to the top of the source tree"
  75. }
  76. $script:JOBS=((Get-CimInstance Win32_ComputerSystem).NumberOfLogicalProcessors)
  77. }
  78. function buildOllama() {
  79. if ($null -eq ${env:OLLAMA_SKIP_GENERATE}) {
  80. Remove-Item -ea 0 -recurse -force -path "${script:SRC_DIR}\dist\windows-${script:ARCH}"
  81. New-Item "${script:SRC_DIR}\dist\windows-${script:ARCH}\lib\ollama\" -ItemType Directory -ea 0
  82. & cmake --fresh --preset CPU --install-prefix $script:DIST_DIR
  83. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  84. & cmake --build --preset CPU --parallel $script:JOBS
  85. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  86. & cmake --install build --component CPU --strip
  87. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  88. $hashEnv = @{}
  89. Get-ChildItem env: | foreach { $hashEnv[$_.Name] = $_.Value }
  90. if ("$script:CUDA_DIRS".Contains("v11")) {
  91. $hashEnv.Keys | foreach { if ($_.Contains("CUDA_PATH_V11")) { $v11="$_" }}
  92. $env:CUDAToolkit_ROOT=$hashEnv[$v11]
  93. write-host "Building CUDA v11 backend libraries"
  94. # Note: cuda v11 requires msvc 2019 so force the older generator
  95. # to avoid 2022 (or newer) from being used as the default
  96. & cmake --fresh --preset "CUDA 11" -G "Visual Studio 16 2019" --install-prefix $script:DIST_DIR
  97. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  98. & cmake --build --preset "CUDA 11" --parallel $script:JOBS
  99. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  100. & cmake --install build --component "CUDA" --strip
  101. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  102. }
  103. if ("$script:CUDA_DIRS".Contains("v12")) {
  104. $hashEnv.Keys | foreach { if ($_.Contains("CUDA_PATH_V12")) { $v12="$_" }}
  105. $env:CUDAToolkit_ROOT=$hashEnv[$v12]
  106. write-host "Building CUDA v12 backend libraries"
  107. & cmake --fresh --preset "CUDA 12" --install-prefix $script:DIST_DIR
  108. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  109. & cmake --build --preset "CUDA 12" --parallel $script:JOBS
  110. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  111. & cmake --install build --component "CUDA" --strip
  112. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  113. }
  114. if ($env:HIP_PATH) {
  115. write-host "Building ROCm backend libraries"
  116. if ($null -ne $script:NINJA_DIR) {
  117. $env:PATH="$script:NINJA_DIR;$env:PATH"
  118. }
  119. $env:HIPCXX="${env:HIP_PATH}\bin\clang++.exe"
  120. $env:HIP_PLATFORM="amd"
  121. $env:CMAKE_PREFIX_PATH="${env:HIP_PATH}"
  122. & cmake --fresh --preset "ROCm 6" -G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ --install-prefix $script:DIST_DIR
  123. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  124. $env:HIPCXX=""
  125. $env:HIP_PLATFORM=""
  126. $env:CMAKE_PREFIX_PATH=""
  127. & cmake --build --preset "ROCm" --parallel $script:JOBS
  128. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  129. & cmake --install build --component "HIP" --strip
  130. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  131. }
  132. } else {
  133. write-host "Skipping generate step with OLLAMA_SKIP_GENERATE set"
  134. }
  135. write-host "Building ollama CLI"
  136. & go build -trimpath -ldflags "-s -w -X=github.com/ollama/ollama/version.Version=$script:VERSION -X=github.com/ollama/ollama/server.mode=release" .
  137. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  138. cp .\ollama.exe "${script:DIST_DIR}\"
  139. }
  140. function buildApp() {
  141. write-host "Building Ollama App"
  142. cd "${script:SRC_DIR}\app"
  143. & windres -l 0 -o ollama.syso ollama.rc
  144. & go build -trimpath -ldflags "-s -w -H windowsgui -X=github.com/ollama/ollama/version.Version=$script:VERSION -X=github.com/ollama/ollama/server.mode=release" -o "${script:SRC_DIR}\dist\windows-${script:TARGET_ARCH}-app.exe" .
  145. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  146. }
  147. function gatherDependencies() {
  148. if ($null -eq $env:VCToolsRedistDir) {
  149. write-error "Unable to locate VC Install location - please use a Developer shell"
  150. exit 1
  151. }
  152. write-host "Gathering runtime dependencies from $env:VCToolsRedistDir"
  153. cd "${script:SRC_DIR}"
  154. md "${script:DIST_DIR}\lib\ollama" -ea 0 > $null
  155. # TODO - this varies based on host build system and MSVC version - drive from dumpbin output
  156. # currently works for Win11 + MSVC 2019 + Cuda V11
  157. if ($script:TARGET_ARCH -eq "amd64") {
  158. $depArch="x64"
  159. } else {
  160. $depArch=$script:TARGET_ARCH
  161. }
  162. if ($depArch -eq "x64") {
  163. write-host "cp ${env:VCToolsRedistDir}\${depArch}\Microsoft.VC*.CRT\msvcp140*.dll ${script:DIST_DIR}\lib\ollama\"
  164. cp "${env:VCToolsRedistDir}\${depArch}\Microsoft.VC*.CRT\msvcp140*.dll" "${script:DIST_DIR}\lib\ollama\"
  165. write-host "cp ${env:VCToolsRedistDir}\${depArch}\Microsoft.VC*.CRT\vcruntime140.dll ${script:DIST_DIR}\lib\ollama\"
  166. cp "${env:VCToolsRedistDir}\${depArch}\Microsoft.VC*.CRT\vcruntime140.dll" "${script:DIST_DIR}\lib\ollama\"
  167. write-host "cp ${env:VCToolsRedistDir}\${depArch}\Microsoft.VC*.CRT\vcruntime140_1.dll ${script:DIST_DIR}\lib\ollama\"
  168. cp "${env:VCToolsRedistDir}\${depArch}\Microsoft.VC*.CRT\vcruntime140_1.dll" "${script:DIST_DIR}\lib\ollama\"
  169. $llvmCrtDir="$env:VCToolsRedistDir\..\..\..\Tools\Llvm\${depArch}\bin"
  170. foreach ($part in $("runtime", "stdio", "filesystem", "math", "convert", "heap", "string", "time", "locale", "environment")) {
  171. write-host "cp ${llvmCrtDir}\api-ms-win-crt-${part}*.dll ${script:DIST_DIR}\lib\ollama\"
  172. cp "${llvmCrtDir}\api-ms-win-crt-${part}*.dll" "${script:DIST_DIR}\lib\ollama\"
  173. }
  174. } else {
  175. # Carying the dll's doesn't seem to work, so use the redist installer
  176. copy-item -path "${env:VCToolsRedistDir}\vc_redist.arm64.exe" -destination "${script:DIST_DIR}" -verbose
  177. }
  178. cp "${script:SRC_DIR}\app\ollama_welcome.ps1" "${script:SRC_DIR}\dist\"
  179. }
  180. function sign() {
  181. if ("${env:KEY_CONTAINER}") {
  182. write-host "Signing Ollama executables, scripts and libraries"
  183. & "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${script:OLLAMA_CERT}" `
  184. /csp "Google Cloud KMS Provider" /kc ${env:KEY_CONTAINER} `
  185. $(get-childitem -path "${script:SRC_DIR}\dist" -r -include @('ollama_welcome.ps1')) `
  186. $(get-childitem -path "${script:SRC_DIR}\dist\windows-*" -r -include @('*.exe', '*.dll'))
  187. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  188. } else {
  189. write-host "Signing not enabled"
  190. }
  191. }
  192. function buildInstaller() {
  193. if ($null -eq ${script:INNO_SETUP_DIR}) {
  194. write-host "Inno Setup not present, skipping installer build"
  195. return
  196. }
  197. write-host "Building Ollama Installer"
  198. cd "${script:SRC_DIR}\app"
  199. $env:PKG_VERSION=$script:PKG_VERSION
  200. if ("${env:KEY_CONTAINER}") {
  201. & "${script:INNO_SETUP_DIR}\ISCC.exe" /DARCH=$script:TARGET_ARCH /SMySignTool="${script:SignTool} sign /fd sha256 /t http://timestamp.digicert.com /f ${script:OLLAMA_CERT} /csp `$qGoogle Cloud KMS Provider`$q /kc ${env:KEY_CONTAINER} `$f" .\ollama.iss
  202. } else {
  203. & "${script:INNO_SETUP_DIR}\ISCC.exe" /DARCH=$script:TARGET_ARCH .\ollama.iss
  204. }
  205. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  206. }
  207. function distZip() {
  208. if (Test-Path -Path "${script:SRC_DIR}\dist\windows-amd64") {
  209. write-host "Generating stand-alone distribution zip file ${script:SRC_DIR}\dist\ollama-windows-amd64.zip"
  210. Compress-Archive -Path "${script:SRC_DIR}\dist\windows-amd64\*" -DestinationPath "${script:SRC_DIR}\dist\ollama-windows-amd64.zip" -Force
  211. }
  212. if (Test-Path -Path "${script:SRC_DIR}\dist\windows-arm64") {
  213. write-host "Generating stand-alone distribution zip file ${script:SRC_DIR}\dist\ollama-windows-arm64.zip"
  214. Compress-Archive -Path "${script:SRC_DIR}\dist\windows-arm64\*" -DestinationPath "${script:SRC_DIR}\dist\ollama-windows-arm64.zip" -Force
  215. }
  216. }
  217. checkEnv
  218. try {
  219. if ($($args.count) -eq 0) {
  220. buildOllama
  221. buildApp
  222. gatherDependencies
  223. sign
  224. buildInstaller
  225. distZip
  226. } else {
  227. for ( $i = 0; $i -lt $args.count; $i++ ) {
  228. write-host "performing $($args[$i])"
  229. & $($args[$i])
  230. }
  231. }
  232. } catch {
  233. write-host "Build Failed"
  234. write-host $_
  235. } finally {
  236. set-location $script:SRC_DIR
  237. $env:PKG_VERSION=""
  238. }