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     [ValidateNotNull()]
57     [ValidateSet("Visual Studio 12 2013","Visual Studio 11 2012")]
58     $GENERATOR = "Visual Studio 12 2013",
59 
60     [parameter(Mandatory=$False)]
61     [String]
62     $INSTALL
63 )
64 
65 
L()66 Function L() {
67     Param(
68         [parameter(Mandatory=$true)]
69         [String]
70         [ValidateNotNull()]
71         $str
72     )
73 
74     Write-Host "INFO> $str"
75 }
76 
D()77 Function D() {
78     Param(
79         [parameter(Mandatory=$true)]
80         [String]
81         [ValidateNotNull()]
82         $str
83     )
84 
85     # Use this trigger to toggle debug output
86     [bool]$debug = $true
87 
88     if ($debug) {
89         Write-Host "DEBUG> $str"
90     }
91 }
92 
Get-Batchfile($file)93 function Get-Batchfile ($file) {
94     $cmd = "`"$file`" & set"
95     cmd /c $cmd | Foreach-Object {
96         $p, $v = $_.split('=')
97         Set-Item -path env:$p -value $v
98     }
99 }
100 
101 # Enables access to Visual Studio variables via "vsvars32.bat"
Set-VS12()102 function Set-VS12()
103 {
104     Try {
105         $vs12comntools = (Get-ChildItem env:VS120COMNTOOLS).Value
106         $batchFile = [System.IO.Path]::Combine($vs12comntools, "vsvars32.bat")
107         Get-Batchfile $BatchFile
108         [System.Console]::Title = "Visual Studio 2010 Windows PowerShell"
109      } Catch {
110         $ErrorMessage = $_.Exception.Message
111         L "Error: $ErrorMessage"
112         return $false
113      }
114      return $true
115 }
116 
117 # Executes msbuild to build or install projects
118 # Throws Exception on error
Call-MSBuild($path, $config)119 function Call-MSBuild($path, $config)
120 {
121     $command = "msbuild $path /p:Configuration='$config' /m"
122     L "Executing: $($command)"
123     msbuild $path /p:Configuration="$config" /m
124 
125     if(-Not $?) {
126         Throw "Failure executing command: $($command)"
127     }
128 
129     return $true
130 }
131 
Execute()132 Function Execute() {
133     If ($HELP.IsPresent) {
134         ShowHelp
135     }
136 
137     # Validating arguments.
138     # This type of validation (rather than using ValidateSet()) is required to make .bat wrapper work
139 
140     D "Input Platforms: $PLATFORMS_IN"
141     $platforms = New-Object System.Collections.ArrayList
142     $PLATFORMS_IN.Split("," ,[System.StringSplitOptions]::RemoveEmptyEntries) | ForEach {
143         $_ = $_.Trim()
144         if ("WP","WS" -Contains $_) {
145             [void]$platforms.Add($_)
146             D "$_ is valid"
147         } else {
148             Throw "$($_) is not valid! Please use WP, WS"
149         }
150     }
151     D "Processed Platforms: $platforms"
152 
153     D "Input Versions: $VERSIONS_IN"
154     $versions = New-Object System.Collections.ArrayList
155     $VERSIONS_IN.Split("," ,[System.StringSplitOptions]::RemoveEmptyEntries) | ForEach {
156         $_ = $_.Trim()
157         if ("8.0","8.1" -Contains $_) {
158             [void]$versions.Add($_)
159             D "$_ is valid"
160         } else {
161             Throw "$($_) is not valid! Please use 8.0, 8.1"
162         }
163     }
164     D "Processed Versions: $versions"
165 
166     D "Input Architectures: $ARCHITECTURES_IN"
167     $architectures = New-Object System.Collections.ArrayList
168     $ARCHITECTURES_IN.Split("," ,[System.StringSplitOptions]::RemoveEmptyEntries) | ForEach {
169         $_ = $_.Trim()
170         if ("x86","x64","ARM" -Contains $_) {
171             $architectures.Add($_) > $null
172             D "$_ is valid"
173         } else {
174             Throw "$($_) is not valid! Please use x86, x64, ARM"
175         }
176     }
177     D "Processed Architectures: $architectures"
178 
179     # Assuming we are in '<ocv-sources>/platforms/winrt' we should move up to sources root directory
180     Push-Location ../../
181 
182     $SRC = Get-Location
183 
184     $def_architectures = @{
185         "x86" = "";
186         "x64" = " Win64"
187         "arm" = " ARM"
188     }
189 
190     # Setting up Visual Studio variables to enable build
191     $shouldBuid = $false
192     If ($BUILD.IsPresent) {
193         $shouldBuild = Set-VS12
194     }
195 
196     foreach($plat in $platforms) {
197         # Set proper platform name.
198         $platName = ""
199         Switch ($plat) {
200             "WP" { $platName = "WindowsPhone" }
201             "WS" { $platName = "WindowsStore" }
202         }
203 
204         foreach($vers in $versions) {
205 
206             foreach($arch in $architectures) {
207 
208                 # Set proper architecture. For MSVS this is done by selecting proper generator
209                 $genName = $GENERATOR
210                 Switch ($arch) {
211                     "ARM" { $genName = $GENERATOR + $def_architectures['arm'] }
212                     "x64" { $genName = $GENERATOR + $def_architectures['x64'] }
213                 }
214 
215                 # Constructing path to the install binaries
216                 # Creating these binaries will be done by building CMake-generated INSTALL project from Visual Studio
217                 $installPath = "$SRC\bin\install\$plat\$vers\$arch"
218                 if ($INSTALL) {
219                     # Do not add architrecture to the path since it will be added by OCV CMake logic
220                     $installPath = "$SRC\$INSTALL\$plat\$vers"
221                 }
222 
223                 $path = "$SRC\bin\$plat\$vers\$arch"
224 
225                 L "-----------------------------------------------"
226                 L "Target:"
227                 L "    Directory: $path"
228                 L "    Platform: $platName"
229                 L "    Version: $vers"
230                 L "    Architecture: $arch"
231                 L "    Generator: $genName"
232                 L "    Install Directory: $installPath"
233 
234                 # Delete target directory if exists to ensure that CMake cache is cleared out.
235                 If (Test-Path $path) {
236                     Remove-Item -Recurse -Force $path
237                 }
238 
239                 # Validate if required directory exists, create if it doesn't
240                 New-Item -ItemType Directory -Force -Path $path
241 
242                 # Change location to the respective subdirectory
243                 Push-Location -Path $path
244 
245                 L "Generating project:"
246                 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"
247                 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
248                 L "-----------------------------------------------"
249 
250                 # REFERENCE:
251                 # Executed from '$SRC/bin' folder.
252                 # Targeting x86 WindowsPhone 8.1.
253                 # cmake -G "Visual Studio 12 2013" -DCMAKE_SYSTEM_NAME:String=WindowsPhone -DCMAKE_SYSTEM_VERSION:String=8.1 ..
254 
255 
256                 # Building and installing project
257                 Try {
258                     If ($shouldBuild) {
259                         L "Building and installing project:"
260 
261                         Call-MSBuild "OpenCV.sln" "Debug"
262                         Call-MSBuild "INSTALL.vcxproj" "Debug"
263 
264                         Call-MSBuild "OpenCV.sln" "Release"
265                         Call-MSBuild "INSTALL.vcxproj" "Release"
266                     }
267                 } Catch {
268                     $ErrorMessage = $_.Exception.Message
269                     L "Error: $ErrorMessage"
270 
271                     # Exiting at this point will leave command line pointing at the erroneous configuration directory
272                     exit
273                 }
274 
275                 # Return back to Sources folder
276                 Pop-Location
277             }
278         }
279     }
280 
281     # Return back to Script folder
282     Pop-Location
283 }
284 
ShowHelp()285 Function ShowHelp() {
286     Write-Host "Configures OpenCV and generates projects for specified verion of Visual Studio/platforms/architectures."
287     Write-Host "Must be executed from the sources folder containing main CMakeLists configuration."
288     Write-Host "Parameter keys can be shortened down to a single symbol (e.g. '-a') and are not case sensitive."
289     Write-Host "Proper parameter sequencing is required when omitting keys."
290     Write-Host "Generates the following folder structure, depending on the supplied parameters: "
291     Write-Host "     bin/ "
292     Write-Host "      | "
293     Write-Host "      |-WP "
294     Write-Host "      |  ... "
295     Write-Host "      |-WinRT "
296     Write-Host "      |  |-8.0 "
297     Write-Host "      |  |-8.1 "
298     Write-Host "      |  |  |-x86 "
299     Write-Host "      |  |  |-x64 "
300     Write-Host "      |  |  |-ARM "
301     Write-Host " "
302     Write-Host " USAGE: "
303     Write-Host "   Calling:"
304     Write-Host "     PS> setup_winrt.ps1 [params]"
305     Write-Host "     cmd> setup_winrt.bat [params]"
306     Write-Host "     cmd> PowerShell.exe -ExecutionPolicy Unrestricted -File setup_winrt.ps1 [params]"
307     Write-Host "   Parameters:"
308     Write-Host "     setup_winrt [options] [platform] [version] [architecture] [generator] [install-path]"
309     Write-Host "     setup_winrt -b 'WP' 'x86,ARM' "
310     Write-Host "     setup_winrt -architecture x86 -platform WP "
311     Write-Host "     setup_winrt -arc x86 -plat 'WP,WS' "
312     Write-Host "     setup_winrt -a x86 -g 'Visual Studio 11 2012' -pl WP "
313     Write-Host " WHERE: "
314     Write-Host "     options -  Options to call "
315     Write-Host "                 -h: diplays command line help "
316     Write-Host "                 -b: builds BUILD_ALL and INSTALL projects for each generated configuration in both Debug and Release modes."
317     Write-Host "     platform -  Array of target platforms. "
318     Write-Host "                 Default: WP "
319     Write-Host "                 Example: 'WS,WP' "
320     Write-Host "                 Options: WP, WS ('WindowsPhone', 'WindowsStore'). "
321     Write-Host "                 Note that you'll need to use quotes to specify more than one platform. "
322     Write-Host "     version - Array of platform versions. "
323     Write-Host "                 Default: 8.1 "
324     Write-Host "                 Example: '8.0,8.1' "
325     Write-Host "                 Options: 8.0, 8.1. Available options may be limited depending on your local setup (e.g. SDK availability). "
326     Write-Host "                 Note that you'll need to use quotes to specify more than one version. "
327     Write-Host "     architecture - Array of target architectures to build for. "
328     Write-Host "                 Default: x86 "
329     Write-Host "                 Example: 'ARM,x64' "
330     Write-Host "                 Options: x86, ARM, x64. Available options may be limited depending on your local setup. "
331     Write-Host "                 Note that you'll need to use quotes to specify more than one architecture. "
332     Write-Host "     generator - Visual Studio instance used to generate the projects. "
333     Write-Host "                 Default: Visual Studio 12 2013 "
334     Write-Host "                 Example: 'Visual Studio 11 2012' "
335     Write-Host "                 Use 'cmake --help' to find all available option on your machine. "
336     Write-Host "     install-path - Path to install binaries (relative to the sources directory). "
337     Write-Host "                 Default: <src-dir>\bin\install\<platform>\<version>\<architecture> "
338     Write-Host "                 Example: '../install' "
339 
340     Exit
341 }
342 
343 Execute