1 dnl @synopsis AX_COMPARE_VERSION(VERSION_A, OP, VERSION_B, [ACTION-IF-TRUE], [ACTION-IF-FALSE])
3 dnl This macro compares two version strings. It is used heavily in the
4 dnl macro _AX_PATH_BDB for library checking. Due to the various number
5 dnl of minor-version numbers that can exist, and the fact that string
6 dnl comparisons are not compatible with numeric comparisons, this is
7 dnl not necessarily trivial to do in a autoconf script. This macro
8 dnl makes doing these comparisons easy.
10 dnl The six basic comparisons are available, as well as checking
11 dnl equality limited to a certain number of minor-version levels.
13 dnl The operator OP determines what type of comparison to do, and can
16 dnl eq - equal (test A == B)
17 dnl ne - not equal (test A != B)
18 dnl le - less than or equal (test A <= B)
19 dnl ge - greater than or equal (test A >= B)
20 dnl lt - less than (test A < B)
21 dnl gt - greater than (test A > B)
23 dnl Additionally, the eq and ne operator can have a number after it to
24 dnl limit the test to that number of minor versions.
26 dnl eq0 - equal up to the length of the shorter version
27 dnl ne0 - not equal up to the length of the shorter version
28 dnl eqN - equal up to N sub-version levels
29 dnl neN - not equal up to N sub-version levels
31 dnl When the condition is true, shell commands ACTION-IF-TRUE are run,
32 dnl otherwise shell commands ACTION-IF-FALSE are run. The environment
33 dnl variable 'ax_compare_version' is always set to either 'true' or
38 dnl AX_COMPARE_VERSION([3.15.7],[lt],[3.15.8])
39 dnl AX_COMPARE_VERSION([3.15],[lt],[3.15.8])
41 dnl would both be true.
43 dnl AX_COMPARE_VERSION([3.15.7],[eq],[3.15.8])
44 dnl AX_COMPARE_VERSION([3.15],[gt],[3.15.8])
46 dnl would both be false.
48 dnl AX_COMPARE_VERSION([3.15.7],[eq2],[3.15.8])
50 dnl would be true because it is only comparing two minor versions.
52 dnl AX_COMPARE_VERSION([3.15.7],[eq0],[3.15])
54 dnl would be true because it is only comparing the lesser number of
55 dnl minor versions of the two values.
57 dnl Note: The characters that separate the version numbers do not
58 dnl matter. An empty string is the same as version 0. OP is evaluated
59 dnl by autoconf, not configure, so must be a string, not a variable.
61 dnl The author would like to acknowledge Guido Draheim whose advice
62 dnl about the m4_case and m4_ifvaln functions make this macro only
63 dnl include the portions necessary to perform the specific comparison
64 dnl specified by the OP argument in the final configure script.
67 dnl @author Tim Toolan <toolan@ele.uri.edu>
68 dnl @version 2004-03-01
69 dnl @license GPLWithACException
71 dnl #########################################################################
72 AC_DEFUN([AX_COMPARE_VERSION], [
73 # Used to indicate true or false condition
74 ax_compare_version=false
76 # Convert the two version strings to be compared into a format that
77 # allows a simple string comparison. The end result is that a version
78 # string of the form 1.12.5-r617 will be converted to the form
79 # 0001001200050617. In other words, each number is zero padded to four
80 # digits, and non digits are removed.
81 AS_VAR_PUSHDEF([A],[ax_compare_version_A])
82 A=`echo "$1" | sed -e 's/\([[0-9]]*\)/Z\1Z/g' \
83 -e 's/Z\([[0-9]]\)Z/Z0\1Z/g' \
84 -e 's/Z\([[0-9]][[0-9]]\)Z/Z0\1Z/g' \
85 -e 's/Z\([[0-9]][[0-9]][[0-9]]\)Z/Z0\1Z/g' \
88 AS_VAR_PUSHDEF([B],[ax_compare_version_B])
89 B=`echo "$3" | sed -e 's/\([[0-9]]*\)/Z\1Z/g' \
90 -e 's/Z\([[0-9]]\)Z/Z0\1Z/g' \
91 -e 's/Z\([[0-9]][[0-9]]\)Z/Z0\1Z/g' \
92 -e 's/Z\([[0-9]][[0-9]][[0-9]]\)Z/Z0\1Z/g' \
95 dnl # In the case of le, ge, lt, and gt, the strings are sorted as necessary
96 dnl # then the first line is used to determine if the condition is true.
97 dnl # The sed right after the echo is to remove any indented white space.
98 m4_case(m4_tolower($2),
100 ax_compare_version=`echo "x$A
101 x$B" | sed 's/^ *//' | sort -r | sed "s/x${A}/false/;s/x${B}/true/;1q"`
104 ax_compare_version=`echo "x$A
105 x$B" | sed 's/^ *//' | sort | sed "s/x${A}/false/;s/x${B}/true/;1q"`
108 ax_compare_version=`echo "x$A
109 x$B" | sed 's/^ *//' | sort | sed "s/x${A}/true/;s/x${B}/false/;1q"`
112 ax_compare_version=`echo "x$A
113 x$B" | sed 's/^ *//' | sort -r | sed "s/x${A}/true/;s/x${B}/false/;1q"`
115 dnl Split the operator from the subversion count if present.
116 m4_bmatch(m4_substr($2,2),
118 # A count of zero means use the length of the shorter version.
119 # Determine the number of characters in A and B.
120 ax_compare_version_len_A=`echo "$A" | awk '{print(length)}'`
121 ax_compare_version_len_B=`echo "$B" | awk '{print(length)}'`
123 # Set A to no more than B's length and B to no more than A's length.
124 A=`echo "$A" | sed "s/\(.\{$ax_compare_version_len_B\}\).*/\1/"`
125 B=`echo "$B" | sed "s/\(.\{$ax_compare_version_len_A\}\).*/\1/"`
128 # A count greater than zero means use only that many subversions
129 A=`echo "$A" | sed "s/\(\([[0-9]]\{4\}\)\{m4_substr($2,2)\}\).*/\1/"`
130 B=`echo "$B" | sed "s/\(\([[0-9]]\{4\}\)\{m4_substr($2,2)\}\).*/\1/"`
134 [illegal OP numeric parameter: $2])
137 # Pad zeros at end of numbers to make same length.
138 ax_compare_version_tmp_A="$A`echo $B | sed 's/./0/g'`"
139 B="$B`echo $A | sed 's/./0/g'`"
140 A="$ax_compare_version_tmp_A"
142 # Check for equality or inequality as necessary.
143 m4_case(m4_tolower(m4_substr($2,0,2)),
145 test "x$A" = "x$B" && ax_compare_version=true
148 test "x$A" != "x$B" && ax_compare_version=true
150 AC_WARNING([illegal OP parameter: $2])
154 AS_VAR_POPDEF([A])dnl
155 AS_VAR_POPDEF([B])dnl
157 dnl # Execute ACTION-IF-TRUE / ACTION-IF-FALSE.
158 if test "$ax_compare_version" = "true" ; then
159 m4_ifvaln([$4],[$4],[:])dnl
160 m4_ifvaln([$5],[else $5])dnl
162 ]) dnl AX_COMPARE_VERSION