build_windows.ps1 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. $script:NVIDIA_DIR=(get-item "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v*\bin\")[0]
  15. $script:INNO_SETUP_DIR=(get-item "C:\Program Files*\Inno Setup*\")[0]
  16. $script:DEPS_DIR="${script:SRC_DIR}\dist\windeps"
  17. $env:CGO_ENABLED="1"
  18. echo "Checking version"
  19. if (!$env:VERSION) {
  20. $data=(git describe --tags --first-parent --abbrev=7 --long --dirty --always)
  21. $pattern="v(.+)"
  22. if ($data -match $pattern) {
  23. $script:VERSION=$matches[1]
  24. }
  25. } else {
  26. $script:VERSION=$env:VERSION
  27. }
  28. $pattern = "(\d+[.]\d+[.]\d+)-(\d+)-"
  29. if ($script:VERSION -match $pattern) {
  30. $script:PKG_VERSION=$matches[1] + "." + $matches[2]
  31. } else {
  32. $script:PKG_VERSION=$script:VERSION
  33. }
  34. write-host "Building Ollama $script:VERSION with package version $script:PKG_VERSION"
  35. # Check for signing key
  36. if ("${env:KEY_CONTAINER}") {
  37. ${env:OLLAMA_CERT}=$(resolve-path "${script:SRC_DIR}\ollama_inc.crt")
  38. Write-host "Code signing enabled"
  39. # Note: 10 Windows Kit signtool crashes with GCP's plugin
  40. ${script:SignTool}="C:\Program Files (x86)\Windows Kits\8.1\bin\x64\signtool.exe"
  41. } else {
  42. write-host "Code signing disabled - please set KEY_CONTAINERS to sign and copy ollama_inc.crt to the top of the source tree"
  43. }
  44. }
  45. function buildOllama() {
  46. write-host "Building ollama CLI"
  47. & go generate ./...
  48. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  49. & go build "-ldflags=-w -s ""-X=github.com/jmorganca/ollama/version.Version=$script:VERSION"" ""-X=github.com/jmorganca/ollama/server.mode=release""" .
  50. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  51. if ("${env:KEY_CONTAINER}") {
  52. & "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${env:OLLAMA_CERT}" `
  53. /csp "Google Cloud KMS Provider" /kc ${env:KEY_CONTAINER} ollama.exe
  54. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  55. }
  56. }
  57. function buildApp() {
  58. write-host "Building Ollama App"
  59. cd "${script:SRC_DIR}\app"
  60. & windres -l 0 -o ollama.syso ollama.rc
  61. & go build "-ldflags=-H windowsgui -w -s ""-X=github.com/jmorganca/ollama/version.Version=$script:VERSION"" ""-X=github.com/jmorganca/ollama/server.mode=release""" .
  62. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  63. if ("${env:KEY_CONTAINER}") {
  64. & "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${env:OLLAMA_CERT}" `
  65. /csp "Google Cloud KMS Provider" /kc ${env:KEY_CONTAINER} app.exe
  66. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  67. }
  68. }
  69. function gatherDependencies() {
  70. write-host "Gathering runtime dependencies"
  71. cd "${script:SRC_DIR}"
  72. rm -ea 0 -recurse -force -path "${script:DEPS_DIR}"
  73. md "${script:DEPS_DIR}" -ea 0 > $null
  74. # TODO - this varies based on host build system and MSVC version - drive from dumpbin output
  75. # currently works for Win11 + MSVC 2019 + Cuda V11
  76. cp "${env:VCToolsRedistDir}\x64\Microsoft.VC*.CRT\msvcp140.dll" "${script:DEPS_DIR}\"
  77. cp "${env:VCToolsRedistDir}\x64\Microsoft.VC*.CRT\vcruntime140.dll" "${script:DEPS_DIR}\"
  78. cp "${env:VCToolsRedistDir}\x64\Microsoft.VC*.CRT\vcruntime140_1.dll" "${script:DEPS_DIR}\"
  79. cp "${script:NVIDIA_DIR}\cudart64_*.dll" "${script:DEPS_DIR}\"
  80. cp "${script:NVIDIA_DIR}\cublas64_*.dll" "${script:DEPS_DIR}\"
  81. cp "${script:NVIDIA_DIR}\cublasLt64_*.dll" "${script:DEPS_DIR}\"
  82. cp "${script:SRC_DIR}\app\ollama_welcome.ps1" "${script:SRC_DIR}\dist\"
  83. if ("${env:KEY_CONTAINER}") {
  84. write-host "about to sign"
  85. foreach ($file in (get-childitem "${script:DEPS_DIR}/cu*.dll") + @("${script:SRC_DIR}\dist\ollama_welcome.ps1")){
  86. write-host "signing $file"
  87. & "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${env:OLLAMA_CERT}" `
  88. /csp "Google Cloud KMS Provider" /kc ${env:KEY_CONTAINER} $file
  89. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  90. }
  91. }
  92. }
  93. function buildInstaller() {
  94. write-host "Building Ollama Installer"
  95. cd "${script:SRC_DIR}\app"
  96. $env:PKG_VERSION=$script:PKG_VERSION
  97. if ("${env:KEY_CONTAINER}") {
  98. & "${script:INNO_SETUP_DIR}\ISCC.exe" /SMySignTool="${script:SignTool} sign /fd sha256 /t http://timestamp.digicert.com /f ${env:OLLAMA_CERT} /csp `$qGoogle Cloud KMS Provider`$q /kc ${env:KEY_CONTAINER} `$f" .\ollama.iss
  99. } else {
  100. & "${script:INNO_SETUP_DIR}\ISCC.exe" .\ollama.iss
  101. }
  102. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  103. }
  104. try {
  105. checkEnv
  106. buildOllama
  107. buildApp
  108. gatherDependencies
  109. buildInstaller
  110. } catch {
  111. write-host "Build Failed"
  112. write-host $_
  113. } finally {
  114. set-location $script:SRC_DIR
  115. $env:PKG_VERSION=""
  116. }