Debian Codegraph @

external/linker/eng/common/post-build/symbols-validation.ps1

raw
1 param(
2 [Parameter(Mandatory=$true)][string] $InputPath, # Full path to directory where NuGet packages to be checked are stored
3 [Parameter(Mandatory=$true)][string] $ExtractPath, # Full path to directory where the packages will be extracted during validation
4 [Parameter(Mandatory=$true)][string] $DotnetSymbolVersion # Version of dotnet symbol to use
5 )
6
7 function FirstMatchingSymbolDescriptionOrDefault {
8 param(
9 [string] $FullPath, # Full path to the module that has to be checked
10 [string] $TargetServerParam, # Parameter to pass to `Symbol Tool` indicating the server to lookup for symbols
11 [string] $SymbolsPath
12 )
13
14 $FileName = [System.IO.Path]::GetFileName($FullPath)
15 $Extension = [System.IO.Path]::GetExtension($FullPath)
16
17 # Those below are potential symbol files that the `dotnet symbol` might
18 # return. Which one will be returned depend on the type of file we are
19 # checking and which type of file was uploaded.
20
21 # The file itself is returned
22 $SymbolPath = $SymbolsPath + '\' + $FileName
23
24 # PDB file for the module
25 $PdbPath = $SymbolPath.Replace($Extension, '.pdb')
26
27 # PDB file for R2R module (created by crossgen)
28 $NGenPdb = $SymbolPath.Replace($Extension, '.ni.pdb')
29
30 # DBG file for a .so library
31 $SODbg = $SymbolPath.Replace($Extension, '.so.dbg')
32
33 # DWARF file for a .dylib
34 $DylibDwarf = $SymbolPath.Replace($Extension, '.dylib.dwarf')
35
36 $dotnetSymbolExe = "$env:USERPROFILE\.dotnet\tools"
37 $dotnetSymbolExe = Resolve-Path "$dotnetSymbolExe\dotnet-symbol.exe"
38
39 & $dotnetSymbolExe --symbols --modules --windows-pdbs $TargetServerParam $FullPath -o $SymbolsPath | Out-Null
40
41 if (Test-Path $PdbPath) {
42 return 'PDB'
43 }
44 elseif (Test-Path $NGenPdb) {
45 return 'NGen PDB'
46 }
47 elseif (Test-Path $SODbg) {
48 return 'DBG for SO'
49 }
50 elseif (Test-Path $DylibDwarf) {
51 return 'Dwarf for Dylib'
52 }
53 elseif (Test-Path $SymbolPath) {
54 return 'Module'
55 }
56 else {
57 return $null
58 }
59 }
60
61 function CountMissingSymbols {
62 param(
63 [string] $PackagePath # Path to a NuGet package
64 )
65
66 # Ensure input file exist
67 if (!(Test-Path $PackagePath)) {
68 Write-PipelineTaskError "Input file does not exist: $PackagePath"
69 ExitWithExitCode 1
70 }
71
72 # Extensions for which we'll look for symbols
73 $RelevantExtensions = @('.dll', '.exe', '.so', '.dylib')
74
75 # How many files are missing symbol information
76 $MissingSymbols = 0
77
78 $PackageId = [System.IO.Path]::GetFileNameWithoutExtension($PackagePath)
79 $PackageGuid = New-Guid
80 $ExtractPath = Join-Path -Path $ExtractPath -ChildPath $PackageGuid
81 $SymbolsPath = Join-Path -Path $ExtractPath -ChildPath 'Symbols'
82
83 [System.IO.Compression.ZipFile]::ExtractToDirectory($PackagePath, $ExtractPath)
84
85 Get-ChildItem -Recurse $ExtractPath |
86 Where-Object {$RelevantExtensions -contains $_.Extension} |
87 ForEach-Object {
88 if ($_.FullName -Match '\\ref\\') {
89 Write-Host "`t Ignoring reference assembly file " $_.FullName
90 return
91 }
92
93 $SymbolsOnMSDL = FirstMatchingSymbolDescriptionOrDefault $_.FullName '--microsoft-symbol-server' $SymbolsPath
94 $SymbolsOnSymWeb = FirstMatchingSymbolDescriptionOrDefault $_.FullName '--internal-server' $SymbolsPath
95
96 Write-Host -NoNewLine "`t Checking file " $_.FullName "... "
97
98 if ($SymbolsOnMSDL -ne $null -and $SymbolsOnSymWeb -ne $null) {
99 Write-Host "Symbols found on MSDL ($SymbolsOnMSDL) and SymWeb ($SymbolsOnSymWeb)"
100 }
101 else {
102 $MissingSymbols++
103
104 if ($SymbolsOnMSDL -eq $null -and $SymbolsOnSymWeb -eq $null) {
105 Write-Host 'No symbols found on MSDL or SymWeb!'
106 }
107 else {
108 if ($SymbolsOnMSDL -eq $null) {
109 Write-Host 'No symbols found on MSDL!'
110 }
111 else {
112 Write-Host 'No symbols found on SymWeb!'
113 }
114 }
115 }
116 }
117
118 Pop-Location
119
120 return $MissingSymbols
121 }
122
123 function CheckSymbolsAvailable {
124 if (Test-Path $ExtractPath) {
125 Remove-Item $ExtractPath -Force -Recurse -ErrorAction SilentlyContinue
126 }
127
128 Get-ChildItem "$InputPath\*.nupkg" |
129 ForEach-Object {
130 $FileName = $_.Name
131
132 # These packages from Arcade-Services include some native libraries that
133 # our current symbol uploader can't handle. Below is a workaround until
134 # we get issue: https://github.com/dotnet/arcade/issues/2457 sorted.
135 if ($FileName -Match 'Microsoft\.DotNet\.Darc\.') {
136 Write-Host "Ignoring Arcade-services file: $FileName"
137 Write-Host
138 return
139 }
140 elseif ($FileName -Match 'Microsoft\.DotNet\.Maestro\.Tasks\.') {
141 Write-Host "Ignoring Arcade-services file: $FileName"
142 Write-Host
143 return
144 }
145
146 Write-Host "Validating $FileName "
147 $Status = CountMissingSymbols "$InputPath\$FileName"
148
149 if ($Status -ne 0) {
150 Write-PipelineTelemetryError -Category 'CheckSymbols' -Message "Missing symbols for $Status modules in the package $FileName"
151 ExitWithExitCode $exitCode
152 }
153
154 Write-Host
155 }
156 }
157
158 function InstallDotnetSymbol {
159 $dotnetSymbolPackageName = 'dotnet-symbol'
160
161 $dotnetRoot = InitializeDotNetCli -install:$true
162 $dotnet = "$dotnetRoot\dotnet.exe"
163 $toolList = & "$dotnet" tool list --global
164
165 if (($toolList -like "*$dotnetSymbolPackageName*") -and ($toolList -like "*$dotnetSymbolVersion*")) {
166 Write-Host "dotnet-symbol version $dotnetSymbolVersion is already installed."
167 }
168 else {
169 Write-Host "Installing dotnet-symbol version $dotnetSymbolVersion..."
170 Write-Host 'You may need to restart your command window if this is the first dotnet tool you have installed.'
171 & "$dotnet" tool install $dotnetSymbolPackageName --version $dotnetSymbolVersion --verbosity "minimal" --global
172 }
173 }
174
175 try {
176 . $PSScriptRoot\post-build-utils.ps1
177
178 Add-Type -AssemblyName System.IO.Compression.FileSystem
179
180 InstallDotnetSymbol
181
182 CheckSymbolsAvailable
183 }
184 catch {
185 Write-Host $_.ScriptStackTrace
186 Write-PipelineTelemetryError -Category 'CheckSymbols' -Message $_
187 ExitWithExitCode 1
188 }