build_windows.ps1 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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:ARCH = (([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture).ToString().ToLower()).Replace("x64", "amd64")
  9. $script:TARGET_ARCH=$script:ARCH
  10. Write-host "Building for ${script:TARGET_ARCH}"
  11. write-host "Locating required tools and paths"
  12. $script:SRC_DIR=$PWD
  13. if ($null -eq $env:VCToolsRedistDir) {
  14. $MSVC_INSTALL=(Get-CimInstance MSFT_VSInstance -Namespace root/cimv2/vs)[0].InstallLocation
  15. $env:VCToolsRedistDir=(get-item "${MSVC_INSTALL}\VC\Redist\MSVC\*")[0]
  16. }
  17. # Locate CUDA versions
  18. # Note: this assumes every version found will be built
  19. $cudaList=(get-item "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v*\bin\" -ea 'silentlycontinue')
  20. if ($cudaList.length -eq 0) {
  21. $d=(get-command -ea 'silentlycontinue' nvcc).path
  22. if ($null -ne $d) {
  23. $script:CUDA_DIRS=@($d| split-path -parent)
  24. }
  25. } else {
  26. $script:CUDA_DIRS=$cudaList
  27. }
  28. $inoSetup=(get-item "C:\Program Files*\Inno Setup*\")
  29. if ($inoSetup.length -gt 0) {
  30. $script:INNO_SETUP_DIR=$inoSetup[0]
  31. }
  32. $script:DIST_DIR="${script:SRC_DIR}\dist\windows-${script:TARGET_ARCH}"
  33. $env:CGO_ENABLED="1"
  34. Write-Output "Checking version"
  35. if (!$env:VERSION) {
  36. $data=(git describe --tags --first-parent --abbrev=7 --long --dirty --always)
  37. $pattern="v(.+)"
  38. if ($data -match $pattern) {
  39. $script:VERSION=$matches[1]
  40. }
  41. } else {
  42. $script:VERSION=$env:VERSION
  43. }
  44. $pattern = "(\d+[.]\d+[.]\d+).*"
  45. if ($script:VERSION -match $pattern) {
  46. $script:PKG_VERSION=$matches[1]
  47. } else {
  48. $script:PKG_VERSION="0.0.0"
  49. }
  50. write-host "Building Ollama $script:VERSION with package version $script:PKG_VERSION"
  51. # Note: Windows Kits 10 signtool crashes with GCP's plugin
  52. if ($null -eq $env:SIGN_TOOL) {
  53. ${script:SignTool}="C:\Program Files (x86)\Windows Kits\8.1\bin\x64\signtool.exe"
  54. } else {
  55. ${script:SignTool}=${env:SIGN_TOOL}
  56. }
  57. if ("${env:KEY_CONTAINER}") {
  58. ${script:OLLAMA_CERT}=$(resolve-path "${script:SRC_DIR}\ollama_inc.crt")
  59. Write-host "Code signing enabled"
  60. } else {
  61. write-host "Code signing disabled - please set KEY_CONTAINERS to sign and copy ollama_inc.crt to the top of the source tree"
  62. }
  63. }
  64. function buildOllama() {
  65. write-host "Building ollama CLI"
  66. if ($null -eq ${env:OLLAMA_SKIP_GENERATE}) {
  67. Remove-Item -ea 0 -recurse -force -path "${script:SRC_DIR}\dist\windows-${script:ARCH}"
  68. # TODO - consider trying to parallelize this with Start-ThreadJob, but env vars can't be used to toggle
  69. # which targets to build
  70. # Start by skipping CUDA to build everything else
  71. pwsh -Command { $env:OLLAMA_SKIP_CUDA_GENERATE="1"; & go generate ./... }
  72. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  73. # Then skip everyhting else and build all the CUDA variants
  74. foreach ($env:CUDA_LIB_DIR in $script:CUDA_DIRS) {
  75. write-host "Building CUDA ${env:CUDA_LIB_DIR}"
  76. if ($env:CUDA_LIB_DIR.Contains("v12")) {
  77. pwsh -Command {
  78. $env:OLLAMA_SKIP_CUDA_GENERATE=""
  79. $env:OLLAMA_SKIP_STATIC_GENERATE="1"
  80. $env:OLLAMA_SKIP_CPU_GENERATE="1"
  81. $env:OLLAMA_SKIP_ONEAPI_GENERATE="1"
  82. $env:OLLAMA_SKIP_ROCM_GENERATE="1"
  83. $env:CMAKE_CUDA_ARCHITECTURES="60;61;62;70;72;75;80;86;87;89;90;90a"
  84. $env:OLLAMA_CUSTOM_CUDA_DEFS="-DGGML_CUDA_USE_GRAPHS=on"
  85. $env:CUDA_PATH=split-path -path $env:CUDA_LIB_DIR -parent
  86. $env:PATH="$envs:CUDA_LIB_DIR;$env:PATH"
  87. & go generate ./...
  88. }
  89. } else {
  90. pwsh -Command {
  91. $env:OLLAMA_SKIP_CUDA_GENERATE=""
  92. $env:OLLAMA_SKIP_STATIC_GENERATE="1"
  93. $env:OLLAMA_SKIP_CPU_GENERATE="1"
  94. $env:OLLAMA_SKIP_ONEAPI_GENERATE="1"
  95. $env:OLLAMA_SKIP_ROCM_GENERATE="1"
  96. $env:CMAKE_CUDA_ARCHITECTURES="50;52;53;60;61;62;70;72;75;80;86"
  97. $env:OLLAMA_CUSTOM_CUDA_DEFS=""
  98. $env:CUDA_PATH=split-path -path $env:CUDA_LIB_DIR -parent
  99. $env:PATH="$envs:CUDA_LIB_DIR;$env:PATH"
  100. & go generate ./...
  101. }
  102. }
  103. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  104. }
  105. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  106. } else {
  107. write-host "Skipping generate step with OLLAMA_SKIP_GENERATE set"
  108. }
  109. & go build -trimpath -ldflags "-s -w -X=github.com/ollama/ollama/version.Version=$script:VERSION -X=github.com/ollama/ollama/server.mode=release" .
  110. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  111. if ("${env:KEY_CONTAINER}") {
  112. & "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${script:OLLAMA_CERT}" `
  113. /csp "Google Cloud KMS Provider" /kc ${env:KEY_CONTAINER} ollama.exe
  114. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  115. }
  116. New-Item -ItemType Directory -Path .\dist\windows-${script:TARGET_ARCH}\ -Force
  117. cp .\ollama.exe .\dist\windows-${script:TARGET_ARCH}\
  118. }
  119. function buildApp() {
  120. write-host "Building Ollama App"
  121. cd "${script:SRC_DIR}\app"
  122. & windres -l 0 -o ollama.syso ollama.rc
  123. & 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" -o "${script:SRC_DIR}\dist\windows-${script:TARGET_ARCH}-app.exe" .
  124. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  125. if ("${env:KEY_CONTAINER}") {
  126. & "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${script:OLLAMA_CERT}" `
  127. /csp "Google Cloud KMS Provider" /kc ${env:KEY_CONTAINER} app.exe
  128. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  129. }
  130. }
  131. function gatherDependencies() {
  132. if ($null -eq $env:VCToolsRedistDir) {
  133. write-error "Unable to locate VC Install location - please use a Developer shell"
  134. exit 1
  135. }
  136. write-host "Gathering runtime dependencies from $env:VCToolsRedistDir"
  137. cd "${script:SRC_DIR}"
  138. md "${script:DIST_DIR}\lib\ollama" -ea 0 > $null
  139. # TODO - this varies based on host build system and MSVC version - drive from dumpbin output
  140. # currently works for Win11 + MSVC 2019 + Cuda V11
  141. if ($script:TARGET_ARCH -eq "amd64") {
  142. $depArch="x64"
  143. } else {
  144. $depArch=$script:TARGET_ARCH
  145. }
  146. if ($depArch -eq "amd64") {
  147. cp "${env:VCToolsRedistDir}\${depArch}\Microsoft.VC*.CRT\msvcp140*.dll" "${script:DIST_DIR}\lib\ollama\"
  148. cp "${env:VCToolsRedistDir}\${depArch}\Microsoft.VC*.CRT\vcruntime140.dll" "${script:DIST_DIR}\lib\ollama\"
  149. cp "${env:VCToolsRedistDir}\${depArch}\Microsoft.VC*.CRT\vcruntime140_1.dll" "${script:DIST_DIR}\lib\ollama\"
  150. $llvmCrtDir="$env:VCToolsRedistDir\..\..\..\Tools\Llvm\${depArch}\bin"
  151. foreach ($part in $("runtime", "stdio", "filesystem", "math", "convert", "heap", "string", "time", "locale", "environment")) {
  152. write-host "cp ${llvmCrtDir}\api-ms-win-crt-${part}*.dll ${script:DIST_DIR}\lib\ollama\"
  153. cp "${llvmCrtDir}\api-ms-win-crt-${part}*.dll" "${script:DIST_DIR}\lib\ollama\"
  154. }
  155. } else {
  156. # Carying the dll's doesn't seem to work, so use the redist installer
  157. copy-item -path "${env:VCToolsRedistDir}\vc_redist.arm64.exe" -destination "${script:DIST_DIR}" -verbose
  158. }
  159. cp "${script:SRC_DIR}\app\ollama_welcome.ps1" "${script:SRC_DIR}\dist\"
  160. if ("${env:KEY_CONTAINER}") {
  161. write-host "about to sign"
  162. foreach ($file in (get-childitem "${script:DIST_DIR}\lib\ollama\cu*.dll") + @("${script:SRC_DIR}\dist\ollama_welcome.ps1")){
  163. write-host "signing $file"
  164. & "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${script:OLLAMA_CERT}" `
  165. /csp "Google Cloud KMS Provider" /kc ${env:KEY_CONTAINER} $file
  166. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  167. }
  168. }
  169. }
  170. function buildInstaller() {
  171. if ($null -eq ${script:INNO_SETUP_DIR}) {
  172. write-host "Inno Setup not present, skipping installer build"
  173. return
  174. }
  175. write-host "Building Ollama Installer"
  176. cd "${script:SRC_DIR}\app"
  177. $env:PKG_VERSION=$script:PKG_VERSION
  178. if ("${env:KEY_CONTAINER}") {
  179. & "${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
  180. } else {
  181. & "${script:INNO_SETUP_DIR}\ISCC.exe" /DARCH=$script:TARGET_ARCH .\ollama.iss
  182. }
  183. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  184. }
  185. function distZip() {
  186. write-host "Generating stand-alone distribution zip file ${script:SRC_DIR}\dist\ollama-windows-${script:TARGET_ARCH}.zip"
  187. Compress-Archive -Path "${script:SRC_DIR}\dist\windows-${script:TARGET_ARCH}\*" -DestinationPath "${script:SRC_DIR}\dist\ollama-windows-${script:TARGET_ARCH}.zip" -Force
  188. }
  189. checkEnv
  190. try {
  191. if ($($args.count) -eq 0) {
  192. buildOllama
  193. buildApp
  194. gatherDependencies
  195. buildInstaller
  196. distZip
  197. } else {
  198. for ( $i = 0; $i -lt $args.count; $i++ ) {
  199. write-host "performing $($args[$i])"
  200. & $($args[$i])
  201. }
  202. }
  203. } catch {
  204. write-host "Build Failed"
  205. write-host $_
  206. } finally {
  207. set-location $script:SRC_DIR
  208. $env:PKG_VERSION=""
  209. }