-
Notifications
You must be signed in to change notification settings - Fork 234
Expand file tree
/
Copy pathbuild.ps1
More file actions
295 lines (260 loc) · 10.5 KB
/
build.ps1
File metadata and controls
295 lines (260 loc) · 10.5 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<#
.SYNOPSIS
OceanBase Lite Windows build script — mirrors build.sh on Linux/macOS.
.EXAMPLE
.\build.ps1 -h
.\build.ps1 init
.\build.ps1 release
.\build.ps1 release --ninja
.\build.ps1 release --ninja -j 16
.\build.ps1 debug
.\build.ps1 clean
#>
param(
[Parameter(Position = 0)]
[string]$Action = "debug",
[switch]$Ninja,
[Alias("j")]
[int]$Jobs = 0,
[switch]$h
)
$ErrorActionPreference = "Stop"
$TOPDIR = $PSScriptRoot
# ── Dependency path defaults (override via env vars) ────────────────
$DefaultVcpkgDir = if ($env:OB_VCPKG_DIR) { $env:OB_VCPKG_DIR } else { "C:/VcpkgInstalled/x64-windows" }
$DefaultOpenSSLDir = if ($env:OB_OPENSSL_DIR) { $env:OB_OPENSSL_DIR } else { "C:/Program Files/OpenSSL-Win64" }
$DefaultLLVMDir = if ($env:OB_LLVM_DIR) { $env:OB_LLVM_DIR } else { "C:/Program Files/LLVM18" }
$DefaultWinDepsZip = if ($env:OB_WIN_DEPS_ZIP) { $env:OB_WIN_DEPS_ZIP } else { "$TOPDIR/win_deps.zip" }
# ── Helpers ─────────────────────────────────────────────────────────
function Write-Log { param([string]$msg) Write-Host "[build.ps1] $msg" }
function Write-Err { param([string]$msg) Write-Host "[build.ps1][ERROR] $msg" -ForegroundColor Red }
function Show-Usage {
Write-Host @"
Usage:
.\build.ps1 -h Show this help
.\build.ps1 init Extract pre-built deps (win_deps.zip)
.\build.ps1 clean Remove build_* directories
.\build.ps1 [BuildType] Configure only (cmake)
.\build.ps1 [BuildType] --ninja Configure + compile (ninja)
.\build.ps1 [BuildType] --ninja -j 16 Configure + compile with 16 jobs
.\build.ps1 package Build release + generate MSI/ZIP installer
BuildType:
debug Debug build (default)
release RelWithDebInfo build
relwithdebinfo Alias for release
Environment variables (override dependency paths):
OB_VCPKG_DIR vcpkg install root (default: C:/VcpkgInstalled/x64-windows)
OB_OPENSSL_DIR OpenSSL root (default: C:/Program Files/OpenSSL-Win64)
OB_LLVM_DIR LLVM 18 root (default: C:/Program Files/LLVM18)
OB_WIN_DEPS_ZIP Path to deps zip (default: <project>/win_deps.zip)
"@
}
if ($Jobs -eq 0) {
$cpuCount = (Get-CimInstance Win32_Processor | Measure-Object -Property NumberOfLogicalProcessors -Sum).Sum
if (-not $cpuCount -or $cpuCount -lt 1) { $cpuCount = 4 }
$totalMemGB = [math]::Floor((Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory / 1GB)
$memJobs = [math]::Max(1, [math]::Floor($totalMemGB / 3))
$Jobs = [math]::Min($cpuCount, $memJobs)
Write-Log "Auto jobs: $Jobs (cpus=$cpuCount, mem=${totalMemGB}GB, ~3GB/job)"
}
# ── init: extract dependency archive ────────────────────────────────
function Do-Init {
$zipPath = $DefaultWinDepsZip
if (-not (Test-Path $zipPath)) {
Write-Err "Deps archive not found: $zipPath"
Write-Log "Set OB_WIN_DEPS_ZIP or place win_deps.zip in the project root."
Write-Log ""
Write-Log "To create win_deps.zip on a machine that already has deps installed:"
Write-Log " .\build.ps1 pack"
exit 1
}
Write-Log "Extracting deps from $zipPath ..."
$destDir = "$TOPDIR\win_deps"
if (Test-Path $destDir) {
Write-Log "Removing existing win_deps/ ..."
Remove-Item -Recurse -Force $destDir
}
Expand-Archive -Path $zipPath -DestinationPath $destDir -Force
Write-Log "Dependencies extracted to $destDir"
Write-Log ""
Write-Log "Set the following env vars (or they will use defaults):"
Write-Log " `$env:OB_VCPKG_DIR = '$destDir\vcpkg\x64-windows'"
Write-Log " `$env:OB_OPENSSL_DIR = '$destDir\openssl'"
Write-Log " `$env:OB_LLVM_DIR = '$destDir\llvm18'"
}
# ── pack: create dependency archive from current machine ────────────
function Do-Pack {
$packDir = "$TOPDIR\win_deps_staging"
if (Test-Path $packDir) { Remove-Item -Recurse -Force $packDir }
New-Item -ItemType Directory -Path $packDir | Out-Null
Write-Log "Packing vcpkg from: $DefaultVcpkgDir"
if (Test-Path $DefaultVcpkgDir) {
$vcpkgDest = "$packDir\vcpkg\x64-windows"
New-Item -ItemType Directory -Path "$packDir\vcpkg" -Force | Out-Null
Copy-Item -Recurse -Path $DefaultVcpkgDir -Destination $vcpkgDest
} else {
Write-Err "vcpkg dir not found: $DefaultVcpkgDir"
}
Write-Log "Packing OpenSSL from: $DefaultOpenSSLDir"
if (Test-Path $DefaultOpenSSLDir) {
Copy-Item -Recurse -Path $DefaultOpenSSLDir -Destination "$packDir\openssl"
} else {
Write-Err "OpenSSL dir not found: $DefaultOpenSSLDir"
}
Write-Log "Packing LLVM from: $DefaultLLVMDir"
if (Test-Path $DefaultLLVMDir) {
Copy-Item -Recurse -Path $DefaultLLVMDir -Destination "$packDir\llvm18"
} else {
Write-Err "LLVM dir not found: $DefaultLLVMDir"
}
$outZip = "$TOPDIR\win_deps.zip"
Write-Log "Compressing to $outZip ..."
if (Test-Path $outZip) { Remove-Item $outZip }
Compress-Archive -Path "$packDir\*" -DestinationPath $outZip -CompressionLevel Optimal
Remove-Item -Recurse -Force $packDir
Write-Log "Done! Archive: $outZip"
}
# ── clean ───────────────────────────────────────────────────────────
function Do-Clean {
Write-Log "Cleaning build directories ..."
Get-ChildItem -Path $TOPDIR -Directory -Filter "build*" | ForEach-Object {
Write-Log " Removing $($_.Name)"
Remove-Item -Recurse -Force $_.FullName
}
Write-Log "Clean done."
}
# ── cmake configure ────────────────────────────────────────────────
function Do-Build {
param(
[string]$BuildType,
[string[]]$ExtraCMakeArgs = @()
)
$buildDir = "$TOPDIR\build_$($BuildType.ToLower())"
if (-not (Test-Path $buildDir)) {
New-Item -ItemType Directory -Path $buildDir | Out-Null
}
$cmakeArgs = @(
$TOPDIR,
"-G", "Ninja",
"-DCMAKE_EXPORT_COMPILE_COMMANDS=1",
"-DCMAKE_BUILD_TYPE=$BuildType",
"-DOB_USE_LLD=ON",
"-DOB_VCPKG_DIR=$DefaultVcpkgDir",
"-DOB_OPENSSL_DIR=$DefaultOpenSSLDir",
"-DOB_LLVM_DIR=$DefaultLLVMDir"
) + $ExtraCMakeArgs
Write-Log "CMake configure: build_$($BuildType.ToLower())"
Write-Log " Build type : $BuildType"
Write-Log " VcpkgDir : $DefaultVcpkgDir"
Write-Log " OpenSSLDir : $DefaultOpenSSLDir"
Write-Log " LLVMDir : $DefaultLLVMDir"
Write-Log ""
Push-Location $buildDir
try {
& cmake @cmakeArgs 2>&1 | Out-Host
if ($LASTEXITCODE -ne 0) {
Write-Err "CMake configure failed (exit code $LASTEXITCODE)"
exit $LASTEXITCODE
}
Write-Log "CMake configure succeeded."
# Copy compile_commands.json to project root for IDE support
$ccJson = "$buildDir\compile_commands.json"
if (Test-Path $ccJson) {
Copy-Item $ccJson "$TOPDIR\compile_commands.json" -Force
Write-Log "compile_commands.json copied to project root."
}
}
finally {
Pop-Location
}
return $buildDir
}
# ── ninja build ─────────────────────────────────────────────────────
function Do-Ninja {
param([string]$BuildDir)
Write-Log "Building with Ninja (-j $Jobs) in $BuildDir ..."
Push-Location $BuildDir
try {
& ninja -j $Jobs observer 2>&1 | Out-Host
if ($LASTEXITCODE -ne 0) {
Write-Err "Build failed (exit code $LASTEXITCODE)"
exit $LASTEXITCODE
}
Write-Log "Build succeeded!"
}
finally {
Pop-Location
}
}
# ── package: build release + create installer ───────────────────────
function Do-Package {
$buildDir = Do-Build -BuildType "RelWithDebInfo" -ExtraCMakeArgs @("-DOB_BUILD_PACKAGE=ON")
Do-Ninja -BuildDir $buildDir
Write-Log "Creating installer package in $buildDir ..."
Push-Location $buildDir
try {
$wixFound = Get-Command wix -ErrorAction SilentlyContinue
if ($wixFound) {
Write-Log "WiX v4 found, generating MSI..."
& cpack -G WIX -C RelWithDebInfo 2>&1 | Out-Host
if ($LASTEXITCODE -ne 0) {
Write-Log "WiX MSI generation failed, falling back to ZIP..."
& cpack -G ZIP -C RelWithDebInfo 2>&1 | Out-Host
}
} else {
Write-Log "WiX not found, generating ZIP package..."
Write-Log " To generate MSI: dotnet tool install --global wix"
& cpack -G ZIP -C RelWithDebInfo 2>&1 | Out-Host
}
if ($LASTEXITCODE -ne 0) {
Write-Err "Package generation failed (exit code $LASTEXITCODE)"
exit $LASTEXITCODE
}
$packages = Get-ChildItem -Path $buildDir -Include "seekdb-*.msi","seekdb-*.zip" -File
if ($packages) {
Write-Log "Package(s) created:"
foreach ($pkg in $packages) {
Write-Log " $($pkg.FullName)"
}
}
Write-Log "Package build succeeded!"
}
finally {
Pop-Location
}
}
# ── Main ────────────────────────────────────────────────────────────
if ($h) {
Show-Usage
exit 0
}
switch ($Action.ToLower()) {
"init" {
Do-Init
}
"pack" {
Do-Pack
}
"package" {
Do-Package
}
"clean" {
Do-Clean
}
{ $_ -in "release", "relwithdebinfo" } {
$buildDir = Do-Build -BuildType "RelWithDebInfo"
if ($Ninja) { Do-Ninja -BuildDir $buildDir }
}
{ $_ -in "debug", "" } {
$buildDir = Do-Build -BuildType "Debug"
if ($Ninja) { Do-Ninja -BuildDir $buildDir }
}
"-h" {
Show-Usage
}
default {
Write-Err "Unknown action: $Action"
Show-Usage
exit 1
}
}