Files
ANSCORE/tools/bump_ort_sdk.ps1
2026-06-18 05:55:31 +10:00

132 lines
5.9 KiB
PowerShell

<#
.SYNOPSIS
Bump the link-time ONNX Runtime SDK in C:\ANSLibs\onnxruntime to a new version
(e.g. 1.23 -> 1.24), safely and reversibly. ANSCORE compiles against this folder.
.DESCRIPTION
Replaces C:\ANSLibs\onnxruntime\{include,lib} + the runtime<ver>_openvino<ver>.txt
marker with a new ORT SDK, after validating the new SDK and backing up the old one.
It does NOT touch the runtime DLLs in SharedCore\ep - those are a separate step.
ANSCORE only #includes onnxruntime_cxx_api.h (the EP-agnostic C++ API), so any
matching ORT build's include\ works (GPU build's headers are fine).
SEQUENCING NOTE: bumping the link header to API N makes any runtime EP folder
whose core is < N a latent risk. Unify the runtime (incl. the OpenVINO EP) to the
target version FIRST, then run this. After running, rebuild ANSCORE and validate
the runtime with: tools\verify_ep.ps1 -RequiredApi <N>
.PARAMETER SdkPath
Folder of the NEW ORT SDK. Must contain:
include\onnxruntime_c_api.h (with the new ORT_API_VERSION)
lib\onnxruntime.lib
Typically the unzipped onnxruntime-win-x64-gpu-<ver> release, or an Intel
OpenVINO ORT build of the same version.
.PARAMETER AnsLibs
ANSLibs root. Default: C:\ANSLibs
.PARAMETER Marker
New marker filename to drop in (documents the version, mirrors the existing
'runtime1.23.0_openvino2025.3.0.txt'). Optional - if omitted, no marker change.
.PARAMETER WhatIf
Show what would happen without changing anything.
.EXAMPLE
# Dry run first
.\bump_ort_sdk.ps1 -SdkPath D:\dl\onnxruntime-win-x64-gpu-1.24.0 `
-Marker runtime1.24.0_openvino2025.3.0.txt -WhatIf
# Then for real
.\bump_ort_sdk.ps1 -SdkPath D:\dl\onnxruntime-win-x64-gpu-1.24.0 `
-Marker runtime1.24.0_openvino2025.3.0.txt
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[Parameter(Mandatory = $true)][string] $SdkPath,
[string] $AnsLibs = "C:\ANSLibs",
[string] $Marker
)
$ErrorActionPreference = "Stop"
function Get-OrtApiFromHeader([string]$header) {
if (-not (Test-Path -LiteralPath $header)) { return $null }
$line = Select-String -LiteralPath $header -Pattern 'define\s+ORT_API_VERSION\s+(\d+)' | Select-Object -First 1
if ($line) { return [int]$line.Matches[0].Groups[1].Value }
return $null
}
if (-not (Test-Path -LiteralPath $SdkPath)) {
Write-Host "FAIL - SdkPath not found: $SdkPath" -ForegroundColor Red
Write-Host "Point -SdkPath at an unzipped ORT release folder (with include\ and lib\)." -ForegroundColor Yellow
exit 2
}
$ortDir = Join-Path $AnsLibs 'onnxruntime'
$newInc = Join-Path $SdkPath 'include'
$newLib = Join-Path $SdkPath 'lib'
$newHdr = Join-Path $newInc 'onnxruntime_c_api.h'
# ---- Validate NEW sdk -------------------------------------------------------
Write-Host "== Validating new SDK at: $SdkPath =="
$problems = @()
if (-not (Test-Path -LiteralPath $newHdr)) { $problems += "missing include\onnxruntime_c_api.h" }
if (-not (Test-Path -LiteralPath (Join-Path $newInc 'onnxruntime_cxx_api.h'))) { $problems += "missing include\onnxruntime_cxx_api.h" }
if (-not (Test-Path -LiteralPath (Join-Path $newLib 'onnxruntime.lib'))) { $problems += "missing lib\onnxruntime.lib" }
if ($problems) {
Write-Host "FAIL - new SDK is incomplete:" -ForegroundColor Red
$problems | ForEach-Object { Write-Host " - $_" -ForegroundColor Red }
Write-Host "Point -SdkPath at an unzipped ORT release that has include\ and lib\." -ForegroundColor Yellow
exit 2
}
$newApi = Get-OrtApiFromHeader $newHdr
$oldApi = Get-OrtApiFromHeader (Join-Path $ortDir 'include\onnxruntime_c_api.h')
$newApiDisp = if ($newApi) { $newApi } else { '??' }
$oldApiDisp = if ($oldApi) { $oldApi } else { 'none' }
Write-Host (" new ORT_API_VERSION : {0}" -f $newApiDisp)
Write-Host (" current (ANSLibs) : {0}" -f $oldApiDisp)
if ($newApi -and $oldApi -and $newApi -le $oldApi) {
Write-Host ("WARNING: new API ({0}) is not greater than current ({1}). Continue only if intentional." -f $newApi,$oldApi) -ForegroundColor Yellow
}
# ---- Back up CURRENT onnxruntime SDK ---------------------------------------
$stamp = (Get-Date -Format 'yyyyMMdd_HHmmss')
$backup = Join-Path $AnsLibs ("onnxruntime.bak_$stamp")
Write-Host ""
Write-Host "== Backing up current SDK -> $backup =="
if ($PSCmdlet.ShouldProcess($ortDir, "copy to $backup")) {
Copy-Item -LiteralPath $ortDir -Destination $backup -Recurse -Force
}
# ---- Swap include\ and lib\ -------------------------------------------------
Write-Host "== Installing new include\ and lib\ =="
foreach ($sub in @('include','lib')) {
$dst = Join-Path $ortDir $sub
$src = Join-Path $SdkPath $sub
if ($PSCmdlet.ShouldProcess($dst, "replace from $src")) {
if (Test-Path -LiteralPath $dst) { Remove-Item -LiteralPath $dst -Recurse -Force }
Copy-Item -LiteralPath $src -Destination $dst -Recurse -Force
}
}
# ---- Marker file ------------------------------------------------------------
if ($Marker) {
Write-Host "== Updating version marker -> $Marker =="
if ($PSCmdlet.ShouldProcess($ortDir, "swap marker to $Marker")) {
Get-ChildItem -LiteralPath $ortDir -Filter 'runtime*.txt' -File -ErrorAction SilentlyContinue |
ForEach-Object { Remove-Item -LiteralPath $_.FullName -Force }
New-Item -ItemType File -Path (Join-Path $ortDir $Marker) -Force | Out-Null
}
}
Write-Host ""
Write-Host "== DONE ==" -ForegroundColor Green
Write-Host "Next steps:" -ForegroundColor Cyan
$apiForSteps = if ($newApi) { $newApi } else { 'N' }
Write-Host (" 1. Clean-rebuild ANSCORE (it now compiles against ORT_API_VERSION {0})." -f $apiForSteps)
Write-Host (" 2. Validate the runtime: tools\verify_ep.ps1 -RequiredApi {0}" -f $apiForSteps)
Write-Host " (the openvino EP folder must also be at the new version or it will FAIL.)"
Write-Host (" 3. If anything breaks, revert: Remove-Item '$ortDir' -Recurse -Force; Rename-Item '$backup' 'onnxruntime'")