Fix fully-connected layer in case of number of rows less than 4
[platform/upstream/opencv.git] / platforms / winrt / setup_winrt.ps1
1 <#
2 Copyright (c) Microsoft Open Technologies, Inc.
3 All rights reserved.
4
5 (3-clause BSD License)
6
7 Redistribution and use in source and binary forms, with or without modification, are permitted provided that
8 the following conditions are met:
9
10 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
11 following disclaimer.
12
13 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
14 following disclaimer in the documentation and/or other materials provided with the distribution.
15
16 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or
17 promote products derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
20 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
22 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27 #>
28
29 [CmdletBinding()]
30 Param(
31     [parameter(Mandatory=$False)]
32     [switch]
33     $HELP,
34
35     [parameter(Mandatory=$False)]
36     [switch]
37     $BUILD,
38
39     [parameter(Mandatory=$False)]
40     [Array]
41     [ValidateNotNull()]
42     $PLATFORMS_IN = "WP",
43
44     [parameter(Mandatory=$False)]
45     [Array]
46     [ValidateNotNull()]
47     $VERSIONS_IN = "8.1",
48
49     [parameter(Mandatory=$False)]
50     [Array]
51     [ValidateNotNull()]
52     $ARCHITECTURES_IN = "x86",
53
54     [parameter(Mandatory=$False)]
55     [String]
56     $TESTS = "None",
57
58     [parameter(Mandatory=$False)]
59     [String]
60     [ValidateNotNull()]
61     [ValidateSet("Visual Studio 15 2017","Visual Studio 14 2015","Visual Studio 12 2013","Visual Studio 11 2012")]
62     $GENERATOR = "Visual Studio 15 2017",
63
64     [parameter(Mandatory=$False)]
65     [String]
66     $INSTALL
67 )
68
69
70 Function L() {
71     Param(
72         [parameter(Mandatory=$true)]
73         [String]
74         [ValidateNotNull()]
75         $str
76     )
77
78     Write-Host "INFO> $str"
79 }
80
81 Function D() {
82     Param(
83         [parameter(Mandatory=$true)]
84         [String]
85         [ValidateNotNull()]
86         $str
87     )
88
89     # Use this trigger to toggle debug output
90     [bool]$debug = $true
91
92     if ($debug) {
93         Write-Host "DEBUG> $str"
94     }
95 }
96
97 function Get-Batchfile ($file) {
98     $cmd = "`"$file`" & set"
99     cmd /c $cmd | Foreach-Object {
100         $p, $v = $_.split('=')
101         Set-Item -path env:$p -value $v
102     }
103 }
104
105 # Enables access to Visual Studio variables via "vsvars32.bat"
106 function Set-VS12()
107 {
108     Try {
109         $vs12comntools = (Get-ChildItem env:VS120COMNTOOLS).Value
110         $batchFile = [System.IO.Path]::Combine($vs12comntools, "vsvars32.bat")
111         Get-Batchfile $BatchFile
112         [System.Console]::Title = "Visual Studio 2010 Windows PowerShell"
113      } Catch {
114         $ErrorMessage = $_.Exception.Message
115         L "Error: $ErrorMessage"
116         return $false
117      }
118      return $true
119 }
120
121 # Executes msbuild to build or install projects
122 # Throws Exception on error
123 function Call-MSBuild($path, $config)
124 {
125     $command = "msbuild $path /p:Configuration='$config' /m"
126     L "Executing: $($command)"
127     msbuild $path /p:Configuration="$config" /m
128
129     if(-Not $?) {
130         Throw "Failure executing command: $($command)"
131     }
132
133     return $true
134 }
135
136 function RunAccuracyTests($path) {
137     md "$path\bin\Release\accuracy"
138     python "$PSScriptRoot\..\..\modules\ts\misc\run.py" -w "$path\bin\Release\accuracy" -a "$path\bin\Release"
139 }
140
141 function RunPerfTests($path) {
142     md "$path\bin\Release\perf"
143     python "$PSScriptRoot\..\..\modules\ts\misc\run.py" -w "$path\bin\Release\perf" "$path\bin\Release"
144 }
145
146 Function Execute() {
147     If ($HELP.IsPresent) {
148         ShowHelp
149     }
150
151     # Validating arguments.
152     # This type of validation (rather than using ValidateSet()) is required to make .bat wrapper work
153
154     D "Input Platforms: $PLATFORMS_IN"
155     $platforms = New-Object System.Collections.ArrayList
156     $PLATFORMS_IN.Split("," ,[System.StringSplitOptions]::RemoveEmptyEntries) | ForEach {
157         $_ = $_.Trim()
158         if ("WP","WS" -Contains $_) {
159             [void]$platforms.Add($_)
160             D "$_ is valid"
161         } else {
162             Throw "$($_) is not valid! Please use WP, WS"
163         }
164     }
165     D "Processed Platforms: $platforms"
166
167     D "Input Versions: $VERSIONS_IN"
168     $versions = New-Object System.Collections.ArrayList
169     $VERSIONS_IN.Split("," ,[System.StringSplitOptions]::RemoveEmptyEntries) | ForEach {
170         $_ = $_.Trim()
171         if ("8.0","8.1","10.0" -Contains $_) {
172             [void]$versions.Add($_)
173             D "$_ is valid"
174         } else {
175             Throw "$($_) is not valid! Please use 8.0, 8.1, 10.0"
176         }
177     }
178     D "Processed Versions: $versions"
179
180     D "Input Architectures: $ARCHITECTURES_IN"
181     $architectures = New-Object System.Collections.ArrayList
182     $ARCHITECTURES_IN.Split("," ,[System.StringSplitOptions]::RemoveEmptyEntries) | ForEach {
183         $_ = $_.Trim()
184         if ("x86","x64","ARM" -Contains $_) {
185             $architectures.Add($_) > $null
186             D "$_ is valid"
187         } else {
188             Throw "$($_) is not valid! Please use x86, x64, ARM"
189         }
190     }
191
192     D "Processed Architectures: $architectures"
193
194     # Assuming we are in '<ocv-sources>/platforms/winrt' we should move up to sources root directory
195     Push-Location ../../
196
197     $SRC = Get-Location
198
199     $def_architectures = @{
200         "x86" = "";
201         "x64" = " Win64"
202         "arm" = " ARM"
203     }
204
205     # Setting up Visual Studio variables to enable build
206     $shouldBuid = $false
207     If ($BUILD.IsPresent) {
208         $shouldBuild = Set-VS12
209     }
210
211     foreach($plat in $platforms) {
212         # Set proper platform name.
213         $platName = ""
214         Switch ($plat) {
215             "WP" { $platName = "WindowsPhone" }
216             "WS" { $platName = "WindowsStore" }
217         }
218
219         foreach($vers in $versions) {
220
221             foreach($arch in $architectures) {
222
223                 # Set proper architecture. For MSVS this is done by selecting proper generator
224                 $genName = $GENERATOR
225                 Switch ($arch) {
226                     "ARM" { $genName = $GENERATOR + $def_architectures['arm'] }
227                     "x64" { $genName = $GENERATOR + $def_architectures['x64'] }
228                 }
229
230                 # Constructing path to the install binaries
231                 # Creating these binaries will be done by building CMake-generated INSTALL project from Visual Studio
232                 $installPath = "$SRC\bin\install\$plat\$vers\$arch"
233                 if ($INSTALL) {
234                     # Do not add architrecture to the path since it will be added by OCV CMake logic
235                     $installPath = "$SRC\$INSTALL\$plat\$vers"
236                 }
237
238                 $path = "$SRC\bin\$plat\$vers\$arch"
239
240                 L "-----------------------------------------------"
241                 L "Target:"
242                 L "    Directory: $path"
243                 L "    Platform: $platName"
244                 L "    Version: $vers"
245                 L "    Architecture: $arch"
246                 L "    Generator: $genName"
247                 L "    Install Directory: $installPath"
248
249                 # Delete target directory if exists to ensure that CMake cache is cleared out.
250                 If (Test-Path $path) {
251                     Remove-Item -Recurse -Force $path
252                 }
253
254                 # Validate if required directory exists, create if it doesn't
255                 New-Item -ItemType Directory -Force -Path $path
256
257                 # Change location to the respective subdirectory
258                 Push-Location -Path $path
259
260                 L "Generating project:"
261                 L "cmake -G $genName -DCMAKE_SYSTEM_NAME:String=$platName -DCMAKE_SYSTEM_VERSION:String=$vers -DCMAKE_VS_EFFECTIVE_PLATFORMS:String=$arch -DCMAKE_INSTALL_PREFIX:PATH=$installPath $SRC"
262                 cmake -G $genName -DCMAKE_SYSTEM_NAME:String=$platName -DCMAKE_SYSTEM_VERSION:String=$vers -DCMAKE_VS_EFFECTIVE_PLATFORMS:String=$arch -DCMAKE_INSTALL_PREFIX:PATH=$installPath $SRC
263                 L "-----------------------------------------------"
264
265                 # REFERENCE:
266                 # Executed from '$SRC/bin' folder.
267                 # Targeting x86 WindowsPhone 8.1.
268                 # cmake -G "Visual Studio 12 2013" -DCMAKE_SYSTEM_NAME:String=WindowsPhone -DCMAKE_SYSTEM_VERSION:String=8.1 ..
269
270
271                 # Building and installing project
272                 Try {
273                     If ($shouldBuild) {
274                         L "Building and installing project:"
275
276                         Call-MSBuild "OpenCV.sln" "Debug"
277                         Call-MSBuild "INSTALL.vcxproj" "Debug"
278
279                         Call-MSBuild "OpenCV.sln" "Release"
280                         Call-MSBuild "INSTALL.vcxproj" "Release"
281
282                         Try {
283                             # Running tests for release versions:
284                             If ($TESTS -eq "ALL") {
285                                 RunAccuracyTests "$path"
286                                 RunPerfTests "$path"
287                             } else {
288                                 If($TESTS -eq "ACC") {
289                                     RunAccuracyTests "$path"
290                                 }
291                                 If($TESTS -eq "PERF") {
292                                     RunPerfTests "$path"
293                                 }
294                             }
295                         } Catch {
296                             $ErrorMessage = $_.Exception.Message
297                             L "Error: $ErrorMessage"
298                             exit
299                         }
300                     }
301                 } Catch {
302                     $ErrorMessage = $_.Exception.Message
303                     L "Error: $ErrorMessage"
304
305                     # Exiting at this point will leave command line pointing at the erroneous configuration directory
306                     exit
307                 }
308
309                 # Return back to Sources folder
310                 Pop-Location
311             }
312         }
313     }
314
315     # Return back to Script folder
316     Pop-Location
317 }
318
319 Function ShowHelp() {
320     Write-Host "Configures OpenCV and generates projects for specified version of Visual Studio/platforms/architectures."
321     Write-Host "Must be executed from the sources folder containing main CMakeLists configuration."
322     Write-Host "Parameter keys can be shortened down to a single symbol (e.g. '-a') and are not case sensitive."
323     Write-Host "Proper parameter sequencing is required when omitting keys."
324     Write-Host "Generates the following folder structure, depending on the supplied parameters: "
325     Write-Host "     bin/ "
326     Write-Host "      | "
327     Write-Host "      |-WP "
328     Write-Host "      |  ... "
329     Write-Host "      |-WinRT "
330     Write-Host "      |  |-8.0 "
331     Write-Host "      |  |-8.1 "
332     Write-Host "      |  |  |-x86 "
333     Write-Host "      |  |  |-x64 "
334     Write-Host "      |  |  |-ARM "
335     Write-Host " "
336     Write-Host " USAGE: "
337     Write-Host "   Calling:"
338     Write-Host "     PS> setup_winrt.ps1 [params]"
339     Write-Host "     cmd> setup_winrt.bat [params]"
340     Write-Host "     cmd> PowerShell.exe -ExecutionPolicy Unrestricted -File setup_winrt.ps1 [params]"
341     Write-Host "   Parameters:"
342     Write-Host "     setup_winrt [options] [platform] [version] [architecture] [tests] [generator] [install-path]"
343     Write-Host "     setup_winrt -b 'WP' 'x86,ARM' "
344     Write-Host "     setup_winrt -b 'WP' 'x86,ARM' ALL"
345     Write-Host "     setup_winrt -b 'WP' 'x86,ARM' -test PERF "
346     Write-Host "     setup_winrt -architecture x86 -platform WP "
347     Write-Host "     setup_winrt -arc x86 -plat 'WP,WS' "
348     Write-Host "     setup_winrt -a x86 -g 'Visual Studio 15 2017' -pl WP "
349     Write-Host " WHERE: "
350     Write-Host "     options -  Options to call "
351     Write-Host "                 -h: displays command line help "
352     Write-Host "                 -b: builds BUILD_ALL and INSTALL projects for each generated configuration in both Debug and Release modes."
353     Write-Host "     platform -  Array of target platforms. "
354     Write-Host "                 Default: WP "
355     Write-Host "                 Example: 'WS,WP' "
356     Write-Host "                 Options: WP, WS ('WindowsPhone', 'WindowsStore'). "
357     Write-Host "                 Note that you'll need to use quotes to specify more than one platform. "
358     Write-Host "     version - Array of platform versions. "
359     Write-Host "                 Default: 8.1 "
360     Write-Host "                 Example: '8.0,8.1' "
361     Write-Host "                 Options: 8.0, 8.1, 10.0. Available options may be limited depending on your local setup (e.g. SDK availability). "
362     Write-Host "                 Note that you'll need to use quotes to specify more than one version. "
363     Write-Host "     architecture - Array of target architectures to build for. "
364     Write-Host "                 Default: x86 "
365     Write-Host "                 Example: 'ARM,x64' "
366     Write-Host "                 Options: x86, ARM, x64. Available options may be limited depending on your local setup. "
367     Write-Host "                 Note that you'll need to use quotes to specify more than one architecture. "
368     Write-Host "     tests - Test sets to run. Requires -b option otherwise ignored. "
369     Write-Host "                 Default: None. "
370     Write-Host "                 Example: 'ALL' "
371     Write-Host "                 Options: ACC, PERF, ALL. "
372     Write-Host "     generator - Visual Studio instance used to generate the projects. "
373     Write-Host "                 Default: Visual Studio 12 2013 "
374     Write-Host "                 Example: 'Visual Studio 11 2012' "
375     Write-Host "                 Use 'cmake --help' to find all available option on your machine. "
376     Write-Host "     install-path - Path to install binaries (relative to the sources directory). "
377     Write-Host "                 Default: <src-dir>\bin\install\<platform>\<version>\<architecture> "
378     Write-Host "                 Example: '../install' "
379
380     Exit
381 }
382
383 Execute