1 dnl # Copyright © 2008 Tim Toolan <toolan@ele.uri.edu>
3 dnl # Copying and distribution of this file, with or without modification,
4 dnl # are permitted in any medium without royalty provided the copyright notice
5 dnl # and this notice are preserved.
7 dnl #########################################################################
8 AC_DEFUN([AX_COMPARE_VERSION], [
11 # Used to indicate true or false condition
12 ax_compare_version=false
14 # Convert the two version strings to be compared into a format that
15 # allows a simple string comparison. The end result is that a version
16 # string of the form 1.12.5-r617 will be converted to the form
17 # 0001001200050617. In other words, each number is zero padded to four
18 # digits, and non digits are removed.
19 AS_VAR_PUSHDEF([A],[ax_compare_version_A])
20 A=`echo "$1" | sed -e 's/\([[0-9]]*\)/Z\1Z/g' \
21 -e 's/Z\([[0-9]]\)Z/Z0\1Z/g' \
22 -e 's/Z\([[0-9]][[0-9]]\)Z/Z0\1Z/g' \
23 -e 's/Z\([[0-9]][[0-9]][[0-9]]\)Z/Z0\1Z/g' \
26 AS_VAR_PUSHDEF([B],[ax_compare_version_B])
27 B=`echo "$3" | sed -e 's/\([[0-9]]*\)/Z\1Z/g' \
28 -e 's/Z\([[0-9]]\)Z/Z0\1Z/g' \
29 -e 's/Z\([[0-9]][[0-9]]\)Z/Z0\1Z/g' \
30 -e 's/Z\([[0-9]][[0-9]][[0-9]]\)Z/Z0\1Z/g' \
33 dnl # In the case of le, ge, lt, and gt, the strings are sorted as necessary
34 dnl # then the first line is used to determine if the condition is true.
35 dnl # The sed right after the echo is to remove any indented white space.
36 m4_case(m4_tolower($2),
38 ax_compare_version=`echo "x$A
39 x$B" | sed 's/^ *//' | sort -r | sed "s/x${A}/false/;s/x${B}/true/;1q"`
42 ax_compare_version=`echo "x$A
43 x$B" | sed 's/^ *//' | sort | sed "s/x${A}/false/;s/x${B}/true/;1q"`
46 ax_compare_version=`echo "x$A
47 x$B" | sed 's/^ *//' | sort | sed "s/x${A}/true/;s/x${B}/false/;1q"`
50 ax_compare_version=`echo "x$A
51 x$B" | sed 's/^ *//' | sort -r | sed "s/x${A}/true/;s/x${B}/false/;1q"`
53 dnl Split the operator from the subversion count if present.
54 m4_bmatch(m4_substr($2,2),
56 # A count of zero means use the length of the shorter version.
57 # Determine the number of characters in A and B.
58 ax_compare_version_len_A=`echo "$A" | $AWK '{print(length)}'`
59 ax_compare_version_len_B=`echo "$B" | $AWK '{print(length)}'`
61 # Set A to no more than B's length and B to no more than A's length.
62 A=`echo "$A" | sed "s/\(.\{$ax_compare_version_len_B\}\).*/\1/"`
63 B=`echo "$B" | sed "s/\(.\{$ax_compare_version_len_A\}\).*/\1/"`
66 # A count greater than zero means use only that many subversions
67 A=`echo "$A" | sed "s/\(\([[0-9]]\{4\}\)\{m4_substr($2,2)\}\).*/\1/"`
68 B=`echo "$B" | sed "s/\(\([[0-9]]\{4\}\)\{m4_substr($2,2)\}\).*/\1/"`
72 [illegal OP numeric parameter: $2])
75 # Pad zeros at end of numbers to make same length.
76 ax_compare_version_tmp_A="$A`echo $B | sed 's/./0/g'`"
77 B="$B`echo $A | sed 's/./0/g'`"
78 A="$ax_compare_version_tmp_A"
80 # Check for equality or inequality as necessary.
81 m4_case(m4_tolower(m4_substr($2,0,2)),
83 test "x$A" = "x$B" && ax_compare_version=true
86 test "x$A" != "x$B" && ax_compare_version=true
88 AC_WARNING([illegal OP parameter: $2])
95 dnl # Execute ACTION-IF-TRUE / ACTION-IF-FALSE.
96 if test "$ax_compare_version" = "true" ; then
97 m4_ifvaln([$4],[$4],[:])dnl
98 m4_ifvaln([$5],[else $5])dnl
100 ]) dnl AX_COMPARE_VERSION