source: box/trunk/infrastructure/m4/ax_compare_version.m4 @ 288

Revision 288, 6.0 KB checked in by martin, 6 years ago (diff)

Now supports Berkeley DB versions >= 4.1 in addition to 1.x. The versions inbetween are not supported because they require code changes and I don't have them available to test against.

Line 
1dnl @synopsis AX_COMPARE_VERSION(VERSION_A, OP, VERSION_B, [ACTION-IF-TRUE], [ACTION-IF-FALSE])
2dnl
3dnl This macro compares two version strings. It is used heavily in the
4dnl macro _AX_PATH_BDB for library checking. Due to the various number
5dnl of minor-version numbers that can exist, and the fact that string
6dnl comparisons are not compatible with numeric comparisons, this is
7dnl not necessarily trivial to do in a autoconf script. This macro
8dnl makes doing these comparisons easy.
9dnl
10dnl The six basic comparisons are available, as well as checking
11dnl equality limited to a certain number of minor-version levels.
12dnl
13dnl The operator OP determines what type of comparison to do, and can
14dnl be one of:
15dnl
16dnl  eq  - equal (test A == B)
17dnl  ne  - not equal (test A != B)
18dnl  le  - less than or equal (test A <= B)
19dnl  ge  - greater than or equal (test A >= B)
20dnl  lt  - less than (test A < B)
21dnl  gt  - greater than (test A > B)
22dnl
23dnl Additionally, the eq and ne operator can have a number after it to
24dnl limit the test to that number of minor versions.
25dnl
26dnl  eq0 - equal up to the length of the shorter version
27dnl  ne0 - not equal up to the length of the shorter version
28dnl  eqN - equal up to N sub-version levels
29dnl  neN - not equal up to N sub-version levels
30dnl
31dnl When the condition is true, shell commands ACTION-IF-TRUE are run,
32dnl otherwise shell commands ACTION-IF-FALSE are run. The environment
33dnl variable 'ax_compare_version' is always set to either 'true' or
34dnl 'false' as well.
35dnl
36dnl Examples:
37dnl
38dnl   AX_COMPARE_VERSION([3.15.7],[lt],[3.15.8])
39dnl   AX_COMPARE_VERSION([3.15],[lt],[3.15.8])
40dnl
41dnl would both be true.
42dnl
43dnl   AX_COMPARE_VERSION([3.15.7],[eq],[3.15.8])
44dnl   AX_COMPARE_VERSION([3.15],[gt],[3.15.8])
45dnl
46dnl would both be false.
47dnl
48dnl   AX_COMPARE_VERSION([3.15.7],[eq2],[3.15.8])
49dnl
50dnl would be true because it is only comparing two minor versions.
51dnl
52dnl   AX_COMPARE_VERSION([3.15.7],[eq0],[3.15])
53dnl
54dnl would be true because it is only comparing the lesser number of
55dnl minor versions of the two values.
56dnl
57dnl Note: The characters that separate the version numbers do not
58dnl matter. An empty string is the same as version 0. OP is evaluated
59dnl by autoconf, not configure, so must be a string, not a variable.
60dnl
61dnl The author would like to acknowledge Guido Draheim whose advice
62dnl about the m4_case and m4_ifvaln functions make this macro only
63dnl include the portions necessary to perform the specific comparison
64dnl specified by the OP argument in the final configure script.
65dnl
66dnl @category Misc
67dnl @author Tim Toolan <toolan@ele.uri.edu>
68dnl @version 2004-03-01
69dnl @license GPLWithACException
70
71dnl #########################################################################
72AC_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
101x$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
105x$B" | sed 's/^ *//' | sort | sed "s/x${A}/false/;s/x${B}/true/;1q"`
106  ],
107  [le],[
108    ax_compare_version=`echo "x$A
109x$B" | sed 's/^ *//' | sort | sed "s/x${A}/true/;s/x${B}/false/;1q"`
110  ],
111  [ge],[
112    ax_compare_version=`echo "x$A
113x$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
Note: See TracBrowser for help on using the repository browser.