3v1n0

generate-full-compile-commands.sh

Mar 9th, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. THIS_PATH=$(dirname $0)
  4.  
  5. if [ "$1" = "--update-root" ]; then
  6. update_root=true
  7. shift
  8. fi
  9.  
  10. if [ -z "$*" ]; then
  11. targets=$THIS_PATH/_BUILD/*/compile_commands.json $THIS_PATH/*/compile_commands.json
  12. else
  13. for i in $*; do
  14. targets+="$THIS_PATH/_BUILD/$i $THIS_PATH/$i "
  15. done
  16. fi
  17.  
  18. function count_files()
  19. {
  20. grep "\"file\"" -c $1 2> /dev/null || echo 0
  21. }
  22.  
  23. function print_stats()
  24. {
  25. echo "$1 has $(count_files $1) files"
  26. }
  27.  
  28. function update_commands()
  29. {
  30. path=$1
  31. shift
  32. extra_paths=$*
  33.  
  34. print_stats $path/compile_commands.json
  35.  
  36. for p in $path $extra_paths; do
  37. update_paths+="-p $p "
  38. done
  39.  
  40. compdb $update_paths list -1 > $path/compile_commands_full.json
  41. compdb_ret=$?
  42.  
  43. if [ $compdb_ret == 0 ] && [ -f $path/compile_commands_full.json ]; then
  44. if [ $(count_files $path/compile_commands_full.json) -gt 0 ]; then
  45. mv -v $path/compile_commands_full.json $path/compile_commands.json
  46.  
  47. for p in $path $extra_paths; do
  48. touch $p/compile_commands_headers.stamp
  49. done
  50. else
  51. rm -f $path/compile_commands_full.json
  52. rm -f $path/compile_commands_headers.stamp
  53. fi
  54. fi
  55.  
  56. print_stats $path/compile_commands.json
  57.  
  58. exit $compdb_ret
  59. }
  60.  
  61. subpaths=""
  62.  
  63. for i in $targets; do
  64. if [ -d $i ]; then
  65. path=$i
  66. else
  67. path=$(dirname $i)
  68. fi
  69.  
  70. if [ ! -f $path/compile_commands.json ]; then
  71. continue
  72. fi
  73.  
  74. if [ -z "$update_root" ]; then
  75. update_commands $path
  76. echo ""
  77. fi
  78.  
  79. subpaths+="$path "
  80. done
  81.  
  82. if [ -z "$subpaths" ]; then
  83. echo "No path containing a 'compile_commands.json' found"
  84. exit 1;
  85. fi
  86.  
  87. if [ -n "$update_root" ]; then
  88. echo "===== UPDATING MAIN FILE ====="
  89.  
  90. if [ ! -f "compile_commands.json" ]; then
  91. echo "Missing compile_commands.json, creating a new one..."
  92. echo "[]" > $THIS_PATH/compile_commands.json
  93. fi
  94.  
  95. exec update_commands $THIS_PATH $subpaths
  96. fi
Advertisement
Add Comment
Please, Sign In to add comment