mycocci.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #/bin/bash
  2. # StarPU --- Runtime system for heterogeneous multicore architectures.
  3. #
  4. # Copyright (C) 2012 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. mode="chain" # By default, let's do everything !
  17. results="./results" # TODO Add an option for this
  18. scripts_dir=
  19. target=
  20. spatch=`which spatch`
  21. if [ $spatch = "" ]
  22. then
  23. echo "Could not find spatch."
  24. exit 1;
  25. fi
  26. delete_if_empty()
  27. {
  28. test -s $1 || rm $1
  29. }
  30. create_directories()
  31. {
  32. if [ $1 == "chain" ]
  33. then
  34. # XXX mkdir -p ${results}/{context,org,patch,report} ?
  35. mkdir -p ${results}/context
  36. mkdir -p ${results}/org
  37. mkdir -p ${results}/patch
  38. mkdir -p ${results}/report
  39. else
  40. mkdir -p ${results}/$1
  41. fi
  42. }
  43. run_script()
  44. {
  45. script=$1
  46. mode=$2
  47. # Make sure the script explicitely defines this virtual rule.
  48. grep "^virtual ${mode}" $script > /dev/null || return;
  49. output_file=`basename ${script}`
  50. output_file=${results}/${mode}/${output_file%cocci}${mode}
  51. error_file=${output_file%${mode}}err
  52. options="-very_quiet"
  53. echo "Running ${script} with mode ${mode}."
  54. # Seems like we do not need to specify -dir.
  55. # XXX Try to use -o
  56. $spatch $script $target -D $mode ${options} > ${output_file} 2>${error_file}
  57. # Nobody cares about empty files
  58. delete_if_empty ${output_file}
  59. delete_if_empty ${error_file}
  60. }
  61. run_scripts ()
  62. {
  63. mode=$1
  64. for script in `find $scripts_dir -name "*.cocci"`
  65. do
  66. # XXX This is ugly.
  67. if [ $mode = "context" -o $mode = "chain" ]
  68. then
  69. run_script ${script} "context"
  70. fi
  71. if [ $mode = "org" -o $mode = "chain" ]
  72. then
  73. run_script ${script} "org"
  74. fi
  75. if [ $mode = "patch" -o $mode = "chain" ]
  76. then
  77. run_script ${script} "patch"
  78. fi
  79. if [ $mode = "report" -o $mode = "chain" ]
  80. then
  81. run_script ${script} "report"
  82. fi
  83. done
  84. }
  85. usage ()
  86. {
  87. echo "\
  88. $0 [OPTIONS]
  89. -h Print this help
  90. -m <mode> Mode to use : chain, context, org, patch, report
  91. -s <scripts_dir> Path to the directory where the cocci scripts are stored
  92. -t <target> File or directory to parse
  93. "
  94. }
  95. while getopts ":hm:s:t:" opt;
  96. do
  97. case $opt in
  98. h)
  99. usage
  100. exit 0;
  101. ;;
  102. m)
  103. mode=$OPTARG;
  104. ;;
  105. s)
  106. scripts_dir=$OPTARG;
  107. ;;
  108. t)
  109. target="$target $OPTARG";
  110. ;;
  111. \?)
  112. echo "Invalid option -$OPTARG"
  113. exit 1
  114. ;;
  115. \:)
  116. echo "Option -$OPTARG requires an argument"
  117. exit 1
  118. ;;
  119. esac
  120. done
  121. # Is that {} thing portable ?
  122. test -n "$target" || { echo "-t <target> must be specified" && exit 1; }
  123. test -n "$scripts_dir" || { echo "-s <scripts_dir> must be specified" && exit 1; }
  124. create_directories $mode
  125. if [ "$mode" = "chain" ]
  126. then
  127. run_scripts "context"
  128. run_scripts "org"
  129. run_scripts "patch"
  130. run_scripts "report"
  131. else
  132. run_scripts $mode
  133. fi
  134. exit 0