|
| 1 | +<# |
| 2 | +# Copyright The OpenTelemetry Authors |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +
|
| 5 | +.SYNOPSIS |
| 6 | + Install OpenTelemetry C++ third-party dependencies using CMake. |
| 7 | +
|
| 8 | +.DESCRIPTION |
| 9 | + PowerShell counterpart to ci/install_thirdparty.sh. |
| 10 | +
|
| 11 | + This script configures and builds the CMake project in install/cmake to |
| 12 | + install third-party packages into a provided prefix. |
| 13 | +
|
| 14 | +.PARAMETER --install-dir |
| 15 | + Path where third-party packages will be installed (required). |
| 16 | +
|
| 17 | +.PARAMETER --tags-file |
| 18 | + File containing tags for third-party packages (optional). |
| 19 | +
|
| 20 | +.PARAMETER --packages |
| 21 | + Semicolon-separated list of packages to build (optional). Default installs all. |
| 22 | +
|
| 23 | +.EXAMPLE |
| 24 | + ./ci/install_thirdparty.ps1 --install-dir C:/third_party |
| 25 | +
|
| 26 | +.EXAMPLE |
| 27 | + ./ci/install_thirdparty.ps1 --install-dir C:/third_party --tags-file C:/tags.txt --packages "grpc;protobuf" |
| 28 | +#> |
| 29 | + |
| 30 | +Set-StrictMode -Version Latest |
| 31 | +$ErrorActionPreference = 'Stop' |
| 32 | + |
| 33 | +function Show-Usage { |
| 34 | + $scriptName = Split-Path -Leaf $PSCommandPath |
| 35 | + Write-Host ('Usage: {0} --install-dir <path> [--tags-file <file>] [--packages "<pkg1>;<pkg2>"]' -f $scriptName) |
| 36 | + Write-Host " --install-dir <path> Path where third-party packages will be installed (required)" |
| 37 | + Write-Host " --tags-file <file> File containing tags for third-party packages (optional)" |
| 38 | + Write-Host ' --packages "<pkg1>;<pkg2>;..." Semicolon-separated list of packages to build (optional). Default installs all third-party packages.' |
| 39 | + Write-Host ' --build-type <build type> CMake build type (optional)' |
| 40 | + Write-Host ' --build-shared-libs <ON|OFF> Build shared libraries (optional)' |
| 41 | + Write-Host " -h, --help Show this help message" |
| 42 | +} |
| 43 | + |
| 44 | +function Invoke-External { |
| 45 | + param( |
| 46 | + [Parameter(Mandatory = $true)] |
| 47 | + [string] $FilePath, |
| 48 | + |
| 49 | + [Parameter(Mandatory = $false)] |
| 50 | + [string[]] $Arguments = @() |
| 51 | + ) |
| 52 | + |
| 53 | + & $FilePath @Arguments |
| 54 | + $exitCode = $LASTEXITCODE |
| 55 | + if ($exitCode -ne 0) { |
| 56 | + throw "Command failed (exit $exitCode): $FilePath $($Arguments -join ' ')" |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +$ThirdpartyTagsFile = '' |
| 61 | +$ThirdpartyPackages = '' |
| 62 | +$ThirdpartyInstallDir = '' |
| 63 | +$CmakeBuildType = '' |
| 64 | +$CmakeBuildSharedLibs = '' |
| 65 | + |
| 66 | +# Match install_thirdparty.sh behavior: parse GNU-style args from $args. |
| 67 | +for ($i = 0; $i -lt $args.Count; ) { |
| 68 | + $a = [string]$args[$i] |
| 69 | + |
| 70 | + switch ($a) { |
| 71 | + '--install-dir' { |
| 72 | + if (($i + 1) -ge $args.Count -or ([string]$args[$i + 1]).StartsWith('--')) { |
| 73 | + [Console]::Error.WriteLine('Error: --install-dir requires a value') |
| 74 | + Show-Usage |
| 75 | + exit 1 |
| 76 | + } |
| 77 | + $ThirdpartyInstallDir = [string]$args[$i + 1] |
| 78 | + $i += 2 |
| 79 | + continue |
| 80 | + } |
| 81 | + '--tags-file' { |
| 82 | + if (($i + 1) -ge $args.Count -or ([string]$args[$i + 1]).StartsWith('--')) { |
| 83 | + [Console]::Error.WriteLine('Error: --tags-file requires a value') |
| 84 | + Show-Usage |
| 85 | + exit 1 |
| 86 | + } |
| 87 | + $ThirdpartyTagsFile = [string]$args[$i + 1] |
| 88 | + $i += 2 |
| 89 | + continue |
| 90 | + } |
| 91 | + '--packages' { |
| 92 | + if (($i + 1) -ge $args.Count -or ([string]$args[$i + 1]).StartsWith('--')) { |
| 93 | + [Console]::Error.WriteLine('Error: --packages requires a value') |
| 94 | + Show-Usage |
| 95 | + exit 1 |
| 96 | + } |
| 97 | + $ThirdpartyPackages = [string]$args[$i + 1] |
| 98 | + $i += 2 |
| 99 | + continue |
| 100 | + } |
| 101 | + '--build-type' { |
| 102 | + if (($i + 1) -ge $args.Count -or ([string]$args[$i + 1]).StartsWith('--')) { |
| 103 | + [Console]::Error.WriteLine('Error: --build-type requires a value') |
| 104 | + Show-Usage |
| 105 | + exit 1 |
| 106 | + } |
| 107 | + $CmakeBuildType = [string]$args[$i + 1] |
| 108 | + $i += 2 |
| 109 | + continue |
| 110 | + } |
| 111 | + '--build-shared-libs' { |
| 112 | + if (($i + 1) -ge $args.Count -or ([string]$args[$i + 1]).StartsWith('--')) { |
| 113 | + [Console]::Error.WriteLine('Error: --build-shared-libs requires a value') |
| 114 | + Show-Usage |
| 115 | + exit 1 |
| 116 | + } |
| 117 | + $CmakeBuildSharedLibs = [string]$args[$i + 1] |
| 118 | + $i += 2 |
| 119 | + continue |
| 120 | + } |
| 121 | + '-h' { Show-Usage; exit 0 } |
| 122 | + '--help' { Show-Usage; exit 0 } |
| 123 | + default { |
| 124 | + [Console]::Error.WriteLine(("Unknown option: {0}" -f $a)) |
| 125 | + Show-Usage |
| 126 | + exit 1 |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +if ([string]::IsNullOrWhiteSpace($ThirdpartyInstallDir)) { |
| 132 | + [Console]::Error.WriteLine('Error: --install-dir is a required argument.') |
| 133 | + Show-Usage |
| 134 | + exit 1 |
| 135 | +} |
| 136 | + |
| 137 | +$SrcDir = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Definition) |
| 138 | + |
| 139 | +$CxxStandard = $env:CXX_STANDARD |
| 140 | +if ([string]::IsNullOrWhiteSpace($CxxStandard)) { |
| 141 | + $CxxStandard = '17' |
| 142 | +} |
| 143 | + |
| 144 | +$TempRoot = $env:TEMP |
| 145 | +if ([string]::IsNullOrWhiteSpace($TempRoot)) { |
| 146 | + # Fallback if TEMP isn't set. |
| 147 | + $TempRoot = [System.IO.Path]::GetTempPath() |
| 148 | +} |
| 149 | + |
| 150 | +$ThirdpartyBuildDir = Join-Path $TempRoot 'otel-cpp-third-party-build' |
| 151 | +if (Test-Path -LiteralPath $ThirdpartyBuildDir) { |
| 152 | + Remove-Item -LiteralPath $ThirdpartyBuildDir -Recurse -Force |
| 153 | +} |
| 154 | + |
| 155 | +if (-not (Get-Command cmake -ErrorAction SilentlyContinue)) { |
| 156 | + throw 'cmake was not found in PATH. Please install CMake and ensure it is available on PATH.' |
| 157 | +} |
| 158 | + |
| 159 | +$CmakeSourceDir = Join-Path $SrcDir 'install' 'cmake' |
| 160 | +if (-not (Test-Path -LiteralPath $CmakeSourceDir)) { |
| 161 | + throw "CMake source directory not found: $CmakeSourceDir" |
| 162 | +} |
| 163 | + |
| 164 | +$OtelcppProtoPath = $env:OTELCPP_PROTO_PATH |
| 165 | +if ($null -eq $OtelcppProtoPath) { |
| 166 | + $OtelcppProtoPath = '' |
| 167 | +} |
| 168 | + |
| 169 | +$CmakeConfigureArgs = @( |
| 170 | + '-S', $CmakeSourceDir, |
| 171 | + '-B', $ThirdpartyBuildDir, |
| 172 | + "-DCMAKE_INSTALL_PREFIX=$ThirdpartyInstallDir", |
| 173 | + "-DCMAKE_CXX_STANDARD=$CxxStandard", |
| 174 | + "-DOTELCPP_THIRDPARTY_TAGS_FILE=$ThirdpartyTagsFile", |
| 175 | + "-DOTELCPP_PROTO_PATH=$OtelcppProtoPath", |
| 176 | + "-DOTELCPP_THIRDPARTY_INSTALL_LIST=$ThirdpartyPackages" |
| 177 | +) |
| 178 | + |
| 179 | +$parallel = [Math]::Max([Environment]::ProcessorCount, 1) |
| 180 | + |
| 181 | +$CmakeBuildArgs = @( |
| 182 | + '--build', $ThirdpartyBuildDir, |
| 183 | + '--clean-first', |
| 184 | + '--parallel', $parallel |
| 185 | +) |
| 186 | + |
| 187 | +if (-not [string]::IsNullOrWhiteSpace($CmakeBuildType)) { |
| 188 | + $CmakeConfigureArgs += "-DCMAKE_BUILD_TYPE=$CmakeBuildType" |
| 189 | + $CmakeBuildArgs += @('--config', $CmakeBuildType) |
| 190 | +} |
| 191 | + |
| 192 | +if (-not [string]::IsNullOrWhiteSpace($CmakeBuildSharedLibs)) { |
| 193 | + $CmakeConfigureArgs += "-DBUILD_SHARED_LIBS=$CmakeBuildSharedLibs" |
| 194 | +} |
| 195 | + |
| 196 | +Invoke-External -FilePath 'cmake' -Arguments $CmakeConfigureArgs |
| 197 | + |
| 198 | +# Use CMake's cross-generator parallel flag. |
| 199 | +Invoke-External -FilePath 'cmake' -Arguments $CmakeBuildArgs |
| 200 | + |
| 201 | +# Keep parity with the bash script (no-op on Windows). |
| 202 | +if ($ThirdpartyInstallDir -eq '/usr/local') { |
| 203 | + if (Get-Command ldconfig -ErrorAction SilentlyContinue) { |
| 204 | + Invoke-External -FilePath 'ldconfig' -Arguments @() |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +Write-Host 'Third-party packages installed successfully.' |
| 209 | +Write-Host "-- THIRDPARTY_INSTALL_DIR: $ThirdpartyInstallDir" |
| 210 | +Write-Host "-- THIRDPARTY_TAGS_FILE: $ThirdpartyTagsFile" |
| 211 | +if ([string]::IsNullOrWhiteSpace($ThirdpartyPackages)) { |
| 212 | + Write-Host '-- THIRDPARTY_PACKAGES: all' |
| 213 | +} |
| 214 | +else { |
| 215 | + Write-Host "-- THIRDPARTY_PACKAGES: $ThirdpartyPackages" |
| 216 | +} |
| 217 | +Write-Host "-- CXX_STANDARD: $CxxStandard" |
| 218 | +if (-not [string]::IsNullOrWhiteSpace($CmakeBuildType)) { |
| 219 | + Write-Host "-- CMAKE_BUILD_TYPE: $CmakeBuildType" |
| 220 | +} |
0 commit comments