|
| 1 | +Import-Module $PSScriptRoot/util -Force |
| 2 | + |
| 3 | +<# |
| 4 | +.SYNOPSIS |
| 5 | + Configure and/or build a CMake project |
| 6 | +.DESCRIPTION |
| 7 | + This command will configure, build, and optionally install a CMake project |
| 8 | +#> |
| 9 | +function Build-CMakeProject { |
| 10 | + [CmdletBinding(PositionalBinding = $false, DefaultParameterSetName = "configure-and-build")] |
| 11 | + param( |
| 12 | + # If set, only configure the project without building |
| 13 | + [Parameter(ParameterSetName = "configure")] |
| 14 | + [switch] |
| 15 | + $OnlyConfigure, |
| 16 | + # If set, only build without configuring |
| 17 | + [Parameter(ParameterSetName = "build")] |
| 18 | + [switch] |
| 19 | + $OnlyBuild, |
| 20 | + # The build directory for the project |
| 21 | + [Parameter(Mandatory)] |
| 22 | + [string] |
| 23 | + $BuildDir, |
| 24 | + # The source directory for the project |
| 25 | + [Parameter(ParameterSetName = "configure", Mandatory)] |
| 26 | + [Parameter(ParameterSetName = "configure-and-build", Mandatory)] |
| 27 | + [string] |
| 28 | + $SourceDir, |
| 29 | + # The CMake generator to use |
| 30 | + [Parameter(ParameterSetName = "configure")] |
| 31 | + [Parameter(ParameterSetName = "configure-and-build")] |
| 32 | + [string] |
| 33 | + $Generator, |
| 34 | + # If set, then CMake will perform a fresh configure |
| 35 | + [Parameter(ParameterSetName = "configure")] |
| 36 | + [Parameter(ParameterSetName = "configure-and-build")] |
| 37 | + [switch] |
| 38 | + $Fresh, |
| 39 | + # If set, the project will be installed to the given prefix |
| 40 | + [Parameter(ParameterSetName = "build")] |
| 41 | + [Parameter(ParameterSetName = "configure-and-build")] |
| 42 | + [string] |
| 43 | + $InstallPath, |
| 44 | + # A mapping of CMake settings that will be used during configuration |
| 45 | + [Parameter(ParameterSetName = "configure")] |
| 46 | + [Parameter(ParameterSetName = "configure-and-build")] |
| 47 | + [hashtable] |
| 48 | + $Settings = @{}, |
| 49 | + # If set, clean before building |
| 50 | + [Parameter(ParameterSetName = "build")] |
| 51 | + [Parameter(ParameterSetName = "configure-and-build")] |
| 52 | + [switch] |
| 53 | + $Clean, |
| 54 | + # Number of parallel jobs to run |
| 55 | + [Parameter(ParameterSetName = "build")] |
| 56 | + [Parameter(ParameterSetName = "configure-and-build")] |
| 57 | + [int] |
| 58 | + $Jobs, |
| 59 | + # The CMake executable to be used |
| 60 | + [string] |
| 61 | + $CMakeExecutable = "cmake" |
| 62 | + ) |
| 63 | + $ErrorActionPreference = "stop" |
| 64 | + |
| 65 | + if ([string]::IsNullOrEmpty($BuildDir)) { |
| 66 | + $BuildDir = Join-Path $PWD "_build" |
| 67 | + } |
| 68 | + |
| 69 | + if (-not $OnlyBuild) { |
| 70 | + $command = @($CMakeExecutable) |
| 71 | + if (-not [string]::IsNullOrEmpty($SourceDir)) { |
| 72 | + $command += "-S$SourceDir" |
| 73 | + } |
| 74 | + $command += "-B$BuildDir" |
| 75 | + if (-not [string]::IsNullOrEmpty($Generator)) { |
| 76 | + $command += "-G$Generator" |
| 77 | + } |
| 78 | + if ($Fresh) { |
| 79 | + $command += "--fresh" |
| 80 | + } |
| 81 | + foreach ($key in $Settings.Keys) { |
| 82 | + $command += "-D$key=$(Format-CMakeValue $Settings[$key])" |
| 83 | + } |
| 84 | + Join-CommandLineArgv $command | Invoke-Expression |
| 85 | + if ($LASTEXITCODE) { |
| 86 | + throw "CMake configuration failed [$LASTEXITCODE]" |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if (-not $OnlyConfigure) { |
| 91 | + $command = @($CMakeExecutable) |
| 92 | + $command += "--build", $BuildDir |
| 93 | + if ($Clean) { |
| 94 | + $command += "--clean-first" |
| 95 | + } |
| 96 | + if ($Jobs) { |
| 97 | + $command += "--parallel", "$Jobs" |
| 98 | + } |
| 99 | + Write-Debug "Executing CMake command: $command" |
| 100 | + Join-CommandLineArgv $command | Invoke-Expression |
| 101 | + if ($LASTEXITCODE) { |
| 102 | + throw "CMake build failed [$LASTEXITCODE]" |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +function Test-CMakeProject { |
| 108 | + [CmdletBinding(PositionalBinding = $false)] |
| 109 | + param( |
| 110 | + # The directory containing the build results of the project |
| 111 | + [Parameter(Mandatory)] |
| 112 | + [string] |
| 113 | + $BuildDir, |
| 114 | + # Number of parallel jobs to run for the test |
| 115 | + [int]$Jobs, |
| 116 | + # The CTest configuration to run |
| 117 | + [string]$Configuration, |
| 118 | + # The CTest executable to use |
| 119 | + [string]$CTestExecutable = "ctest", |
| 120 | + # Stop immediately on the first test failure |
| 121 | + [switch] |
| 122 | + $StopOnFailure, |
| 123 | + # Print test output on failure |
| 124 | + [switch] |
| 125 | + $OutputOnFailure, |
| 126 | + # Use concise progress reporting |
| 127 | + [switch] |
| 128 | + $Progress, |
| 129 | + # Only execute tests matching the given regular expression |
| 130 | + [switch] |
| 131 | + $OnlyMatching, |
| 132 | + # Remaining arguments that are forwarded to CTest directly |
| 133 | + [Parameter(ValueFromRemainingArguments)] |
| 134 | + [string[]] |
| 135 | + $ArgumentList = @() |
| 136 | + ) |
| 137 | + $ErrorActionPreference = "stop" |
| 138 | + |
| 139 | + $ctest_command = @($CTestExecutable) |
| 140 | + if (-not [string]::IsNullOrEmpty($Configuration)) { |
| 141 | + $ctest_command += "--build-config", $Configuration |
| 142 | + } |
| 143 | + if ($Verbose) { |
| 144 | + $ctest_command += "--verbose" |
| 145 | + } |
| 146 | + if ($StopOnFailure) { |
| 147 | + $ctest_command += "--stop-on-failure" |
| 148 | + } |
| 149 | + if ($Jobs) { |
| 150 | + $ctest_command += "--parallel", "$Jobs" |
| 151 | + } |
| 152 | + if ($OutputOnFailure) { |
| 153 | + $ctest_command += "--output-on-failure" |
| 154 | + } |
| 155 | + if ($Progress) { |
| 156 | + $ctest_command += "--progress" |
| 157 | + } |
| 158 | + if ($OnlyMatching) { |
| 159 | + $ctest_command += "--tests-regex", $OnlyMatching |
| 160 | + } |
| 161 | + $ctest_command += $ArgumentList |
| 162 | + |
| 163 | + try { |
| 164 | + [void](Push-Location $BuildDir) |
| 165 | + Join-CommandLineArgv $ctest_command | Invoke-Expression |
| 166 | + if ($LASTEXITCODE) { |
| 167 | + throw "CTest test execution failed [$LASTEXITCODE]" |
| 168 | + } |
| 169 | + } |
| 170 | + finally { |
| 171 | + [void](Pop-Location) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +function Format-CMakeValue($Value) { |
| 176 | + if ($Value -is [System.Boolean]) { |
| 177 | + if ($Value) { "TRUE" } else { "FALSE" } |
| 178 | + } |
| 179 | + elseif ($Value -is [array]) { |
| 180 | + return $Value | ForEach-Object { Format-CMakeValue $_ } | Join-String -Separator ';' |
| 181 | + } |
| 182 | + else { |
| 183 | + return "$Value" |
| 184 | + } |
| 185 | +} |
0 commit comments