build_windows.ps1 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. $script:ARCH = $Env:PROCESSOR_ARCHITECTURE.ToLower()
  9. $script:TARGET_ARCH=$Env:PROCESSOR_ARCHITECTURE.ToLower()
  10. Write-host "Building for ${script:TARGET_ARCH}"
  11. write-host "Locating required tools and paths"
  12. $script:SRC_DIR=$PWD
  13. if (!$env:VCToolsRedistDir) {
  14. $MSVC_INSTALL=(Get-CimInstance MSFT_VSInstance -Namespace root/cimv2/vs)[0].InstallLocation
  15. $env:VCToolsRedistDir=(get-item "${MSVC_INSTALL}\VC\Redist\MSVC\*")[0]
  16. }
  17. # Locate CUDA versions
  18. # Note: this assumes every version found will be built
  19. $cudaList=(get-item "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v*\bin\" -ea 'silentlycontinue')
  20. if ($cudaList.length -eq 0) {
  21. $d=(get-command -ea 'silentlycontinue' nvcc).path
  22. if ($null -ne $d) {
  23. $script:CUDA_DIRS=@($d| split-path -parent)
  24. }
  25. } else {
  26. $script:CUDA_DIRS=$cudaList
  27. }
  28. $script:INNO_SETUP_DIR=(get-item "C:\Program Files*\Inno Setup*\")[0]
  29. $script:DEPS_DIR="${script:SRC_DIR}\dist\windows-${script:TARGET_ARCH}"
  30. $env:CGO_ENABLED="1"
  31. Write-Output "Checking version"
  32. if (!$env:VERSION) {
  33. $data=(git describe --tags --first-parent --abbrev=7 --long --dirty --always)
  34. $pattern="v(.+)"
  35. if ($data -match $pattern) {
  36. $script:VERSION=$matches[1]
  37. }
  38. } else {
  39. $script:VERSION=$env:VERSION
  40. }
  41. $pattern = "(\d+[.]\d+[.]\d+).*"
  42. if ($script:VERSION -match $pattern) {
  43. $script:PKG_VERSION=$matches[1]
  44. } else {
  45. $script:PKG_VERSION="0.0.0"
  46. }
  47. write-host "Building Ollama $script:VERSION with package version $script:PKG_VERSION"
  48. # Note: Windows Kits 10 signtool crashes with GCP's plugin
  49. if ($null -eq $env:SIGN_TOOL) {
  50. ${script:SignTool}="C:\Program Files (x86)\Windows Kits\8.1\bin\x64\signtool.exe"
  51. } else {
  52. ${script:SignTool}=${env:SIGN_TOOL}
  53. }
  54. if ("${env:KEY_CONTAINER}") {
  55. ${script:OLLAMA_CERT}=$(resolve-path "${script:SRC_DIR}\ollama_inc.crt")
  56. Write-host "Code signing enabled"
  57. } else {
  58. write-host "Code signing disabled - please set KEY_CONTAINERS to sign and copy ollama_inc.crt to the top of the source tree"
  59. }
  60. }
  61. function buildOllama() {
  62. write-host "Building ollama CLI"
  63. if ($null -eq ${env:OLLAMA_SKIP_GENERATE}) {
  64. Remove-Item -ea 0 -recurse -force -path "${script:SRC_DIR}\dist\windows-${script:ARCH}"
  65. # TODO - consider trying to parallelize this with Start-ThreadJob, but env vars can't be used to toggle
  66. # which targets to build
  67. # Start by skipping CUDA to build everything else
  68. pwsh -Command { $env:OLLAMA_SKIP_CUDA_GENERATE="1"; & go generate ./... }
  69. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  70. # Then skip everyhting else and build all the CUDA variants
  71. foreach ($env:CUDA_LIB_DIR in $script:CUDA_DIRS) {
  72. write-host "Building CUDA ${env:CUDA_LIB_DIR}"
  73. if ($env:CUDA_LIB_DIR.Contains("v12")) {
  74. pwsh -Command {
  75. $env:OLLAMA_SKIP_CUDA_GENERATE=""
  76. $env:OLLAMA_SKIP_STATIC_GENERATE="1"
  77. $env:OLLAMA_SKIP_CPU_GENERATE="1"
  78. $env:OLLAMA_SKIP_ONEAPI_GENERATE="1"
  79. $env:OLLAMA_SKIP_ROCM_GENERATE="1"
  80. $env:CMAKE_CUDA_ARCHITECTURES="60;61;62;70;72;75;80;86;87;89;90;90a"
  81. $env:OLLAMA_CUSTOM_CUDA_DEFS="-DGGML_CUDA_USE_GRAPHS=on"
  82. $env:CUDA_PATH=split-path -path $env:CUDA_LIB_DIR -parent
  83. $env:PATH="$envs:CUDA_LIB_DIR;$env:PATH"
  84. & go generate ./...
  85. }
  86. } else {
  87. pwsh -Command {
  88. $env:OLLAMA_SKIP_CUDA_GENERATE=""
  89. $env:OLLAMA_SKIP_STATIC_GENERATE="1"
  90. $env:OLLAMA_SKIP_CPU_GENERATE="1"
  91. $env:OLLAMA_SKIP_ONEAPI_GENERATE="1"
  92. $env:OLLAMA_SKIP_ROCM_GENERATE="1"
  93. $env:CMAKE_CUDA_ARCHITECTURES="50;52;53;60;61;62;70;72;75;80;86"
  94. $env:OLLAMA_CUSTOM_CUDA_DEFS=""
  95. $env:CUDA_PATH=split-path -path $env:CUDA_LIB_DIR -parent
  96. $env:PATH="$envs:CUDA_LIB_DIR;$env:PATH"
  97. & go generate ./...
  98. }
  99. }
  100. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  101. }
  102. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  103. } else {
  104. write-host "Skipping generate step with OLLAMA_SKIP_GENERATE set"
  105. }
  106. & go build -trimpath -ldflags "-s -w -X=github.com/ollama/ollama/version.Version=$script:VERSION -X=github.com/ollama/ollama/server.mode=release" .
  107. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  108. if ("${env:KEY_CONTAINER}") {
  109. & "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${script:OLLAMA_CERT}" `
  110. /csp "Google Cloud KMS Provider" /kc ${env:KEY_CONTAINER} ollama.exe
  111. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  112. }
  113. New-Item -ItemType Directory -Path .\dist\windows-${script:TARGET_ARCH}\ -Force
  114. cp .\ollama.exe .\dist\windows-${script:TARGET_ARCH}\
  115. }
  116. function buildApp() {
  117. write-host "Building Ollama App"
  118. cd "${script:SRC_DIR}\app"
  119. & windres -l 0 -o ollama.syso ollama.rc
  120. & 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" .
  121. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  122. if ("${env:KEY_CONTAINER}") {
  123. & "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${script:OLLAMA_CERT}" `
  124. /csp "Google Cloud KMS Provider" /kc ${env:KEY_CONTAINER} app.exe
  125. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  126. }
  127. }
  128. function gatherDependencies() {
  129. write-host "Gathering runtime dependencies"
  130. cd "${script:SRC_DIR}"
  131. md "${script:DEPS_DIR}\lib\ollama" -ea 0 > $null
  132. # TODO - this varies based on host build system and MSVC version - drive from dumpbin output
  133. # currently works for Win11 + MSVC 2019 + Cuda V11
  134. cp "${env:VCToolsRedistDir}\x64\Microsoft.VC*.CRT\msvcp140*.dll" "${script:DEPS_DIR}\lib\ollama\"
  135. cp "${env:VCToolsRedistDir}\x64\Microsoft.VC*.CRT\vcruntime140.dll" "${script:DEPS_DIR}\lib\ollama\"
  136. cp "${env:VCToolsRedistDir}\x64\Microsoft.VC*.CRT\vcruntime140_1.dll" "${script:DEPS_DIR}\lib\ollama\"
  137. foreach ($part in $("runtime", "stdio", "filesystem", "math", "convert", "heap", "string", "time", "locale", "environment")) {
  138. cp "$env:VCToolsRedistDir\..\..\..\Tools\Llvm\x64\bin\api-ms-win-crt-${part}*.dll" "${script:DEPS_DIR}\lib\ollama\"
  139. }
  140. cp "${script:SRC_DIR}\app\ollama_welcome.ps1" "${script:SRC_DIR}\dist\"
  141. if ("${env:KEY_CONTAINER}") {
  142. write-host "about to sign"
  143. foreach ($file in (get-childitem "${script:DEPS_DIR}\lib\ollama\cu*.dll") + @("${script:SRC_DIR}\dist\ollama_welcome.ps1")){
  144. write-host "signing $file"
  145. & "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${script:OLLAMA_CERT}" `
  146. /csp "Google Cloud KMS Provider" /kc ${env:KEY_CONTAINER} $file
  147. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  148. }
  149. }
  150. }
  151. function buildInstaller() {
  152. write-host "Building Ollama Installer"
  153. cd "${script:SRC_DIR}\app"
  154. $env:PKG_VERSION=$script:PKG_VERSION
  155. if ("${env:KEY_CONTAINER}") {
  156. & "${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
  157. } else {
  158. & "${script:INNO_SETUP_DIR}\ISCC.exe" /DARCH=$script:TARGET_ARCH .\ollama.iss
  159. }
  160. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  161. }
  162. function distZip() {
  163. write-host "Generating stand-alone distribution zip file ${script:SRC_DIR}\dist\ollama-windows-${script:TARGET_ARCH}.zip"
  164. Compress-Archive -Path "${script:SRC_DIR}\dist\windows-${script:TARGET_ARCH}\*" -DestinationPath "${script:SRC_DIR}\dist\ollama-windows-${script:TARGET_ARCH}.zip" -Force
  165. }
  166. try {
  167. checkEnv
  168. buildOllama
  169. buildApp
  170. gatherDependencies
  171. buildInstaller
  172. distZip
  173. } catch {
  174. write-host "Build Failed"
  175. write-host $_
  176. } finally {
  177. set-location $script:SRC_DIR
  178. $env:PKG_VERSION=""
  179. }