build_windows.ps1 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\windows-amd64"
  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\windows-amd64\ -Force
  77. cp .\ollama.exe .\dist\windows-amd64\
  78. }
  79. function buildApp() {
  80. write-host "Building Ollama App"
  81. cd "${script:SRC_DIR}\app"
  82. & windres -l 0 -o ollama.syso windows\ollama.rc
  83. & go build -trimpath -ldflags "-s -w -H windowsgui -X=github.com/jmorganca/ollama/version.Version=$script:VERSION -X=github.com/jmorganca/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. md "${script:DEPS_DIR}" -ea 0 > $null
  95. # TODO - this varies based on host build system and MSVC version - drive from dumpbin output
  96. # currently works for Win11 + MSVC 2019 + Cuda V11
  97. cp "${env:VCToolsRedistDir}\x64\Microsoft.VC*.CRT\msvcp140.dll" "${script:DEPS_DIR}\"
  98. cp "${env:VCToolsRedistDir}\x64\Microsoft.VC*.CRT\vcruntime140.dll" "${script:DEPS_DIR}\"
  99. cp "${env:VCToolsRedistDir}\x64\Microsoft.VC*.CRT\vcruntime140_1.dll" "${script:DEPS_DIR}\"
  100. cp "${script:SRC_DIR}\app\windows\ollama_welcome.ps1" "${script:SRC_DIR}\dist\"
  101. if ("${env:KEY_CONTAINER}") {
  102. write-host "about to sign"
  103. foreach ($file in (get-childitem "${script:DEPS_DIR}/cu*.dll") + @("${script:SRC_DIR}\dist\ollama_welcome.ps1")){
  104. write-host "signing $file"
  105. & "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${script:OLLAMA_CERT}" `
  106. /csp "Google Cloud KMS Provider" /kc ${env:KEY_CONTAINER} $file
  107. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  108. }
  109. }
  110. }
  111. function buildInstaller() {
  112. write-host "Building Ollama Installer"
  113. cd "${script:SRC_DIR}\app"
  114. $env:PKG_VERSION=$script:PKG_VERSION
  115. if ("${env:KEY_CONTAINER}") {
  116. & "${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" .\windows\ollama.iss
  117. } else {
  118. & "${script:INNO_SETUP_DIR}\ISCC.exe" .\windows\ollama.iss
  119. }
  120. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  121. }
  122. function distZip() {
  123. write-host "Generating stand-alone distribution zip file ${script:SRC_DIR}\dist\ollama-windows-amd64.zip"
  124. Compress-Archive -Path "${script:SRC_DIR}\dist\windows-amd64\*" -DestinationPath "${script:SRC_DIR}\dist\ollama-windows-amd64.zip" -Force
  125. }
  126. try {
  127. checkEnv
  128. buildOllama
  129. buildApp
  130. gatherDependencies
  131. buildInstaller
  132. distZip
  133. } catch {
  134. write-host "Build Failed"
  135. write-host $_
  136. } finally {
  137. set-location $script:SRC_DIR
  138. $env:PKG_VERSION=""
  139. }