build_windows.ps1 6.5 KB

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