2023-12-27 00:03:45 +00:00
|
|
|
#!powershell
|
|
|
|
#
|
|
|
|
# powershell -ExecutionPolicy Bypass -File .\scripts\build_windows.ps1
|
|
|
|
#
|
|
|
|
# gcloud auth application-default login
|
|
|
|
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
|
|
|
|
function checkEnv() {
|
2024-07-12 21:33:13 +00:00
|
|
|
$script:ARCH = $Env:PROCESSOR_ARCHITECTURE.ToLower()
|
2024-04-27 05:26:03 +00:00
|
|
|
$script:TARGET_ARCH=$Env:PROCESSOR_ARCHITECTURE.ToLower()
|
|
|
|
Write-host "Building for ${script:TARGET_ARCH}"
|
2023-12-27 00:03:45 +00:00
|
|
|
write-host "Locating required tools and paths"
|
|
|
|
$script:SRC_DIR=$PWD
|
|
|
|
if (!$env:VCToolsRedistDir) {
|
|
|
|
$MSVC_INSTALL=(Get-CimInstance MSFT_VSInstance -Namespace root/cimv2/vs)[0].InstallLocation
|
|
|
|
$env:VCToolsRedistDir=(get-item "${MSVC_INSTALL}\VC\Redist\MSVC\*")[0]
|
|
|
|
}
|
2024-07-12 21:33:13 +00:00
|
|
|
# Locate CUDA versions
|
|
|
|
# Note: this assumes every version found will be built
|
|
|
|
$cudaList=(get-item "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v*\bin\" -ea 'silentlycontinue')
|
|
|
|
if ($cudaList.length -eq 0) {
|
2024-03-07 18:54:21 +00:00
|
|
|
$d=(get-command -ea 'silentlycontinue' nvcc).path
|
2024-07-12 21:33:13 +00:00
|
|
|
if ($null -ne $d) {
|
|
|
|
$script:CUDA_DIRS=@($d| split-path -parent)
|
2024-03-07 18:54:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-07-12 21:33:13 +00:00
|
|
|
$script:CUDA_DIRS=$cudaList
|
2024-03-07 18:54:21 +00:00
|
|
|
}
|
|
|
|
|
2023-12-27 00:03:45 +00:00
|
|
|
$script:INNO_SETUP_DIR=(get-item "C:\Program Files*\Inno Setup*\")[0]
|
|
|
|
|
2024-04-27 05:26:03 +00:00
|
|
|
$script:DEPS_DIR="${script:SRC_DIR}\dist\windows-${script:TARGET_ARCH}"
|
2023-12-27 00:03:45 +00:00
|
|
|
$env:CGO_ENABLED="1"
|
2024-07-12 21:33:13 +00:00
|
|
|
Write-Output "Checking version"
|
2023-12-27 00:03:45 +00:00
|
|
|
if (!$env:VERSION) {
|
|
|
|
$data=(git describe --tags --first-parent --abbrev=7 --long --dirty --always)
|
|
|
|
$pattern="v(.+)"
|
|
|
|
if ($data -match $pattern) {
|
|
|
|
$script:VERSION=$matches[1]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$script:VERSION=$env:VERSION
|
|
|
|
}
|
2024-03-07 18:54:21 +00:00
|
|
|
$pattern = "(\d+[.]\d+[.]\d+).*"
|
2023-12-27 00:03:45 +00:00
|
|
|
if ($script:VERSION -match $pattern) {
|
2024-03-07 18:54:21 +00:00
|
|
|
$script:PKG_VERSION=$matches[1]
|
2023-12-27 00:03:45 +00:00
|
|
|
} else {
|
2024-03-07 18:54:21 +00:00
|
|
|
$script:PKG_VERSION="0.0.0"
|
2023-12-27 00:03:45 +00:00
|
|
|
}
|
|
|
|
write-host "Building Ollama $script:VERSION with package version $script:PKG_VERSION"
|
|
|
|
|
2024-03-07 18:54:21 +00:00
|
|
|
# Note: Windows Kits 10 signtool crashes with GCP's plugin
|
|
|
|
if ($null -eq $env:SIGN_TOOL) {
|
|
|
|
${script:SignTool}="C:\Program Files (x86)\Windows Kits\8.1\bin\x64\signtool.exe"
|
|
|
|
} else {
|
|
|
|
${script:SignTool}=${env:SIGN_TOOL}
|
|
|
|
}
|
2023-12-27 00:03:45 +00:00
|
|
|
if ("${env:KEY_CONTAINER}") {
|
2024-02-16 23:33:16 +00:00
|
|
|
${script:OLLAMA_CERT}=$(resolve-path "${script:SRC_DIR}\ollama_inc.crt")
|
2023-12-27 00:03:45 +00:00
|
|
|
Write-host "Code signing enabled"
|
|
|
|
} else {
|
|
|
|
write-host "Code signing disabled - please set KEY_CONTAINERS to sign and copy ollama_inc.crt to the top of the source tree"
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function buildOllama() {
|
|
|
|
write-host "Building ollama CLI"
|
2024-03-07 18:54:21 +00:00
|
|
|
if ($null -eq ${env:OLLAMA_SKIP_GENERATE}) {
|
2024-07-12 21:33:13 +00:00
|
|
|
Remove-Item -ea 0 -recurse -force -path "${script:SRC_DIR}\dist\windows-${script:ARCH}"
|
|
|
|
|
|
|
|
# TODO - consider trying to parallelize this with Start-ThreadJob, but env vars can't be used to toggle
|
|
|
|
# which targets to build
|
|
|
|
|
|
|
|
# Start by skipping CUDA to build everything else
|
|
|
|
pwsh -Command { $env:OLLAMA_SKIP_CUDA_GENERATE="1"; & go generate ./... }
|
|
|
|
if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
|
|
|
|
|
|
|
|
# Then skip everyhting else and build all the CUDA variants
|
|
|
|
foreach ($env:CUDA_LIB_DIR in $script:CUDA_DIRS) {
|
|
|
|
write-host "Building CUDA ${env:CUDA_LIB_DIR}"
|
|
|
|
|
|
|
|
if ($env:CUDA_LIB_DIR.Contains("v12")) {
|
|
|
|
pwsh -Command {
|
|
|
|
$env:OLLAMA_SKIP_CUDA_GENERATE=""
|
|
|
|
$env:OLLAMA_SKIP_STATIC_GENERATE="1"
|
|
|
|
$env:OLLAMA_SKIP_CPU_GENERATE="1"
|
|
|
|
$env:OLLAMA_SKIP_ONEAPI_GENERATE="1"
|
|
|
|
$env:OLLAMA_SKIP_ROCM_GENERATE="1"
|
|
|
|
$env:CMAKE_CUDA_ARCHITECTURES="60;61;62;70;72;75;80;86;87;89;90;90a"
|
|
|
|
$env:OLLAMA_CUSTOM_CUDA_DEFS="-DGGML_CUDA_USE_GRAPHS=on"
|
|
|
|
$env:CUDA_PATH=split-path -path $env:CUDA_LIB_DIR -parent
|
|
|
|
$env:PATH="$envs:CUDA_LIB_DIR;$env:PATH"
|
|
|
|
& go generate ./...
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pwsh -Command {
|
|
|
|
$env:OLLAMA_SKIP_CUDA_GENERATE=""
|
|
|
|
$env:OLLAMA_SKIP_STATIC_GENERATE="1"
|
|
|
|
$env:OLLAMA_SKIP_CPU_GENERATE="1"
|
|
|
|
$env:OLLAMA_SKIP_ONEAPI_GENERATE="1"
|
|
|
|
$env:OLLAMA_SKIP_ROCM_GENERATE="1"
|
|
|
|
$env:CMAKE_CUDA_ARCHITECTURES="50;52;53;60;61;62;70;72;75;80;86"
|
|
|
|
$env:OLLAMA_CUSTOM_CUDA_DEFS=""
|
|
|
|
$env:CUDA_PATH=split-path -path $env:CUDA_LIB_DIR -parent
|
|
|
|
$env:PATH="$envs:CUDA_LIB_DIR;$env:PATH"
|
|
|
|
& go generate ./...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
|
|
|
|
}
|
2024-03-07 18:54:21 +00:00
|
|
|
if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
|
|
|
|
} else {
|
|
|
|
write-host "Skipping generate step with OLLAMA_SKIP_GENERATE set"
|
|
|
|
}
|
2024-03-26 20:04:17 +00:00
|
|
|
& go build -trimpath -ldflags "-s -w -X=github.com/ollama/ollama/version.Version=$script:VERSION -X=github.com/ollama/ollama/server.mode=release" .
|
2023-12-27 00:03:45 +00:00
|
|
|
if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
|
|
|
|
if ("${env:KEY_CONTAINER}") {
|
2024-02-16 23:33:16 +00:00
|
|
|
& "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${script:OLLAMA_CERT}" `
|
2023-12-27 00:03:45 +00:00
|
|
|
/csp "Google Cloud KMS Provider" /kc ${env:KEY_CONTAINER} ollama.exe
|
|
|
|
if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
|
|
|
|
}
|
2024-08-14 23:32:57 +00:00
|
|
|
New-Item -ItemType Directory -Path .\dist\windows-${script:TARGET_ARCH}\bin\ -Force
|
|
|
|
cp .\ollama.exe .\dist\windows-${script:TARGET_ARCH}\bin\
|
2023-12-27 00:03:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function buildApp() {
|
|
|
|
write-host "Building Ollama App"
|
|
|
|
cd "${script:SRC_DIR}\app"
|
2024-02-15 00:10:26 +00:00
|
|
|
& windres -l 0 -o ollama.syso ollama.rc
|
2024-03-26 20:04:17 +00:00
|
|
|
& 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" .
|
2023-12-27 00:03:45 +00:00
|
|
|
if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
|
|
|
|
if ("${env:KEY_CONTAINER}") {
|
2024-02-16 23:33:16 +00:00
|
|
|
& "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${script:OLLAMA_CERT}" `
|
2023-12-27 00:03:45 +00:00
|
|
|
/csp "Google Cloud KMS Provider" /kc ${env:KEY_CONTAINER} app.exe
|
|
|
|
if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function gatherDependencies() {
|
|
|
|
write-host "Gathering runtime dependencies"
|
|
|
|
cd "${script:SRC_DIR}"
|
2024-08-14 23:32:57 +00:00
|
|
|
md "${script:DEPS_DIR}\lib\ollama" -ea 0 > $null
|
2023-12-27 00:03:45 +00:00
|
|
|
|
|
|
|
# TODO - this varies based on host build system and MSVC version - drive from dumpbin output
|
|
|
|
# currently works for Win11 + MSVC 2019 + Cuda V11
|
2024-08-14 23:32:57 +00:00
|
|
|
cp "${env:VCToolsRedistDir}\x64\Microsoft.VC*.CRT\msvcp140*.dll" "${script:DEPS_DIR}\lib\ollama\"
|
|
|
|
cp "${env:VCToolsRedistDir}\x64\Microsoft.VC*.CRT\vcruntime140.dll" "${script:DEPS_DIR}\lib\ollama\"
|
|
|
|
cp "${env:VCToolsRedistDir}\x64\Microsoft.VC*.CRT\vcruntime140_1.dll" "${script:DEPS_DIR}\lib\ollama\"
|
2024-07-09 01:24:21 +00:00
|
|
|
foreach ($part in $("runtime", "stdio", "filesystem", "math", "convert", "heap", "string", "time", "locale", "environment")) {
|
2024-08-14 23:32:57 +00:00
|
|
|
cp "$env:VCToolsRedistDir\..\..\..\Tools\Llvm\x64\bin\api-ms-win-crt-${part}*.dll" "${script:DEPS_DIR}\lib\ollama\"
|
2024-07-09 01:24:21 +00:00
|
|
|
}
|
2023-12-27 00:03:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
cp "${script:SRC_DIR}\app\ollama_welcome.ps1" "${script:SRC_DIR}\dist\"
|
|
|
|
if ("${env:KEY_CONTAINER}") {
|
|
|
|
write-host "about to sign"
|
2024-08-14 23:32:57 +00:00
|
|
|
foreach ($file in (get-childitem "${script:DEPS_DIR}\lib\ollama\cu*.dll") + @("${script:SRC_DIR}\dist\ollama_welcome.ps1")){
|
2023-12-27 00:03:45 +00:00
|
|
|
write-host "signing $file"
|
2024-02-16 23:33:16 +00:00
|
|
|
& "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${script:OLLAMA_CERT}" `
|
2023-12-27 00:03:45 +00:00
|
|
|
/csp "Google Cloud KMS Provider" /kc ${env:KEY_CONTAINER} $file
|
|
|
|
if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildInstaller() {
|
|
|
|
write-host "Building Ollama Installer"
|
|
|
|
cd "${script:SRC_DIR}\app"
|
|
|
|
$env:PKG_VERSION=$script:PKG_VERSION
|
|
|
|
if ("${env:KEY_CONTAINER}") {
|
2024-04-27 05:35:16 +00:00
|
|
|
& "${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
|
2023-12-27 00:03:45 +00:00
|
|
|
} else {
|
2024-04-27 05:35:16 +00:00
|
|
|
& "${script:INNO_SETUP_DIR}\ISCC.exe" /DARCH=$script:TARGET_ARCH .\ollama.iss
|
2023-12-27 00:03:45 +00:00
|
|
|
}
|
|
|
|
if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
|
|
|
|
}
|
|
|
|
|
2024-04-23 19:19:17 +00:00
|
|
|
function distZip() {
|
2024-04-27 05:26:03 +00:00
|
|
|
write-host "Generating stand-alone distribution zip file ${script:SRC_DIR}\dist\ollama-windows-${script:TARGET_ARCH}.zip"
|
|
|
|
Compress-Archive -Path "${script:SRC_DIR}\dist\windows-${script:TARGET_ARCH}\*" -DestinationPath "${script:SRC_DIR}\dist\ollama-windows-${script:TARGET_ARCH}.zip" -Force
|
2024-04-23 19:19:17 +00:00
|
|
|
}
|
|
|
|
|
2023-12-27 00:03:45 +00:00
|
|
|
try {
|
|
|
|
checkEnv
|
|
|
|
buildOllama
|
|
|
|
buildApp
|
|
|
|
gatherDependencies
|
|
|
|
buildInstaller
|
2024-04-23 19:19:17 +00:00
|
|
|
distZip
|
2023-12-27 00:03:45 +00:00
|
|
|
} catch {
|
|
|
|
write-host "Build Failed"
|
|
|
|
write-host $_
|
|
|
|
} finally {
|
|
|
|
set-location $script:SRC_DIR
|
|
|
|
$env:PKG_VERSION=""
|
2024-02-22 22:41:43 +00:00
|
|
|
}
|