Skip to content

Instantly share code, notes, and snippets.

@Trass3r
Last active July 20, 2021 16:55
Show Gist options
  • Save Trass3r/f3fbe6807d28106e917368c33abf45d4 to your computer and use it in GitHub Desktop.
Save Trass3r/f3fbe6807d28106e917368c33abf45d4 to your computer and use it in GitHub Desktop.
Generate compile_commands.json from MSBuild
# first rebuild solution
# then
echo '[' > compile_commands.json
find obj/v140/x64/Debug/ -name 'CL.command.*' | xargs iconv -f utf-16 -t utf-8 | grep -hF '/c' | sed -r -e 's#\\#/#g' -e 's/"/\\"/g' -e 's#/c .+ ((.:/.+)/[^/]+\.[^/]*)$#{\r\n\t"command": "clang-cl.exe -m64 --driver-mode=cl \0 -Wno-error",\r\n\t"file": "\1",\r\n\t"directory": "\2"\r\n},#g' >> compile_commands.json
echo ']' >> compile_commands.json
# example use
"C:\Program Files\Git\usr\bin\find.exe" src -name "*.cpp" | "C:\Program Files\Git\usr\bin\xargs.exe" -n1 -P1 -t "C:\Program Files\LLVM\bin\clang-check.exe" -p . -fixit
find src -name '*.cpp' | xargs -n1 -P1 -t 'C:\Program Files\LLVM\bin\clang-tidy.exe' -p . -header-filter=.* -fix -checks=cppcoreguidelines-pro-type-member-init
# other checks: modernize-use-override cppcoreguidelines-special-member-functions,cppcoreguidelines-pro-type-member-init
#### getting the directory right:
longest_common_prefix()
{
readarray names
declare -a parts
declare i=0
name=${names[0]}
while x=$(dirname "$name"); [ "$x" != "/" ]
do
parts[$i]="$x"
i=$(($i + 1))
name="$x"
done
for prefix in "${parts[@]}" /
do
for name in "${names[@]}"
do
if [ "${name#$prefix/}" = "${name}" ]
then continue 2
fi
done
echo "$prefix"
break
done
}
handleFile()
{
echo "$1"
projectName=$(echo "$1" | sed -r 's#.+/([^/]+)/[^/]+.tlog/[^/]+.tlog$#\1#g')
echo $projectName
directory=$(iconv -f utf-16 -t utf-8 "$1" | grep -hF '^' | sed -r -e 's#\\#/#g' | sed 's/\^/\//' | longest_common_prefix)
directory=${directory:1}
echo $directory
iconv -f utf-16 -t utf-8 "$1" | grep -hF '/c' | sed -r -e 's#\\#/#g' -e 's/"/\\"/g' -e "s#/c .+ ((.:/.+)/[^/]+\.[^/]*)\$#{\r\n\t\"command\": \"clang-cl.exe -m64 --driver-mode=cl \\0 -Wno-error\",\r\n\t\"file\": \"\\1\",\r\n\t\"directory\": \"$directory\"\r\n},#g" >> compile_commands.json
}
export -f handleFile
export -f longest_common_prefix
echo '[' > compile_commands.json
find obj/v140/x64/Debug -name 'CL.command.*' -exec bash -c 'handleFile "$0"' {} \;
echo ']' >> compile_commands.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment