Skip to content

Commit 6ddf806

Browse files
committed
init commit
1 parent 010bf94 commit 6ddf806

File tree

5 files changed

+349
-1
lines changed

5 files changed

+349
-1
lines changed

OnnxRuntimeBuilder.ps1

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
Add-Type -AssemblyName System.Windows.Forms # Required for file dialog
2+
3+
# Global configuration file path
4+
$configFilePath = "pythonpath.txt"
5+
6+
# Custom function to display the menu (replace with your actual menu display logic if needed)
7+
function Show-Menu {
8+
param (
9+
[string]$Title
10+
)
11+
Clear-Host
12+
Write-Host "====== $Title ======"
13+
# Write-Host "1: Build ONNX Runtime version 1.14.1 MinSizeRelease"
14+
Write-Host "2: Build ONNX Runtime version 1.14.1 Debug"
15+
# Write-Host "3: Build ONNX Runtime version 1.16.2 MinSizeRelease"
16+
Write-Host "4: Build ONNX Runtime version 1.16.2 Debug"
17+
Write-Host "===================="
18+
}
19+
20+
# Check if the path points to python.exe and the file exists
21+
function IsValidPythonPath($path) {
22+
return ($path -like "*python.exe") -and (Test-Path $path)
23+
}
24+
25+
# Get the Python path, either from a config file or user input
26+
function GetCustomPythonPath($minVersion = 308, $maxVersion = 311) {
27+
if (Test-Path $configFilePath) {
28+
$path = Get-Content $configFilePath
29+
if (IsValidPythonPath $path) {
30+
Write-Host "Found Python path in pythonpath.txt"
31+
return $path
32+
}
33+
}
34+
35+
# add dots to python version
36+
$minVersionStr = $minVersion -replace '(\d{1})(\d{2})', '$1.$2'
37+
$maxVersionStr = $maxVersion -replace '(\d{1})(\d{2})', '$1.$2'
38+
$dialogue = "Select Python $minVersionStr-$maxVersionStr executable"
39+
40+
$fileDialog = New-Object System.Windows.Forms.OpenFileDialog
41+
$fileDialog.Filter = "Python Executable|python.exe"
42+
$fileDialog.Title = $dialogue
43+
Write-Host $dialogue
44+
45+
do {
46+
$result = $fileDialog.ShowDialog()
47+
if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
48+
$path = $fileDialog.FileName
49+
} else {
50+
Write-Host "No file selected, please try again."
51+
}
52+
} while (-not (IsValidPythonPath $path))
53+
54+
Set-Content -Path $configFilePath -Value $path
55+
return $path
56+
}
57+
58+
function ExtractTarball($tarGzPath, $destinationDir) {
59+
# Create the destination directory if it doesn't exist
60+
if (-not (Test-Path $destinationDir)) {
61+
New-Item -ItemType Directory -Path $destinationDir
62+
}
63+
64+
# Extract tar.gz
65+
# x = extract, z = gzip & file is .tar.gz or .tgz, v = verbose, f = next arg is filename
66+
tar -xzf $tarGzPath -C $destinationDir
67+
68+
# Optionally, remove the compressed file
69+
# Remove-Item $tarGzPath
70+
}
71+
72+
function ValidatePythonVersion($pythonPath, $minVersion = 308, $maxVersion = 311)
73+
{
74+
# Validate the installed Python version
75+
76+
$pythonVersionOutput = & $pythonPath --version 2>&1
77+
$pythonVersionMatch = [regex]::Match($pythonVersionOutput, 'Python (\d+)\.(\d+)')
78+
79+
if ($pythonVersionMatch.Success) {
80+
$majorVersion = [int]$pythonVersionMatch.Groups[1].Value
81+
$minorVersion = [int]$pythonVersionMatch.Groups[2].Value
82+
$pythonVersion = $majorVersion * 100 + $minorVersion
83+
84+
# Compare using integer comparison
85+
if ($pythonVersion -lt $minVersion -or $pythonVersion -gt $maxVersion) {
86+
$minVersionStr = $minVersion -replace '(\d{1})(\d{2})', '$1.$2'
87+
$maxVersionStr = $maxVersion -replace '(\d{1})(\d{2})', '$1.$2'
88+
Write-Host "Python version is not in the required range ($maxVersionStr-$minVersionStr). Found version: $pythonVersion"
89+
return $false
90+
}
91+
else
92+
{
93+
Write-Host "Python version is in the required range $minVersion - $maxVersion. Found version: $pythonVersion"
94+
return $true
95+
}
96+
} else {
97+
throw "Unable to determine Python version. Output: $pythonVersionOutput"
98+
}
99+
}
100+
101+
try {
102+
# Get user selection
103+
Show-Menu -Title 'Select ONNX Runtime version to build, max python version is determined by the ONNX Runtime version'
104+
$selection = Read-Host "Please select the version to build, using 2 or 4"
105+
106+
# Determine the version to build based on selection
107+
# ONNX Runtime 1.14.1 requires Python 3.7-3.10
108+
# ONNX Runtime 1.16.2 requires Python 3.8-3.11
109+
# minSizeRelease requires .ort model using ort-builder, this is not included yet
110+
switch ($selection) {
111+
# "1" {
112+
# $onnxruntimeVersion = "1.14.1"
113+
# $minReqVersion = 307
114+
# $maxReqVersion = 310
115+
# $buildType = "MinSizeRelease"}
116+
"2" {
117+
$onnxruntimeVersion = "1.14.1"
118+
$minReqVersion = 307
119+
$maxReqVersion = 310
120+
$buildType = "Debug"}
121+
# "3" {
122+
# $onnxruntimeVersion = "1.16.2"
123+
# $minReqVersion = 308
124+
# $maxReqVersion = 311
125+
# $buildType = "MinSizeRelease"}
126+
"4" {
127+
$onnxruntimeVersion = "1.16.2"
128+
$minReqVersion = 308
129+
$maxReqVersion = 311
130+
$buildType = "Debug"}
131+
default { Write-Host "Invalid selection"; exit }
132+
}
133+
Write-Host "Selected ONNX Runtime version: $onnxruntimeVersion, build type: $buildType"
134+
135+
# Set the ONNX Runtime folder name
136+
$onnx_rt_folder = "onnxruntime-$onnxruntimeVersion"
137+
138+
139+
# --- Get & validate Python path + version, either from config file or user input ---
140+
Write-Host "`nGetting System Python"
141+
$pythonPath = (Get-Command python).Source
142+
143+
while (-not (ValidatePythonVersion $pythonPath $minReqVersion $maxReqVersion)) {
144+
$pythonPath = GetCustomPythonPath $minReqVersion $maxReqVersion
145+
}
146+
147+
148+
# --- Download ONNX Runtime tarball, if doesnt exist ---
149+
Write-Host "`nDownloading and Extracting $onnx_rt_folder.tar.gz"
150+
$downloadPath = "$onnx_rt_folder.tar.gz"
151+
if (-not (Test-Path $downloadPath)) {
152+
Write-Host "Downloading $onnxruntimeVersion"
153+
Invoke-WebRequest -Uri "https://github.com/microsoft/onnxruntime/archive/refs/tags/v$onnxruntimeVersion.tar.gz" -OutFile $downloadPath
154+
} else {
155+
Write-Host "$downloadPath is already downloaded."
156+
}
157+
158+
159+
# --- Extract ONNX Runtime, if hasn't been extracted ---
160+
$extractPath = "$onnx_rt_folder\"
161+
if (-not (Test-Path $extractPath)) {
162+
Write-Host "`nExtracting onnxruntime"
163+
# Replace ExtractTarball with the correct command or function to extract the tarball
164+
ExtractTarball -tarGzPath $downloadPath -destinationDir $extractPath
165+
Write-Host "`nFinished Extracting onnxruntime."
166+
167+
# move contents of onnxruntime-1.16.2 to onnxruntime
168+
Write-Host "Moving contents of $onnx_rt_folder to onnxruntime"
169+
Move-Item -Path "$extractPath\$onnx_rt_folder\*" -Destination "$extractPath\"
170+
# remove now empty folder
171+
Write-Host "Removing $onnx_rt_folder"
172+
Remove-Item -Path "$extractPath\$onnx_rt_folder\"
173+
} else {
174+
Write-Host "$onnx_rt_folder is already extracted."
175+
}
176+
177+
Write-Host "Navigating to \$onnx_rt_folder\ `n"
178+
Push-Location ".\$onnx_rt_folder\"
179+
180+
181+
# --- Create virtual environment if it doesn't exist ---
182+
if (Test-Path .\venv) {
183+
Write-Host "Virtual environment exists."
184+
. .\venv\Scripts\Activate.ps1
185+
} else {
186+
Write-Host "Creating new venv using Python $pythonPath"
187+
& $pythonPath -m venv venv
188+
}
189+
Write-Host "Activating venv"
190+
. .\venv\Scripts\Activate.ps1
191+
192+
Write-Host "Initializing ONNXRuntime Submodules"
193+
git submodule init
194+
Write-Host "Updating ONNXRuntime Submodules"
195+
git submodule update
196+
197+
Write-Host "Setup Complete.`n"
198+
199+
200+
# --- Call build script, passing the ONNX Runtime folder name (e.g. onnxruntime-1.16.2) ---
201+
Read-Host -Prompt "Press Enter to start Build"
202+
Pop-Location # Go back to base directory
203+
204+
if ($buildType -eq "MinSizeRelease") {
205+
cmd /c ".\build-win_minSizeRelease.bat $onnx_rt_folder"
206+
}
207+
elseif ($buildType -eq "Debug") {
208+
cmd /c ".\build-win_debug.bat $onnx_rt_folder"
209+
}
210+
else {
211+
Write-Host "Invalid build type"
212+
}
213+
}
214+
215+
catch {
216+
Write-Host "An error occurred: $_"
217+
}
218+
219+
# Keep the PowerShell window open
220+
Read-Host -Prompt "Press Enter to exit"

README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

build-win_debug.bat

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
setlocal
2+
3+
REM Argument received: onnxruntime-1.14.1 or onnxruntime-1.16.2
4+
set ONNXRT_FOLDER=%1
5+
echo Argument received: %ONNXRT_FOLDER%
6+
7+
@set "ONNX_CONFIG=%0"
8+
@if "%ONNX_CONFIG%"=="" (
9+
@set "ONNX_CONFIG=model.required_operators_and_types.config"
10+
)
11+
set "CMAKE_BUILD_TYPE=Debug"
12+
13+
mkdir "win-libs\"
14+
15+
"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -format value -property catalog_productLine > tmp || exit \b
16+
set /p version= < tmp
17+
set version=%version:Dev=%
18+
19+
"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -format value -property installationPath > tmp || exit \b
20+
set /p installationPath= < tmp
21+
22+
"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -format value -property catalog_productLineVersion > tmp || exit \b
23+
set /p year= < tmp
24+
25+
del tmp
26+
27+
call %ONNXRT_FOLDER%\build.bat ^
28+
--config="%CMAKE_BUILD_TYPE%" ^
29+
--cmake_generator="Visual Studio %version% %year%" ^
30+
--parallel ^
31+
--use_full_protobuf ^
32+
--enable_msvc_static_runtime ^
33+
--skip_tests ^
34+
|| exit \b
35+
36+
call "%installationPath%\VC\Auxiliary\Build\vcvarsall.bat" x86_x64 ^
37+
|| exit \b
38+
39+
REM Builds x64 / x86 depending on system architecture?
40+
lib.exe /OUT:".\win-libs\\%ONNXRT_FOLDER%-win-x86_64_%CMAKE_BUILD_TYPE%.lib" ^
41+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\_deps\onnx-build\%CMAKE_BUILD_TYPE%\onnx.lib" ^
42+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\_deps\onnx-build\%CMAKE_BUILD_TYPE%\onnx_proto.lib" ^
43+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnxruntime_graph.lib" ^
44+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnxruntime_mlas.lib" ^
45+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnxruntime_optimizer.lib" ^
46+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnxruntime_providers.lib" ^
47+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnxruntime_common.lib" ^
48+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnxruntime_session.lib" ^
49+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnxruntime_flatbuffers.lib" ^
50+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnxruntime_test_utils.lib" ^
51+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnxruntime_framework.lib" ^
52+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnxruntime_util.lib" ^
53+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnx_test_data_proto.lib" ^
54+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnx_test_runner_common.lib" ^
55+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\_deps\re2-build\%CMAKE_BUILD_TYPE%\re2.lib" ^
56+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\_deps\protobuf-build\%CMAKE_BUILD_TYPE%\libprotobufd.lib" ^
57+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\_deps\abseil_cpp-build\absl\hash\%CMAKE_BUILD_TYPE%\absl_hash.lib" ^
58+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\_deps\abseil_cpp-build\absl\hash\%CMAKE_BUILD_TYPE%\absl_city.lib" ^
59+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\_deps\abseil_cpp-build\absl\hash\%CMAKE_BUILD_TYPE%\absl_low_level_hash.lib" ^
60+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\_deps\abseil_cpp-build\absl\base\%CMAKE_BUILD_TYPE%\absl_throw_delegate.lib" ^
61+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\_deps\abseil_cpp-build\absl\base\%CMAKE_BUILD_TYPE%\absl_raw_logging_internal.lib" ^
62+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\_deps\abseil_cpp-build\absl\container\%CMAKE_BUILD_TYPE%\absl_raw_hash_set.lib"
63+

build-win_minSizeRelease.bat

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
setlocal
2+
3+
REM Argument received: onnxruntime-1.14.1 or onnxruntime-1.16.2
4+
set ONNXRT_FOLDER=%1
5+
echo Argument received: %ONNXRT_FOLDER%
6+
7+
@set "ONNX_CONFIG=%1"
8+
@if "%ONNX_CONFIG%"=="" (
9+
@set "ONNX_CONFIG=model.required_operators_and_types.config"
10+
)
11+
set "CMAKE_BUILD_TYPE=MinSizeRel"
12+
mkdir "win-libs\"
13+
14+
"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -format value -property catalog_productLine > tmp || exit \b
15+
set /p version= < tmp
16+
set version=%version:Dev=%
17+
18+
"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -format value -property installationPath > tmp || exit \b
19+
set /p installationPath= < tmp
20+
21+
"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -format value -property catalog_productLineVersion > tmp || exit \b
22+
set /p year= < tmp
23+
24+
del tmp
25+
26+
27+
28+
call %ONNXRT_FOLDER%\build.bat ^
29+
--config="%CMAKE_BUILD_TYPE%" ^
30+
--cmake_generator="Visual Studio %version% %year%" ^
31+
--parallel ^
32+
--minimal_build ^
33+
--disable_ml_ops --disable_exceptions --disable_rtti ^
34+
--include_ops_by_config "%ONNX_CONFIG%" ^
35+
--enable_reduced_operator_type_support ^
36+
--enable_msvc_static_runtime ^
37+
--skip_tests ^
38+
|| exit \b
39+
40+
call "%installationPath%\VC\Auxiliary\Build\vcvarsall.bat" x86_x64 ^
41+
|| exit \b
42+
43+
lib.exe /OUT:".\win-libs\\%ONNXRT_FOLDER%-win-x86_64_%CMAKE_BUILD_TYPE%.lib" ^
44+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnx.lib" ^
45+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnx_proto.lib" ^
46+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnxruntime_graph.lib" ^
47+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnxruntime_mlas.lib" ^
48+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnxruntime_optimizer.lib" ^
49+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnxruntime_providers.lib" ^
50+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnxruntime_common.lib" ^
51+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnxruntime_session.lib" ^
52+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnxruntime_flatbuffers.lib" ^
53+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnxruntime_test_utils.lib" ^
54+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnxruntime_framework.lib" ^
55+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnxruntime_util.lib" ^
56+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnx_test_data_proto.lib" ^
57+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\%CMAKE_BUILD_TYPE%\onnx_test_runner_common.lib" ^
58+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\_deps\re2-build\%CMAKE_BUILD_TYPE%\re2.lib" ^
59+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\_deps\protobuf-build\%CMAKE_BUILD_TYPE%\libprotobuf-lite.lib" ^
60+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\_deps\abseil_cpp-build\absl\hash\%CMAKE_BUILD_TYPE%\absl_hash.lib" ^
61+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\_deps\abseil_cpp-build\absl\hash\%CMAKE_BUILD_TYPE%\absl_city.lib" ^
62+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\_deps\abseil_cpp-build\absl\hash\%CMAKE_BUILD_TYPE%\absl_low_level_hash.lib" ^
63+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\_deps\abseil_cpp-build\absl\base\%CMAKE_BUILD_TYPE%\absl_throw_delegate.lib" ^
64+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\_deps\abseil_cpp-build\absl\base\%CMAKE_BUILD_TYPE%\absl_raw_logging_internal.lib" ^
65+
".\%ONNXRT_FOLDER%\build\Windows\%CMAKE_BUILD_TYPE%\_deps\abseil_cpp-build\absl\container\%CMAKE_BUILD_TYPE%\absl_raw_hash_set.lib"

pythonpath.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
C:\_Coding\OnnxRuntimeBuilder\Python3.10.0\python.exe

0 commit comments

Comments
 (0)