forked from jamulussoftware/jamulus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy_windows.ps1
318 lines (268 loc) · 10.1 KB
/
deploy_windows.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
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
param(
# Replace default path with system Qt installation folder if necessary
[string] $QtInstallPath = "C:\Qt\5.15.2",
[string] $QtCompile32 = "msvc2019",
[string] $QtCompile64 = "msvc2019_64",
[string] $AsioSDKName = "asiosdk_2.3.3_2019-06-14",
[string] $AsioSDKUrl = "https://download.steinberg.net/sdk_downloads/asiosdk_2.3.3_2019-06-14.zip",
[string] $NsisName = "nsis-3.06.1",
[string] $NsisUrl = "https://downloads.sourceforge.net/project/nsis/NSIS%203/3.06.1/nsis-3.06.1.zip",
[string] $BuildOption = ""
)
# 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"
# Stop at all errors
$ErrorActionPreference = "Stop"
# 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
)
$request = [System.Net.WebRequest]::Create($url)
$request.AllowAutoRedirect=$false
$response=$request.GetResponse()
if ($response.StatusCode -eq "Found")
{
$response.GetResponseHeader("Location")
}
}
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] $Name,
[Parameter(Mandatory=$true)]
[string] $Destination
)
if (Test-Path -Path "$WindowsPath\$Destination") { return }
$TempFileName = [System.IO.Path]::GetTempFileName() + ".zip"
$TempDir = [System.IO.Path]::GetTempPath()
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
Move-Item -Path "$TempDir\$Name" -Destination "$WindowsPath\$Destination" -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 $AsioSDKUrl `
-Name $AsioSDKName -Destination "ASIOSDK2"
Install-Dependency -Uri $NsisUrl `
-Name $NsisName -Destination "NSIS"
}
# Setup environment variables and build tool paths
Function Initialize-Build-Environment
{
param(
[Parameter(Mandatory=$true)]
[string] $QtInstallPath,
[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"
$QtMsvcSpecPath = "$QtInstallPath\$QtCompile64\bin"
}
else
{
$VcVarsBin = "$VsInstallPath\VC\Auxiliary\build\vcvars32.bat"
$QtMsvcSpecPath = "$QtInstallPath\$QtCompile32\bin"
}
# Setup Qt executables paths for later calls
Set-Item Env:QtQmakePath "$QtMsvcSpecPath\qmake.exe"
Set-Item Env:QtWinDeployPath "$QtMsvcSpecPath\windeployqt.exe"
""
"**********************************************************************"
"Using Visual Studio/Build Tools environment settings located at"
$VcVarsBin
"**********************************************************************"
""
"**********************************************************************"
"Using Qt binaries for Visual C++ located at"
$QtMsvcSpecPath
"**********************************************************************"
""
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."
}
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"
}
# 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
}
# 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
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
{
param(
[Parameter(Mandatory=$true)]
[string] $QtInstallPath
)
foreach ($_ in ("x86_64", "x86"))
{
$OriginalEnv = Get-ChildItem Env:
Initialize-Build-Environment -QtInstallPath $QtInstallPath -BuildArch $_
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 "$WindowsPath\NSIS\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 "$WindowsPath\NSIS\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
{
param(
[Parameter(Mandatory=$true)]
[string] $QtInstallPath
)
if (!(Test-Path -path "$WindowsPath\nsProcess.dll")) {
echo "Building nsProcess..."
$OriginalEnv = Get-ChildItem Env:
Initialize-Build-Environment -QtInstallPath $QtInstallPath -BuildArch "x86"
Invoke-Native-Command -Command "msbuild" `
-Arguments ("$WindowsPath\nsProcess\nsProcess.sln", '/p:Configuration="Release UNICODE"', `
"/p:Platform=Win32")
Move-Item -Path "$WindowsPath\nsProcess\Release\nsProcess.dll" -Destination "$WindowsPath\nsProcess.dll" -Force
Remove-Item -Path "$WindowsPath\nsProcess\Release\" -Force -Recurse
$OriginalEnv | % { Set-Item "Env:$($_.Name)" $_.Value }
}
}
Clean-Build-Environment
Install-Dependencies
Build-App-Variants -QtInstallPath $QtInstallPath
Build-NSProcess -QtInstallPath $QtInstallPath
Build-Installer -BuildOption $BuildOption