1 __require_tool_version_compare
()
4 # Locally ignore failures, otherwise we'll exit whenever $1 and $2
9 # Use only awk features that work with 7th edition Unix awk (1978).
10 # My, what an old awk you have, Mr. Solaris!
12 while (length(v1) || length(v2)) {
13 # Set d1 to be the next thing to compare from v1, and likewise for d2.
14 # Normally this is a single character, but if v1 and v2 contain digits,
15 # compare them as integers and fractions as strverscmp does.
16 if (v1 ~ /^[0-9]/ && v2 ~ /^[0-9]/) {
17 # Split v1 and v2 into their leading digit string components d1 and d2,
18 # and advance v1 and v2 past the leading digit strings.
19 for (len1 = 1; substr(v1, len1 + 1) ~ /^[0-9]/; len1++) continue
20 for (len2 = 1; substr(v2, len2 + 1) ~ /^[0-9]/; len2++) continue
21 d1 = substr(v1, 1, len1); v1 = substr(v1, len1 + 1)
22 d2 = substr(v2, 1, len2); v2 = substr(v2, len2 + 1)
25 # Compare two fractions.
26 while (d1 ~ /^0/ && d2 ~ /^0/) {
27 d1 = substr(d1, 2); len1--
28 d2 = substr(d2, 2); len2--
30 if (len1 != len2 && ! (len1 && len2 && substr(d1, 1, 1) == substr(d2, 1, 1))) {
31 # The two components differ in length, and the common prefix
32 # contains only leading zeros. Consider the longer to be less.
36 # Otherwise, compare as strings.
41 # A fraction is less than an integer.
46 # An integer is greater than a fraction.
49 # Compare two integers.
55 # The normal case, without worrying about digits.
56 if (v1 == "") d1 = v1; else { d1 = substr(v1, 1, 1); v1 = substr(v1,2) }
57 if (v2 == "") d2 = v2; else { d2 = substr(v2, 1, 1); v2 = substr(v2,2) }
64 awk "$awk_strverscmp" v1
="$1" v2
="$2" /dev
/null
74 __require_tool_fatal
()
80 # Usage: require_tool program version
81 # Returns: 0 if $1 version if greater equals than $2, 1 otherwise.
82 # In case of error, message is written on error output.
84 # Example: require_tool gcc 4.6
85 # Use GCC environment variable if defined instead of lookup for the tool
89 envvar_name
=$(echo $1 | tr '[:lower:]' '[:upper:]')
90 tool
=$(printenv $envvar_name || echo $1)
91 local version
=$
($tool --version 2>/dev
/null
| \
92 sed -n 's/.*[^0-9.]\([0-9]*\.[0-9.]*\).*/\1/p;q')
93 if test x
"$version" = x
; then
94 echo "$tool is required" >/dev
/stderr
97 case $(__require_tool_version_compare "$2" "$version") in
99 echo "$1 $2 or better is required: this is $tool $version" >/dev
/stderr
108 require_tool.sh - Ensure version of a tool is greater than the one expected
111 require_tool.sh [ -h ]
116 TOOL is the name or path of the program to check. If the name is specified, its
117 path is deduced from PATH environment variable. If environment variable TOOL
118 (in upper-case characters) is defined, considers its value as path to the tool.
120 MIN_VERSION is a string representing the minimum required version.
123 * locate path to the program.
124 * execute $ TOOL_PATH --version
125 * extract version from standard output.
126 * compare this version to the expected one.
130 Display this message and exit 0
133 if program is not found or its version is prior to expected version,
134 a message is written to error output.
137 returns 0 if program version if greater equals than expected version,
141 $ require_tool.sh emacs 23
142 $ CC=g++ require_tool.sh cc 4.6
143 $ require_tool.sh zsh 4.5
156 if [ $# -gt 2 ] ; then
157 echo "ERROR: expecting 2 parameters. Please see option --help"