mycocci.sh 3.2 KB

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