parseargs.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0+
  3. #
  4. # the corresponding .litmus.out file, and does not judge the result.
  5. #
  6. # . scripts/parseargs.sh
  7. #
  8. # Include into other Linux kernel tools/memory-model scripts.
  9. #
  10. # Copyright IBM Corporation, 2018
  11. #
  12. # Author: Paul E. McKenney <[email protected]>
  13. T=/tmp/parseargs.sh.$$
  14. mkdir $T
  15. # Initialize one parameter: initparam name default
  16. initparam () {
  17. echo if test -z '"$'$1'"' > $T/s
  18. echo then >> $T/s
  19. echo $1='"'$2'"' >> $T/s
  20. echo export $1 >> $T/s
  21. echo fi >> $T/s
  22. echo $1_DEF='$'$1 >> $T/s
  23. . $T/s
  24. }
  25. initparam LKMM_DESTDIR "."
  26. initparam LKMM_HERD_OPTIONS "-conf linux-kernel.cfg"
  27. initparam LKMM_JOBS `getconf _NPROCESSORS_ONLN`
  28. initparam LKMM_PROCS "3"
  29. initparam LKMM_TIMEOUT "1m"
  30. scriptname=$0
  31. usagehelp () {
  32. echo "Usage $scriptname [ arguments ]"
  33. echo " --destdir path (place for .litmus.out, default by .litmus)"
  34. echo " --herdopts -conf linux-kernel.cfg ..."
  35. echo " --jobs N (number of jobs, default one per CPU)"
  36. echo " --procs N (litmus tests with at most this many processes)"
  37. echo " --timeout N (herd7 timeout (e.g., 10s, 1m, 2hr, 1d, '')"
  38. echo "Defaults: --destdir '$LKMM_DESTDIR_DEF' --herdopts '$LKMM_HERD_OPTIONS_DEF' --jobs '$LKMM_JOBS_DEF' --procs '$LKMM_PROCS_DEF' --timeout '$LKMM_TIMEOUT_DEF'"
  39. exit 1
  40. }
  41. usage () {
  42. usagehelp 1>&2
  43. }
  44. # checkarg --argname argtype $# arg mustmatch cannotmatch
  45. checkarg () {
  46. if test $3 -le 1
  47. then
  48. echo $1 needs argument $2 matching \"$5\"
  49. usage
  50. fi
  51. if echo "$4" | grep -q -e "$5"
  52. then
  53. :
  54. else
  55. echo $1 $2 \"$4\" must match \"$5\"
  56. usage
  57. fi
  58. if echo "$4" | grep -q -e "$6"
  59. then
  60. echo $1 $2 \"$4\" must not match \"$6\"
  61. usage
  62. fi
  63. }
  64. while test $# -gt 0
  65. do
  66. case "$1" in
  67. --destdir)
  68. checkarg --destdir "(path to directory)" "$#" "$2" '.\+' '^--'
  69. LKMM_DESTDIR="$2"
  70. mkdir $LKMM_DESTDIR > /dev/null 2>&1
  71. if ! test -e "$LKMM_DESTDIR"
  72. then
  73. echo "Cannot create directory --destdir '$LKMM_DESTDIR'"
  74. usage
  75. fi
  76. if test -d "$LKMM_DESTDIR" -a -w "$LKMM_DESTDIR" -a -x "$LKMM_DESTDIR"
  77. then
  78. :
  79. else
  80. echo "Directory --destdir '$LKMM_DESTDIR' insufficient permissions to create files"
  81. usage
  82. fi
  83. shift
  84. ;;
  85. --herdopts|--herdopt)
  86. checkarg --destdir "(herd7 options)" "$#" "$2" '.*' '^--'
  87. LKMM_HERD_OPTIONS="$2"
  88. shift
  89. ;;
  90. -j[1-9]*)
  91. njobs="`echo $1 | sed -e 's/^-j//'`"
  92. trailchars="`echo $njobs | sed -e 's/[0-9]\+\(.*\)$/\1/'`"
  93. if test -n "$trailchars"
  94. then
  95. echo $1 trailing characters "'$trailchars'"
  96. usagehelp
  97. fi
  98. LKMM_JOBS="`echo $njobs | sed -e 's/^\([0-9]\+\).*$/\1/'`"
  99. ;;
  100. --jobs|--job|-j)
  101. checkarg --jobs "(number)" "$#" "$2" '^[1-9][0-9]\+$' '^--'
  102. LKMM_JOBS="$2"
  103. shift
  104. ;;
  105. --procs|--proc)
  106. checkarg --procs "(number)" "$#" "$2" '^[0-9]\+$' '^--'
  107. LKMM_PROCS="$2"
  108. shift
  109. ;;
  110. --timeout)
  111. checkarg --timeout "(timeout spec)" "$#" "$2" '^\([0-9]\+[smhd]\?\|\)$' '^--'
  112. LKMM_TIMEOUT="$2"
  113. shift
  114. ;;
  115. *)
  116. echo Unknown argument $1
  117. usage
  118. ;;
  119. esac
  120. shift
  121. done
  122. if test -z "$LKMM_TIMEOUT"
  123. then
  124. LKMM_TIMEOUT_CMD=""; export LKMM_TIMEOUT_CMD
  125. else
  126. LKMM_TIMEOUT_CMD="timeout $LKMM_TIMEOUT"; export LKMM_TIMEOUT_CMD
  127. fi
  128. rm -rf $T