uisupport: Also fix doubleclick in nick views
[quassel.git] / scripts / build / appveyorHelp.psm1
1 $ErrorActionPreference="Stop"
2
3 $script:INSTALL_DIR="$env:APPVEYOR_BUILD_FOLDER\work\install"
4 $CMAKE_INSTALL_ROOT="`"$INSTALL_DIR`"" -replace "\\", "/"
5 $env:PATH="$env:PATH;$script:INSTALL_DIR"
6
7 if(!$env:CI -eq "true")
8 {
9     function Push-AppveyorArtifact()
10     {
11         Write-Host "Push-AppveyorArtifact $ARGS"
12     }
13     function Start-FileDownload([string] $url, [string] $out)
14     {
15         if(!$out)
16         {
17             $out = $url.SubString($url.LastIndexOf("/"))
18         }
19         wget $url -Outfile $out
20     }
21 }
22
23 function LogExec()
24 {
25     $OldErrorActionPreference=$ErrorActionPreference
26     $ErrorActionPreference="Continue"
27     $LastExitCode = 0
28     Write-Host $Args[0], $Args[1..(($Args.Count)-1)]
29     & $Args[0] $Args[1..(($Args.Count)-1)]
30     if(!$LastExitCode -eq 0)
31     {
32         exit $LastExitCode
33     }
34     $ErrorActionPreference=$OldErrorActionPreference
35 }
36
37 #Set environment variables for Visual Studio Command Prompt
38 #http://stackoverflow.com/questions/2124753/how-i-can-use-powershell-with-the-visual-studio-command-prompt
39 function BAT-CALL([string] $path, [string] $arg)
40 {
41     Write-Host "Calling `"$path`" `"$arg`""
42     cmd /c  "$path" "$arg" `&`& set `|`| exit 1|
43     foreach {
44       if ($_ -match "=") {
45         $v = $_.split("=")
46         #Write-Host "ENV:\$($v[0])=$($v[1])"
47         set-item -force -path "ENV:\$($v[0])"  -value "$($v[1])"
48       }
49     }
50     if($LastExitCode -eq 1) {
51         Write-Error "$path not found."
52     }
53 }
54
55 function Get-QtDir()
56 {
57     $ver = 5.5
58     if($env:QT_VER)
59     {
60         $ver = $env:QT_VER
61     }
62     return "C:\Qt\$ver\$env:COMPILER\"
63 }
64
65 function SETUP-QT()
66 {
67     [string] $compiler=$env:COMPILER
68     $qtDir = Get-QtDir
69     $script:QT_BINARY_DIRS = @($qtDir)
70
71     BAT-CALL  "$qtDir\bin\qtenv2.bat"
72
73     if ($compiler.StartsWith("mingw"))
74     {
75         # supported values are
76         #mingw49_32
77         #mingw53_32
78         #remove sh.exe from path
79         $env:PATH=$env:PATH -replace "C:\\Program Files \(x86\)\\Git\\bin", ""
80         $script:MAKE="mingw32-make"
81         $script:CMAKE_GENERATOR="MinGW Makefiles"
82         $script:STRIP=@("strip", "-s")
83         if ($compiler -eq "mingw49_32")
84         {
85             $script:QT_BINARY_DIRS += (Resolve-Path "$qtDir\..\..\Tools\mingw492_32\opt\")
86         }
87         elseif ($compiler -eq "mingw53_32")
88         {
89             $script:QT_BINARY_DIRS += (Resolve-Path "$qtDir\..\..\Tools\mingw530_32\opt\")
90         }
91     }
92     elseif ($compiler.StartsWith("msvc"))
93     {
94         $arch = "x86"
95         if($compiler.EndsWith("64"))
96         {
97             $arch = "amd64"
98         }
99         $compilerDirs = @{
100                 "msvc2010" = "VS100COMNTOOLS";
101                 "msvc2012" = "VS110COMNTOOLS";
102                 "msvc2013" = "VS120COMNTOOLS";
103                 "msvc2015" = "VS140COMNTOOLS"
104             }
105
106         $compilerVar = $compilerDirs[$compiler.Split("_")[0]]
107         $compilerDir = (get-item -path "env:\$($compilerVar)").Value
108         BAT-CALL "$compilerDir\..\..\VC\vcvarsall.bat" $arch
109         $script:MAKE="nmake"
110         $script:CMAKE_GENERATOR="NMake Makefiles"
111         if($arch -eq "x86")
112         {
113             $script:QT_BINARY_DIRS += ("C:\OpenSSL-Win32")
114         }
115         else
116         {
117             $script:QT_BINARY_DIRS += ("C:\OpenSSL-Win64")
118         }
119     }
120 }
121
122 function Install-ChocolateyModule([string] $module, [string[]] $myargs)
123 {
124     Write-Host "Install chocolatey package $module"
125     LogExec appveyor-retry cinst $module @myargs -y
126     # Retry installation in case it fails; remove 'appveyor-retry' to run in a generic manner
127     # See http://help.appveyor.com/discussions/suggestions/816-generic-wrapper-for-retry#comment_40579488
128     # And https://github.com/appveyor/ci/issues/418
129 }
130
131 function Install-CmakeGitModule([string] $url, [hashtable] $arguments)
132 {
133     $module = $url.SubString($url.LastIndexOf("/")+1)
134     $module = $module.Substring(0,$module.Length - 4)
135     if(!$arguments.Contains("branch"))
136     {
137         $arguments["branch"] = "master"
138     }
139     if(!$arguments.Contains("buildType"))
140     {
141         $arguments["buildType"] = "Release"
142     }
143     mkdir -Force $env:APPVEYOR_BUILD_FOLDER\work\build\$module
144     pushd $env:APPVEYOR_BUILD_FOLDER\work\git
145     LogExec git clone -q --depth 1 --branch ([string]$arguments["branch"]) $url $module
146     popd
147     pushd  $env:APPVEYOR_BUILD_FOLDER\work\build\$module
148     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"]
149     LogExec  $script:MAKE install
150     popd
151 }
152
153 function Init([string[]] $chocoDeps, [System.Collections.Specialized.OrderedDictionary] $cmakeModules)
154 {
155     $script:MAKE=""
156     $script:CMAKE_GENERATOR=""
157     $script:STRIP=$null
158
159     mkdir -Force $env:APPVEYOR_BUILD_FOLDER\work\image | Out-Null
160     mkdir -Force $env:APPVEYOR_BUILD_FOLDER\work\build | Out-Null
161
162     SETUP-QT
163
164     if($chocoDeps -contains "ninja") {
165         $script:CMAKE_GENERATOR="Ninja"
166         $script:MAKE="ninja"
167     }
168
169     if ( !(Test-Path "$env:APPVEYOR_BUILD_FOLDER\work\install" ) )
170     {
171         mkdir -Force $env:APPVEYOR_BUILD_FOLDER\work\install | Out-Null
172         mkdir -Force $env:APPVEYOR_BUILD_FOLDER\work\git | Out-Null
173
174         foreach($module in $chocoDeps) {
175             if($module -eq "nsis")
176             {
177                 Install-ChocolateyModule "nsis.portable" @("-pre")
178                 continue
179             }
180             Install-ChocolateyModule $module
181         }
182
183         foreach($key in $cmakeModules.Keys) {
184             Install-CmakeGitModule $key $cmakeModules[$key]
185         }
186
187         [string] $compiler=$env:COMPILER
188         if($compiler.StartsWith("msvc"))
189         {
190             Write-Host "Downloading vcredist.exe"
191             if ($compiler.StartsWith("msvc2015"))
192             {
193                 if($compiler.EndsWith("64"))
194                 {
195                     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
196                 }
197                 else
198                 {
199                     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
200                 }
201             }
202             else
203             {
204                 if($compiler.EndsWith("64"))
205                 {
206                     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
207                 }
208                 else
209                 {
210                     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
211                 }
212             }
213         }
214     }
215 }
216
217 function relativePath([string] $root, [string] $path)
218 {
219     pushd $root
220     $out = Resolve-Path -Relative $path
221     popd
222     return $out
223 }
224
225 function StripFile([string] $name)
226 {
227     if($script:STRIP) {
228         if( $name.EndsWith(".dll") -or $name.EndsWith(".exe"))
229         {
230             Write-Host "strip file $name"
231             LogExec @script:STRIP $name
232         }
233     }
234 }
235
236 function Get-DeployImageName()
237 {
238     $version = Get-Version
239     if($env:APPVEYOR_REPO_TAG -eq "true") {
240         return "$env:APPVEYOR_PROJECT_NAME`_$version`_Qt$env:QT_VER`_$env:COMPILER"
241     }else{
242         return "$env:APPVEYOR_PROJECT_NAME`_$env:APPVEYOR_REPO_BRANCH`_$version`_Qt$env:QT_VER`_$env:COMPILER"
243     }
244 }
245
246 function Get-Version()
247 {
248     if($env:APPVEYOR_REPO_TAG -eq "true") {
249         return $env:APPVEYOR_REPO_TAG_NAME
250     }else{
251         $commit = ([string]$env:APPVEYOR_REPO_COMMIT).SubString(0,6)
252         return $commit
253     }
254 }
255
256 function CmakeImageInstall()
257 {
258     $imageName = Get-DeployImageName
259     $destDir = "$env:APPVEYOR_BUILD_FOLDER\work\cmakeDeployImage\$imageName"
260     $env:DESTDIR = $destDir
261     LogExec $script:MAKE install
262     $env:DESTDIR = $null
263     if(!$LastExitCode -eq 0)
264     {
265         Write-Error "Build Failed"
266     }
267     $env:DESTDIR=$null
268     $prefix=$script:INSTALL_DIR
269     if( $prefix.substring(1,1) -eq ":")
270     {
271         $prefix=$prefix.substring(3)
272     }
273     Write-Host "move $destDir\$prefix to $destDir"
274     mv -Force "$destDir\$prefix\*" "$destDir"
275     $rootLeftOver = $prefix.substring(0, $prefix.indexOf("\"))
276     rm -Recurse "$destDir\$rootLeftOver"
277 }
278
279 function CreateDeployImage([string[]] $whiteList, [string[]] $blackList)
280 {
281     $imageName = Get-DeployImageName
282     $deployPath = "$env:APPVEYOR_BUILD_FOLDER\work\deployImage\$imageName"
283
284     function copyWithWhitelist([string] $root)
285     {
286         $files = ls $root -Recurse
287         foreach($fileName in $files.FullName)
288         {
289             $relPath = (relativePath $root $fileName).SubString(2)
290             if($whiteList | Where {$relPath -match $_})
291             {
292                 if($blackList | Where {$relPath -match $_})
293                 {
294                     continue
295                 }
296                 if(!(Test-Path $deployPath\$relPath))
297                 {
298                     Write-Host "copy $fileName to $deployPath\$relPath"
299                     mkdir -Force (Split-Path -Parent $deployPath\$relPath) | Out-Null
300                     cp -Force $fileName $deployPath\$relPath
301                     StripFile $deployPath\$relPath
302                 }
303             }
304         }
305     }
306     Write-Host "CreateDeployImage $imageName"
307     mkdir $deployPath | Out-Null
308
309     copyWithWhitelist "$env:APPVEYOR_BUILD_FOLDER\work\cmakeDeployImage\$imageName"
310     copyWithWhitelist "$env:APPVEYOR_BUILD_FOLDER\work\install\"
311     foreach($folder in $script:QT_BINARY_DIRS)
312     {
313         copyWithWhitelist $folder
314     }
315     Write-Host "Deploy path $deployPath"
316     return $deployPath
317 }
318
319 function 7ZipDeployImage()
320 {
321     $imageName = Get-DeployImageName
322     LogExec 7za a "$env:APPVEYOR_BUILD_FOLDER\work\deployImage\$imageName.7z" "$env:APPVEYOR_BUILD_FOLDER\work\deployImage\$imageName"
323     Push-AppveyorArtifact "$env:APPVEYOR_BUILD_FOLDER\work\deployImage\$imageName.7z"
324 }
325
326 function NsisDeployImage([string] $scriptName)
327 {
328     $imageName = Get-DeployImageName
329     $installerName = "$env:APPVEYOR_BUILD_FOLDER\work\deployImage\$imageName.exe"
330     $version = Get-Version
331     if(([string]$env:COMPILER).StartsWith("msvc"))
332     {
333         $redist = "$env:APPVEYOR_BUILD_FOLDER\work\install\vcredist.exe"
334     }else{
335         $redist = "none"
336     }
337     if($env:COMPILER.EndsWith("64"))
338     {
339         $defaultinstdir = "`$PROGRAMFILES64"
340     }else{
341         $defaultinstdir = "`$PROGRAMFILES"
342     }
343     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
344     Push-AppveyorArtifact $installerName
345 }
346
347 # based on http://thesurlyadmin.com/2013/01/07/remove-empty-directories-recursively/
348 function DeleteEmptyFodlers([string] $root)
349 {
350     $Folders = @()
351     foreach($Folder in (Get-ChildItem -Path $root -Recurse -Directory))
352        {
353             $Folders += New-Object PSObject -Property @{
354                 Object = $Folder
355                 Depth = ($Folder.FullName.Split("\")).Count
356             }
357     }
358     $Folders = $Folders | Sort Depth -Descending
359
360     foreach($Folder in $Folders)
361     {
362        If ($Folder.Object.GetFileSystemInfos().Count -eq 0)
363        {
364             Write-Host "Delete empty dir:" $Folder.Object.FullName
365             Remove-Item -Path $Folder.Object.FullName -Force
366        }
367     }
368
369 }
370
371 Write-Host "CMAKE_INSTALL_ROOT: $CMAKE_INSTALL_ROOT"
372 Write-Host "Image-Name: ", (Get-DeployImageName)
373
374 Export-ModuleMember -Function @("Init","CmakeImageInstall", "CreateDeployImage", "LogExec", "7ZipDeployImage", "NsisDeployImage", "DeleteEmptyFodlers") -Variable @("CMAKE_INSTALL_ROOT")