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