Repository navigation
Expand file tree
/
Copy pathbuild-module.ps1
More file actions
280 lines (242 loc) · 7.61 KB
/
Copy pathbuild-module.ps1
File metadata and controls
280 lines (242 loc) · 7.61 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<#
.SYNOPSIS
Unified build script for compiling C# Binary Module (AOT and Managed)
.DESCRIPTION
Easily build both/AOT/managed versions of the C# Binary Module
#>
param(
[ValidateSet('aot', 'managed', 'both')]
[Alias('t')]
[string]$Type = 'both',
[Alias('c')]
[switch]$Clean,
[switch]$Help
)
function Show-Help
{
Write-Host "Usage: ./build-module.ps1 [OPTIONS]"
Write-Host ""
Write-Host "Options:"
Write-Host " -Type, -t <string> Build type: aot, managed, or both (default: both)"
Write-Host " -Clean, -c Clean build artifacts before building"
Write-Host " -Help Show this help message"
Write-Host ""
Write-Host "Notes:"
Write-Host " On Windows the AOT build auto-detects the MSVC toolset via vswhere"
Write-Host " and initializes vcvars64 itself (works on VS Insiders / preview-only"
Write-Host " toolset installs). No manual Developer Command Prompt needed."
Write-Host ""
Write-Host "Examples:"
Write-Host " ./build-module.ps1 # Build both AOT and managed"
Write-Host " ./build-module.ps1 -t aot # Build only AOT version"
Write-Host " ./build-module.ps1 -Type managed # Build only managed version"
Write-Host " ./build-module.ps1 -c # Clean only"
Write-Host " ./build-module.ps1 -t AOT -c # Clean then build AOT (case-insensitive)"
}
function Clean-BuildArtifacts
{
Write-Host "Cleaning build artifacts..." -ForegroundColor Yellow
# Clean bin directories
if (Test-Path "bin")
{
Remove-Item -Recurse -Force "bin"
}
if (Test-Path "dnGLua.BinaryModule\bin")
{
Remove-Item -Recurse -Force "dnGLua.BinaryModule\bin"
}
# Clean output directories created by build script
if (Test-Path "bin-aot")
{
Remove-Item -Recurse -Force "bin-aot"
}
if (Test-Path "bin-managed")
{
Remove-Item -Recurse -Force "bin-managed"
}
# Clean obj directories
if (Test-Path "dnGLua.BinaryModule\obj")
{
Remove-Item -Recurse -Force "dnGLua.BinaryModule\obj"
}
Write-Host "Clean completed" -ForegroundColor Green
}
function Get-VcVarsInvocation
{
# Locate Visual Studio via vswhere and decide how to initialize vcvars64.
# Returns $null on non-Windows or when no VS with C++ tools is found.
if (-not $IsWindows)
{
return $null
}
$vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
if (-not (Test-Path $vswhere))
{
return $null
}
$vsDir = & $vswhere -latest -prerelease -products * -property installationPath | Select-Object -First 1
if (-not $vsDir)
{
return $null
}
$vsDir = $vsDir.Trim()
$vcDir = Join-Path $vsDir 'VC\Auxiliary\Build'
$vcvars64 = Join-Path $vcDir 'vcvars64.bat'
if (-not (Test-Path $vcvars64))
{
return $null
}
# Prefer the stable default named by v145.default.txt when its props file
# exists; otherwise pin the newest installed toolset. Preview-only installs
# can name a default toolset whose lib directory is incomplete (e.g. no
# LIBCMT.lib), which breaks the ILCompiler link step.
$arg = $null
$stableOk = $false
$defaultToolsetFile = Join-Path $vcDir 'Microsoft.VCToolsVersion.v145.default.txt'
if (Test-Path $defaultToolsetFile)
{
$vctver = (Get-Content $defaultToolsetFile -TotalCount 1).Trim()
$vctmm = ($vctver -split '\.')[0..1] -join '.'
$stableOk = Test-Path (Join-Path $vcDir "v145\Microsoft.VCToolsVersion.VC.$vctmm.props")
}
if (-not $stableOk)
{
$newest = Get-ChildItem (Join-Path $vsDir 'VC\Tools\MSVC') -Directory -Filter '14.*' -ErrorAction SilentlyContinue |
Sort-Object { [version]$_.Name } -Descending | Select-Object -First 1
if ($newest)
{
$arg = "-vcvars_ver=$($newest.Name)"
Write-Host "MSVC default toolset unusable, using $($newest.Name)" -ForegroundColor Yellow
}
}
return @{ Path = $vcvars64; Arg = $arg }
}
function Build-AotModule
{
Write-Host "Building AOT module..." -ForegroundColor Cyan
#Push-Location "dnGLua.BinaryModule"
try
{
# Update NuGet
#& nuget update -self
#if ($LASTEXITCODE -ne 0) {
# Write-Host "Warning: NuGet update failed, continuing..." -ForegroundColor Yellow
#}
# Try NuGet restore (may fail)
#& nuget restore
#if ($LASTEXITCODE -ne 0) {
# Write-Host "NuGet restore not applicable, using dotnet restore..." -ForegroundColor Yellow
#}
# Force restore
& dotnet restore --force
if ($LASTEXITCODE -ne 0)
{
throw "dotnet restore failed"
}
# Build AOT binary module for current platform
$vc = Get-VcVarsInvocation
if ($vc)
{
# Stale MSBuild nodes keep the environment they were started with and
# would not see the vcvars variables, so shut them down first.
& dotnet build-server shutdown | Out-Null
# Run the publish inside a vcvars environment: ILCompiler's vswhere
# detection fails on VS Insiders installs that only register preview
# MSVC toolsets, and IlcUseEnvironmentalTools bypasses it entirely.
$cmdLine = "`"$($vc.Path)`""
if ($vc.Arg)
{
$cmdLine += " $($vc.Arg)"
}
$cmdLine += " >nul 2>&1 && dotnet publish dnGLua.BinaryModule/dnGLua.BinaryModule.csproj -c Release /p:PublishAot=true /p:IlcUseEnvironmentalTools=true -v detailed -o bin-aot"
& cmd /c $cmdLine
}
else
{
& dotnet publish dnGLua.BinaryModule/dnGLua.BinaryModule.csproj -c Release /p:PublishAot=true -v detailed -o bin-aot
}
if ($LASTEXITCODE -ne 0)
{
throw "AOT build failed"
}
Write-Host "AOT build completed successfully" -ForegroundColor Green
}
finally
{
#Pop-Location
}
}
function Build-ManagedModule
{
Write-Host "Building managed module..." -ForegroundColor Cyan
#Push-Location "dnGLua.BinaryModule"
try
{
# Update NuGet
#& nuget update -self
#if ($LASTEXITCODE -ne 0) {
# Write-Host "Warning: NuGet update failed, continuing..." -ForegroundColor Yellow
#}
# Try NuGet restore (may fail)
#& nuget restore
#if ($LASTEXITCODE -ne 0) {
# Write-Host "NuGet restore not applicable, using dotnet restore..." -ForegroundColor Yellow
#}
# Force restore
& dotnet restore --force
if ($LASTEXITCODE -ne 0)
{
throw "dotnet restore failed"
}
# Build managed binary module
& dotnet build dnGLua.BinaryModule/dnGLua.BinaryModule.csproj -c Release --no-restore --no-incremental -v detailed -o bin-managed
if ($LASTEXITCODE -ne 0)
{
throw "Managed build failed"
}
Write-Host "Managed build completed successfully" -ForegroundColor Green
}
finally
{
#Pop-Location
}
}
# Main execution
if ($Help)
{
Show-Help
exit 0
}
# Make Type parameter case-insensitive
$Type = $Type.ToLowerInvariant()
# Check if we're in the right directory
if (-not (Test-Path "dnGLua.BinaryModule\dnGLua.BinaryModule.csproj"))
{
Write-Host "Error: dnGLua.BinaryModule.csproj not found!" -ForegroundColor Red
Write-Host "Please run this script from the project root directory." -ForegroundColor Red
exit 1
}
# Clean if requested
if ($Clean)
{
Clean-BuildArtifacts
# If only -Clean was specified (no -Type), exit after cleaning
if ($Type -eq 'both' -and -not $PSBoundParameters.ContainsKey('Type'))
{
Write-Host "Clean completed. No build specified." -ForegroundColor Green
exit 0
}
}
# Build based on type
$buildAot = $Type -eq 'aot' -or $Type -eq 'both'
$buildManaged = $Type -eq 'managed' -or $Type -eq 'both'
Write-Host "Building: $Type" -ForegroundColor Cyan
if ($buildAot)
{
Build-AotModule
}
if ($buildManaged)
{
Build-ManagedModule
}
Write-Host "All builds completed successfully!" -ForegroundColor Green