-
Notifications
You must be signed in to change notification settings - Fork 409
Adding script to generate compile commands database #5883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 36 commits
9ab36b7
3d8dc83
8323d8f
f6908a3
fc32011
0f23f7f
f215bdd
061dbcd
00f9789
a3befce
e2ed1bb
1c0141c
213caaf
eb97073
c3cddcb
46c9ca6
93df0d8
5d24606
4a0ad1d
0d6b13f
5ecd1b2
221f460
7aa3f44
c3b1f8e
b7e9fe9
fcb6344
649a900
28c7dd2
472d62d
53ea440
f63dbf3
37f8e52
f2cb8e3
b196ba3
6da3435
119b742
331fa32
04a0b16
1a2ab6a
695a614
728d2dd
d465a4e
acfe357
028635c
4cffe83
5d136de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| Param( | ||
guimafelipe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| [string]$Ms2ccVersion = "1.3.0", | ||
| [switch]$Update | ||
|
|
||
| ) | ||
|
|
||
| $binlogFileBase = Join-Path (Split-Path $PSScriptRoot -parent) "BuildOutput\Binlogs" | ||
| $binlogFiles = Get-ChildItem -Path $binlogFileBase -Filter *.binlog -Recurse | Select-Object -ExpandProperty FullName | ||
|
|
||
| Write-Host "Binlog files found:" | ||
| $binlogFiles | ForEach-Object { Write-Host $_ } | ||
guimafelipe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| $VCToolsInstallDir = . "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" -Latest -prerelease -requires Microsoft.Component.MSBuild -property InstallationPath | ||
| write-host "VCToolsInstallDir: $VCToolsInstallDir" | ||
guimafelipe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
guimafelipe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
guimafelipe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| $msBuildPath = "$VCToolsInstallDir\MSBuild\Current\Bin\msbuild.exe" | ||
| write-host "msBuildPath: $msBuildPath" | ||
guimafelipe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Remove-Item "temp-filtered.log" -ErrorAction SilentlyContinue | ||
guimafelipe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Remove-Item "temp-filtered2.log" -ErrorAction SilentlyContinue | ||
|
|
||
| # Target "ClCompile" in file "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets" from project "C:\WindowsAppSDK\dev\Detours\Detours.vcxproj" (target "_ClCompile" depends on it): | ||
guimafelipe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # Context here would be: "C:\WindowsAppSDK\dev\Detours" | ||
|
|
||
| foreach ($binlogFile in $binlogFiles) { | ||
guimafelipe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (-Not (Test-Path $binlogFile)) { | ||
| Write-Error "Binlog file not found: $binlogFile" | ||
| exit 1 | ||
| } | ||
|
|
||
| & $msBuildPath $binlogFile /v:normal /noconlog /flp:logfile=temp.log | ||
guimafelipe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Select-String -Path "temp.log" -Pattern "Target ""ClCompile"" in file","Cl.exe" | | ||
| ForEach-Object { $_.Line } | | ||
| Where-Object { $_ -notmatch "Tracker.exe" } | | ||
| Out-File -FilePath "temp-filtered.log" -Append -Encoding utf8 | ||
|
|
||
| Remove-Item "temp.log" | ||
guimafelipe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Write-Host "Processed binlog file: $binlogFile" | ||
guimafelipe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| Write-Host "Filtered log file generated at: $(Get-Location)\temp-filtered.log" | ||
guimafelipe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| $contextPath = "" | ||
|
|
||
| # for each line in temp-filtered.log, if it is a "Target " line, extract the project directory and save in the contextPath variable | ||
| # if it is a "Cl.exe" line, prepend the contextPath to every source file in that line only if it is | ||
| # a relative path, then output the modified line to temp-filtered-2.log | ||
| # The source files are the arguments that ends with .cpp, .c, or .h | ||
| # They can have spaces in them. If so, they are enclosed in quotes. | ||
|
|
||
| $lines = Get-Content "temp-filtered.log" | ||
|
|
||
| Write-Host "Processing filtered log to adjust file paths..." | ||
guimafelipe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| foreach ($line in $lines) { | ||
| if ($line -match 'Target "ClCompile" in file "([^"]+)" from project "([^"]+)"') { | ||
| $contextPath = Split-Path $matches[2] -parent | ||
| Write-Host "Context path set to: $contextPath" | ||
| } elseif ($line -match 'Cl\.exe (.+)$') { | ||
|
||
| $clLine = $matches[1] | ||
| $args = $clLine -split ' (?=(?:[^"]*"[^"]*")*[^"]*$)' | ||
|
||
|
|
||
| $modifiedArgs = @() | ||
| foreach ($arg in $args) { | ||
| if ($arg -match '^(.*\.(cpp|c|h))$' -or $arg -match '^"(.*\.(cpp|c|h))"$') { | ||
guimafelipe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $filePath = $arg.Trim('"') | ||
| if (-Not ([System.IO.Path]::IsPathRooted($filePath))) { | ||
| $filePath = Join-Path $contextPath $filePath | ||
| } | ||
| if ($arg.StartsWith('"') -and $arg.EndsWith('"')) { | ||
| $modifiedArgs += '"' + $filePath + '"' | ||
| } else { | ||
| $modifiedArgs += $filePath | ||
| } | ||
| } else { | ||
| $modifiedArgs += $arg | ||
| } | ||
| } | ||
|
|
||
| $modifiedLine = "Cl.exe " + ($modifiedArgs -join ' ') | ||
| Add-Content -Path "temp-filtered2.log" -Value $modifiedLine | ||
| } | ||
| } | ||
|
|
||
| Write-Host "Adjusted file paths and generated: $(Get-Location)\temp-filtered2.log" | ||
|
|
||
| $ms2ccPath = Join-Path $PSScriptRoot "ms2cc" | ||
| $ms2ccExe = Join-Path $ms2ccPath "ms2cc.exe" | ||
|
|
||
| if (-Not (Test-Path $ms2ccExe)) { | ||
| Write-Host "Downloading ms2cc..." | ||
| $ms2ccUrl = "https://github.com/freddiehaddad/ms2cc/releases/download/v$Ms2ccVersion/ms2cc-$Ms2ccVersion.zip" | ||
guimafelipe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $zipPath = Join-Path $PSScriptRoot "ms2cc-$Ms2ccVersion.zip" | ||
guimafelipe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| try { | ||
| Invoke-WebRequest -Uri $ms2ccUrl -OutFile $zipPath -UseBasicParsing | ||
|
||
| Write-Host "Downloaded ms2cc to: $zipPath" | ||
|
|
||
| if (-Not (Test-Path $ms2ccPath)) { | ||
| New-Item -ItemType Directory -Path $ms2ccPath -Force | Out-Null | ||
| } | ||
|
|
||
| Expand-Archive -Path $zipPath -DestinationPath $ms2ccPath -Force | ||
| Write-Host "Extracted ms2cc to: $ms2ccPath" | ||
|
|
||
| Remove-Item $zipPath -Force | ||
| Write-Host "Cleaned up zip file" | ||
guimafelipe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (Test-Path $ms2ccExe) { | ||
| Write-Host "ms2cc successfully downloaded and extracted" | ||
| } else { | ||
| Write-Error "Failed to extract ms2cc.exe" | ||
| exit 1 | ||
| } | ||
| } | ||
| catch { | ||
| Write-Error "Failed to download or extract ms2cc: $_" | ||
| exit 1 | ||
| } | ||
| } else { | ||
| Write-Host "ms2cc already exists at: $ms2ccExe" | ||
| } | ||
|
|
||
| $backupPath = $null | ||
|
|
||
| # If the -Update flag is provided, we want to update an existing compile_commands.json | ||
| # instead of overwriting it. | ||
| if ($Update) { | ||
| # Save the old compile_commands.json if it exists in a new file | ||
| $compileCommandsPath = Join-Path (Split-Path $PSScriptRoot -parent) "compile_commands.json" | ||
| if (Test-Path $compileCommandsPath) { | ||
| $backupPath = Join-Path (Split-Path $PSScriptRoot -parent) "compile_commands_old.json" | ||
guimafelipe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Copy-Item -Path $compileCommandsPath -Destination $backupPath | ||
| Write-Host "Backed up existing compile_commands.json to: $backupPath" | ||
| } else { | ||
| Write-Host "No existing compile_commands.json found to back up." | ||
| } | ||
| } | ||
|
|
||
| & $ms2ccExe -i "temp-filtered2.log" -d (Split-Path $PSScriptRoot -parent) -p | ||
guimafelipe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Remove-Item "temp-filtered.log" | ||
| Remove-Item "temp-filtered2.log" | ||
| Write-Host "Temporary files cleaned up." | ||
|
|
||
| if ($Update -and (Test-Path $backupPath)) { | ||
| # Merge the old and new compile_commands.json files | ||
| $newCommands = Get-Content -Raw -Path (Join-Path (Split-Path $PSScriptRoot -parent) "compile_commands.json") | ConvertFrom-Json | ||
| $oldCommands = Get-Content -Raw -Path $backupPath | ConvertFrom-Json | ||
|
|
||
| # What defines the uniqueness of a command is the combination of "file" and "directory" | ||
| # We want to merge the old and new commands, preferring the new ones in case of duplicates | ||
| $mergedCommands = @{} | ||
| foreach ($cmd in $oldCommands + $newCommands) { | ||
| $key = "$($cmd.file)|$($cmd.directory)" | ||
| $mergedCommands[$key] = $cmd | ||
| } | ||
| $mergedCommands.Values | ConvertTo-Json -Depth 10 | Set-Content -Path (Join-Path (Split-Path $PSScriptRoot -parent) "compile_commands.json") -Encoding utf8 | ||
| Write-Host "Merged old and new compile_commands.json files." | ||
|
|
||
| Remove-Item $backupPath -Force | ||
| } | ||
|
|
||
| Write-Host "Compilation database generated at: $(Split-Path $PSScriptRoot -parent)\compile_commands.json" | ||
Uh oh!
There was an error while loading. Please reload this page.