-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ps1
More file actions
179 lines (154 loc) · 5.46 KB
/
Copy pathbuild.ps1
File metadata and controls
179 lines (154 loc) · 5.46 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
param(
[switch]$Clean
)
$ErrorActionPreference = "Stop"
$Root = Split-Path -Parent $MyInvocation.MyCommand.Path
$OutDir = Join-Path $Root "out"
$AssetsDir = Join-Path $Root "assets"
$IconPath = Join-Path $AssetsDir "SuperLightBattery.ico"
$IconGenerator = Join-Path $AssetsDir "generate-icon.ps1"
$InstallerIconPath = Join-Path $AssetsDir "SuperLightBatteryInstaller.ico"
$InstallerIconGenerator = Join-Path $AssetsDir "generate-installer-icon.ps1"
$AppSource = Join-Path $Root "src\superlightbattery.c"
$AppRc = Join-Path $Root "src\app.rc"
$AppRes = Join-Path $OutDir "app.res"
$AppExe = Join-Path $OutDir "SuperLightBattery.exe"
$AppObj = Join-Path $OutDir "superlightbattery.obj"
$LauncherSource = Join-Path $Root "src\launcher.c"
$LauncherRc = Join-Path $Root "src\launcher.rc"
$LauncherRes = Join-Path $OutDir "launcher.res"
$LauncherExe = Join-Path $OutDir "SuperLightBatteryLauncher.exe"
$LauncherObj = Join-Path $OutDir "launcher.obj"
$InstallerSource = Join-Path $Root "src\installer.c"
$InstallerRc = Join-Path $Root "src\installer.rc"
$InstallerRes = Join-Path $OutDir "installer.res"
$InstallerExe = Join-Path $OutDir "SuperLightBatteryInstaller.exe"
$InstallerObj = Join-Path $OutDir "installer.obj"
function Find-VsDevCmd {
$vswhere = Join-Path ${env:ProgramFiles(x86)} "Microsoft Visual Studio\Installer\vswhere.exe"
if (Test-Path $vswhere) {
$path = & $vswhere -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -find "Common7\Tools\VsDevCmd.bat" | Select-Object -First 1
if ($path) {
return $path
}
}
$candidates = @(
"${env:ProgramFiles}\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat",
"${env:ProgramFiles}\Microsoft Visual Studio\2022\Professional\Common7\Tools\VsDevCmd.bat",
"${env:ProgramFiles}\Microsoft Visual Studio\2022\Enterprise\Common7\Tools\VsDevCmd.bat",
"${env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\Community\Common7\Tools\VsDevCmd.bat",
"${env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\Professional\Common7\Tools\VsDevCmd.bat",
"${env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\Enterprise\Common7\Tools\VsDevCmd.bat"
)
foreach ($candidate in $candidates) {
if (Test-Path $candidate) {
return $candidate
}
}
return $null
}
if ($Clean -and (Test-Path $OutDir)) {
Remove-Item -LiteralPath $OutDir -Recurse -Force
}
New-Item -ItemType Directory -Force -Path $OutDir | Out-Null
if (-not (Test-Path $IconPath)) {
if (-not (Test-Path $IconGenerator)) {
throw "Missing icon and missing generator: $IconGenerator"
}
& $IconGenerator -OutputPath $IconPath
}
if (-not (Test-Path $InstallerIconPath)) {
if (-not (Test-Path $InstallerIconGenerator)) {
throw "Missing installer icon and missing generator: $InstallerIconGenerator"
}
& $InstallerIconGenerator -OutputPath $InstallerIconPath
}
$vsdevcmd = Find-VsDevCmd
if (-not $vsdevcmd) {
throw "Could not find Visual Studio C++ build tools. Install MSVC x64 tools or run from a Developer PowerShell."
}
function New-RcArgs {
param(
[string]$Source,
[string]$Output
)
return "/nologo /fo `"$Output`" `"$Source`""
}
function New-ClArgs {
param(
[string]$Source,
[string]$Exe,
[string]$Obj,
[string]$Res,
[string[]]$Libs,
[string[]]$CompileArgs = @(),
[string[]]$LinkArgs = @(),
[switch]$NoGl,
[switch]$NoLtcg
)
$args = @(
"/nologo",
"/std:c11",
"/W4",
"/O1",
"/Gw",
"/Gy",
"/MT",
"/DUNICODE",
"/D_UNICODE"
)
if (-not $NoGl) {
$args += "/GL"
}
$args += $CompileArgs
$args += @(
"/Fo:`"$Obj`"",
"/Fe:`"$Exe`"",
"`"$Source`"",
"/link",
"/nologo",
"`"$Res`""
)
$args += $LinkArgs
if (-not $NoLtcg) {
$args += "/LTCG"
}
$args += @("/OPT:REF", "/OPT:ICF", "/INCREMENTAL:NO")
$args += $Libs
return ($args -join " ")
}
$appRcArgs = New-RcArgs -Source $AppRc -Output $AppRes
$launcherRcArgs = New-RcArgs -Source $LauncherRc -Output $LauncherRes
$installerRcArgs = New-RcArgs -Source $InstallerRc -Output $InstallerRes
$appArgs = New-ClArgs `
-Source $AppSource `
-Exe $AppExe `
-Obj $AppObj `
-Res $AppRes `
-LinkArgs @("/SUBSYSTEM:CONSOLE") `
-Libs @("setupapi.lib", "hid.lib", "shell32.lib", "user32.lib", "gdi32.lib")
$launcherArgs = New-ClArgs `
-Source $LauncherSource `
-Exe $LauncherExe `
-Obj $LauncherObj `
-Res $LauncherRes `
-CompileArgs @("/GS-") `
-LinkArgs @("/SUBSYSTEM:WINDOWS", "/ENTRY:LauncherEntry", "/NODEFAULTLIB") `
-NoGl `
-NoLtcg `
-Libs @("kernel32.lib", "user32.lib")
$installerArgs = New-ClArgs `
-Source $InstallerSource `
-Exe $InstallerExe `
-Obj $InstallerObj `
-Res $InstallerRes `
-LinkArgs @("/SUBSYSTEM:WINDOWS") `
-Libs @("shell32.lib", "ole32.lib", "uuid.lib", "user32.lib", "advapi32.lib", "gdi32.lib", "comctl32.lib")
$cmd = "`"$vsdevcmd`" -arch=x64 -host_arch=x64 >NUL 2>NUL && rc $appRcArgs && rc $launcherRcArgs && rc $installerRcArgs && cl $appArgs && cl $launcherArgs && cl $installerArgs"
cmd.exe /d /s /c $cmd
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}
Write-Host "Built out\SuperLightBattery.exe"
Write-Host "Built out\SuperLightBatteryLauncher.exe"
Write-Host "Built out\SuperLightBatteryInstaller.exe"