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