Repository navigation
Expand file tree
/
Copy pathLuaChain.ps1
More file actions
513 lines (407 loc) · 16.5 KB
/
Copy pathLuaChain.ps1
File metadata and controls
513 lines (407 loc) · 16.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
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
Add-Type -Assembly "System.IO.Compression.Filesystem"
$ErrorActionPreference = 'Stop'
$PSNativeCommandUseErrorActionPreference = $true
# Paths in use
$venvPath = ".\.venv"
if (-not (Test-Path -Path $venvPath)) {New-Item -Path $venvPath -ItemType Directory}
$venvPath = Convert-Path $venvPath
$toolchainPath = "$venvPath\toolchain"
$binPath = "$toolchainPath\bin"
$dlsPath = "$toolchainPath\dls"
$sourcePath = "$toolchainPath\src"
$luaBinPath = "$venvPath\bin"
$luaLuaRocksPath = "$venvPath\luarocks"
$luaIncludePath = "$venvPath\include"
$luaLibPath = "$venvPath\lib"
# Versions
# update lua version here!
$luaVersion = "5.4.8"
$majorLuaVersion = "5.4"
$buildVersion = "54" # used in .dll and .a files
$minisignVersion = "0.12"
$zigVersion = "0.16.0"
$luarocksVersion = "3.13.0"
$makeVersion = "4.4.1"
$cmakeVersion = "4.4.2"
$ninjaVersion = "1.13.2"
# Downloads
$minisign = "minisign-$minisignVersion-win64"
$zig = "zig-x86_64-windows-$zigVersion"
$lua = "lua-$luaVersion"
$luarocks = "luarocks-$luarocksVersion-windows-64"
$make = "make-$makeVersion-without-guile-w32-bin"
$cmake = "cmake-$cmakeVersion-windows-x86_64"
function Choose-ZigInstall {
# Define your options
$choices = @("&Toolchain", "&System")
# Set up the menu details
$title = "Zig Install"
$message = "Download Zig in local folder (Toolchain) or use existing system install (System)"
$defaultChoice = 0
# Launch the interactive menu
$index = $Host.UI.PromptForChoice($title, $message, $choices, $defaultChoice)
# Get the selected item
$zigInstallLocation = $choices[$index].Replace("&", "")
return $zigInstallLocation
}
function Choose-LuaOrLuaJIT {
$choices = @("&Lua", "Lua&JIT")
$title = "Lua Install"
$message = "Setup Lua or LuaJIT"
$defaultChoice = 0
# Launch the interactive menu
$index = $Host.UI.PromptForChoice($title, $message, $choices, $defaultChoice)
# Get the selected item
$luaOrLuaJIT = $choices[$index].Replace("&", "")
return $luaOrLuaJIT
}
function Choose-LuaVersion {
param(
[string]$LuaOrLuaJIT
)
$luaVersion = ""
if ($LuaOrLuaJIT -eq "Lua") {
$choices = @("5.5", "5.4")
$title = "Lua Version to install"
$message = "Which specific version of Lua to install"
$defaultChoice = 0
# Launch the interactive menu
$index = $Host.UI.PromptForChoice($title, $message, $choices, $defaultChoice)
# Get the selected item
$luaVersion = $choices[$index].Replace("&", "")
} elseif ($LuaOrLuaJIT -eq "LuaJIT") {
$choices = @("2.1", "2.0")
$title = "LuaJIT Version to install"
$message = "Which specific version of LuaJIT to install"
$defaultChoice = 0
# Launch the interactive menu
$index = $Host.UI.PromptForChoice($title, $message, $choices, $defaultChoice)
# Get the selected item
$luaVersion = $choices[$index].Replace("&", "")
}
return $luaVersion
}
function Create-Paths {
Write-Output "Creating file structure"
if (-not (Test-Path -Path $toolchainPath)) {New-Item -Path $toolchainPath -ItemType Directory}
if (-not (Test-Path -Path $dlsPath)) {New-Item -Path $dlsPath -ItemType Directory}
if (-not (Test-Path -Path $binPath)) {New-Item -Path $binPath -ItemType Directory}
if (-not (Test-Path -Path $luaBinPath)) {New-Item -Path $luaBinPath -ItemType Directory}
if (-not (Test-Path -Path $luaLuaRocksPath)) {New-Item -Path $luaLuaRocksPath -ItemType Directory}
if (-not (Test-Path -Path $luaIncludePath)) {New-Item -Path $luaIncludePath -ItemType Directory}
if (-not (Test-Path -Path $luaLibPath)) {New-Item -Path $luaLibPath -ItemType Directory}
}
function Set-Envs {
Write-Output "Adding toolchain bin path to env:Path"
# Setup path so we can use the toolchain that was just setup!
$env:Path = $binPath + ";" + $env:Path
$env:Path
}
function Verify-minisign {
param(
[string]$Filepath,
[string[]]$Pubkey
)
& minisign @("-Vm", "$Filepath", "-P", "$Pubkey")
if ($LASTEXITCODE -ne 0) { throw "minisign failed with exit code $LASTEXITCODE" }
# The signature files provided by the ZSF include a "file" field in the trusted
# comment that indicates the tarball the signature applies to.
# This must be verified against the requested tarball name to prevent downgrade attacks.
Write-Output "Verifying trusted comment..."
$sigLines = Get-Content -Path "$Filepath.minisig"
# Access specific components
$untrustedComment = $sigLines[0]
$globalSignature = $sigLines[1]
$trustedComment = $sigLines[2]
$fileSignature = $sigLines[3]
if ($trustedComment -match 'file:(?<fileName>\S+)') {
$fileName = $Matches['fileName']
if ($fileName -ne $Filepath.Split("/")[-1]) {
throw "trustedComment in signature doesn't align with tarballName"
} else {
Write-Output "All verifications passed!"
}
} else {
throw "unable to find file in trusted comment, something has gone awry!"
}
}
function Get-minisign {
Write-Output "Getting minisign..."
Invoke-WebRequest -Uri "https://github.com/jedisct1/minisign/releases/download/$minisignVersion/$minisign.zip" -OutFile "$dlsPath/$minisign.zip" -ErrorAction Stop
Invoke-WebRequest -Uri "https://github.com/jedisct1/minisign/releases/download/$minisignVersion/$minisign.zip.minisig" -OutFile "$dlsPath/$minisign.zip.minisig" -ErrorAction Stop
Write-Output "Verifying minisign..."
Write-Output "Extracting minisign..."
[System.IO.Compression.ZipFile]::ExtractToDirectory("$dlsPath/$minisign.zip", $sourcePath)
Copy-Item "$sourcePath/minisign-win64/x86_64/minisign.exe" "$binPath" -Force
Remove-Item "$sourcePath/minisign-win64" -Recurse -Force
Verify-minisign -Filepath "$dlsPath/$minisign.zip" -Pubkey "RWQf6LRCGA9i53mlYecO4IzT51TGPpvWucNSCh1CBM0QTaLn73Y7GFO3"
Write-Output "minisign installed!"
}
function Get-Zig {
Write-Output "Getting Zig..."
$pubkey = "RWSGOq2NVecA2UPNdBUZykf1CCb147pkmdtYxgb3Ti+JO/wCYvhbAb/U"
$tarballName = "$zig.zip"
# To improve uptime, optionally cache this GET:
Invoke-WebRequest -Uri "https://ziglang.org/download/community-mirrors.txt" -OutFile "$dlsPath/community-mirrors.txt" -ErrorAction Stop
$mirrorsFile = Get-Content -Path "$dlsPath/community-mirrors.txt" -Raw
$mirrors = $mirrorsFile.Trim() -split '\r?\n'
# ASCII-encoded, one mirror per line, newlines are LF, there is a trailing newline.
$shuffled = $mirrors | Sort-Object { Get-Random }
foreach ($mirrorUrl in $shuffled) {
try {
Write-Output "Downloading Zig..."
Invoke-WebRequest -Uri "$mirrorUrl/$tarballName\?source=LuaChain.ps1" -OutFile "$dlsPath/$tarballName"
# NEVER SKIP THESE STEPS. The signature must be verified before the tarball is deemed safe.
Invoke-WebRequest -Uri "$mirrorUrl/$tarballName.minisig?source=LuaChain.ps1" -OutFile "$dlsPath/$tarballName.minisig"
Write-Output "Verifying Zig signature..."
Verify-minisign -Filepath "$dlsPath/$tarballName" -Pubkey $pubkey
Write-Output "Successfully fetched Zig!"
break
}
catch {
Write-Warning "Something went wrong with Zig download and verification, attempting another mirror"
break
}
}
}
function Extract-Zig {
Write-Output "Extracting Zig..."
[System.IO.Compression.ZipFile]::ExtractToDirectory("$dlsPath/$zig.zip", $sourcePath)
Copy-Item "$sourcePath/$zig/*" "$toolchainPath/" -Recurse -Force
Move-Item "$toolchainPath/zig.exe" "$binPath" -Force
Remove-Item "$sourcePath/$zig" -Recurse -Force
}
# adding these seem to break cmake because they are not "valid" windows executables. Setting up in luarocks and as env vars instead!
function Create-compilerTools {
foreach ($tool in @("ar", "cc", "c++", "ranlib", "rc")) {
$bashContents = @"
#!/bin/sh
exec zig $tool "`$@"
"@
$batContents = @"
@echo off
zig $tool %*
"@
Set-Content -Path "$binPath/$tool" -Value $bashContents
Set-Content -Path "$binPath/$tool.bat" -Value $batContents
}
}
function Get-make {
Write-Output "Getting make..."
Invoke-WebRequest -UserAgent 'Wget' -Uri "https://sourceforge.net/projects/ezwinports/files/$make.zip/download" -OutFile "$dlsPath/$make.zip"
Write-Output "Verifying make..."
if ((Get-FileHash -Path "$dlsPath/$make.zip" -Algorithm SHA1).Hash -ne "F5D9F5450536BFB6DC11C5053CD36BA3699AAFA5") {
$hash = (Get-FileHash -Path "$dlsPath/$make.zip" -Algorithm SHA1).Hash
Write-Output "Hi $hash"
throw "make sha1 hash does not match, aborting install..."
}
if ((Get-FileHash -Path "$dlsPath/$make.zip" -Algorithm MD5).Hash -ne "174F3896527D4E81EB1A09B09280E91A") {
throw "make md5 hash does not match, aborting install..."
}
}
function Extract-make {
Write-Output "Extracting make..."
[System.IO.Compression.ZipFile]::ExtractToDirectory("$dlsPath/$make.zip", $toolchainPath)
}
function Get-cmake {
Write-Output "Getting cmake..."
Invoke-WebRequest -Uri "https://github.com/Kitware/CMake/releases/download/v$cmakeVersion/$cmake.zip" -OutFile "$dlsPath/$cmake.zip"
Write-Output "Verifying cmake..."
if ((Get-FileHash -Path "$dlsPath/$cmake.zip" -Algorithm SHA256).Hash -ne "E8139D85B3813BC38833142AE1940472E9A587E9B5D2718AC1804C60F4E57A64") {
throw "cmake hash does not match, aborting install..."
}
}
function Extract-cmake {
Write-Output "Extracting cmake..."
[System.IO.Compression.ZipFile]::ExtractToDirectory("$dlsPath/$cmake.zip", $sourcePath)
Copy-Item "$sourcePath/$cmake/*" $binPath -Recurse -Force
Remove-Item "$sourcePath/$cmake" -Recurse -Force
}
function Get-ninja {
Write-Output "Getting ninja..."
$filename = "ninja-win.zip"
Invoke-WebRequest -Uri "https://github.com/ninja-build/ninja/releases/download/v$ninjaVersion/$filename" -OutFile "$dlsPath/$filename"
Write-Output "Verifying ninja..."
if ((Get-FileHash -Path "$dlsPath/$filename" -Algorithm SHA256).Hash -ne "07FC8261B42B20E71D1720B39068C2E14FFCEE6396B76FB7A795FB460B78DC65") {
throw "ninja hash does not match, aborting install..."
}
}
function Extract-ninja {
Write-Output "Extracting ninja..."
[System.IO.Compression.ZipFile]::ExtractToDirectory("$dlsPath/ninja-win.zip", $binPath)
}
function Run-command {
param(
[string]$Command,
[string[]]$Arguments
)
Write-Host "$Command $Arguments"
& $Command $Arguments
if ($LASTEXITCODE -ne 0) { throw "command failed with exit code $LASTEXITCODE" }
}
function Get-lua {
Write-Output "Getting lua..."
Invoke-WebRequest -Uri "https://lua.org/ftp/$lua.tar.gz" -OutFile "$dlsPath/$lua.tar.gz"
if ((Get-FileHash -Path "$dlsPath/$lua.tar.gz" -Algorithm SHA256).Hash -ne "4f18ddae154e793e46eeab727c59ef1c0c0c2b744e7b94219710d76f530629ae") {
throw "lua file has does not match, aborting install"
}
}
function Extract-lua {
Write-Output "Extracting lua..."
# specify the windows tar, git bash tar _really_ doesn't like the colons...
Run-command -Command "C:\Windows\System32\tar.exe" -Arguments @("-xzf", "$dlsPath\$lua.tar.gz", "-C", "$sourcePath")
if ($LASTEXITCODE -ne 0) { throw "lua failed to extract exit code $LASTEXITCODE" }
}
function Build-lua {
# command options
$cc = @("cc", "-std=gnu99")
$cflags = @("-O2", "-Wall", "-Wextra")
$staticCflags = $cflags + "-DLUA_BUILD_AS_DLL"
$archFile = "liblua$buildVersion.a"
$dllFile = "lua$buildVersion.dll"
$luaFile = "lua.exe"
# get all files in lua src dir
cd "$sourcePath/$lua/src"
$files = Get-ChildItem -Path "." -File
$objects = @()
$libObjects = @()
$luacObjects = @("luac.o", "print.o")
foreach ($file in $files) {
if ($file.Extension -eq ".c" -and $file.BaseName -ne "onelua") {
$object = "$($file.BaseName).o"
$objects += $object
$commandSuffix = @("-c", "-o", $object, $file.Name)
$zigArgs = @()
if ($luacObjects.Contains($object)) {
$zigArgs = $cc + $cflags + $commandSuffix
} else {
$zigArgs = $cc + $staticCflags + $commandSuffix
if ($object -ne "lua.o") {
$libObjects += $object
}
}
Run-command -Command "zig.exe" -Arguments $zigArgs
}
}
$arArgs = @("ar", "rcu", $archFile) + $libObjects
Run-command -Command zig.exe -Arguments $arArgs
Run-command -Command zig.exe -Arguments @("ranlib", $archfile)
Run-command -Command zig.exe -Arguments ($cc + @("-s", "-shared", "-o", $dllFile) + $libObjects)
Run-command -Command zig.exe -Arguments ($cc + @("-o", $luaFile, "-s", "lua.o", $dllFile))
cd $venvPath
}
function Install-lua {
Write-Output "Installing Lua..."
Copy-Item "$sourcePath/$lua/src/lua.exe" $luaBinPath
Copy-Item "$sourcePath/$lua/src/lua$buildVersion.dll" $luaBinPath
Copy-Item "$sourcePath/$lua/src/lua.h" $luaIncludePath
Copy-Item "$sourcePath/$lua/src/luaconf.h" $luaIncludePath
Copy-Item "$sourcePath/$lua/src/lualib.h" $luaIncludePath
Copy-Item "$sourcePath/$lua/src/lauxlib.h" $luaIncludePath
if (Test-Path -Path "$sourcePath/$lua/src/lua.hpp" -PathType Leaf) {
Copy-Item "$sourcePath/$lua/src/lua.hpp" $luaIncludePath
}
Copy-Item "$sourcePath/$lua/src/liblua$buildVersion.a" $luaLibPath
Write-Output "Lua is installed!"
}
function Get-luarocks {
Write-Output "Getting luarocks..."
Invoke-WebRequest -Uri "https://luarocks.github.io/luarocks/releases/$luarocks.zip" -OutFile "$dlsPath/$luarocks.zip"
Write-Output "Verifying luarocks..."
if ((Get-FileHash -Path "$dlsPath/$luarocks.zip" -Algorithm SHA256).Hash -ne "0897ADE5D459D55CD1962A948153745A6749FEB345403C68AAA9207388557AB9") {
throw "luarocks hash does not match, aborting install..."
}
}
function Extract-luarocks {
Write-Output "Extracting luarocks..."
[System.IO.Compression.ZipFile]::ExtractToDirectory("$dlsPath/$luarocks.zip", $sourcePath)
Copy-Item "$sourcePath/$luarocks/*" $luaBinPath -Recurse -Force
Remove-Item "$sourcePath/$luarocks" -Recurse -Force
Write-Output "LuaRocks is installed!"
}
function Setup-venv {
Write-Output "Creating venv"
$bashVenv = @"
# Source - https://stackoverflow.com/a/246128
# Posted by dogbane, modified by community. See post 'Timeline' for change history
# Retrieved 2026-08-22, License - CC BY-SA 4.0
SCRIPT_DIR=`$( cd -- "`$( dirname -- "`${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
TOOLCHAIN_BIN=`$(realpath "`$SCRIPT_DIR/../toolchain/bin")
PATH=`$SCRIPT_DIR:`$TOOLCHAIN_BIN:`$PATH
export PATH
export AR="zig.exe ar"
export CC="zig.exe cc"
export CXX="zig.exe c++"
export LD="zig.exe cc"
export RC="zig.exe rc"
export RANLIB="zig.exe ranlib"
# force luarocks to think it's not msys
unset MSYSTEM
export LUAROCKS_CONFIG=`$(realpath "`$SCRIPT_DIR/../luarocks/config.lua")
"@
Set-Content -Path "$luaBinPath/activate" -Value $bashVenv
}
function Write-config {
param(
[string]$LuaOrLuaJIT
)
$luaVersion = $majorLuaVersion
if ($LuaOrLuaJIT -eq "LuaJIT") {
$luaVersion = "5.1"
}
Write-Output "Writing out LuaRocks config"
$luaRocksConfig = @"
rocks_trees = {
{ name = [[user]],
root = home..[[/luarocks]],
},
{ name = [[system]],
root = [[$venvPath]],
},
}
external_deps_dirs = {
[[c:\windows\system32]]
}
variables = {
MSVCRT = 'UCRTBASE',
CC = [[zig.exe cc]],
CXX = [[zig.exe c++]],
CXX = [[zig.exe c++]],
MAKE = [[make.exe]],
RC = [[zig.exe rc]],
LD = [[zig.exe cc]],
AR = [[zig.exe ar]],
RANLIB = [[zig.exe ranlib]],
}
cmake_generator = "Ninja"
lua_version = "$luaVersion"
fs_use_modules = true -- use luarocks builtin modules for fetching, extracting, filesystem ops, etc
"@
Set-Content -Path "$luaLuaRocksPath/config.lua" -Value $luaRocksConfig
}
# not planning to use these for now, might pass in via command line args instead?
# $luaOrLuaJIT = Choose-LuaOrLuaJIT
# $luaVersion = Choose-LuaVersion -LuaOrLuaJIT $luaOrLuaJIT
# uncomment each of these as you test/need them.
Create-Paths
Set-Envs
Get-minisign
Get-Zig
Extract-Zig
Create-compilerTools
Get-make
Extract-make
Get-cmake
Extract-cmake
Get-ninja
Extract-ninja
Get-lua
Extract-lua
Build-lua
Install-lua
Get-luarocks
Extract-luarocks
Setup-venv
Write-config -LuaOrLuaJIT "Lua"
Write-Output "Done! You Lua + LuaRocks environment and toolchain are ready to go!"
Write-Output " Git Bash: source .venv/bin/activate"