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