ca4dabb45e14d8cd38e07c4ffadf9473ceb2193b

Author
Tristan Carel <tcarel@exalead.com>
Committer
Tristan Carel <tcarel@exalead.com>
Date

Message

New tool require_tool.sh

Diff

This diff is truncated to protect this page.

  1diff --git a/tools/require_tool.sh b/tools/require_tool.sh
  2new file mode 100755
  3index 0000000000000000000000000000000000000000..42da2ccf9371e39cee34b2b23be38d047a4d52f8
  4--- /dev/null
  5+++ b/tools/require_tool.sh
  6@@ -0,0 +1,161 @@
  7+__require_tool_version_compare ()
  8+{
  9+  (
 10+    # Locally ignore failures, otherwise we'll exit whenever $1 and $2
 11+    # are not equal!
 12+    set +e
 13+
 14+awk_strverscmp='
 15+  # Use only awk features that work with 7th edition Unix awk (1978).
 16+  # My, what an old awk you have, Mr. Solaris!
 17+  END {
 18+    while (length(v1) || length(v2)) {
 19+      # Set d1 to be the next thing to compare from v1, and likewise for d2.
 20+      # Normally this is a single character, but if v1 and v2 contain digits,
 21+      # compare them as integers and fractions as strverscmp does.
 22+      if (v1 ~ /^[0-9]/ && v2 ~ /^[0-9]/) {
 23+	# Split v1 and v2 into their leading digit string components d1 and d2,
 24+	# and advance v1 and v2 past the leading digit strings.
 25+	for (len1 = 1; substr(v1, len1 + 1) ~ /^[0-9]/; len1++) continue
 26+	for (len2 = 1; substr(v2, len2 + 1) ~ /^[0-9]/; len2++) continue
 27+	d1 = substr(v1, 1, len1); v1 = substr(v1, len1 + 1)
 28+	d2 = substr(v2, 1, len2); v2 = substr(v2, len2 + 1)
 29+	if (d1 ~ /^0/) {
 30+	  if (d2 ~ /^0/) {
 31+	    # Compare two fractions.
 32+	    while (d1 ~ /^0/ && d2 ~ /^0/) {
 33+	      d1 = substr(d1, 2); len1--
 34+	      d2 = substr(d2, 2); len2--
 35+	    }
 36+	    if (len1 != len2 && ! (len1 && len2 && substr(d1, 1, 1) == substr(d2, 1, 1))) {
 37+	      # The two components differ in length, and the common prefix
 38+	      # contains only leading zeros.  Consider the longer to be less.
 39+	      d1 = -len1
 40+	      d2 = -len2
 41+	    } else {
 42+	      # Otherwise, compare as strings.
 43+	      d1 = "x" d1
 44+	      d2 = "x" d2
 45+	    }
 46+	  } else {
 47+	    # A fraction is less than an integer.
 48+	    exit 1
 49+	  }
 50+	} else {
 51+	  if (d2 ~ /^0/) {
 52+	    # An integer is greater than a fraction.
 53+	    exit 2
 54+	  } else {
 55+	    # Compare two integers.
 56+	    d1 += 0
 57+	    d2 += 0
 58+	  }
 59+	}
 60+      } else {
 61+	# The normal case, without worrying about digits.
 62+	if (v1 == "") d1 = v1; else { d1 = substr(v1, 1, 1); v1 = substr(v1,2) }
 63+	if (v2 == "") d2 = v2; else { d2 = substr(v2, 1, 1); v2 = substr(v2,2) }
 64+      }
 65+      if (d1 < d2) exit 1
 66+      if (d1 > d2) exit 2
 67+    }
 68+  }
 69+'
 70+    awk "$awk_strverscmp" v1="$1" v2="$2" /dev/null
 71+    case $? in
 72+      1)  echo '<';;
 73+      0)  echo '=';;
 74+      2)  echo '>';;
 75+    esac
 76+  )
 77+}
 78+
 79+
 80+__require_tool_fatal ()
 81+{
 82+    echo $@ >/dev/stderr
 83+    return 1
 84+}
 85+
 86+# Usage: require_tool program version
 87+# Returns: 0 if $1 version if greater equals than $2, 1 otherwise.
 88+# In case of error, message is written on error output.
 89+#
 90+# Example: require_tool gcc 4.6
 91+# Use GCC environment variable if defined instead of lookup for the tool
 92+# in the environment.
 93+require_tool ()
 94+{
 95+  envvar_name=$(echo $1 | tr '[:lower:]' '[:upper:]')
 96+  tool=$(printenv $envvar_name || echo $1)
 97+  local version=$($tool --version 2>/dev/null| \
 98+    sed -n 's/.*[^0-9.]\([0-9][0-9.]*\).*/\1/p;q')
 99+  if test x"$version" = x ; then
100+      echo "$tool is required" >/dev/stderr
101+      return 1
102+  fi
103+  case $(__require_tool_version_compare "$2" "$version") in
104+    '>')
105+	  echo "$1 $2 or better is required: this is $tool $version" >/dev/stderr