build_windows.ps1 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. # Locate CUDA versions
  28. # Note: this assumes every version found will be built
  29. $cudaList=(get-item "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v*\bin\" -ea 'silentlycontinue')
  30. if ($cudaList.length -eq 0) {
  31. $d=(get-command -ea 'silentlycontinue' nvcc).path
  32. if ($null -ne $d) {
  33. $script:CUDA_DIRS=@($d| split-path -parent)
  34. }
  35. } else {
  36. $script:CUDA_DIRS=$cudaList
  37. }
  38. $inoSetup=(get-item "C:\Program Files*\Inno Setup*\")
  39. if ($inoSetup.length -gt 0) {
  40. $script:INNO_SETUP_DIR=$inoSetup[0]
  41. }
  42. $script:DIST_DIR="${script:SRC_DIR}\dist\windows-${script:TARGET_ARCH}"
  43. $env:CGO_ENABLED="1"
  44. Write-Output "Checking version"
  45. if (!$env:VERSION) {
  46. $data=(git describe --tags --first-parent --abbrev=7 --long --dirty --always)
  47. $pattern="v(.+)"
  48. if ($data -match $pattern) {
  49. $script:VERSION=$matches[1]
  50. }
  51. } else {
  52. $script:VERSION=$env:VERSION
  53. }
  54. $pattern = "(\d+[.]\d+[.]\d+).*"
  55. if ($script:VERSION -match $pattern) {
  56. $script:PKG_VERSION=$matches[1]
  57. } else {
  58. $script:PKG_VERSION="0.0.0"
  59. }
  60. write-host "Building Ollama $script:VERSION with package version $script:PKG_VERSION"
  61. # Note: Windows Kits 10 signtool crashes with GCP's plugin
  62. if ($null -eq $env:SIGN_TOOL) {
  63. ${script:SignTool}="C:\Program Files (x86)\Windows Kits\8.1\bin\x64\signtool.exe"
  64. } else {
  65. ${script:SignTool}=${env:SIGN_TOOL}
  66. }
  67. if ("${env:KEY_CONTAINER}") {
  68. ${script:OLLAMA_CERT}=$(resolve-path "${script:SRC_DIR}\ollama_inc.crt")
  69. Write-host "Code signing enabled"
  70. } else {
  71. write-host "Code signing disabled - please set KEY_CONTAINERS to sign and copy ollama_inc.crt to the top of the source tree"
  72. }
  73. }
  74. function buildOllama() {
  75. if ($null -eq ${env:OLLAMA_SKIP_GENERATE}) {
  76. Remove-Item -ea 0 -recurse -force -path "${script:SRC_DIR}\dist\windows-${script:ARCH}"
  77. # TODO - consider trying to parallelize this with Start-ThreadJob, but env vars can't be used to toggle
  78. # which targets to build
  79. # Start by skipping CUDA to build everything else
  80. write-host "Building ollama runners"
  81. powershell -Command { $env:OLLAMA_SKIP_CUDA_GENERATE="1"; & go generate ./... }
  82. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  83. # Then skip everyhting else and build all the CUDA variants
  84. foreach ($env:CUDA_LIB_DIR in $script:CUDA_DIRS) {
  85. write-host "Building CUDA ${env:CUDA_LIB_DIR} runner"
  86. if ($env:CUDA_LIB_DIR.Contains("v12")) {
  87. powershell -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="60;61;62;70;72;75;80;86;87;89;90;90a"
  94. $env:OLLAMA_CUSTOM_CUDA_DEFS="-DGGML_CUDA_USE_GRAPHS=on"
  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. } else {
  100. powershell -Command {
  101. $env:OLLAMA_SKIP_CUDA_GENERATE=""
  102. $env:OLLAMA_SKIP_STATIC_GENERATE="1"
  103. $env:OLLAMA_SKIP_CPU_GENERATE="1"
  104. $env:OLLAMA_SKIP_ONEAPI_GENERATE="1"
  105. $env:OLLAMA_SKIP_ROCM_GENERATE="1"
  106. $env:CMAKE_CUDA_ARCHITECTURES="50;52;53;60;61;62;70;72;75;80;86"
  107. $env:OLLAMA_CUSTOM_CUDA_DEFS=""
  108. $env:CUDA_PATH=split-path -path $env:CUDA_LIB_DIR -parent
  109. $env:PATH="$envs:CUDA_LIB_DIR;$env:PATH"
  110. & go generate ./...
  111. }
  112. }
  113. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  114. }
  115. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  116. } else {
  117. write-host "Skipping generate step with OLLAMA_SKIP_GENERATE set"
  118. }
  119. write-host "Building ollama CLI"
  120. & go build -trimpath -ldflags "-s -w -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} ollama.exe
  125. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  126. }
  127. New-Item -ItemType Directory -Path .\dist\windows-${script:TARGET_ARCH}\ -Force
  128. cp .\ollama.exe .\dist\windows-${script:TARGET_ARCH}\
  129. }
  130. function buildApp() {
  131. write-host "Building Ollama App"
  132. cd "${script:SRC_DIR}\app"
  133. & windres -l 0 -o ollama.syso ollama.rc
  134. & 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" .
  135. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  136. if ("${env:KEY_CONTAINER}") {
  137. & "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${script:OLLAMA_CERT}" `
  138. /csp "Google Cloud KMS Provider" /kc ${env:KEY_CONTAINER} "${script:SRC_DIR}\dist\windows-${script:TARGET_ARCH}-app.exe"
  139. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  140. }
  141. }
  142. function gatherDependencies() {
  143. if ($null -eq $env:VCToolsRedistDir) {
  144. write-error "Unable to locate VC Install location - please use a Developer shell"
  145. exit 1
  146. }
  147. write-host "Gathering runtime dependencies from $env:VCToolsRedistDir"
  148. cd "${script:SRC_DIR}"
  149. md "${script:DIST_DIR}\lib\ollama" -ea 0 > $null
  150. # TODO - this varies based on host build system and MSVC version - drive from dumpbin output
  151. # currently works for Win11 + MSVC 2019 + Cuda V11
  152. if ($script:TARGET_ARCH -eq "amd64") {
  153. $depArch="x64"
  154. } else {
  155. $depArch=$script:TARGET_ARCH
  156. }
  157. if ($depArch -eq "amd64") {
  158. cp "${env:VCToolsRedistDir}\${depArch}\Microsoft.VC*.CRT\msvcp140*.dll" "${script:DIST_DIR}\lib\ollama\"
  159. cp "${env:VCToolsRedistDir}\${depArch}\Microsoft.VC*.CRT\vcruntime140.dll" "${script:DIST_DIR}\lib\ollama\"
  160. cp "${env:VCToolsRedistDir}\${depArch}\Microsoft.VC*.CRT\vcruntime140_1.dll" "${script:DIST_DIR}\lib\ollama\"
  161. $llvmCrtDir="$env:VCToolsRedistDir\..\..\..\Tools\Llvm\${depArch}\bin"
  162. foreach ($part in $("runtime", "stdio", "filesystem", "math", "convert", "heap", "string", "time", "locale", "environment")) {
  163. write-host "cp ${llvmCrtDir}\api-ms-win-crt-${part}*.dll ${script:DIST_DIR}\lib\ollama\"
  164. cp "${llvmCrtDir}\api-ms-win-crt-${part}*.dll" "${script:DIST_DIR}\lib\ollama\"
  165. }
  166. } else {
  167. # Carying the dll's doesn't seem to work, so use the redist installer
  168. copy-item -path "${env:VCToolsRedistDir}\vc_redist.arm64.exe" -destination "${script:DIST_DIR}" -verbose
  169. }
  170. cp "${script:SRC_DIR}\app\ollama_welcome.ps1" "${script:SRC_DIR}\dist\"
  171. if ("${env:KEY_CONTAINER}") {
  172. write-host "about to sign"
  173. foreach ($file in (get-childitem "${script:DIST_DIR}\lib\ollama\cu*.dll") + @("${script:SRC_DIR}\dist\ollama_welcome.ps1")){
  174. write-host "signing $file"
  175. & "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${script:OLLAMA_CERT}" `
  176. /csp "Google Cloud KMS Provider" /kc ${env:KEY_CONTAINER} $file
  177. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  178. }
  179. }
  180. }
  181. function buildInstaller() {
  182. if ($null -eq ${script:INNO_SETUP_DIR}) {
  183. write-host "Inno Setup not present, skipping installer build"
  184. return
  185. }
  186. write-host "Building Ollama Installer"
  187. cd "${script:SRC_DIR}\app"
  188. $env:PKG_VERSION=$script:PKG_VERSION
  189. if ("${env:KEY_CONTAINER}") {
  190. & "${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
  191. } else {
  192. & "${script:INNO_SETUP_DIR}\ISCC.exe" /DARCH=$script:TARGET_ARCH .\ollama.iss
  193. }
  194. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  195. }
  196. function distZip() {
  197. write-host "Generating stand-alone distribution zip file ${script:SRC_DIR}\dist\ollama-windows-${script:TARGET_ARCH}.zip"
  198. Compress-Archive -Path "${script:SRC_DIR}\dist\windows-${script:TARGET_ARCH}\*" -DestinationPath "${script:SRC_DIR}\dist\ollama-windows-${script:TARGET_ARCH}.zip" -Force
  199. }
  200. checkEnv
  201. try {
  202. if ($($args.count) -eq 0) {
  203. buildOllama
  204. buildApp
  205. gatherDependencies
  206. buildInstaller
  207. distZip
  208. } else {
  209. for ( $i = 0; $i -lt $args.count; $i++ ) {
  210. write-host "performing $($args[$i])"
  211. & $($args[$i])
  212. }
  213. }
  214. } catch {
  215. write-host "Build Failed"
  216. write-host $_
  217. } finally {
  218. set-location $script:SRC_DIR
  219. $env:PKG_VERSION=""
  220. }