diff --git a/.clangd b/.clangd index 4a53082..4d8fac2 100644 --- a/.clangd +++ b/.clangd @@ -1,2 +1,4 @@ CompileFlags: CompilationDatabase: build/editor-x86 + Add: [--driver-mode=cl, --target=i686-pc-windows-msvc, -fms-compatibility-version=19.29] + Remove: [/FS, /O2, /Ob2] diff --git a/configure-clangd.ps1 b/configure-clangd.ps1 new file mode 100644 index 0000000..b87e153 --- /dev/null +++ b/configure-clangd.ps1 @@ -0,0 +1,58 @@ +[CmdletBinding()] +param() + +$ErrorActionPreference = 'Stop' +$root = $PSScriptRoot.Replace('\', '/') +$project = Get-Content -LiteralPath "$PSScriptRoot/decomp.json" -Raw | ConvertFrom-Json +$compile = Get-Content -LiteralPath "$PSScriptRoot/$($project.compileCommands)" -Raw | ConvertFrom-Json +$workspace = Get-Content -LiteralPath "$PSScriptRoot/.td/config.json" -Raw | ConvertFrom-Json +$runner = $workspace.runners.PSObject.Properties[$project.toolchain].Value + +if (-not $runner -or $runner.type -ne 'native' -or -not $runner.toolchainRoot) { + throw "clangd configuration requires this workspace's native Windows toolchain." +} + +$toolchain = $runner.toolchainRoot.Replace('\', '/') +$compiler = $compile.compiler.Replace('${TC}', $toolchain).Replace('${SRC}', $root) + +if (-not (Test-Path -LiteralPath $compiler -PathType Leaf)) { + throw "Configured compiler not found: $compiler" +} + +$systemIncludes = @($compile.env.INCLUDE.Replace('${TC}', $toolchain).Replace('${SRC}', $root).Split(';') | Where-Object { $_ }) + +foreach ($include in $systemIncludes) { + if (-not (Test-Path -LiteralPath $include -PathType Container)) { + throw "Configured include directory not found: $include" + } +} + +$commands = @( + foreach ($unit in $compile.units.PSObject.Properties) { + $file = "$root/$($unit.Name)" + + if (-not (Test-Path -LiteralPath $file -PathType Leaf)) { + throw "Configured source file not found: $file" + } + + $arguments = @($compiler) + $arguments += @($compile.shapes[[int]$unit.Value] | ForEach-Object { + $_.Replace('${TC}', $toolchain).Replace('${SRC}', $root) + }) + $arguments += @($systemIncludes | ForEach-Object { "/imsvc$_" }) + $arguments += $file + + [ordered]@{ + directory = $root + file = $file + arguments = $arguments + } + } +) + +$outputDirectory = Join-Path $PSScriptRoot 'build/editor-x86' +[void](New-Item -ItemType Directory -Path $outputDirectory -Force) +$output = Join-Path $outputDirectory 'compile_commands.json' +$json = ConvertTo-Json -InputObject $commands -Depth 6 +[System.IO.File]::WriteAllText($output, $json + "`n", [System.Text.UTF8Encoding]::new($false)) +Write-Output "Generated $($commands.Count) clangd compilation commands: $output"