-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ps1
More file actions
523 lines (448 loc) · 18.1 KB
/
build.ps1
File metadata and controls
523 lines (448 loc) · 18.1 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# This script is used to build Genestealer for all of its available targets. Use any of the below flags to include a build for that target.
# This will build a Windows 64-bit Agent and a GS-Client by default.
param($version,
[switch] $help = $false, [switch] $win = $false, [switch] $client = $false,
[switch] $test = $false, [switch] $release = $false, [switch] $all = $false,
[switch] $android64 = $false, [switch] $bsd = $false, [switch] $upload = $false,
[switch] $x64 = $false, [switch] $x86 = $false, [switch] $iso = $false,
[switch] $linux = $false, [switch] $alert = $false,
[switch] $raspi = $false, [switch] $rust9x = $false, [switch] $ppc64le = $false,
[switch] $mipsel = $false, [switch] $mips = $false, [switch] $armv7 = $false,
[switch] $armv7hf = $false, [switch] $aarch64 = $false, [switch] $mips64el = $false,
[switch] $aarch64_android = $false, [switch] $clean = $false, [switch] $dart = $false,
[switch] $win7 = $false, [switch] $xp = $false, [switch] $mac = $false,
[switch] $client_docs = $false, [switch] $agent_docs = $false, [switch] $docs = $false,
[switch] $miri = $false, [string] $bin = $null, [switch] $setversions = $false)
# Exit if anything errors out
$ErrorActionPreference = "Stop"
if ($help -eq $true) {
Write-Host "This script is used to build Genestealer. By default this script will do nothing, so be sure to specify the needed flags. Below are all available options for this script:
-aarch64: Build for 64-bit ARM
-aarch64_android: Build for 64-bit ARM using Android ABI
-alert: Have the builder say 'build complete' when all builds finish (building for all supported targets from scratch can take hours)
-all: Build for all supported targets, except Rust9x ones
-android64: Build for x86_64 Android
-armv7: Build for ARMv7 systems
-armv7hf: Build for ARMv7 systems with hardware floating point support
-bin: The test binary to run under Miri, if the there are multiple binaries within a test project
-bsd: Build for x86_64 FreeBSD
-clean: Remove all contents from the target directory (note the target directory contains the build artifacts from cargo/cross; however, the builds directory contains all builds in a compressed format)
-client: Build the Python Client
-dev: Whether the script is ran on my desktop dev environment (This exists since my build set up has different WSL paths for genestealer_test code, so most likely useless to anyone else)
-docs: Build docs for Agent and Client
-setversions: Set the OS and subsystem versions for the built binaries
-help: Show this help message and exit
-iso: Package the Genestealer binaries into an ISO instead of a ZIP archive, requires the Windows Assessment and Deployment Kit (Windows ADK)
-linux: Build for x86_64 Musl (supports majority of Linux based distros)
-mac: Build for x86_64 Mac OSX 10.15
-mips: Build for MIPS (will use big endian) and dynamically link with GNU GLIBC
-mipsel: Build for MIPS little endian and dynamically link with GNU GLIBC
-mips64el: Build for MIPS64 little endian and dynamically link with GNU GLIBC
-miri: Run the given target using Miri for debugging puproses
-msi: Build a Windows MSI installer (this was left in the dust since it's convoluted to get it working. It's being left here in case someone else wants to pick it back up and as a reference.
That being said, the current Windows install method works fine, so if it ain't broke...)
-no64: Do not build for 64-bit systems
-ppc64le: Build for PowerPC64le and dynamically link to GNU GLIBC
-raspi: Build for ARMv7 Raspberry Pi (this should support Pi 3 and up)
-release: All builds should be built in release mode, default builds in debug mode
-rust9x: Build for 32-bit Windows 9X/ME. This requires docker and the Genestealer/containers/Dockerfile.rust9x-v1.76-b to be built. Follow docs/Tutorials/Agent/build_agent.md
-test: Add the built packages to the MEDIA_DIR set within this script to be used for testing
-upload: Upload the builds to the UPLOAD_URL set within this script
-x86: Include x86 builds for all specified, and available, builds
-xp: Build for Windows XP, this requires VC-LTL5 and YY-Thunks, be sure to set the ENV vars within .cargo/config.toml for the project
-win: Build for Windows x64
-win7: Build for Windows 7 32-bit, this also works for Windows 2008r2 32/64-bit
"
exit 0
}
$GS_DIR = "$HOME\projects\rust-tests"
$RUST_BUILD_DIR = "$GS_DIR"
$USER = "mid"
$WINAT_DLL_X86 = "D:\winat\libs\x86\winat.dll"
$WINAT_DLL_X64 = "D:\winat\libs\x86_64\winat.dll"
$BUILDS_DIR = "$GS_DIR\builds"
if (-Not (Test-Path -Path $BUILDS_DIR)) {
New-Item -Path $BUILDS_DIR -ItemType "directory"
}
# The files to exclude when compressing GS binaries/files.
# .d - Ignore all Makefile-compatible dependency lists
$EXCLUDE = @("*.d")
# Get the Agent version from it's cargo.toml
$AGENT_VERSION = ((Get-Content "$GS_DIR\Cargo.toml" -TotalCount 3)[-1] | Select-String -Pattern '\d+\.\d+\.\d+').matches.value
if ($all -eq $true) {
$win = $true
$x86 = $true
}
# Compress the given build files into an archive
function Compress-Build {
param (
$Files,
$TargetTriple,
$OutputDir,
$Tag
)
if ( $null -ne $Tag ) {
$Tag = "-$Tag"
}
if ($release -eq $true) {
$BuildDir = "release"
} else {
$BuildDir = "debug"
}
$Timestamp = Get-Date -UFormat "%d-%m-%Y-%R" | ForEach-Object { $_ -replace ":", "" }
$ArchiveName = "rust-tests$Tag-$TargetTriple-$BuildDir-v$AGENT_VERSION--$Timestamp"
if ($iso -eq $true) {
Write-ColorOutput "Creating ISO for build $ArchiveName" Black Gray
& 'C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\amd64\Oscdimg\oscdimg.exe' -n -d -m .\target\$TargetTriple\$BuildDir\ "$BUILDS_DIR\$ArchiveName.iso"
if ($upload -eq $true) {
scp "$BUILDS_DIR\$ArchiveName.iso" mid@kvm-r2-u37.qqcyber.net:/home/mid/rust9x/
}
} else {
Write-ColorOutput "Creating ZIP archive for build $ArchiveName" Black Gray
$DestinationPath = "$OutputDir/$ArchiveName.zip"
# Compress the given files and put the archive in the projects builds directory
Compress-Archive @Files -Force -DestinationPath $DestinationPath -CompressionLevel "Optimal"
if ($LastExitCode -ne 0) {
exit 1
}
}
}
function Set-Versions {
$EDITBIN = "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\bin\Hostx64\x86\editbin.exe"
if ($release -eq $true) {
$files = Get-ChildItem -Path "$RUST_BUILD_DIR\target\$TargetTriple\release\*.exe" -Exclude $EXCLUDE
} else {
$files = Get-ChildItem -Path "$RUST_BUILD_DIR\target\$TargetTriple\debug\*.exe" -Exclude $EXCLUDE
}
for ($i=0; $i -lt $files.Count; $i++) {
& $EDITBIN $files[$i].FullName /SUBSYSTEM:CONSOLE,5.01 /OSVERSION:5.0 /RELEASE
if ($LastExitCode -ne 0)
{ exit 1 }
}
}
# Write
function Write-ColorOutput {
param (
[string]$txt,
$fg = "Yellow",
$bg = "DarkCyan"
)
Write-Host $txt -ForegroundColor $fg -BackgroundColor $bg
}
# Alert that the build has finished
function Set-Alert {
# Create a new SpVoice objects
$voice = New-Object -ComObject Sapi.spvoice
# Set the speed - positive numbers are faster, negative numbers, slower
$voice.rate = 0
# Say something
$voice.speak("Build complete")
}
# Build docs for Genestealer
function New-AgentDocs {
cargo doc --no-deps
}
# Wraps cargo/cross to build the Genestealer Agent
function New-Build {
param (
[string]$TargetTriple,
[switch]$UseCross = $false,
$CargoArgs = $null,
$Toolchain = $null,
[switch]$NoDefaultFeatures = $false,
$Features = $null
)
Write-ColorOutput "Building $TargetTriple..."
if ($UseCross -eq $true) {
$cmd = "cross"
} else {
$cmd = "cargo"
}
if ($null -ne $Toolchain) {
$cmd += " $ToolChain"
}
$cmd += " build --target $TargetTriple"
if ($release -eq $true) {
$cmd += " --release"
}
if ($NoDefaultFeatures -eq $true) {
$cmd += " --no-default-features"
}
if ($null -ne $Features) {
$feat = $Features -join ","
$cmd += " --features $feat"
}
if ($null -ne $CargoArgs) {
$cmd += " $CargoArgs"
}
Write-ColorOutput "$cmd" Black Gray
Invoke-Expression $cmd
if ($LastExitCode -ne 0)
{ exit 1 }
if ($setversions -eq $true) {
Set-Versions
}
if ($release -eq $true) {
$BuildDir = ".\target\$TargetTriple\release\"
} else {
$BuildDir = ".\target\$TargetTriple\debug\"
}
if ($x86 -eq $true) {
Copy-Item $WINAT_DLL_X86 $BuildDir -Force
} else {
Copy-Item $WINAT_DLL_X64 $BuildDir -Force
}
}
# Wraps cargo/cross to run the test code
function Start-WithMiri {
param (
[string]$TargetTriple,
[switch]$UseCross = $false,
$CargoArgs = $null,
$Toolchain = $null,
[switch]$NoDefaultFeatures = $false,
$Features = $null
)
Write-ColorOutput "Running $TargetTriple..."
if ($UseCross -eq $true) {
$cmd = "cross"
} else {
$cmd = "cargo"
}
if ($null -ne $Toolchain) {
$cmd += " $ToolChain"
}
$cmd += " +nightly miri run --target $TargetTriple"
if ($release -eq $true) {
$cmd += " --release"
}
if ($null -ne $bin) {
$cmd += " --bin $bin"
}
if ($NoDefaultFeatures -eq $true) {
$cmd += " --no-default-features"
}
if ($null -ne $Features) {
$feat = $Features -join ","
$cmd += " --features $feat"
}
if ($null -ne $CargoArgs) {
$cmd += " $CargoArgs"
}
Write-ColorOutput "$cmd" Black Gray
Invoke-Expression $cmd
if ($LastExitCode -ne 0)
{ exit 1 }
}
# Build for Windows 64-bit
function New-Win64 {
$TargetTriple = "x86_64-pc-windows-msvc"
if ($test -eq $true) {
$OutputDir = "$MEDIA_DIR\x64\"
} else {
$OutputDir = $BUILDS_DIR
}
New-Build $TargetTriple
if ($release -eq $true) {
$files = Get-ChildItem -Path "$RUST_BUILD_DIR\target\$TargetTriple\release\*.exe" -Exclude $EXCLUDE
} else {
$files = Get-ChildItem -Path "$RUST_BUILD_DIR\target\$TargetTriple\debug\*.exe" -Exclude $EXCLUDE
}
$x64_compress = @{
Path = $files
}
Compress-Build -Files $x64_compress -TargetTriple $TargetTriple -OutputDir $OutputDir
}
# Build for Windows 32-bit
function New-Win32 {
$TargetTriple = "i686-pc-windows-msvc"
if ($test -eq $true) {
$OutputDir = "$MEDIA_DIR\x86\"
} else {
$OutputDir = $BUILDS_DIR
}
New-Build $TargetTriple
if ($release -eq $true) {
$BuildDir = "release"
$files = Get-ChildItem -Path "$RUST_BUILD_DIR\target\$TargetTriple\$BuildDir\*.exe" -Exclude $EXCLUDE
} else {
$BuildDir = "debug"
$files = Get-ChildItem -Path "$RUST_BUILD_DIR\target\$TargetTriple\$BuildDir\*.exe" -Exclude $EXCLUDE
}
$x86_compress = @{
Path = $files
}
Compress-Build -Files $x86_compress -TargetTriple $TargetTriple -OutputDir $OutputDir
}
function New-WinXP32 {
$TargetTriple = "i686-pc-windows-msvc"
if ($test -eq $true) {
$OutputDir = "$MEDIA_DIR\x86\"
} else {
$OutputDir = $BUILDS_DIR
}
New-Build $TargetTriple
if ($release -eq $true) {
$BuildDir = "release"
$files = Get-ChildItem -Path "$RUST_BUILD_DIR\target\$TargetTriple\$BuildDir\*.exe" -Exclude $EXCLUDE
$files += $WINAT_DLL_X86
} else {
$BuildDir = "debug"
$files = Get-ChildItem -Path "$RUST_BUILD_DIR\target\$TargetTriple\$BuildDir\*.exe" -Exclude $EXCLUDE
$files += $WINAT_DLL_X86
}
$x86_compress = @{
Path = $files
}
Compress-Build -Files $x86_compress -TargetTriple $TargetTriple -OutputDir $OutputDir
}
# Run for Windows 32-bit using Miri
function Start-Win32 {
$TargetTriple = "i686-pc-windows-msvc"
Start-WithMiri $TargetTriple
}
# Build for Windows 7 32-bit, should also suffice for 64-bit
function New-Win7-32 {
$TargetTriple = "i686-win7-windows-msvc"
if ($test -eq $true) {
$OutputDir = "$MEDIA_DIR\x86\"
} else {
$OutputDir = $BUILDS_DIR
}
Write-ColorOutput("Building $TargetTriple...")
# Install the nightly toolchain if required
$NightlyInstalled = $(rustup target list --installed --toolchain nightly)
if ( $null -eq $NightlyInstalled ) {
Write-ColorOutput "Installing required nightly toolchain..."
rustup install nightly-2023-12-31
}
# Install the rust-src component if required
$NightlyComponents = $(rustup component list --toolchain nightly --installed)
if ( !$NightlyComponents.Contains("rust-src") ) {
Write-ColorOutput "Installing required rust-src for nightly toolchain..."
rustup component add rust-src --toolchain nightly-x86_64-pc-windows-msvc
}
# This target requires the nightly compiler since it is a Tier 3 target and its std lib needs to be compiled.
# This nightly toolchain can be installed with:
# rustup install nightly-2023-12-31
# rustup component add rust-src --toolchain nightly-x86_64-pc-windows-msvc
# There is an issue where the windows crate's build script doesn't get to add the library's directory to the linker path,
# so we need to do this by parsing the cargo +nightly metadata to determine what directories need to be added as link paths.
# See this post for more info and the fix: https://github.com/rust-lang/rust/issues/99466
# The below command will output the metadata for the project, convert from JSON to w/e Powershell uses, parse out all packages with names matching
# `windows_i686_msvc`, get the manifest path from each of these package objects, prepend a "-L " to each manifest's path, and combine all of these into a string.
# In short, the below command creates a string like "-L <PATH1> -L <PATH2> -L <PATH3> ..."
$LibPaths = cargo +nightly metadata -q | ConvertFrom-Json | ForEach-Object { $_.packages } | Where-Object { $_.name -match "windows_i686_msvc" } | ForEach-Object { "-L " + $_.manifest_path -replace ("Cargo.toml", "lib") }
# Was having issues getting the static link flag and panic=abort flags to be included in the RUSTFLAGS variable, so instead of using ./cargo/config.toml and
# the below variable, we are just iterating all flags needed for this build below.
$env:RUSTFLAGS="-C target-feature=+crt-static -C panic=abort $LibPaths"; cargo +nightly build --release --target $TargetTriple -Z build-std=std,panic_abort --no-default-features --features win7
# Clear these flags to prevent issues with other builds
$env:RUSTFLAGS=""
if ($LastExitCode -ne 0)
{ exit 1 }
$files = Get-ChildItem -Path "$RUST_BUILD_DIR\target\$TargetTriple\release\*.exe" -Exclude $EXCLUDE
$files += "$GS_DIR\docs\Tutorials\Agent\install_agent.md"
$x86_compress = @{
Path = $files
}
Compress-Build -Files $x86_compress -TargetTriple $TargetTriple -OutputDir $OutputDir
}
# Build for Windows XP 32-bit
function New-WinXP32 {
$TargetTriple = "i686-pc-windows-msvc"
if ($test -eq $true) {
$OutputDir = "$MEDIA_DIR\x86\"
} else {
$OutputDir = $BUILDS_DIR
}
New-Build $TargetTriple
if ($release -eq $true) {
$files = Get-ChildItem -Path "$RUST_BUILD_DIR\target\$TargetTriple\release\*.exe" -Exclude $EXCLUDE
} else {
$files = Get-ChildItem -Path "$RUST_BUILD_DIR\target\$TargetTriple\debug\*.exe" -Exclude $EXCLUDE
}
$x86_compress = @{
Path = $files
}
Compress-Build -Files $x86_compress -TargetTriple $TargetTriple -OutputDir $OutputDir -Tag "win_xp"
}
function New-WinXP64 {
$TargetTriple = "x86_64-pc-windows-msvc"
if ($test -eq $true) {
$OutputDir = "$MEDIA_DIR\x64\"
} else {
$OutputDir = $BUILDS_DIR
}
New-Build $TargetTriple
if ($release -eq $true) {
$files = Get-ChildItem -Path "$RUST_BUILD_DIR\target\$TargetTriple\release\*.exe" -Exclude $EXCLUDE
} else {
$files = Get-ChildItem -Path "$RUST_BUILD_DIR\target\$TargetTriple\debug\*.exe" -Exclude $EXCLUDE
}
$x64_compress = @{
Path = $files
}
Compress-Build -Files $x64_compress -TargetTriple $TargetTriple -OutputDir $OutputDir -Tag "win_xp"
}
# Upload the builds within the BUILD_DIR to the UPLOAD_URL
function Add-Builds {
scp -r "$BUILDS_DIR\*" "$USER@${UPLOAD_URL}:$UPLOAD_PATH/"
if ($LastExitCode -ne 0)
{ exit 1 }
}
Set-Location -Path $GS_DIR
# Check Docker is using the correct context
$nix = @($aarch64, $aarch64_android, $armv7, $armv7hf, $linux, $bsd, $android64, $raspi, $mac, $mips, $mipsel, $mips64el, $ppc64le)
if ($nix -contains $true ) {
$v = $(docker version)
if ($LastExitCode -ne 0) {
Write-ColorOutput "ERROR: Docker needs to be running to build for Unix/Linux systems" Black Red
exit 1
}
if (-Not $(docker context show).Contains("linux")) {
Write-ColorOutput "ERROR: Docker needs to be running in its Linux context to build for Unix/Linux systems" Black Red
exit 1
}
}
# Run the selected target with miri
if ($miri -eq $true) {
if (($win -eq $true) -and ($x86 -eq $true)) {
Start-Win32
}
} else {
# # # Build the selected targets # # #
if (($win -eq $true) -and ($x64 -eq $true)) {
New-Win64
}
if (($win -eq $true) -and ($x86 -eq $true)) {
New-Win32
}
if ($win7 -eq $true) {
New-Win7-32
}
if (($xp -eq $true) -and ($x86 -eq $true)) {
New-WinXP32
}
if (($xp -eq $true) -and ($x64 -eq $true)) {
New-WinXP64
}
if ($android64 -eq $true) {
New-Android64
}
if ($aarch64_android -eq $true) {
New-Aarch64Android
}
if ($raspi -eq $true) {
New-RasPi
}
if ($rust9x -eq $true) {
New-Rust9x
}
Set-Alert
if ($agent_docs -eq $true) {
New-AgentDocs
}
if ($clean -eq $true) {
cargo clean
}
}