Use Qt 5.6 and msvc2015 (#197)
[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     if ($compiler.StartsWith("mingw49"))
73     {
74         #remove sh.exe from path
75         $env:PATH=$env:PATH -replace "C:\\Program Files \(x86\)\\Git\\bin", ""
76         $script:MAKE="mingw32-make"
77         $script:CMAKE_GENERATOR="MinGW Makefiles"
78         $script:STRIP=@("strip", "-s")
79         $script:QT_BINARY_DIRS += (Resolve-Path "$qtDir\..\..\Tools\mingw492_32\opt\")
80     }
81     elseif ($compiler.StartsWith("msvc"))
82     {
83         $arch = "x86"
84         if($compiler.EndsWith("64"))
85         {
86             $arch = "amd64"
87         }
88         $compilerDirs = @{
89                 "msvc2010" = "VS100COMNTOOLS";
90                 "msvc2012" = "VS110COMNTOOLS";
91                 "msvc2013" = "VS120COMNTOOLS";
92                 "msvc2015" = "VS140COMNTOOLS"
93             }
94
95         $compilerVar = $compilerDirs[$compiler.Split("_")[0]]
96         $compilerDir = (get-item -path "env:\$($compilerVar)").Value
97         BAT-CALL "$compilerDir\..\..\VC\vcvarsall.bat" $arch
98         $script:MAKE="nmake"
99         $script:CMAKE_GENERATOR="NMake Makefiles"
100         if($arch -eq "x86")
101         {
102             $script:QT_BINARY_DIRS += ("C:\OpenSSL-Win32")
103         }
104         else
105         {
106             $script:QT_BINARY_DIRS += ("C:\OpenSSL-Win64")
107         }
108     }
109 }
110
111 function Install-ChocolatelyModule([string] $module, [string[]] $myargs)
112 {
113     Write-Host "Install chocolately package $module"
114     LogExec cinst $module @myargs -y
115 }
116
117 function Install-CmakeGitModule([string] $url, [hashtable] $arguments)
118 {
119     $module = $url.SubString($url.LastIndexOf("/")+1)
120     $module = $module.Substring(0,$module.Length - 4)
121     if(!$arguments.Contains("branch"))
122     {
123         $arguments["branch"] = "master"
124     }
125     if(!$arguments.Contains("buildType"))
126     {
127         $arguments["buildType"] = "Release"
128     }
129     mkdir -Force $env:APPVEYOR_BUILD_FOLDER\work\build\$module
130     pushd $env:APPVEYOR_BUILD_FOLDER\work\git
131     LogExec git clone -q --depth 1 --branch ([string]$arguments["branch"]) $url $module
132     popd
133     pushd  $env:APPVEYOR_BUILD_FOLDER\work\build\$module
134     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"]
135     LogExec  $script:MAKE install
136     popd
137 }
138
139 function Init([string[]] $chocoDeps, [System.Collections.Specialized.OrderedDictionary] $cmakeModules)
140 {
141     $script:MAKE=""
142     $script:CMAKE_GENERATOR=""
143     $script:STRIP=$null
144
145     mkdir -Force $env:APPVEYOR_BUILD_FOLDER\work\image | Out-Null
146     mkdir -Force $env:APPVEYOR_BUILD_FOLDER\work\build | Out-Null
147
148     SETUP-QT
149
150     if($chocoDeps -contains "ninja") {
151         $script:CMAKE_GENERATOR="Ninja"
152         $script:MAKE="ninja"
153     }
154
155     if ( !(Test-Path "$env:APPVEYOR_BUILD_FOLDER\work\install" ) )
156     {
157         mkdir -Force $env:APPVEYOR_BUILD_FOLDER\work\install | Out-Null
158         mkdir -Force $env:APPVEYOR_BUILD_FOLDER\work\git | Out-Null
159
160         foreach($module in $chocoDeps) {
161             if($module -eq "nsis")
162             {
163                 Install-ChocolatelyModule "nsis.portable" @("-pre")
164                 continue
165             }
166             Install-ChocolatelyModule $module
167         }
168
169         foreach($key in $cmakeModules.Keys) {
170             Install-CmakeGitModule $key $cmakeModules[$key]
171         }
172
173         [string] $compiler=$env:COMPILER
174         if($compiler.StartsWith("msvc"))
175         {
176             Write-Host "Downloading vcredist.exe"
177             if ($compiler.StartsWith("msvc2015"))
178             {
179                 if($compiler.EndsWith("64"))
180                 {
181                     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
182                 }
183                 else
184                 {
185                     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
186                 }
187             }
188             else
189             {
190                 if($compiler.EndsWith("64"))
191                 {
192                     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
193                 }
194                 else
195                 {
196                     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
197                 }
198             }
199         }
200     }
201 }
202
203 function relativePath([string] $root, [string] $path)
204 {
205     pushd $root
206     $out = Resolve-Path -Relative $path
207     popd
208     return $out
209 }
210
211 function StripFile([string] $name)
212 {
213     if($script:STRIP) {
214         if( $name.EndsWith(".dll") -or $name.EndsWith(".exe"))
215         {
216             Write-Host "strip file $name"
217             LogExec @script:STRIP $name
218         }
219     }
220 }
221
222 function Get-DeployImageName()
223 {
224     $version = Get-Version
225     if($env:APPVEYOR_REPO_TAG -eq "true") {
226         return "$env:APPVEYOR_PROJECT_NAME`_$version`_Qt$env:QT_VER`_$env:COMPILER"
227     }else{
228         return "$env:APPVEYOR_PROJECT_NAME`_$env:APPVEYOR_REPO_BRANCH`_$version`_Qt$env:QT_VER`_$env:COMPILER"
229     }
230 }
231
232 function Get-Version()
233 {
234     if($env:APPVEYOR_REPO_TAG -eq "true") {
235         return $env:APPVEYOR_REPO_TAG_NAME
236     }else{
237         $commit = ([string]$env:APPVEYOR_REPO_COMMIT).SubString(0,6)
238         return $commit
239     }
240 }
241
242 function CmakeImageInstall()
243 {
244     $imageName = Get-DeployImageName
245     $destDir = "$env:APPVEYOR_BUILD_FOLDER\work\cmakeDeployImage\$imageName"
246     $env:DESTDIR = $destDir
247     LogExec $script:MAKE install
248     $env:DESTDIR = $null
249     if(!$LastExitCode -eq 0)
250     {
251         Write-Error "Build Failed"
252     }
253     $env:DESTDIR=$null
254     $prefix=$script:INSTALL_DIR
255     if( $prefix.substring(1,1) -eq ":")
256     {
257         $prefix=$prefix.substring(3)
258     }
259     Write-Host "move $destDir\$prefix to $destDir"
260     mv -Force "$destDir\$prefix\*" "$destDir"
261     $rootLeftOver = $prefix.substring(0, $prefix.indexOf("\"))
262     rm -Recurse "$destDir\$rootLeftOver"
263 }
264
265 function CreateDeployImage([string[]] $whiteList, [string[]] $blackList)
266 {
267     $imageName = Get-DeployImageName
268     $deployPath = "$env:APPVEYOR_BUILD_FOLDER\work\deployImage\$imageName"
269
270     function copyWithWhitelist([string] $root)
271     {
272         $files = ls $root -Recurse
273         foreach($fileName in $files.FullName)
274         {
275             $relPath = (relativePath $root $fileName).SubString(2)
276             if($whiteList | Where {$relPath -match $_})
277             {
278                 if($blackList | Where {$relPath -match $_})
279                 {
280                     continue
281                 }
282                 if(!(Test-Path $deployPath\$relPath))
283                 {
284                     Write-Host "copy $fileName to $deployPath\$relPath"
285                     mkdir -Force (Split-Path -Parent $deployPath\$relPath) | Out-Null
286                     cp -Force $fileName $deployPath\$relPath
287                     StripFile $deployPath\$relPath
288                 }
289             }
290         }
291     }
292     Write-Host "CreateDeployImage $imageName"
293     mkdir $deployPath | Out-Null
294
295     copyWithWhitelist "$env:APPVEYOR_BUILD_FOLDER\work\cmakeDeployImage\$imageName"
296     copyWithWhitelist "$env:APPVEYOR_BUILD_FOLDER\work\install\"
297     foreach($folder in $script:QT_BINARY_DIRS)
298     {
299         copyWithWhitelist $folder
300     }
301     Write-Host "Deploy path $deployPath"
302     return $deployPath
303 }
304
305 function 7ZipDeployImage()
306 {
307     $imageName = Get-DeployImageName
308     LogExec 7za a "$env:APPVEYOR_BUILD_FOLDER\work\deployImage\$imageName.7z" "$env:APPVEYOR_BUILD_FOLDER\work\deployImage\$imageName"
309     Push-AppveyorArtifact "$env:APPVEYOR_BUILD_FOLDER\work\deployImage\$imageName.7z"
310 }
311
312 function NsisDeployImage([string] $scriptName)
313 {
314     $imageName = Get-DeployImageName
315     $installerName = "$env:APPVEYOR_BUILD_FOLDER\work\deployImage\$imageName.exe"
316     $version = Get-Version
317     if(([string]$env:COMPILER).StartsWith("msvc"))
318     {
319         $redist = "$env:APPVEYOR_BUILD_FOLDER\work\install\vcredist.exe"
320     }else{
321         $redist = "none"
322     }
323     if($env:COMPILER.EndsWith("64"))
324     {
325         $defaultinstdir = "`$PROGRAMFILES64"
326     }else{
327         $defaultinstdir = "`$PROGRAMFILES"
328     }
329     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
330     Push-AppveyorArtifact $installerName
331 }
332
333 # based on http://thesurlyadmin.com/2013/01/07/remove-empty-directories-recursively/
334 function DeleteEmptyFodlers([string] $root)
335 {
336     $Folders = @()
337     foreach($Folder in (Get-ChildItem -Path $root -Recurse -Directory))
338        {
339             $Folders += New-Object PSObject -Property @{
340                 Object = $Folder
341                 Depth = ($Folder.FullName.Split("\")).Count
342             }
343     }
344     $Folders = $Folders | Sort Depth -Descending
345
346     foreach($Folder in $Folders)
347     {
348        If ($Folder.Object.GetFileSystemInfos().Count -eq 0)
349        {
350             Write-Host "Delete empty dir:" $Folder.Object.FullName
351             Remove-Item -Path $Folder.Object.FullName -Force
352        }
353     }
354
355 }
356
357 Write-Host "CMAKE_INSTALL_ROOT: $CMAKE_INSTALL_ROOT"
358 Write-Host "Image-Name: ", (Get-DeployImageName)
359
360 Export-ModuleMember -Function @("Init","CmakeImageInstall", "CreateDeployImage", "LogExec", "7ZipDeployImage", "NsisDeployImage", "DeleteEmptyFodlers") -Variable @("CMAKE_INSTALL_ROOT")