Skip to content
Myr

ax_compare_version.m4

This file

Path

m4/ax_compare_version.m4

177 lines · 6.4 KiB

Last changed by

Not known.

History of this file

Take it

1# ===========================================================================2#    http://www.gnu.org/software/autoconf-archive/ax_compare_version.html3# ===========================================================================4#5# SYNOPSIS6#7#   AX_COMPARE_VERSION(VERSION_A, OP, VERSION_B, [ACTION-IF-TRUE], [ACTION-IF-FALSE])8#9# DESCRIPTION10#11#   This macro compares two version strings. Due to the various number of12#   minor-version numbers that can exist, and the fact that string13#   comparisons are not compatible with numeric comparisons, this is not14#   necessarily trivial to do in a autoconf script. This macro makes doing15#   these comparisons easy.16#17#   The six basic comparisons are available, as well as checking equality18#   limited to a certain number of minor-version levels.19#20#   The operator OP determines what type of comparison to do, and can be one21#   of:22#23#    eq  - equal (test A == B)24#    ne  - not equal (test A != B)25#    le  - less than or equal (test A <= B)26#    ge  - greater than or equal (test A >= B)27#    lt  - less than (test A < B)28#    gt  - greater than (test A > B)29#30#   Additionally, the eq and ne operator can have a number after it to limit31#   the test to that number of minor versions.32#33#    eq0 - equal up to the length of the shorter version34#    ne0 - not equal up to the length of the shorter version35#    eqN - equal up to N sub-version levels36#    neN - not equal up to N sub-version levels37#38#   When the condition is true, shell commands ACTION-IF-TRUE are run,39#   otherwise shell commands ACTION-IF-FALSE are run. The environment40#   variable 'ax_compare_version' is always set to either 'true' or 'false'41#   as well.42#43#   Examples:44#45#     AX_COMPARE_VERSION([3.15.7],[lt],[3.15.8])46#     AX_COMPARE_VERSION([3.15],[lt],[3.15.8])47#48#   would both be true.49#50#     AX_COMPARE_VERSION([3.15.7],[eq],[3.15.8])51#     AX_COMPARE_VERSION([3.15],[gt],[3.15.8])52#53#   would both be false.54#55#     AX_COMPARE_VERSION([3.15.7],[eq2],[3.15.8])56#57#   would be true because it is only comparing two minor versions.58#59#     AX_COMPARE_VERSION([3.15.7],[eq0],[3.15])60#61#   would be true because it is only comparing the lesser number of minor62#   versions of the two values.63#64#   Note: The characters that separate the version numbers do not matter. An65#   empty string is the same as version 0. OP is evaluated by autoconf, not66#   configure, so must be a string, not a variable.67#68#   The author would like to acknowledge Guido Draheim whose advice about69#   the m4_case and m4_ifvaln functions make this macro only include the70#   portions necessary to perform the specific comparison specified by the71#   OP argument in the final configure script.72#73# LICENSE74#75#   Copyright (c) 2008 Tim Toolan <toolan@ele.uri.edu>76#77#   Copying and distribution of this file, with or without modification, are78#   permitted in any medium without royalty provided the copyright notice79#   and this notice are preserved. This file is offered as-is, without any80#   warranty.8182#serial 118384dnl #########################################################################85AC_DEFUN([AX_COMPARE_VERSION], [86  AC_REQUIRE([AC_PROG_AWK])8788  # Used to indicate true or false condition89  ax_compare_version=false9091  # Convert the two version strings to be compared into a format that92  # allows a simple string comparison.  The end result is that a version93  # string of the form 1.12.5-r617 will be converted to the form94  # 0001001200050617.  In other words, each number is zero padded to four95  # digits, and non digits are removed.96  AS_VAR_PUSHDEF([A],[ax_compare_version_A])97  A=`echo "$1" | sed -e 's/\([[0-9]]*\)/Z\1Z/g' \98                     -e 's/Z\([[0-9]]\)Z/Z0\1Z/g' \99                     -e 's/Z\([[0-9]][[0-9]]\)Z/Z0\1Z/g' \100                     -e 's/Z\([[0-9]][[0-9]][[0-9]]\)Z/Z0\1Z/g' \101                     -e 's/[[^0-9]]//g'`102103  AS_VAR_PUSHDEF([B],[ax_compare_version_B])104  B=`echo "$3" | sed -e 's/\([[0-9]]*\)/Z\1Z/g' \105                     -e 's/Z\([[0-9]]\)Z/Z0\1Z/g' \106                     -e 's/Z\([[0-9]][[0-9]]\)Z/Z0\1Z/g' \107                     -e 's/Z\([[0-9]][[0-9]][[0-9]]\)Z/Z0\1Z/g' \108                     -e 's/[[^0-9]]//g'`109110  dnl # In the case of le, ge, lt, and gt, the strings are sorted as necessary111  dnl # then the first line is used to determine if the condition is true.112  dnl # The sed right after the echo is to remove any indented white space.113  m4_case(m4_tolower($2),114  [lt],[115    ax_compare_version=`echo "x$A116x$B" | sed 's/^ *//' | sort -r | sed "s/x${A}/false/;s/x${B}/true/;1q"`117  ],118  [gt],[119    ax_compare_version=`echo "x$A120x$B" | sed 's/^ *//' | sort | sed "s/x${A}/false/;s/x${B}/true/;1q"`121  ],122  [le],[123    ax_compare_version=`echo "x$A124x$B" | sed 's/^ *//' | sort | sed "s/x${A}/true/;s/x${B}/false/;1q"`125  ],126  [ge],[127    ax_compare_version=`echo "x$A128x$B" | sed 's/^ *//' | sort -r | sed "s/x${A}/true/;s/x${B}/false/;1q"`129  ],[130    dnl Split the operator from the subversion count if present.131    m4_bmatch(m4_substr($2,2),132    [0],[133      # A count of zero means use the length of the shorter version.134      # Determine the number of characters in A and B.135      ax_compare_version_len_A=`echo "$A" | $AWK '{print(length)}'`136      ax_compare_version_len_B=`echo "$B" | $AWK '{print(length)}'`137138      # Set A to no more than B's length and B to no more than A's length.139      A=`echo "$A" | sed "s/\(.\{$ax_compare_version_len_B\}\).*/\1/"`140      B=`echo "$B" | sed "s/\(.\{$ax_compare_version_len_A\}\).*/\1/"`141    ],142    [[0-9]+],[143      # A count greater than zero means use only that many subversions144      A=`echo "$A" | sed "s/\(\([[0-9]]\{4\}\)\{m4_substr($2,2)\}\).*/\1/"`145      B=`echo "$B" | sed "s/\(\([[0-9]]\{4\}\)\{m4_substr($2,2)\}\).*/\1/"`146    ],147    [.+],[148      AC_WARNING(149        [illegal OP numeric parameter: $2])150    ],[])151152    # Pad zeros at end of numbers to make same length.153    ax_compare_version_tmp_A="$A`echo $B | sed 's/./0/g'`"154    B="$B`echo $A | sed 's/./0/g'`"155    A="$ax_compare_version_tmp_A"156157    # Check for equality or inequality as necessary.158    m4_case(m4_tolower(m4_substr($2,0,2)),159    [eq],[160      test "x$A" = "x$B" && ax_compare_version=true161    ],162    [ne],[163      test "x$A" != "x$B" && ax_compare_version=true164    ],[165      AC_WARNING([illegal OP parameter: $2])166    ])167  ])168169  AS_VAR_POPDEF([A])dnl170  AS_VAR_POPDEF([B])dnl171172  dnl # Execute ACTION-IF-TRUE / ACTION-IF-FALSE.173  if test "$ax_compare_version" = "true" ; then174    m4_ifvaln([$4],[$4],[:])dnl175    m4_ifvaln([$5],[else $5])dnl176  fi177]) dnl AX_COMPARE_VERSION