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