-
Notifications
You must be signed in to change notification settings - Fork 162
/
appveyorHelp.psm1
362 lines (328 loc) · 11.3 KB
/
appveyorHelp.psm1
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
Write-Host "Appveyor Helper scrips https://github.com/TheOneRing/appVeyorHelp"
$ErrorActionPreference="Stop"
$script:INSTALL_DIR="$env:APPVEYOR_BUILD_FOLDER\work\install"
$CMAKE_INSTALL_ROOT="`"$INSTALL_DIR`"" -replace "\\", "/"
$env:PATH="$env:PATH;$script:INSTALL_DIR"
if(!$env:CI -eq "true")
{
function Push-AppveyorArtifact()
{
Write-Host "Push-AppveyorArtifact $ARGS"
}
function Start-FileDownload([string] $url, [string] $out)
{
if(!$out)
{
$out = $url.SubString($url.LastIndexOf("/"))
}
wget $url -Outfile $out
}
}
function LogExec()
{
$OldErrorActionPreference=$ErrorActionPreference
$ErrorActionPreference="Continue"
$LastExitCode = 0
Write-Host $Args[0], $Args[1..(($Args.Count)-1)]
& $Args[0] $Args[1..(($Args.Count)-1)]
if(!$LastExitCode -eq 0)
{
exit $LastExitCode
}
$ErrorActionPreference=$OldErrorActionPreference
}
#Set environment variables for Visual Studio Command Prompt
#http://stackoverflow.com/questions/2124753/how-i-can-use-powershell-with-the-visual-studio-command-prompt
function BAT-CALL([string] $path, [string] $arg)
{
Write-Host "Calling `"$path`" `"$arg`""
cmd /c "$path" "$arg" `&`& set `|`| exit 1|
foreach {
if ($_ -match "=") {
$v = $_.split("=")
#Write-Host "ENV:\$($v[0])=$($v[1])"
set-item -force -path "ENV:\$($v[0])" -value "$($v[1])"
}
}
if($LastExitCode -eq 1) {
Write-Error "$path not found."
}
}
function Get-QtDir()
{
$ver = 5.5
if($env:QT_VER)
{
$ver = $env:QT_VER
}
return "C:\Qt\$ver\$env:COMPILER\"
}
function SETUP-QT()
{
[string] $compiler=$env:COMPILER
$qtDir = Get-QtDir
$script:QT_BINARY_DIRS = @($qtDir)
BAT-CALL "$qtDir\bin\qtenv2.bat"
if ($compiler.StartsWith("mingw49"))
{
#remove sh.exe from path
$env:PATH=$env:PATH -replace "C:\\Program Files \(x86\)\\Git\\bin", ""
$script:MAKE="mingw32-make"
$script:CMAKE_GENERATOR="MinGW Makefiles"
$script:STRIP=@("strip", "-s")
$script:QT_BINARY_DIRS += (Resolve-Path "$qtDir\..\..\Tools\mingw492_32\opt\")
}
elseif ($compiler.StartsWith("msvc"))
{
$arch = "x86"
if($compiler.EndsWith("64"))
{
$arch = "amd64"
}
$compilerDirs = @{
"msvc2010" = "VS100COMNTOOLS";
"msvc2012" = "VS110COMNTOOLS";
"msvc2013" = "VS120COMNTOOLS";
"msvc2015" = "VS140COMNTOOLS"
}
$compilerVar = $compilerDirs[$compiler.Split("_")[0]]
$compilerDir = (get-item -path "env:\$($compilerVar)").Value
BAT-CALL "$compilerDir\..\..\VC\vcvarsall.bat" $arch
$script:MAKE="nmake"
$script:CMAKE_GENERATOR="NMake Makefiles"
if($arch -eq "x86")
{
$script:QT_BINARY_DIRS += ("C:\OpenSSL-Win32")
}
else
{
$script:QT_BINARY_DIRS += ("C:\OpenSSL-Win64")
}
}
}
function Install-ChocolatelyModule([string] $module, [string[]] $myargs)
{
Write-Host "Install chocolately package $module"
LogExec cinst $module @myargs -y
}
function Install-CmakeGitModule([string] $url, [hashtable] $arguments)
{
$module = $url.SubString($url.LastIndexOf("/")+1)
$module = $module.Substring(0,$module.Length - 4)
if(!$arguments.Contains("branch"))
{
$arguments["branch"] = "master"
}
if(!$arguments.Contains("buildType"))
{
$arguments["buildType"] = "Release"
}
mkdir -Force $env:APPVEYOR_BUILD_FOLDER\work\build\$module
pushd $env:APPVEYOR_BUILD_FOLDER\work\git
LogExec git clone -q --depth 1 --branch ([string]$arguments["branch"]) $url $module
popd
pushd $env:APPVEYOR_BUILD_FOLDER\work\build\$module
LogExec cmake -G $script:CMAKE_GENERATOR ("-DCMAKE_BUILD_TYPE=`"{0}`"" -f [string]$arguments["buildType"]) $env:APPVEYOR_BUILD_FOLDER\work\git\$module -DCMAKE_INSTALL_PREFIX="$CMAKE_INSTALL_ROOT" $arguments["options"]
LogExec $script:MAKE install
popd
}
function Init([string[]] $chocoDeps, [System.Collections.Specialized.OrderedDictionary] $cmakeModules)
{
$script:MAKE=""
$script:CMAKE_GENERATOR=""
$script:STRIP=$null
mkdir -Force $env:APPVEYOR_BUILD_FOLDER\work\image | Out-Null
mkdir -Force $env:APPVEYOR_BUILD_FOLDER\work\build | Out-Null
SETUP-QT
if($chocoDeps -contains "ninja") {
$script:CMAKE_GENERATOR="Ninja"
$script:MAKE="ninja"
}
if ( !(Test-Path "$env:APPVEYOR_BUILD_FOLDER\work\install" ) )
{
mkdir -Force $env:APPVEYOR_BUILD_FOLDER\work\install | Out-Null
mkdir -Force $env:APPVEYOR_BUILD_FOLDER\work\git | Out-Null
foreach($module in $chocoDeps) {
if($module -eq "nsis")
{
Install-ChocolatelyModule "nsis.portable" @("-pre")
continue
}
Install-ChocolatelyModule $module
}
foreach($key in $cmakeModules.Keys) {
Install-CmakeGitModule $key $cmakeModules[$key]
}
[string] $compiler=$env:COMPILER
if($compiler.StartsWith("msvc"))
{
Write-Host "Downloading vcredist.exe"
if ($compiler.StartsWith("msvc2015"))
{
if($compiler.EndsWith("64"))
{
Start-FileDownload https://download.microsoft.com/download/9/3/F/93FCF1E7-E6A4-478B-96E7-D4B285925B00/vc_redist.x64.exe $env:APPVEYOR_BUILD_FOLDER\work\install\vcredist.exe
}
else
{
Start-FileDownload https://download.microsoft.com/download/9/3/F/93FCF1E7-E6A4-478B-96E7-D4B285925B00/vc_redist.x86.exe $env:APPVEYOR_BUILD_FOLDER\work\install\vcredist.exe
}
}
else
{
if($compiler.EndsWith("64"))
{
Start-FileDownload http://download.microsoft.com/download/2/E/6/2E61CFA4-993B-4DD4-91DA-3737CD5CD6E3/vcredist_x64.exe $env:APPVEYOR_BUILD_FOLDER\work\install\vcredist.exe
}
else
{
Start-FileDownload http://download.microsoft.com/download/2/E/6/2E61CFA4-993B-4DD4-91DA-3737CD5CD6E3/vcredist_x86.exe $env:APPVEYOR_BUILD_FOLDER\work\install\vcredist.exe
}
}
}
}
}
function relativePath([string] $root, [string] $path)
{
pushd $root
$out = Resolve-Path -Relative $path
popd
return $out
}
function StripFile([string] $name)
{
if($script:STRIP) {
if( $name.EndsWith(".dll") -or $name.EndsWith(".exe"))
{
Write-Host "strip file $name"
LogExec @script:STRIP $name
}
}
}
function Get-DeployImageName()
{
$version = Get-Version
if($env:APPVEYOR_REPO_TAG -eq "true") {
return "$env:APPVEYOR_PROJECT_NAME`_$version`_Qt$env:QT_VER`_$env:COMPILER"
}else{
return "$env:APPVEYOR_PROJECT_NAME`_$env:APPVEYOR_REPO_BRANCH`_$version`_Qt$env:QT_VER`_$env:COMPILER"
}
}
function Get-Version()
{
if($env:APPVEYOR_REPO_TAG -eq "true") {
return $env:APPVEYOR_REPO_TAG_NAME
}else{
$commit = ([string]$env:APPVEYOR_REPO_COMMIT).SubString(0,6)
return $commit
}
}
function CmakeImageInstall()
{
$imageName = Get-DeployImageName
$destDir = "$env:APPVEYOR_BUILD_FOLDER\work\cmakeDeployImage\$imageName"
$env:DESTDIR = $destDir
LogExec $script:MAKE install
$env:DESTDIR = $null
if(!$LastExitCode -eq 0)
{
Write-Error "Build Failed"
}
$env:DESTDIR=$null
$prefix=$script:INSTALL_DIR
if( $prefix.substring(1,1) -eq ":")
{
$prefix=$prefix.substring(3)
}
Write-Host "move $destDir\$prefix to $destDir"
mv -Force "$destDir\$prefix\*" "$destDir"
$rootLeftOver = $prefix.substring(0, $prefix.indexOf("\"))
rm -Recurse "$destDir\$rootLeftOver"
}
function CreateDeployImage([string[]] $whiteList, [string[]] $blackList)
{
$imageName = Get-DeployImageName
$deployPath = "$env:APPVEYOR_BUILD_FOLDER\work\deployImage\$imageName"
function copyWithWhitelist([string] $root)
{
$files = ls $root -Recurse
foreach($fileName in $files.FullName)
{
$relPath = (relativePath $root $fileName).SubString(2)
if($whiteList | Where {$relPath -match $_})
{
if($blackList | Where {$relPath -match $_})
{
continue
}
if(!(Test-Path $deployPath\$relPath))
{
Write-Host "copy $fileName to $deployPath\$relPath"
mkdir -Force (Split-Path -Parent $deployPath\$relPath) | Out-Null
cp -Force $fileName $deployPath\$relPath
StripFile $deployPath\$relPath
}
}
}
}
Write-Host "CreateDeployImage $imageName"
mkdir $deployPath | Out-Null
copyWithWhitelist "$env:APPVEYOR_BUILD_FOLDER\work\cmakeDeployImage\$imageName"
copyWithWhitelist "$env:APPVEYOR_BUILD_FOLDER\work\install\"
foreach($folder in $script:QT_BINARY_DIRS)
{
copyWithWhitelist $folder
}
Write-Host "Deploy path $deployPath"
return $deployPath
}
function 7ZipDeployImage()
{
$imageName = Get-DeployImageName
LogExec 7za a "$env:APPVEYOR_BUILD_FOLDER\work\deployImage\$imageName.7z" "$env:APPVEYOR_BUILD_FOLDER\work\deployImage\$imageName"
Push-AppveyorArtifact "$env:APPVEYOR_BUILD_FOLDER\work\deployImage\$imageName.7z"
}
function NsisDeployImage([string] $scriptName)
{
$imageName = Get-DeployImageName
$installerName = "$env:APPVEYOR_BUILD_FOLDER\work\deployImage\$imageName.exe"
$version = Get-Version
if(([string]$env:COMPILER).StartsWith("msvc"))
{
$redist = "$env:APPVEYOR_BUILD_FOLDER\work\install\vcredist.exe"
}else{
$redist = "none"
}
if($env:COMPILER.EndsWith("64"))
{
$defaultinstdir = "`$PROGRAMFILES64"
}else{
$defaultinstdir = "`$PROGRAMFILES"
}
LogExec makensis.exe /DgitDir=$env:APPVEYOR_BUILD_FOLDER /Dsetupname=$installerName /Dcaption=$imageName /Dversion=$version /Dcompiler=$env:COMPILER /Dvcredist=$redist /Ddefaultinstdir=$defaultinstdir /Dsrcdir=$env:APPVEYOR_BUILD_FOLDER\work\deployImage\$imageName $scriptName
Push-AppveyorArtifact $installerName
}
# based on http://thesurlyadmin.com/2013/01/07/remove-empty-directories-recursively/
function DeleteEmptyFodlers([string] $root)
{
$Folders = @()
foreach($Folder in (Get-ChildItem -Path $root -Recurse -Directory))
{
$Folders += New-Object PSObject -Property @{
Object = $Folder
Depth = ($Folder.FullName.Split("\")).Count
}
}
$Folders = $Folders | Sort Depth -Descending
foreach($Folder in $Folders)
{
If ($Folder.Object.GetFileSystemInfos().Count -eq 0)
{
Write-Host "Delete empty dir:" $Folder.Object.FullName
Remove-Item -Path $Folder.Object.FullName -Force
}
}
}
Write-Host "CMAKE_INSTALL_ROOT: $CMAKE_INSTALL_ROOT"
Write-Host "Image-Name: ", (Get-DeployImageName)
Export-ModuleMember -Function @("Init","CmakeImageInstall", "CreateDeployImage", "LogExec", "7ZipDeployImage", "NsisDeployImage", "DeleteEmptyFodlers") -Variable @("CMAKE_INSTALL_ROOT")