-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathdeploy_windows.ps1
More file actions
372 lines (321 loc) · 12.5 KB
/
deploy_windows.ps1
File metadata and controls
372 lines (321 loc) · 12.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
param (
# Replace default path with system Qt installation folder if necessary
[string] $QtInstallPath32 = "C:\Qt\5.15.2",
[string] $QtInstallPath64 = "C:\Qt\6.8.1",
[string] $QtCompile32 = "msvc2019",
[string] $QtCompile64 = "msvc2022_64",
# Important:
# - Do not update ASIO SDK without checking for license-related changes.
# - Do not copy (parts of) the ASIO SDK into the Jamulus source tree without
# further consideration as it would make the license situation more complicated.
#
# The following version pinnings are semi-automatically checked for
# updates. Verify .github/workflows/bump-dependencies.yaml when changing those manually:
[string] $AsioSDKUrl = "https://download.steinberg.net/sdk_downloads/asiosdk_2.3.3_2019-06-14.zip",
[string] $NsisUrl = "https://downloads.sourceforge.net/project/nsis/NSIS%203/3.11/nsis-3.11.zip",
[string] $BuildOption = ""
)
# Fail early on all errors
$ErrorActionPreference = "Stop"
# Invoke-WebRequest is really slow by default because it renders a progress bar.
# Disabling this, improves vastly performance:
$ProgressPreference = 'SilentlyContinue'
# change directory to the directory above (if needed)
Set-Location -Path "$PSScriptRoot\..\"
# Global constants
$RootPath = "$PWD"
$BuildPath = "$RootPath\build"
$DeployPath = "$RootPath\deploy"
$WindowsPath ="$RootPath\windows"
$AppName = "Jamulus"
# Execute native command with errorlevel handling
Function Invoke-Native-Command {
param(
[string] $Command,
[string[]] $Arguments
)
& "$Command" @Arguments
if ($LastExitCode -Ne 0)
{
Throw "Native command $Command returned with exit code $LastExitCode"
}
}
# Cleanup existing build folders
Function Clean-Build-Environment
{
if (Test-Path -Path $BuildPath) { Remove-Item -Path $BuildPath -Recurse -Force }
if (Test-Path -Path $DeployPath) { Remove-Item -Path $DeployPath -Recurse -Force }
New-Item -Path $BuildPath -ItemType Directory
New-Item -Path $DeployPath -ItemType Directory
}
# For sourceforge links we need to get the correct mirror (especially NISIS) Thanks: https://www.powershellmagazine.com/2013/01/29/pstip-retrieve-a-redirected-url/
Function Get-RedirectedUrl {
param(
[Parameter(Mandatory=$true)]
[string] $url
)
$numAttempts = 10
$sleepTime = 10
$maxSleepTime = 80
for ($attempt = 1; $attempt -le $numAttempts; $attempt++) {
try {
$request = [System.Net.WebRequest]::Create($url)
$request.AllowAutoRedirect=$true
$response=$request.GetResponse()
$response.ResponseUri.AbsoluteUri
$response.Close()
return
} catch {
if ($attempt -lt $numAttempts) {
Write-Warning "Caught error: $_"
Write-Warning "Get-RedirectedUrl: Fetch attempt #${attempt}/${numAttempts} for $url failed, trying again in ${sleepTime}s"
Start-Sleep -Seconds $sleepTime
$sleepTime = [Math]::Min($sleepTime * 2, $maxSleepTime)
continue
}
Write-Error "Get-RedirectedUrl: All ${numAttempts} fetch attempts for $url failed, failing whole call"
throw
}
}
}
function Initialize-Module-Here ($m) { # see https://stackoverflow.com/a/51692402
# If module is imported say that and do nothing
if (Get-Module | Where-Object {$_.Name -eq $m}) {
Write-Output "Module $m is already imported."
}
else {
# If module is not imported, but available on disk then import
if (Get-Module -ListAvailable | Where-Object {$_.Name -eq $m}) {
Import-Module $m
}
else {
# If module is not imported, not available on disk, but is in online gallery then install and import
if (Find-Module -Name $m | Where-Object {$_.Name -eq $m}) {
Install-Module -Name $m -Force -Verbose -Scope CurrentUser
Import-Module $m
}
else {
# If module is not imported, not available and not in online gallery then abort
Write-Output "Module $m not imported, not available and not in online gallery, exiting."
EXIT 1
}
}
}
}
# Download and uncompress dependency in ZIP format
Function Install-Dependency
{
param(
[Parameter(Mandatory=$true)]
[string] $Uri,
[Parameter(Mandatory=$true)]
[string] $Destination
)
if (Test-Path -Path "$WindowsPath\$Destination")
{
echo "Using ${WindowsPath}\${Destination} from previous run (e.g. actions/cache)"
return
}
$TempFileName = [System.IO.Path]::GetTempFileName() + ".zip"
$TempPath = [System.IO.Path]::GetTempPath()
$TempGuid = [System.Guid]::NewGuid()
# Create a unique empty directory to unpack into
$TempDir = (Join-Path $TempPath $TempGuid)
New-Item -ItemType Directory -Path $TempDir
if ($Uri -Match "downloads.sourceforge.net")
{
$Uri = Get-RedirectedUrl -URL $Uri
}
Invoke-WebRequest -Uri $Uri -OutFile $TempFileName
echo $TempFileName
Expand-Archive -Path $TempFileName -DestinationPath $TempDir -Force
echo $WindowsPath\$Destination
# Because we unpacked into a new directory, we can use * for the directory in the archive,
# so that we do not need to know the directory name the archive was packed from.
Move-Item -Path "$TempDir\*" -Destination "$WindowsPath\$Destination" -Force
Remove-Item -Path $TempDir -Recurse -Force
Remove-Item -Path $TempFileName -Force
}
# Install VSSetup (Visual Studio detection), ASIO SDK and NSIS Installer
Function Install-Dependencies
{
if (-not (Get-PackageProvider -Name nuget).Name -eq "nuget") {
Install-PackageProvider -Name "Nuget" -Scope CurrentUser -Force
}
Initialize-Module-Here -m "VSSetup"
Install-Dependency -Uri $NsisUrl -Destination "..\libs\NSIS\NSIS-source"
if ($BuildOption -Notmatch "jack") {
# Don't download ASIO SDK on Jamulus JACK builds to save
# resources and to be extra-sure license-wise.
Install-Dependency -Uri $AsioSDKUrl -Destination "..\libs\ASIOSDK2"
}
}
# Setup environment variables and build tool paths
Function Initialize-Build-Environment
{
param(
[Parameter(Mandatory=$true)]
[string] $BuildArch
)
# Look for Visual Studio/Build Tools 2017 or later (version 15.0 or above)
$VsInstallPath = Get-VSSetupInstance | `
Select-VSSetupInstance -Product "*" -Version "15.0" -Latest | `
Select-Object -ExpandProperty "InstallationPath"
if ($VsInstallPath -Eq "") { $VsInstallPath = "<N/A>" }
if ($BuildArch -Eq "x86_64")
{
$VcVarsBin = "$VsInstallPath\VC\Auxiliary\build\vcvars64.bat"
}
else
{
$VcVarsBin = "$VsInstallPath\VC\Auxiliary\build\vcvars32.bat"
}
""
"**********************************************************************"
"Using Visual Studio/Build Tools environment settings located at"
$VcVarsBin
"**********************************************************************"
""
if (-Not (Test-Path -Path $VcVarsBin))
{
Throw "Microsoft Visual Studio ($BuildArch variant) is not installed. " + `
"Please install Visual Studio 2017 or above it before running this script."
}
# Import environment variables set by vcvarsXX.bat into current scope
$EnvDump = [System.IO.Path]::GetTempFileName()
Invoke-Native-Command -Command "cmd" `
-Arguments ("/c", "`"$VcVarsBin`" && set > `"$EnvDump`"")
foreach ($_ in Get-Content -Path $EnvDump)
{
if ($_ -Match "^([^=]+)=(.*)$")
{
Set-Item "Env:$($Matches[1])" $Matches[2]
}
}
Remove-Item -Path $EnvDump -Force
}
# Setup Qt environment variables and build tool paths
Function Initialize-Qt-Build-Environment
{
param(
[Parameter(Mandatory=$true)]
[string] $QtInstallPath,
[Parameter(Mandatory=$true)]
[string] $QtCompile
)
$QtMsvcSpecPath = "$QtInstallPath\$QtCompile\bin"
# Setup Qt executables paths for later calls
Set-Item Env:QtQmakePath "$QtMsvcSpecPath\qmake.exe"
Set-Item Env:QtWinDeployPath "$QtMsvcSpecPath\windeployqt.exe"
"**********************************************************************"
"Using Qt binaries for Visual C++ located at"
$QtMsvcSpecPath
"**********************************************************************"
""
if (-Not (Test-Path -Path $Env:QtQmakePath))
{
Throw "The Qt binaries for Microsoft Visual C++ 2017 or above could not be located at $QtMsvcSpecPath. " + `
"Please install Qt with support for MSVC 2017 or above before running this script," + `
"then call this script with the Qt install location, for example C:\Qt\5.15.2"
}
}
# Build Jamulus x86_64 and x86
Function Build-App
{
param(
[Parameter(Mandatory=$true)]
[string] $BuildConfig,
[Parameter(Mandatory=$true)]
[string] $BuildArch
)
Invoke-Native-Command -Command "$Env:QtQmakePath" `
-Arguments ("$RootPath\$AppName.pro", "CONFIG+=$BuildConfig $BuildArch $BuildOption", `
"-o", "$BuildPath\Makefile")
Set-Location -Path $BuildPath
if (Get-Command "jom.exe" -ErrorAction SilentlyContinue)
{
echo "Building with jom /J ${Env:NUMBER_OF_PROCESSORS}"
Invoke-Native-Command -Command "jom" -Arguments ("/J", "${Env:NUMBER_OF_PROCESSORS}", "$BuildConfig")
}
else
{
echo "Building with nmake (install Qt jom if you want parallel builds)"
Invoke-Native-Command -Command "nmake" -Arguments ("$BuildConfig")
}
Invoke-Native-Command -Command "$Env:QtWinDeployPath" `
-Arguments ("--$BuildConfig", "--compiler-runtime", "--dir=$DeployPath\$BuildArch",
"$BuildPath\$BuildConfig\$AppName.exe")
Move-Item -Path "$BuildPath\$BuildConfig\$AppName.exe" -Destination "$DeployPath\$BuildArch" -Force
Invoke-Native-Command -Command "nmake" -Arguments ("clean")
Set-Location -Path $RootPath
}
# Build and deploy Jamulus 64bit and 32bit variants
function Build-App-Variants
{
foreach ($_ in ("x86_64", "x86"))
{
$OriginalEnv = Get-ChildItem Env:
if ($_ -eq "x86")
{
Initialize-Build-Environment -BuildArch $_
Initialize-Qt-Build-Environment -QtInstallPath $QtInstallPath32 -QtCompile $QtCompile32
}
else
{
Initialize-Build-Environment -BuildArch $_
Initialize-Qt-Build-Environment -QtInstallPath $QtInstallPath64 -QtCompile $QtCompile64
}
Build-App -BuildConfig "release" -BuildArch $_
$OriginalEnv | % { Set-Item "Env:$($_.Name)" $_.Value }
}
}
# Build Windows installer
Function Build-Installer
{
param(
[string] $BuildOption
)
foreach ($_ in Get-Content -Path "$RootPath\$AppName.pro")
{
if ($_ -Match "^VERSION *= *(.*)$")
{
$AppVersion = $Matches[1]
break
}
}
if ($BuildOption -ne "")
{
Invoke-Native-Command -Command "$RootPath\libs\NSIS\NSIS-source\makensis" `
-Arguments ("/v4", "/DAPP_NAME=$AppName", "/DAPP_VERSION=$AppVersion", `
"/DROOT_PATH=$RootPath", "/DWINDOWS_PATH=$WindowsPath", "/DDEPLOY_PATH=$DeployPath", `
"/DBUILD_OPTION=$BuildOption", `
"$WindowsPath\installer.nsi")
}
else
{
Invoke-Native-Command -Command "$RootPath\libs\NSIS\NSIS-source\makensis" `
-Arguments ("/v4", "/DAPP_NAME=$AppName", "/DAPP_VERSION=$AppVersion", `
"/DROOT_PATH=$RootPath", "/DWINDOWS_PATH=$WindowsPath", "/DDEPLOY_PATH=$DeployPath", `
"$WindowsPath\installer.nsi")
}
}
# Build and copy NS-Process dll
Function Build-NSProcess
{
if (!(Test-Path -path "$RootPath\libs\NSIS\nsProcess.dll")) {
echo "Building nsProcess..."
$OriginalEnv = Get-ChildItem Env:
Initialize-Build-Environment -BuildArch "x86"
Invoke-Native-Command -Command "msbuild" `
-Arguments ("$RootPath\libs\NSIS\nsProcess\nsProcess.sln", '/p:Configuration="Release UNICODE"', `
"/p:Platform=Win32")
Move-Item -Path "$RootPath\libs\NSIS\nsProcess\Release\nsProcess.dll" -Destination "$RootPath\libs\NSIS\nsProcess.dll" -Force
Remove-Item -Path "$RootPath\libs\NSIS\nsProcess\Release\" -Force -Recurse
$OriginalEnv | % { Set-Item "Env:$($_.Name)" $_.Value }
}
}
Clean-Build-Environment
Install-Dependencies
Build-App-Variants
Build-NSProcess
Build-Installer -BuildOption $BuildOption