| 1 | dnl @synopsis AX_COMPARE_VERSION(VERSION_A, OP, VERSION_B, [ACTION-IF-TRUE], [ACTION-IF-FALSE]) |
|---|
| 2 | dnl |
|---|
| 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. |
|---|
| 9 | dnl |
|---|
| 10 | dnl The six basic comparisons are available, as well as checking |
|---|
| 11 | dnl equality limited to a certain number of minor-version levels. |
|---|
| 12 | dnl |
|---|
| 13 | dnl The operator OP determines what type of comparison to do, and can |
|---|
| 14 | dnl be one of: |
|---|
| 15 | dnl |
|---|
| 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) |
|---|
| 22 | dnl |
|---|
| 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. |
|---|
| 25 | dnl |
|---|
| 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 |
|---|
| 30 | dnl |
|---|
| 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 |
|---|
| 34 | dnl 'false' as well. |
|---|
| 35 | dnl |
|---|
| 36 | dnl Examples: |
|---|
| 37 | dnl |
|---|
| 38 | dnl AX_COMPARE_VERSION([3.15.7],[lt],[3.15.8]) |
|---|
| 39 | dnl AX_COMPARE_VERSION([3.15],[lt],[3.15.8]) |
|---|
| 40 | dnl |
|---|
| 41 | dnl would both be true. |
|---|
| 42 | dnl |
|---|
| 43 | dnl AX_COMPARE_VERSION([3.15.7],[eq],[3.15.8]) |
|---|
| 44 | dnl AX_COMPARE_VERSION([3.15],[gt],[3.15.8]) |
|---|
| 45 | dnl |
|---|
| 46 | dnl would both be false. |
|---|
| 47 | dnl |
|---|
| 48 | dnl AX_COMPARE_VERSION([3.15.7],[eq2],[3.15.8]) |
|---|
| 49 | dnl |
|---|
| 50 | dnl would be true because it is only comparing two minor versions. |
|---|
| 51 | dnl |
|---|
| 52 | dnl AX_COMPARE_VERSION([3.15.7],[eq0],[3.15]) |
|---|
| 53 | dnl |
|---|
| 54 | dnl would be true because it is only comparing the lesser number of |
|---|
| 55 | dnl minor versions of the two values. |
|---|
| 56 | dnl |
|---|
| 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. |
|---|
| 60 | dnl |
|---|
| 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. |
|---|
| 65 | dnl |
|---|
| 66 | dnl @category Misc |
|---|
| 67 | dnl @author Tim Toolan <toolan@ele.uri.edu> |
|---|
| 68 | dnl @version 2004-03-01 |
|---|
| 69 | dnl @license GPLWithACException |
|---|
| 70 | |
|---|
| 71 | dnl ######################################################################### |
|---|
| 72 | AC_DEFUN([AX_COMPARE_VERSION], [ |
|---|
| 73 | # Used to indicate true or false condition |
|---|
| 74 | ax_compare_version=false |
|---|
| 75 | |
|---|
| 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' \ |
|---|
| 86 | -e 's/[[^0-9]]//g'` |
|---|
| 87 | |
|---|
| 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' \ |
|---|
| 93 | -e 's/[[^0-9]]//g'` |
|---|
| 94 | |
|---|
| 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), |
|---|
| 99 | [lt],[ |
|---|
| 100 | ax_compare_version=`echo "x$A |
|---|
| 101 | x$B" | sed 's/^ *//' | sort -r | sed "s/x${A}/false/;s/x${B}/true/;1q"` |
|---|
| 102 | ], |
|---|
| 103 | [gt],[ |
|---|
| 104 | ax_compare_version=`echo "x$A |
|---|
| 105 | x$B" | sed 's/^ *//' | sort | sed "s/x${A}/false/;s/x${B}/true/;1q"` |
|---|
| 106 | ], |
|---|
| 107 | [le],[ |
|---|
| 108 | ax_compare_version=`echo "x$A |
|---|
| 109 | x$B" | sed 's/^ *//' | sort | sed "s/x${A}/true/;s/x${B}/false/;1q"` |
|---|
| 110 | ], |
|---|
| 111 | [ge],[ |
|---|
| 112 | ax_compare_version=`echo "x$A |
|---|
| 113 | x$B" | sed 's/^ *//' | sort -r | sed "s/x${A}/true/;s/x${B}/false/;1q"` |
|---|
| 114 | ],[ |
|---|
| 115 | dnl Split the operator from the subversion count if present. |
|---|
| 116 | m4_bmatch(m4_substr($2,2), |
|---|
| 117 | [0],[ |
|---|
| 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)}'` |
|---|
| 122 | |
|---|
| 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/"` |
|---|
| 126 | ], |
|---|
| 127 | [[0-9]+],[ |
|---|
| 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/"` |
|---|
| 131 | ], |
|---|
| 132 | [.+],[ |
|---|
| 133 | AC_WARNING( |
|---|
| 134 | [illegal OP numeric parameter: $2]) |
|---|
| 135 | ],[]) |
|---|
| 136 | |
|---|
| 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" |
|---|
| 141 | |
|---|
| 142 | # Check for equality or inequality as necessary. |
|---|
| 143 | m4_case(m4_tolower(m4_substr($2,0,2)), |
|---|
| 144 | [eq],[ |
|---|
| 145 | test "x$A" = "x$B" && ax_compare_version=true |
|---|
| 146 | ], |
|---|
| 147 | [ne],[ |
|---|
| 148 | test "x$A" != "x$B" && ax_compare_version=true |
|---|
| 149 | ],[ |
|---|
| 150 | AC_WARNING([illegal OP parameter: $2]) |
|---|
| 151 | ]) |
|---|
| 152 | ]) |
|---|
| 153 | |
|---|
| 154 | AS_VAR_POPDEF([A])dnl |
|---|
| 155 | AS_VAR_POPDEF([B])dnl |
|---|
| 156 | |
|---|
| 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 |
|---|
| 161 | fi |
|---|
| 162 | ]) dnl AX_COMPARE_VERSION |
|---|