-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ps1
259 lines (222 loc) · 7.56 KB
/
build.ps1
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
param (
[string]$Config = ".\debug.json",
[Parameter(DontShow)]
$defaults = (Get-Content $Config | ConvertFrom-Json),
[switch]$DebugMode = $defaults.DebugMode,
[switch]$Tiny = $defaults.Tiny,
[switch]$Sound = $defaults.Sound,
[switch]$Fullscreen = $defaults.Fullscreen,
[int]$XRes = $defaults.XRes,
[int]$YRes = $defaults.YRes,
[string]$OutName = $defaults.OutName,
[switch]$NoExe,
[switch]$Recompile,
[switch]$Disasm,
[switch]$SuffixWithRes,
[int]$CrinklerTries = 0,
[switch]$Capture,
[switch]$NoVideo
)
if ($MyInvocation.BoundParameters['defaults']) {
throw "The -defaults parameter is not allowed. Use -Config to specify the configuration file."
}
Write-Host "DebugMode: $DebugMode"
Write-Host "Tiny: $Tiny"
Write-Host "Sound: $Sound"
Write-Host "Fullscreen: $Fullscreen"
Write-Host "XRes: $XRes"
Write-Host "YRes: $YRes"
Write-Host ""
$sourceDir = 'src' # Source files directory
$buildDir = 'obj' # Output directory of object files
$disasmDir = 'dis' # Output directory of disasembled files
$infoColor = "Cyan"
# Check if MSVC build tools are accessible
try {
Get-Command "cl" -ErrorAction Stop | Out-Null
Get-Command "link" -ErrorAction Stop | Out-Null
} catch {
Write-Error "MSVC Build Tools cl.exe or link.exe not found."
return
}
# Check if Shader Minifirer is available
try {
Get-Command "shader_minifier" -ErrorAction Stop | Out-Null
} catch {
Write-Error "shader_minifier.exe not found."
return
}
# Utility functions to test if a given file needs to be updated based
# on its dependencies last update
function ItemNeedsUpdate($itemPath, $dependsPaths) {
if(-not (Test-Path -Path $itemPath)) {
return $true
}
$item = Get-Item $itemPath
foreach($dependsPath in $dependsPaths) {
$depend = Get-Item $dependsPath
if($depend.LastWriteTime -gt $item.LastWriteTime) {
return $true
}
}
return $false
}
# Generate the minified shader source, since this operation can
# take some time we only generate the file if it has been changed
$shadersDir = "$sourceDir/shaders"
$shadersIncludeFile = "$sourceDir/shaders.inl"
$shaderFiles = Get-ChildItem -Path $shadersDir -Recurse `
| Where-Object{$_.Extension -match '^.(frag|vert|glsl)$'} `
| ForEach-Object {$_.FullName}
if((ItemNeedsUpdate $shadersIncludeFile $shaderFiles) -or $Recompile) {
Write-Host "Minifying shaders..." -ForegroundColor $infoColor
shader_minifier $shaderFiles -o $shadersIncludeFile --aggressive-inlining
}
# Available options:
# https://learn.microsoft.com/en-us/cpp/build/reference/compiler-options-listed-by-category?view=msvc-170
$compileOptions = @(
'/c', # Compile without linking (generate object files only)
'/O1', '/Os', '/Oi', # Basic optimization
'/arch:IA32', # Force to use x87 float instructions
'/fp:fast', # Allow reordering of float operations
"/Fo$buildDir/" # Output directory
)
if($Capture) {
$compileOptions += '/DCAPTURE'
if($NoVideo) {
$Fullscreen = $false
} else {
$compileOptions += '/DVIDEO'
$Fullscreen = $true
}
$DebugMode = $true
$Tiny = $false
}
if($DebugMode) {
$compileOptions += '/DDEBUG'
} else {
$compileOptions += '/GS-' # No buffer security check
}
if($Sound) {
$compileOptions += '/DSOUND'
}
if($Fullscreen) {
$compileOptions += '/DFULLSCREEN'
}
$compileOptions += "/DXRES=$XRes"
$compileOptions += "/DYRES=$YRes"
$sourceFiles = Get-ChildItem -Path $sourceDir -Filter "*.c" -Recurse `
| ForEach-Object {$_.FullName}
if (-not (Test-Path -Path $buildDir)) {
mkdir $buildDir | Out-Null
}
# Compile
# Basic incremental build implementation
function FindDependencies($fullFilePath) {
$paths = @{}
$ignore = @("glext.h", "khrplatform.h")
function RecurseDependencies($fullFilePath) {
$parentPath = Split-Path $fullFilePath -Parent
$fileContent = Get-Content $fullFilePath
$includePattern = '^\s*#include\s+"([^"]+)"'
foreach ($line in $fileContent) {
if ($line -match $includePattern) {
$includedFilePath = $Matches[1]
$fullIncludePath = Resolve-Path "$parentPath/$includedFilePath"
$fullIncludePath = $fullIncludePath.ToString()
$includedFile = Split-Path $fullIncludePath -Leaf
if($includedFile -in $ignore) {
continue
}
if(-not $paths.ContainsKey($fullIncludePath)) {
$paths[$fullIncludePath] = $true
RecurseDependencies $fullIncludePath
}
}
}
}
RecurseDependencies $fullFilePath
$paths | Select-Object -ExpandProperty Keys
}
# Compile only the sources that have been modified except if
# explicitly told to compile all or if compile options changed
$prevOptsPath = "./build.tmp"
if(-not $Recompile) {
if(-not (Test-Path -Path $prevOptsPath)) {
$Recompile = $true
} else {
$prevOpts = @(Get-Content -Path $prevOptsPath)
$cmp = Compare-Object $compileOptions $prevOpts
$Recompile = -not $null -eq $cmp
}
}
if($Recompile) {
$compileOptions | Set-Content -Path $prevOptsPath
}
$compileSources = @()
if(-not $Recompile) {
foreach($source in $sourceFiles) {
$objPath = "$buildDir/$((Get-Item $source).BaseName).obj"
$depsPaths = @($source)
$depsPaths += FindDependencies (Resolve-Path $source)
if(ItemNeedsUpdate $objPath $depsPaths) {
$compileSources += $source
}
}
} else {
$compileSources = $sourceFiles
}
if($compileSources.count -eq 0) {
Write-Host "Up to date. Nothing to compile." -ForegroundColor $infoColor
} else {
Write-Host "Compile options: $compileOptions"
Write-Host "Compiling" -ForegroundColor $infoColor
cl $compileOptions $compileSources
}
$objectFiles = Get-ChildItem -Path $buildDir -Filter "*.obj" -Recurse `
| ForEach-Object {$_.FullName}
if(-not $NoExe) {
if($SuffixWithRes) {
$outFile = "$OutName-$YRes.exe"
} else {
$outFile = "$OutName.exe"
}
if($Capture) {
$Tiny = $false # No compression when in capture mode
$outFile = "capture_$outFile"
}
# Link
if ($Tiny) {
Write-Host "Linking with crinkler" -ForegroundColor $infoColor
# Doc: https://github.com/runestubbe/Crinkler/blob/master/doc/manual.txt
$extraOptions = @()
if($CrinklerTries -gt 0) {
$extraOptions += "/ORDERTRIES:$CrinklerTries"
}
crinkler /OUT:$outFile `
/SUBSYSTEM:WINDOWS `
/ENTRY:wWinMain `
/TINYHEADER `
$extraOptions `
$objectFiles `
kernel32.lib user32.lib gdi32.lib opengl32.lib bufferoverflowu.lib Winmm.lib
} else {
Write-Host "Default linking" -ForegroundColor $infoColor
link /OUT:$outFile `
$objectFiles `
user32.lib gdi32.lib opengl32.lib Winmm.lib
}
Write-Host "Output file: $outFile" -ForegroundColor $infoColor
}
# Optional disassembly for debugging
if($Disasm) {
if(-not (Test-Path -Path $disasmDir)) {
mkdir $disasmDir | Out-Null
}
Write-Host "Disassembling generated object files" -ForegroundColor $infoColor
foreach($objectFile in $objectFiles) {
$baseName = (Split-Path $objectFile -Leaf).Split('.')[0]
$outOption = "/OUT:./$disasmDir/$baseName.asm"
dumpbin /DISASM $objectFile $outOption | Out-Null
}
}