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