mycocci.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. output_file=`basename ${script}`
  48. output_file=${results}/${mode}/${output_file%cocci}${mode}
  49. error_file=${output_file%${mode}}err
  50. options="-very_quiet"
  51. echo "Running ${script} with mode ${mode}."
  52. # Seems like we do not need to specify -dir.
  53. # XXX Try to use -o
  54. $spatch $script $target -D $mode ${options} > ${output_file} 2>${error_file}
  55. # Nobody cares about empty files
  56. delete_if_empty ${output_file}
  57. delete_if_empty ${error_file}
  58. }
  59. run_scripts ()
  60. {
  61. mode=$1
  62. for script in `find $scripts_dir -name "*.cocci"`
  63. do
  64. # XXX This is ugly.
  65. if [ $mode = "context" -o $mode = "chain" ]
  66. then
  67. run_script ${script} "context"
  68. fi
  69. if [ $mode = "org" -o $mode = "chain" ]
  70. then
  71. run_script ${script} "org"
  72. fi
  73. if [ $mode = "patch" -o $mode = "chain" ]
  74. then
  75. run_script ${script} "patch"
  76. fi
  77. if [ $mode = "report" -o $mode = "chain" ]
  78. then
  79. run_script ${script} "report"
  80. fi
  81. done
  82. }
  83. usage ()
  84. {
  85. echo "\
  86. $0 [OPTIONS]
  87. -h Print this help
  88. -m <mode> Mode to use : chain, context, org, patch, report
  89. -s <scripts_dir> Path to the directory where the cocci scripts are stored
  90. -t <target> File or directory to parse
  91. "
  92. }
  93. while getopts ":hm:s:t:" opt;
  94. do
  95. case $opt in
  96. h)
  97. usage
  98. exit 0;
  99. ;;
  100. m)
  101. mode=$OPTARG;
  102. ;;
  103. s)
  104. scripts_dir=$OPTARG;
  105. ;;
  106. t)
  107. target=$OPTARG;
  108. ;;
  109. \?)
  110. echo "Invalid option -$OPTARG"
  111. exit 1
  112. ;;
  113. \:)
  114. echo "Option -$OPTARG requires an argument"
  115. exit 1
  116. ;;
  117. esac
  118. done
  119. # Is that {} thing portable ?
  120. test -n "$target" || { echo "-t <target> must be specified" && exit 1; }
  121. test -n "$scripts_dir" || { echo "-s <scripts_dir> must be specified" && exit 1; }
  122. create_directories $mode
  123. if [ "$mode" = "chain" ]
  124. then
  125. run_scripts "context"
  126. run_scripts "org"
  127. run_scripts "patch"
  128. run_scripts "report"
  129. else
  130. run_scripts $mode
  131. fi
  132. exit 0