DEVenv/Internal/win_clang_merge_compilation_command_files.bat

74 lines
2.5 KiB
Batchfile

@echo off
REM NOTE: Overview (Version 1)
REM ----------------------------------------------------------------------------
REM
REM Merge multiple compilation command files generated by clang -MJ and dump it
REM to standard output as a JSON array. This script lets you merge the output
REM files clang dumps on execution of this command without relying on any
REM external tools, just pure batch scripting.
REM
REM An entry generated by clang -MJ, looks like this:
REM
REM {
REM "directory": "/home/user/dev/llvm/build",
REM "file": "/tmp/foo.cpp",
REM "output": "foo.o",
REM "arguments": ["/usr/bin/clang-5.0", "-xc++", "/tmp/foo.cpp", "--driver-mode=g++", "-Wall", "-I", "/home/user/dev/libcpp/libcpp/include", "-c", "--target=x86_64-unknown-linux-gnu"]
REM }
REM
REM See: https://sarcasm.github.io/notes/dev/compilation-database.html#clang
REM
REM NOTE: Examples/Integration
REM ----------------------------------------------------------------------------
REM On Windows you can generate this file easily on MSVC projects by utilising
REM the Clang MSVC CL wrapper in clang-cl which converts MSVC flags into Clang
REM compatible flags.
REM
REM clang-cl -clang:-MJcompile.json -nologo -W4 -Z7 your_cpp_file.cpp
REM
REM If you already use clang on Windows
REM
REM clang -MJcompile.json your_cpp_file.cpp
REM
REM You may merge the generated files into a valid compile_commands.json via
REM this script (by default it dumps to standard out) i.e.
REM
REM clang_merge_compilation_command_files.bat compile.json > compile_commands.json
REM
REM NOTE: What's it for?
REM ----------------------------------------------------------------------------
REM Compilation commands can be used by LSP so that LSP daemons, like clangd are
REM able to semantically understand the code and provide code completion, hints
REM AST operations and so forth.
setlocal EnableDelayedExpansion
if [%1]==[] (
echo Usage: %0 [compilation command files...]
exit /b -1
)
REM Count the number of arguments we received
set arg_count=0
for %%x in (%*) do ( set /A arg_count+=1 )
REM Open a JSON array to splat the JSON compile commands objects into
echo [
REM Append the compile commands in
set arg_next_index=0
for %%x in (%*) do (
set /A arg_next_index+=1
set /P contents=<%%x
REM On the last compile command to append to the array, remove the trailing
REM comma on the JSON object
if !arg_next_index! == !arg_count! (
set contents=!contents:~0,-1!
)
echo !contents!
)
REM Close the JSON array
echo ]