build_windows.ps1 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. write-host "Locating required tools and paths"
  9. $script:SRC_DIR=$PWD
  10. if (!$env:VCToolsRedistDir) {
  11. $MSVC_INSTALL=(Get-CimInstance MSFT_VSInstance -Namespace root/cimv2/vs)[0].InstallLocation
  12. $env:VCToolsRedistDir=(get-item "${MSVC_INSTALL}\VC\Redist\MSVC\*")[0]
  13. }
  14. # Try to find the CUDA dir
  15. if ($null -eq $env:NVIDIA_DIR) {
  16. $d=(get-command -ea 'silentlycontinue' nvcc).path
  17. if ($d -ne $null) {
  18. $script:NVIDIA_DIR=($d| split-path -parent)
  19. } else {
  20. $cudaList=(get-item "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v*\bin\" -ea 'silentlycontinue')
  21. if ($cudaList.length > 0) {
  22. $script:NVIDIA_DIR=$cudaList[0]
  23. }
  24. }
  25. } else {
  26. $script:NVIDIA_DIR=$env:NVIDIA_DIR
  27. }
  28. $script:INNO_SETUP_DIR=(get-item "C:\Program Files*\Inno Setup*\")[0]
  29. $script:DEPS_DIR="${script:SRC_DIR}\dist\windeps"
  30. $env:CGO_ENABLED="1"
  31. echo "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. & go generate ./...
  65. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  66. } else {
  67. write-host "Skipping generate step with OLLAMA_SKIP_GENERATE set"
  68. }
  69. & go build -trimpath -ldflags "-s -w -X=github.com/ollama/ollama/version.Version=$script:VERSION -X=github.com/ollama/ollama/server.mode=release" .
  70. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  71. if ("${env:KEY_CONTAINER}") {
  72. & "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${script:OLLAMA_CERT}" `
  73. /csp "Google Cloud KMS Provider" /kc ${env:KEY_CONTAINER} ollama.exe
  74. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  75. }
  76. New-Item -ItemType Directory -Path .\dist -Force
  77. cp .\ollama.exe .\dist\ollama-windows-amd64.exe
  78. }
  79. function buildApp() {
  80. write-host "Building Ollama App"
  81. cd "${script:SRC_DIR}\app"
  82. & windres -l 0 -o ollama.syso ollama.rc
  83. & 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" .
  84. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  85. if ("${env:KEY_CONTAINER}") {
  86. & "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${script:OLLAMA_CERT}" `
  87. /csp "Google Cloud KMS Provider" /kc ${env:KEY_CONTAINER} app.exe
  88. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  89. }
  90. }
  91. function gatherDependencies() {
  92. write-host "Gathering runtime dependencies"
  93. cd "${script:SRC_DIR}"
  94. rm -ea 0 -recurse -force -path "${script:DEPS_DIR}"
  95. md "${script:DEPS_DIR}" -ea 0 > $null
  96. # TODO - this varies based on host build system and MSVC version - drive from dumpbin output
  97. # currently works for Win11 + MSVC 2019 + Cuda V11
  98. cp "${env:VCToolsRedistDir}\x64\Microsoft.VC*.CRT\msvcp140.dll" "${script:DEPS_DIR}\"
  99. cp "${env:VCToolsRedistDir}\x64\Microsoft.VC*.CRT\vcruntime140.dll" "${script:DEPS_DIR}\"
  100. cp "${env:VCToolsRedistDir}\x64\Microsoft.VC*.CRT\vcruntime140_1.dll" "${script:DEPS_DIR}\"
  101. cp "${script:NVIDIA_DIR}\cudart64_*.dll" "${script:DEPS_DIR}\"
  102. cp "${script:NVIDIA_DIR}\cublas64_*.dll" "${script:DEPS_DIR}\"
  103. cp "${script:NVIDIA_DIR}\cublasLt64_*.dll" "${script:DEPS_DIR}\"
  104. cp "${script:SRC_DIR}\app\ollama_welcome.ps1" "${script:SRC_DIR}\dist\"
  105. if ("${env:KEY_CONTAINER}") {
  106. write-host "about to sign"
  107. foreach ($file in (get-childitem "${script:DEPS_DIR}/cu*.dll") + @("${script:SRC_DIR}\dist\ollama_welcome.ps1")){
  108. write-host "signing $file"
  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} $file
  111. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  112. }
  113. }
  114. }
  115. function buildInstaller() {
  116. write-host "Building Ollama Installer"
  117. cd "${script:SRC_DIR}\app"
  118. $env:PKG_VERSION=$script:PKG_VERSION
  119. if ("${env:KEY_CONTAINER}") {
  120. & "${script:INNO_SETUP_DIR}\ISCC.exe" /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
  121. } else {
  122. & "${script:INNO_SETUP_DIR}\ISCC.exe" .\ollama.iss
  123. }
  124. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  125. }
  126. try {
  127. checkEnv
  128. buildOllama
  129. buildApp
  130. gatherDependencies
  131. buildInstaller
  132. } catch {
  133. write-host "Build Failed"
  134. write-host $_
  135. } finally {
  136. set-location $script:SRC_DIR
  137. $env:PKG_VERSION=""
  138. }