mycocci.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/bin/bash
  2. # StarPU --- Runtime system for heterogeneous multicore architectures.
  3. #
  4. # Copyright (C) 2012-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  5. #
  6. # StarPU is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU Lesser General Public License as published by
  8. # the Free Software Foundation; either version 2.1 of the License, or (at
  9. # your option) any later version.
  10. #
  11. # StarPU is distributed in the hope that it will be useful, but
  12. # WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. #
  15. # See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. #
  17. mode="chain" # By default, let's do everything !
  18. results="./results" # TODO Add an option for this
  19. scripts_dir=
  20. target=
  21. spatch=`which spatch`
  22. if [ $spatch = "" ]
  23. then
  24. echo "Could not find spatch."
  25. exit 1;
  26. fi
  27. delete_if_empty()
  28. {
  29. test -s $1 || rm $1
  30. }
  31. create_directories()
  32. {
  33. if [ $1 == "chain" ]
  34. then
  35. # XXX mkdir -p ${results}/{context,org,patch,report} ?
  36. mkdir -p ${results}/context
  37. mkdir -p ${results}/org
  38. mkdir -p ${results}/patch
  39. mkdir -p ${results}/report
  40. else
  41. mkdir -p ${results}/$1
  42. fi
  43. }
  44. run_script()
  45. {
  46. script=$1
  47. mode=$2
  48. # Make sure the script explicitely defines this virtual rule.
  49. grep "^virtual ${mode}" $script > /dev/null || return;
  50. output_file=`basename ${script}`
  51. output_file=${results}/${mode}/${output_file%cocci}${mode}
  52. error_file=${output_file%${mode}}err
  53. options="-very_quiet"
  54. echo "Running ${script} with mode ${mode}."
  55. # Seems like we do not need to specify -dir.
  56. # XXX Try to use -o
  57. $spatch $script $target -D $mode ${options} > ${output_file} 2>${error_file}
  58. # Nobody cares about empty files
  59. delete_if_empty ${output_file}
  60. delete_if_empty ${error_file}
  61. }
  62. run_scripts ()
  63. {
  64. mode=$1
  65. for script in `find $scripts_dir -name "*.cocci"`
  66. do
  67. # XXX This is ugly.
  68. if [ $mode = "context" -o $mode = "chain" ]
  69. then
  70. run_script ${script} "context"
  71. fi
  72. if [ $mode = "org" -o $mode = "chain" ]
  73. then
  74. run_script ${script} "org"
  75. fi
  76. if [ $mode = "patch" -o $mode = "chain" ]
  77. then
  78. run_script ${script} "patch"
  79. fi
  80. if [ $mode = "report" -o $mode = "chain" ]
  81. then
  82. run_script ${script} "report"
  83. fi
  84. done
  85. }
  86. usage ()
  87. {
  88. echo "\
  89. $0 [OPTIONS]
  90. -h Print this help
  91. -m <mode> Mode to use : chain, context, org, patch, report
  92. -s <scripts_dir> Path to the directory where the cocci scripts are stored
  93. -t <target> File or directory to parse
  94. "
  95. }
  96. while getopts ":hm:s:t:" opt;
  97. do
  98. case $opt in
  99. h)
  100. usage
  101. exit 0;
  102. ;;
  103. m)
  104. mode=$OPTARG;
  105. ;;
  106. s)
  107. scripts_dir=$OPTARG;
  108. ;;
  109. t)
  110. target="$target $OPTARG";
  111. ;;
  112. \?)
  113. echo "Invalid option -$OPTARG"
  114. exit 1
  115. ;;
  116. \:)
  117. echo "Option -$OPTARG requires an argument"
  118. exit 1
  119. ;;
  120. esac
  121. done
  122. # Is that {} thing portable ?
  123. test -n "$target" || { echo "-t <target> must be specified" && exit 1; }
  124. test -n "$scripts_dir" || { echo "-s <scripts_dir> must be specified" && exit 1; }
  125. create_directories $mode
  126. if [ "$mode" = "chain" ]
  127. then
  128. run_scripts "context"
  129. run_scripts "org"
  130. run_scripts "patch"
  131. run_scripts "report"
  132. else
  133. run_scripts $mode
  134. fi
  135. exit 0