setup_winrt.ps1 14.2 KB
Newer Older
1
<#
2 3
Copyright (c) Microsoft Open Technologies, Inc.
All rights reserved.
4

5
(3-clause BSD License)
6

7 8
Redistribution and use in source and binary forms, with or without modification, are permitted provided that
the following conditions are met:
9

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or
promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#>

[CmdletBinding()]
Param(
    [parameter(Mandatory=$False)]
    [switch]
    $HELP,

    [parameter(Mandatory=$False)]
    [switch]
    $BUILD,

    [parameter(Mandatory=$False)]
    [Array]
    [ValidateNotNull()]
    $PLATFORMS_IN = "WP",

    [parameter(Mandatory=$False)]
    [Array]
    [ValidateNotNull()]
    $VERSIONS_IN = "8.1",

    [parameter(Mandatory=$False)]
    [Array]
    [ValidateNotNull()]
    $ARCHITECTURES_IN = "x86",

54 55 56 57
    [parameter(Mandatory=$False)]
    [String]
    $TESTS = "None",

58 59 60
    [parameter(Mandatory=$False)]
    [String]
    [ValidateNotNull()]
61 62
    [ValidateSet("Visual Studio 15 2017","Visual Studio 14 2015","Visual Studio 12 2013","Visual Studio 11 2012")]
    $GENERATOR = "Visual Studio 15 2017",
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

    [parameter(Mandatory=$False)]
    [String]
    $INSTALL
)


Function L() {
    Param(
        [parameter(Mandatory=$true)]
        [String]
        [ValidateNotNull()]
        $str
    )

    Write-Host "INFO> $str"
}

Function D() {
    Param(
        [parameter(Mandatory=$true)]
        [String]
        [ValidateNotNull()]
        $str
    )

    # Use this trigger to toggle debug output
    [bool]$debug = $true

    if ($debug) {
        Write-Host "DEBUG> $str"
    }
}

function Get-Batchfile ($file) {
    $cmd = "`"$file`" & set"
    cmd /c $cmd | Foreach-Object {
        $p, $v = $_.split('=')
        Set-Item -path env:$p -value $v
    }
}

# Enables access to Visual Studio variables via "vsvars32.bat"
function Set-VS12()
{
    Try {
        $vs12comntools = (Get-ChildItem env:VS120COMNTOOLS).Value
        $batchFile = [System.IO.Path]::Combine($vs12comntools, "vsvars32.bat")
        Get-Batchfile $BatchFile
        [System.Console]::Title = "Visual Studio 2010 Windows PowerShell"
     } Catch {
        $ErrorMessage = $_.Exception.Message
        L "Error: $ErrorMessage"
        return $false
     }
     return $true
}

# Executes msbuild to build or install projects
# Throws Exception on error
function Call-MSBuild($path, $config)
{
    $command = "msbuild $path /p:Configuration='$config' /m"
    L "Executing: $($command)"
    msbuild $path /p:Configuration="$config" /m

129
    if(-Not $?) {
130
        Throw "Failure executing command: $($command)"
131
    }
132

133
    return $true
134 135
}

136 137 138 139 140 141 142 143 144 145
function RunAccuracyTests($path) {
    md "$path\bin\Release\accuracy"
    python "$PSScriptRoot\..\..\modules\ts\misc\run.py" -w "$path\bin\Release\accuracy" -a "$path\bin\Release"
}

function RunPerfTests($path) {
    md "$path\bin\Release\perf"
    python "$PSScriptRoot\..\..\modules\ts\misc\run.py" -w "$path\bin\Release\perf" "$path\bin\Release"
}

146 147 148 149 150
Function Execute() {
    If ($HELP.IsPresent) {
        ShowHelp
    }

151
    # Validating arguments.
152 153
    # This type of validation (rather than using ValidateSet()) is required to make .bat wrapper work

154
    D "Input Platforms: $PLATFORMS_IN"
155 156 157 158 159 160 161
    $platforms = New-Object System.Collections.ArrayList
    $PLATFORMS_IN.Split("," ,[System.StringSplitOptions]::RemoveEmptyEntries) | ForEach {
        $_ = $_.Trim()
        if ("WP","WS" -Contains $_) {
            [void]$platforms.Add($_)
            D "$_ is valid"
        } else {
162
            Throw "$($_) is not valid! Please use WP, WS"
163 164 165 166 167 168 169 170
        }
    }
    D "Processed Platforms: $platforms"

    D "Input Versions: $VERSIONS_IN"
    $versions = New-Object System.Collections.ArrayList
    $VERSIONS_IN.Split("," ,[System.StringSplitOptions]::RemoveEmptyEntries) | ForEach {
        $_ = $_.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 179 180 181 182 183 184 185 186 187
        }
    }
    D "Processed Versions: $versions"

    D "Input Architectures: $ARCHITECTURES_IN"
    $architectures = New-Object System.Collections.ArrayList
    $ARCHITECTURES_IN.Split("," ,[System.StringSplitOptions]::RemoveEmptyEntries) | ForEach {
        $_ = $_.Trim()
        if ("x86","x64","ARM" -Contains $_) {
            $architectures.Add($_) > $null
            D "$_ is valid"
        } else {
188
            Throw "$($_) is not valid! Please use x86, x64, ARM"
189 190
        }
    }
191

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    D "Processed Architectures: $architectures"

    # Assuming we are in '<ocv-sources>/platforms/winrt' we should move up to sources root directory
    Push-Location ../../

    $SRC = Get-Location

    $def_architectures = @{
        "x86" = "";
        "x64" = " Win64"
        "arm" = " ARM"
    }

    # Setting up Visual Studio variables to enable build
    $shouldBuid = $false
    If ($BUILD.IsPresent) {
        $shouldBuild = Set-VS12
    }

    foreach($plat in $platforms) {
        # Set proper platform name.
        $platName = ""
        Switch ($plat) {
            "WP" { $platName = "WindowsPhone" }
            "WS" { $platName = "WindowsStore" }
        }

        foreach($vers in $versions) {

            foreach($arch in $architectures) {

                # Set proper architecture. For MSVS this is done by selecting proper generator
                $genName = $GENERATOR
                Switch ($arch) {
                    "ARM" { $genName = $GENERATOR + $def_architectures['arm'] }
                    "x64" { $genName = $GENERATOR + $def_architectures['x64'] }
                }

                # Constructing path to the install binaries
                # Creating these binaries will be done by building CMake-generated INSTALL project from Visual Studio
                $installPath = "$SRC\bin\install\$plat\$vers\$arch"
                if ($INSTALL) {
                    # Do not add architrecture to the path since it will be added by OCV CMake logic
                    $installPath = "$SRC\$INSTALL\$plat\$vers"
                }

                $path = "$SRC\bin\$plat\$vers\$arch"

                L "-----------------------------------------------"
                L "Target:"
                L "    Directory: $path"
                L "    Platform: $platName"
                L "    Version: $vers"
                L "    Architecture: $arch"
                L "    Generator: $genName"
                L "    Install Directory: $installPath"

                # Delete target directory if exists to ensure that CMake cache is cleared out.
                If (Test-Path $path) {
                    Remove-Item -Recurse -Force $path
                }

                # Validate if required directory exists, create if it doesn't
                New-Item -ItemType Directory -Force -Path $path

                # Change location to the respective subdirectory
                Push-Location -Path $path

                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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
                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
                L "-----------------------------------------------"

                # REFERENCE:
                # Executed from '$SRC/bin' folder.
                # Targeting x86 WindowsPhone 8.1.
                # cmake -G "Visual Studio 12 2013" -DCMAKE_SYSTEM_NAME:String=WindowsPhone -DCMAKE_SYSTEM_VERSION:String=8.1 ..


                # Building and installing project
                Try {
                    If ($shouldBuild) {
                        L "Building and installing project:"

                        Call-MSBuild "OpenCV.sln" "Debug"
                        Call-MSBuild "INSTALL.vcxproj" "Debug"

                        Call-MSBuild "OpenCV.sln" "Release"
                        Call-MSBuild "INSTALL.vcxproj" "Release"
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299

                        Try {
                            # Running tests for release versions:
                            If ($TESTS -eq "ALL") {
                                RunAccuracyTests "$path"
                                RunPerfTests "$path"
                            } else {
                                If($TESTS -eq "ACC") {
                                    RunAccuracyTests "$path"
                                }
                                If($TESTS -eq "PERF") {
                                    RunPerfTests "$path"
                                }
                            }
                        } Catch {
                            $ErrorMessage = $_.Exception.Message
                            L "Error: $ErrorMessage"
                            exit
                        }
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
                    }
                } Catch {
                    $ErrorMessage = $_.Exception.Message
                    L "Error: $ErrorMessage"

                    # Exiting at this point will leave command line pointing at the erroneous configuration directory
                    exit
                }

                # Return back to Sources folder
                Pop-Location
            }
        }
    }

    # Return back to Script folder
    Pop-Location
}

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 323
    Write-Host "Parameter keys can be shortened down to a single symbol (e.g. '-a') and are not case sensitive."
    Write-Host "Proper parameter sequencing is required when omitting keys."
324 325 326 327 328 329 330 331 332 333 334
    Write-Host "Generates the following folder structure, depending on the supplied parameters: "
    Write-Host "     bin/ "
    Write-Host "      | "
    Write-Host "      |-WP "
    Write-Host "      |  ... "
    Write-Host "      |-WinRT "
    Write-Host "      |  |-8.0 "
    Write-Host "      |  |-8.1 "
    Write-Host "      |  |  |-x86 "
    Write-Host "      |  |  |-x64 "
    Write-Host "      |  |  |-ARM "
335
    Write-Host " "
336 337 338 339 340 341
    Write-Host " USAGE: "
    Write-Host "   Calling:"
    Write-Host "     PS> setup_winrt.ps1 [params]"
    Write-Host "     cmd> setup_winrt.bat [params]"
    Write-Host "     cmd> PowerShell.exe -ExecutionPolicy Unrestricted -File setup_winrt.ps1 [params]"
    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 345
    Write-Host "     setup_winrt -b 'WP' 'x86,ARM' ALL"
    Write-Host "     setup_winrt -b 'WP' 'x86,ARM' -test PERF "
346 347
    Write-Host "     setup_winrt -architecture x86 -platform WP "
    Write-Host "     setup_winrt -arc x86 -plat 'WP,WS' "
348
    Write-Host "     setup_winrt -a x86 -g 'Visual Studio 15 2017' -pl WP "
349 350
    Write-Host " WHERE: "
    Write-Host "     options -  Options to call "
351
    Write-Host "                 -h: displays command line help "
352 353 354 355 356 357 358 359 360
    Write-Host "                 -b: builds BUILD_ALL and INSTALL projects for each generated configuration in both Debug and Release modes."
    Write-Host "     platform -  Array of target platforms. "
    Write-Host "                 Default: WP "
    Write-Host "                 Example: 'WS,WP' "
    Write-Host "                 Options: WP, WS ('WindowsPhone', 'WindowsStore'). "
    Write-Host "                 Note that you'll need to use quotes to specify more than one platform. "
    Write-Host "     version - Array of platform versions. "
    Write-Host "                 Default: 8.1 "
    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 363 364 365 366 367
    Write-Host "                 Note that you'll need to use quotes to specify more than one version. "
    Write-Host "     architecture - Array of target architectures to build for. "
    Write-Host "                 Default: x86 "
    Write-Host "                 Example: 'ARM,x64' "
    Write-Host "                 Options: x86, ARM, x64. Available options may be limited depending on your local setup. "
    Write-Host "                 Note that you'll need to use quotes to specify more than one architecture. "
368 369 370 371
    Write-Host "     tests - Test sets to run. Requires -b option otherwise ignored. "
    Write-Host "                 Default: None. "
    Write-Host "                 Example: 'ALL' "
    Write-Host "                 Options: ACC, PERF, ALL. "
372 373 374 375 376 377 378 379 380 381 382 383
    Write-Host "     generator - Visual Studio instance used to generate the projects. "
    Write-Host "                 Default: Visual Studio 12 2013 "
    Write-Host "                 Example: 'Visual Studio 11 2012' "
    Write-Host "                 Use 'cmake --help' to find all available option on your machine. "
    Write-Host "     install-path - Path to install binaries (relative to the sources directory). "
    Write-Host "                 Default: <src-dir>\bin\install\<platform>\<version>\<architecture> "
    Write-Host "                 Example: '../install' "

    Exit
}

Execute