Imported Upstream version 2.6.0
[debian/amanda] / config / macro-archive / ax_compare_version.m4
1 ##### http://autoconf-archive.cryp.to/ax_compare_version.html
2 #
3 # SYNOPSIS
4 #
5 #   AX_COMPARE_VERSION(VERSION_A, OP, VERSION_B, [ACTION-IF-TRUE], [ACTION-IF-FALSE])
6 #
7 # DESCRIPTION
8 #
9 #   This macro compares two version strings. It is used heavily in the
10 #   macro _AX_PATH_BDB for library checking. Due to the various number
11 #   of minor-version numbers that can exist, and the fact that string
12 #   comparisons are not compatible with numeric comparisons, this is
13 #   not necessarily trivial to do in a autoconf script. This macro
14 #   makes doing these comparisons easy.
15 #
16 #   The six basic comparisons are available, as well as checking
17 #   equality limited to a certain number of minor-version levels.
18 #
19 #   The operator OP determines what type of comparison to do, and can
20 #   be one of:
21 #
22 #    eq  - equal (test A == B)
23 #    ne  - not equal (test A != B)
24 #    le  - less than or equal (test A <= B)
25 #    ge  - greater than or equal (test A >= B)
26 #    lt  - less than (test A < B)
27 #    gt  - greater than (test A > B)
28 #
29 #   Additionally, the eq and ne operator can have a number after it to
30 #   limit the test to that number of minor versions.
31 #
32 #    eq0 - equal up to the length of the shorter version
33 #    ne0 - not equal up to the length of the shorter version
34 #    eqN - equal up to N sub-version levels
35 #    neN - not equal up to N sub-version levels
36 #
37 #   When the condition is true, shell commands ACTION-IF-TRUE are run,
38 #   otherwise shell commands ACTION-IF-FALSE are run. The environment
39 #   variable 'ax_compare_version' is always set to either 'true' or
40 #   'false' as well.
41 #
42 #   Examples:
43 #
44 #     AX_COMPARE_VERSION([3.15.7],[lt],[3.15.8])
45 #     AX_COMPARE_VERSION([3.15],[lt],[3.15.8])
46 #
47 #   would both be true.
48 #
49 #     AX_COMPARE_VERSION([3.15.7],[eq],[3.15.8])
50 #     AX_COMPARE_VERSION([3.15],[gt],[3.15.8])
51 #
52 #   would both be false.
53 #
54 #     AX_COMPARE_VERSION([3.15.7],[eq2],[3.15.8])
55 #
56 #   would be true because it is only comparing two minor versions.
57 #
58 #     AX_COMPARE_VERSION([3.15.7],[eq0],[3.15])
59 #
60 #   would be true because it is only comparing the lesser number of
61 #   minor versions of the two values.
62 #
63 #   Note: The characters that separate the version numbers do not
64 #   matter. An empty string is the same as version 0. OP is evaluated
65 #   by autoconf, not configure, so must be a string, not a variable.
66 #
67 #   The author would like to acknowledge Guido Draheim whose advice
68 #   about the m4_case and m4_ifvaln functions make this macro only
69 #   include the portions necessary to perform the specific comparison
70 #   specified by the OP argument in the final configure script.
71 #
72 # LAST MODIFICATION
73 #
74 #   2004-03-01
75 #
76 # COPYLEFT
77 #
78 #   Copyright (c) 2004 Tim Toolan <toolan@ele.uri.edu>
79 #
80 #   This program is free software; you can redistribute it and/or
81 #   modify it under the terms of the GNU General Public License as
82 #   published by the Free Software Foundation; either version 2 of the
83 #   License, or (at your option) any later version.
84 #
85 #   This program is distributed in the hope that it will be useful, but
86 #   WITHOUT ANY WARRANTY; without even the implied warranty of
87 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
88 #   General Public License for more details.
89 #
90 #   You should have received a copy of the GNU General Public License
91 #   along with this program; if not, write to the Free Software
92 #   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
93 #   02111-1307, USA.
94 #
95 #   As a special exception, the respective Autoconf Macro's copyright
96 #   owner gives unlimited permission to copy, distribute and modify the
97 #   configure scripts that are the output of Autoconf when processing
98 #   the Macro. You need not follow the terms of the GNU General Public
99 #   License when using or distributing such scripts, even though
100 #   portions of the text of the Macro appear in them. The GNU General
101 #   Public License (GPL) does govern all other use of the material that
102 #   constitutes the Autoconf Macro.
103 #
104 #   This special exception to the GPL applies to versions of the
105 #   Autoconf Macro released by the Autoconf Macro Archive. When you
106 #   make and distribute a modified version of the Autoconf Macro, you
107 #   may extend this special exception to the GPL to apply to your
108 #   modified version as well.
109
110 dnl #########################################################################
111 AC_DEFUN([AX_COMPARE_VERSION], [
112   # Used to indicate true or false condition
113   ax_compare_version=false
114
115   # Convert the two version strings to be compared into a format that
116   # allows a simple string comparison.  The end result is that a version
117   # string of the form 1.12.5-r617 will be converted to the form
118   # 0001001200050617.  In other words, each number is zero padded to four
119   # digits, and non digits are removed.
120   AS_VAR_PUSHDEF([A],[ax_compare_version_A])
121   A=`echo "$1" | sed -e 's/\([[0-9]]*\)/Z\1Z/g' \
122                      -e 's/Z\([[0-9]]\)Z/Z0\1Z/g' \
123                      -e 's/Z\([[0-9]][[0-9]]\)Z/Z0\1Z/g' \
124                      -e 's/Z\([[0-9]][[0-9]][[0-9]]\)Z/Z0\1Z/g' \
125                      -e 's/[[^0-9]]//g'`
126
127   AS_VAR_PUSHDEF([B],[ax_compare_version_B])
128   B=`echo "$3" | sed -e 's/\([[0-9]]*\)/Z\1Z/g' \
129                      -e 's/Z\([[0-9]]\)Z/Z0\1Z/g' \
130                      -e 's/Z\([[0-9]][[0-9]]\)Z/Z0\1Z/g' \
131                      -e 's/Z\([[0-9]][[0-9]][[0-9]]\)Z/Z0\1Z/g' \
132                      -e 's/[[^0-9]]//g'`
133
134   dnl # In the case of le, ge, lt, and gt, the strings are sorted as necessary
135   dnl # then the first line is used to determine if the condition is true.
136   dnl # The sed right after the echo is to remove any indented white space.
137   m4_case(m4_tolower($2),
138   [lt],[
139     ax_compare_version=`echo "x$A
140 x$B" | sed 's/^ *//' | sort -r | sed "s/x${A}/false/;s/x${B}/true/;1q"`
141   ],
142   [gt],[
143     ax_compare_version=`echo "x$A
144 x$B" | sed 's/^ *//' | sort | sed "s/x${A}/false/;s/x${B}/true/;1q"`
145   ],
146   [le],[
147     ax_compare_version=`echo "x$A
148 x$B" | sed 's/^ *//' | sort | sed "s/x${A}/true/;s/x${B}/false/;1q"`
149   ],
150   [ge],[
151     ax_compare_version=`echo "x$A
152 x$B" | sed 's/^ *//' | sort -r | sed "s/x${A}/true/;s/x${B}/false/;1q"`
153   ],[
154     dnl Split the operator from the subversion count if present.
155     m4_bmatch(m4_substr($2,2),
156     [0],[
157       # A count of zero means use the length of the shorter version.
158       # Determine the number of characters in A and B.
159       ax_compare_version_len_A=`echo "$A" | awk '{print(length)}'`
160       ax_compare_version_len_B=`echo "$B" | awk '{print(length)}'`
161
162       # Set A to no more than B's length and B to no more than A's length.
163       A=`echo "$A" | sed "s/\(.\{$ax_compare_version_len_B\}\).*/\1/"`
164       B=`echo "$B" | sed "s/\(.\{$ax_compare_version_len_A\}\).*/\1/"`
165     ],
166     [[0-9]+],[
167       # A count greater than zero means use only that many subversions
168       A=`echo "$A" | sed "s/\(\([[0-9]]\{4\}\)\{m4_substr($2,2)\}\).*/\1/"`
169       B=`echo "$B" | sed "s/\(\([[0-9]]\{4\}\)\{m4_substr($2,2)\}\).*/\1/"`
170     ],
171     [.+],[
172       AC_WARNING(
173         [illegal OP numeric parameter: $2])
174     ],[])
175
176     # Pad zeros at end of numbers to make same length.
177     ax_compare_version_tmp_A="$A`echo $B | sed 's/./0/g'`"
178     B="$B`echo $A | sed 's/./0/g'`"
179     A="$ax_compare_version_tmp_A"
180
181     # Check for equality or inequality as necessary.
182     m4_case(m4_tolower(m4_substr($2,0,2)),
183     [eq],[
184       test "x$A" = "x$B" && ax_compare_version=true
185     ],
186     [ne],[
187       test "x$A" != "x$B" && ax_compare_version=true
188     ],[
189       AC_WARNING([illegal OP parameter: $2])
190     ])
191   ])
192
193   AS_VAR_POPDEF([A])dnl
194   AS_VAR_POPDEF([B])dnl
195
196   dnl # Execute ACTION-IF-TRUE / ACTION-IF-FALSE.
197   if test "$ax_compare_version" = "true" ; then
198     m4_ifvaln([$4],[$4],[:])dnl
199     m4_ifvaln([$5],[else $5])dnl
200   fi
201 ]) dnl AX_COMPARE_VERSION