build_windows.ps1 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. & go build "-ldflags=-H windowsgui -w -s ""-X=github.com/jmorganca/ollama/version.Version=$script:VERSION"" ""-X=github.com/jmorganca/ollama/server.mode=release""" .
  61. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  62. if ("${env:KEY_CONTAINER}") {
  63. & "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${env:OLLAMA_CERT}" `
  64. /csp "Google Cloud KMS Provider" /kc ${env:KEY_CONTAINER} app.exe
  65. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  66. }
  67. }
  68. function gatherDependencies() {
  69. write-host "Gathering runtime dependencies"
  70. cd "${script:SRC_DIR}"
  71. rm -ea 0 -recurse -force -path "${script:DEPS_DIR}"
  72. md "${script:DEPS_DIR}" -ea 0 > $null
  73. # TODO - this varies based on host build system and MSVC version - drive from dumpbin output
  74. # currently works for Win11 + MSVC 2019 + Cuda V11
  75. cp "${env:VCToolsRedistDir}\x64\Microsoft.VC*.CRT\msvcp140.dll" "${script:DEPS_DIR}\"
  76. cp "${env:VCToolsRedistDir}\x64\Microsoft.VC*.CRT\vcruntime140.dll" "${script:DEPS_DIR}\"
  77. cp "${env:VCToolsRedistDir}\x64\Microsoft.VC*.CRT\vcruntime140_1.dll" "${script:DEPS_DIR}\"
  78. cp "${script:NVIDIA_DIR}\cudart64_*.dll" "${script:DEPS_DIR}\"
  79. cp "${script:NVIDIA_DIR}\cublas64_*.dll" "${script:DEPS_DIR}\"
  80. cp "${script:NVIDIA_DIR}\cublasLt64_*.dll" "${script:DEPS_DIR}\"
  81. cp "${script:SRC_DIR}\app\ollama_welcome.ps1" "${script:SRC_DIR}\dist\"
  82. if ("${env:KEY_CONTAINER}") {
  83. write-host "about to sign"
  84. foreach ($file in (get-childitem "${script:DEPS_DIR}/cu*.dll") + @("${script:SRC_DIR}\dist\ollama_welcome.ps1")){
  85. write-host "signing $file"
  86. & "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${env:OLLAMA_CERT}" `
  87. /csp "Google Cloud KMS Provider" /kc ${env:KEY_CONTAINER} $file
  88. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  89. }
  90. }
  91. }
  92. function buildInstaller() {
  93. write-host "Building Ollama Installer"
  94. cd "${script:SRC_DIR}\app"
  95. $env:PKG_VERSION=$script:PKG_VERSION
  96. if ("${env:KEY_CONTAINER}") {
  97. & "${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
  98. } else {
  99. & "${script:INNO_SETUP_DIR}\ISCC.exe" .\ollama.iss
  100. }
  101. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  102. }
  103. try {
  104. checkEnv
  105. buildOllama
  106. buildApp
  107. gatherDependencies
  108. buildInstaller
  109. } catch {
  110. write-host "Build Failed"
  111. write-host $_
  112. } finally {
  113. set-location $script:SRC_DIR
  114. $env:PKG_VERSION=""
  115. }