• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# SPDX-License-Identifier: FSFAP
2# ===========================================================================
3#    http://www.gnu.org/software/autoconf-archive/ax_compare_version.html
4# ===========================================================================
5#
6# SYNOPSIS
7#
8#   AX_COMPARE_VERSION(VERSION_A, OP, VERSION_B, [ACTION-IF-TRUE], [ACTION-IF-FALSE])
9#
10# DESCRIPTION
11#
12#   This macro compares two version strings. Due to the various number of
13#   minor-version numbers that can exist, and the fact that string
14#   comparisons are not compatible with numeric comparisons, this is not
15#   necessarily trivial to do in a autoconf script. This macro makes doing
16#   these comparisons easy.
17#
18#   The six basic comparisons are available, as well as checking equality
19#   limited to a certain number of minor-version levels.
20#
21#   The operator OP determines what type of comparison to do, and can be one
22#   of:
23#
24#    eq  - equal (test A == B)
25#    ne  - not equal (test A != B)
26#    le  - less than or equal (test A <= B)
27#    ge  - greater than or equal (test A >= B)
28#    lt  - less than (test A < B)
29#    gt  - greater than (test A > B)
30#
31#   Additionally, the eq and ne operator can have a number after it to limit
32#   the test to that number of minor versions.
33#
34#    eq0 - equal up to the length of the shorter version
35#    ne0 - not equal up to the length of the shorter version
36#    eqN - equal up to N sub-version levels
37#    neN - not equal up to N sub-version levels
38#
39#   When the condition is true, shell commands ACTION-IF-TRUE are run,
40#   otherwise shell commands ACTION-IF-FALSE are run. The environment
41#   variable 'ax_compare_version' is always set to either 'true' or 'false'
42#   as well.
43#
44#   Examples:
45#
46#     AX_COMPARE_VERSION([3.15.7],[lt],[3.15.8])
47#     AX_COMPARE_VERSION([3.15],[lt],[3.15.8])
48#
49#   would both be true.
50#
51#     AX_COMPARE_VERSION([3.15.7],[eq],[3.15.8])
52#     AX_COMPARE_VERSION([3.15],[gt],[3.15.8])
53#
54#   would both be false.
55#
56#     AX_COMPARE_VERSION([3.15.7],[eq2],[3.15.8])
57#
58#   would be true because it is only comparing two minor versions.
59#
60#     AX_COMPARE_VERSION([3.15.7],[eq0],[3.15])
61#
62#   would be true because it is only comparing the lesser number of minor
63#   versions of the two values.
64#
65#   Note: The characters that separate the version numbers do not matter. An
66#   empty string is the same as version 0. OP is evaluated by autoconf, not
67#   configure, so must be a string, not a variable.
68#
69#   The author would like to acknowledge Guido Draheim whose advice about
70#   the m4_case and m4_ifvaln functions make this macro only include the
71#   portions necessary to perform the specific comparison specified by the
72#   OP argument in the final configure script.
73#
74# LICENSE
75#
76#   Copyright (c) 2008 Tim Toolan <toolan@ele.uri.edu>
77#
78#   Copying and distribution of this file, with or without modification, are
79#   permitted in any medium without royalty provided the copyright notice
80#   and this notice are preserved. This file is offered as-is, without any
81#   warranty.
82
83#serial 11
84
85dnl #########################################################################
86AC_DEFUN([AX_COMPARE_VERSION], [
87  AC_REQUIRE([AC_PROG_AWK])
88
89  # Used to indicate true or false condition
90  ax_compare_version=false
91
92  # Convert the two version strings to be compared into a format that
93  # allows a simple string comparison.  The end result is that a version
94  # string of the form 1.12.5-r617 will be converted to the form
95  # 0001001200050617.  In other words, each number is zero padded to four
96  # digits, and non digits are removed.
97  AS_VAR_PUSHDEF([A],[ax_compare_version_A])
98  A=`echo "$1" | sed -e 's/\([[0-9]]*\)/Z\1Z/g' \
99                     -e 's/Z\([[0-9]]\)Z/Z0\1Z/g' \
100                     -e 's/Z\([[0-9]][[0-9]]\)Z/Z0\1Z/g' \
101                     -e 's/Z\([[0-9]][[0-9]][[0-9]]\)Z/Z0\1Z/g' \
102                     -e 's/[[^0-9]]//g'`
103
104  AS_VAR_PUSHDEF([B],[ax_compare_version_B])
105  B=`echo "$3" | sed -e 's/\([[0-9]]*\)/Z\1Z/g' \
106                     -e 's/Z\([[0-9]]\)Z/Z0\1Z/g' \
107                     -e 's/Z\([[0-9]][[0-9]]\)Z/Z0\1Z/g' \
108                     -e 's/Z\([[0-9]][[0-9]][[0-9]]\)Z/Z0\1Z/g' \
109                     -e 's/[[^0-9]]//g'`
110
111  dnl # In the case of le, ge, lt, and gt, the strings are sorted as necessary
112  dnl # then the first line is used to determine if the condition is true.
113  dnl # The sed right after the echo is to remove any indented white space.
114  m4_case(m4_tolower($2),
115  [lt],[
116    ax_compare_version=`echo "x$A
117x$B" | sed 's/^ *//' | sort -r | sed "s/x${A}/false/;s/x${B}/true/;1q"`
118  ],
119  [gt],[
120    ax_compare_version=`echo "x$A
121x$B" | sed 's/^ *//' | sort | sed "s/x${A}/false/;s/x${B}/true/;1q"`
122  ],
123  [le],[
124    ax_compare_version=`echo "x$A
125x$B" | sed 's/^ *//' | sort | sed "s/x${A}/true/;s/x${B}/false/;1q"`
126  ],
127  [ge],[
128    ax_compare_version=`echo "x$A
129x$B" | sed 's/^ *//' | sort -r | sed "s/x${A}/true/;s/x${B}/false/;1q"`
130  ],[
131    dnl Split the operator from the subversion count if present.
132    m4_bmatch(m4_substr($2,2),
133    [0],[
134      # A count of zero means use the length of the shorter version.
135      # Determine the number of characters in A and B.
136      ax_compare_version_len_A=`echo "$A" | $AWK '{print(length)}'`
137      ax_compare_version_len_B=`echo "$B" | $AWK '{print(length)}'`
138
139      # Set A to no more than B's length and B to no more than A's length.
140      A=`echo "$A" | sed "s/\(.\{$ax_compare_version_len_B\}\).*/\1/"`
141      B=`echo "$B" | sed "s/\(.\{$ax_compare_version_len_A\}\).*/\1/"`
142    ],
143    [[0-9]+],[
144      # A count greater than zero means use only that many subversions
145      A=`echo "$A" | sed "s/\(\([[0-9]]\{4\}\)\{m4_substr($2,2)\}\).*/\1/"`
146      B=`echo "$B" | sed "s/\(\([[0-9]]\{4\}\)\{m4_substr($2,2)\}\).*/\1/"`
147    ],
148    [.+],[
149      AC_WARNING(
150        [illegal OP numeric parameter: $2])
151    ],[])
152
153    # Pad zeros at end of numbers to make same length.
154    ax_compare_version_tmp_A="$A`echo $B | sed 's/./0/g'`"
155    B="$B`echo $A | sed 's/./0/g'`"
156    A="$ax_compare_version_tmp_A"
157
158    # Check for equality or inequality as necessary.
159    m4_case(m4_tolower(m4_substr($2,0,2)),
160    [eq],[
161      test "x$A" = "x$B" && ax_compare_version=true
162    ],
163    [ne],[
164      test "x$A" != "x$B" && ax_compare_version=true
165    ],[
166      AC_WARNING([illegal OP parameter: $2])
167    ])
168  ])
169
170  AS_VAR_POPDEF([A])dnl
171  AS_VAR_POPDEF([B])dnl
172
173  dnl # Execute ACTION-IF-TRUE / ACTION-IF-FALSE.
174  if test "$ax_compare_version" = "true" ; then
175    m4_ifvaln([$4],[$4],[:])dnl
176    m4_ifvaln([$5],[else $5])dnl
177  fi
178]) dnl AX_COMPARE_VERSION
179