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 " `n Getting 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 " `n Downloading 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 " `n Extracting onnxruntime"
163+ # Replace ExtractTarball with the correct command or function to extract the tarball
164+ ExtractTarball - tarGzPath $downloadPath - destinationDir $extractPath
165+ Write-Host " `n Finished 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"
0 commit comments