setup_winrt.ps1 15 KB

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