-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompilerHelper.ps1
More file actions
201 lines (178 loc) · 6.89 KB
/
compilerHelper.ps1
File metadata and controls
201 lines (178 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# PowerShell script: DLL Builder
$debug = $false
function Write-CustomError {
param(
[string]$Message,
[string]$Details = ""
)
Write-Host $Message -ForegroundColor Red
if ($debug -and $Details) {
Write-Host $Details -ForegroundColor DarkRed
}
}
# Get .c files only in ..\src
$cFiles = @()
if (Test-Path "..\src") {
$cFiles += Get-ChildItem -Path "..\src" -Filter *.c -File | ForEach-Object { $_.FullName }
}
if (-not $cFiles) {
Write-CustomError "No .c files found in current directory. Exiting."
exit 1
}
# Handle -y and file arguments for auto-accept and selection
$acceptAll = $false
$specifiedFiles = @()
foreach ($arg in $args) {
if ($arg -eq "-y") {
$acceptAll = $true
} elseif ($arg -like "*.c") {
$specifiedFiles += $arg
}
}
$filesToCompile = @()
if ($specifiedFiles.Count -gt 0) {
foreach ($file in $specifiedFiles) {
if ($cFiles | Where-Object { [System.IO.Path]::GetFileName($_) -eq $file }) {
$filesToCompile += ( $cFiles | Where-Object { [System.IO.Path]::GetFileName($_) -eq $file } )
} else {
Write-CustomError "Specified file not found in ..\src: $file"
}
}
} elseif ($acceptAll) {
$filesToCompile = $cFiles
} else {
foreach ($file in $cFiles) {
$answer = Read-Host "Compile '$file'? (Y/N, default N)"
if ($answer -match '^(y|Y)$') {
$filesToCompile += $file
}
}
}
if ($filesToCompile.Count -eq 0) {
Write-CustomError "No files selected for compilation. Exiting."
exit 1
}
# --- CLEANUP: Remove .obj/.dll/.lib/.exp in root and move all outputs to bin/{arch} ---
$binRoot = "..\pyCTools\bin"
$x64Folder = Join-Path $binRoot "x64"
$x86Folder = Join-Path $binRoot "x86"
# Create output folders
foreach ($folder in @($binRoot, $x64Folder, $x86Folder)) {
if (-not (Test-Path $folder)) {
New-Item -ItemType Directory -Path $folder | Out-Null
Write-Host "Created folder: $folder"
}
else {
Write-Host "Folder already exists: $folder"
}
}
# Remove build artifacts from root (not in bin)
$extensionsToClean = @("*.obj", "*.dll", "*.lib", "*.exp")
foreach ($ext in $extensionsToClean) {
Get-ChildItem -Path . -Filter $ext -File | Remove-Item -Force -ErrorAction SilentlyContinue
}
function Get-VSPath {
# Search for Visual Studio's vcvarsall.bat in common locations
$searchPaths = @(
# Standard VS install locations
"$env:ProgramFiles\Microsoft Visual Studio",
"$env:ProgramFiles(x86)\Microsoft Visual Studio",
# Known VS2019 BuildTools path
"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools",
# Start Menu shortcut location (user request)
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2019\Visual Studio Tools\VC"
)
foreach ($root in $searchPaths) {
if (Test-Path $root) {
# If the root is a direct VC tools folder, check for vcvarsall.bat directly
$vcvarsDirect = Join-Path $root "vcvarsall.bat"
if (Test-Path $vcvarsDirect) {
return $root
}
# Otherwise, search subfolders for vcvarsall.bat (limit depth for performance)
$foundVcvars = Get-ChildItem -Path $root -Filter "vcvarsall.bat" -Recurse -ErrorAction SilentlyContinue | Select-Object -First 1
if ($foundVcvars) {
# Return the parent directory containing vcvarsall.bat
return Split-Path $foundVcvars.FullName -Parent
}
}
}
return $null
}
# Function to run VS developer cmd and compile for target arch
function CompileDll {
param(
[string]$Arch, # x86 or x64
[string]$OutFolder,
[string]$File
)
$vsPath = Get-VSPath
if (-not $vsPath) {
$vsPath = Read-Host "Could not auto-detect Visual Studio path. Please enter the path to your VS installation (e.g. C:\Program Files (x86)\Microsoft Visual Studio\2019\Community)"
if (-not (Test-Path $vsPath)) {
Write-CustomError "Provided Visual Studio path does not exist: $vsPath"
return $false
}
}
# Try to find vcvarsall.bat in the provided path or its subfolders
$vcvarsPath = Join-Path $vsPath "VC\Auxiliary\Build\vcvarsall.bat"
if (-not (Test-Path $vcvarsPath)) {
# Try direct path (if user gave path to VC tools folder)
$vcvarsPath = Join-Path $vsPath "vcvarsall.bat"
if (-not (Test-Path $vcvarsPath)) {
# Try searching subfolders
$foundVcvars = Get-ChildItem -Path $vsPath -Filter "vcvarsall.bat" -Recurse -ErrorAction SilentlyContinue | Select-Object -First 1
if ($foundVcvars) {
$vcvarsPath = $foundVcvars.FullName
} else {
Write-CustomError "vcvarsall.bat not found at $vcvarsPath"
return $false
}
}
}
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($File)
$dllName = "${baseName}_${Arch}.dll"
$dllPath = Join-Path $OutFolder $dllName
# Output files for cleanup/move
$objName = "${baseName}.obj"
$libName = "${baseName}.lib"
$expName = "${baseName}.exp"
$cmd = @"
call `"$vcvarsPath`" $Arch
cl /LD $File /Fo`"$objName`" /link /out:`"$dllPath`"
exit /b %errorlevel%
"@
Write-Host "Compiling $File for $Arch..."
$tempFile = [System.IO.Path]::GetTempFileName() + ".bat"
Set-Content -Path $tempFile -Value $cmd -Encoding ASCII
try {
$proc = Start-Process -FilePath "cmd.exe" -ArgumentList "/c `"$tempFile`"" -NoNewWindow -Wait -PassThru
Remove-Item $tempFile -Force
if ($proc.ExitCode -ne 0) {
Write-CustomError "Compilation failed for $File ($Arch)." "Exit code: $($proc.ExitCode)"
return $false
}
} catch {
Remove-Item $tempFile -Force -ErrorAction SilentlyContinue
Write-CustomError "Compilation process failed for $File ($Arch)." $_.Exception.Message
return $false
}
# Move .obj, .lib, .exp to output folder if they exist
foreach ($artifact in @($objName, $libName, $expName)) {
if (Test-Path $artifact) {
Move-Item $artifact $OutFolder -Force
}
}
Write-Host "Compilation succeeded for $File ($Arch). Output: $dllPath"
return $true
}
# Compile each file as its own DLL for x64 and x86, outputs in bin/x64 and bin/x86
foreach ($file in $filesToCompile) {
if (-not (CompileDll -Arch "x64" -OutFolder $x64Folder -File $file)) {
Write-CustomError "x64 build failed for $file."
}
if (-not (CompileDll -Arch "x86" -OutFolder $x86Folder -File $file)) {
Write-CustomError "x86 build failed for $file."
}
}
Write-Host "Build process completed."