]> git.ipfire.org Git - thirdparty/autoconf.git/commitdiff
* lib/m4sugar/m4sh.m4 (_AS_VERSION_COMPARE_PREPARE): New macro.
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 16 Jun 2005 21:03:47 +0000 (21:03 +0000)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 16 Jun 2005 21:03:47 +0000 (21:03 +0000)
(AS_VERSION_COMPARE): New macro.  The API is taken from CVS,
but the implementation is entirely different and is designed
to be compatible with glibc strverscmp.
* tests/m4sh.at (AS_VERSION_COMPARE): New test.

ChangeLog
lib/m4sugar/m4sh.m4
tests/m4sh.at

index 0b304760d932c92907fdbde9dc3bbc15ce1170fe..2b7f912d3a105d78ca997be1d60dbecc3ece59bc 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2005-06-16  Paul Eggert  <eggert@cs.ucla.edu>
 
+       * lib/m4sugar/m4sh.m4 (_AS_VERSION_COMPARE_PREPARE): New macro.
+       (AS_VERSION_COMPARE): New macro.  The API is taken from CVS,
+       but the implementation is entirely different and is designed
+       to be compatible with glibc strverscmp.
+       * tests/m4sh.at (AS_VERSION_COMPARE): New test.
+
        * doc/autoconf.texi (Limitations of Usual Tools): Mention expr bug
        on Mac OS X 10.4 reported by Peter O'Gorman in:
        http://lists.gnu.org/archive/html/autoconf-patches/2005-06/msg00041.html
index a73636b38171e4000bf91b98e637858eda4b2c1e..234c06a4dbd46c08703f6105f71771a994674b46 100644 (file)
@@ -1022,6 +1022,83 @@ _AS_PATH_WALK([$PATH], [echo "PATH: $as_dir"])
 }])
 
 
+# _AS_VERSION_COMPARE_PREPARE
+# ---------------------------
+# Output variables for comparing version numbers.
+m4_defun([_AS_VERSION_COMPARE_PREPARE],
+[[as_awk_strverscmp='
+  END {
+    while (length(v1) || length(v2)) {
+      # Set d1 to be the next thing to compare from v1, and likewise for d2.
+      # Normally this is a single character, but if v1 and v2 contain digits,
+      # compare them as integers and fractions as strverscmp does.
+      d1 = v1; sub(/[^0-9].*/, "", d1); len1 = length(d1)
+      d2 = v2; sub(/[^0-9].*/, "", d2); len2 = length(d2)
+      if (len1 && len2) {
+       # v1 and v2 both have leading digits.
+       v1 = substr(v1, len1 + 1)
+       v2 = substr(v2, len1 + 1)
+       if (d1 ~ /^0/) {
+         if (d2 ~ /^0/) {
+           # Compare two fractions.
+           do {
+             d1 = substr(d1, 2); len1--
+             d2 = substr(d2, 2); len2--
+           } while (d1 ~ /^0/ && d2 ~ /^0/);
+           if (len1 != len2 && ! (len1 && len2 && substr(d1, 1, 1) == substr(d2, 1, 1))) {
+             # The two components differ in length, and the common prefix
+             # contains only leading zeros.  Consider the longer to be less.
+             d1 = -len1
+             d2 = -len2
+           } else {
+             # Otherwise, compare as strings.
+             d1 = "x" d1
+             d2 = "x" d2
+           }
+         } else {
+           # A fraction is less than an integer.
+           exit 1
+         }
+       } else {
+         if (d2 ~ /^0/) {
+           # An integer is greater than a fraction.
+           exit 2
+         } else {
+           # Compare two integers.
+           d1 += 0
+           d2 += 0
+         }
+       }
+      } else {
+       # The normal case, without worrying about digits.
+       if (v1 == "") d1 = v1; else { d1 = substr(v1, 1, 1); v1 = substr(v1,2) }
+       if (v2 == "") d2 = v2; else { d2 = substr(v2, 1, 1); v2 = substr(v2,2) }
+      }
+      if (d1 < d2) exit 1
+      if (d1 > d2) exit 2
+    }
+  }
+']])
+
+# AS_VERSION_COMPARE(VERSION-1, VERSION-2,
+#                    [ACTION-IF-LESS], [ACTION-IF-EQUAL], [ACTION-IF-GREATER])
+# -----------------------------------------------------------------------------
+# Compare two strings possibly containing shell variables as version strings.
+m4_defun([AS_VERSION_COMPARE],
+[AS_REQUIRE([_$0_PREPARE])dnl
+as_arg_v1=$1
+as_arg_v2=$2
+dnl This usage is portable even to ancient awk,
+dnl so don't worry about finding a "nice" awk version.
+awk "$as_awk_strverscmp" v1="$as_arg_v1" v2="$as_arg_v2" /dev/null
+case $? in
+1) $3;;
+0) $4;;
+2) $5;;
+esac[]dnl
+])
+
+
 # AS_HELP_STRING(LHS, RHS, [COLUMN])
 # ----------------------------------
 #
index 0f9e599d1b0d7f9dd265f4e4a5af42666a946038..3e784723d769aa98ce25b937640d128e93713471 100644 (file)
@@ -244,6 +244,52 @@ AT_CLEANUP
 
 
 
+## -------------------- ##
+## AS_VERSION_COMPARE.  ##
+## -------------------- ##
+
+# Build nested dirs.
+AT_SETUP([AS@&t@_VERSION_COMPARE])
+
+AT_DATA_M4SH([script.as],
+[[AS_INIT
+
+m4_define([VERSION_COMPARE_TEST],
+[AS_VERSION_COMPARE([$1], [$3], [result='<'], [result='='], [result='>'])
+test "X$result" = "X$2" ||
+  AS_ERROR([version $1 $result $3; should be $1 $2 $3])
+m4_if([$1], <,
+[AS_VERSION_COMPARE([$3], [$1], [result='<'], [result='='], [result='>'])
+test "X$result" = "X>" ||
+  AS_ERROR([version $3 $result $1; should be $3 > $1])])])
+
+VERSION_COMPARE_TEST([], =, [])
+VERSION_COMPARE_TEST([1.0], =, [1.0])
+VERSION_COMPARE_TEST([alpha-1.0], =, [alpha-1.0])
+
+# These tests are taken from libc/string/tst-svc.expect.
+tst_svc_expect='
+  000 001 00 00a 01 01a 0 0a 2.8 2.8-0.4 20 21 22 212 CP037 CP345 CP1257
+  foo foo-0.4 foo-0.4a foo-0.4b foo-0.5 foo-0.10.5 foo-3.01 foo-3.0
+  foo-3.0.0 foo-3.0.1 foo-3.2 foo-3.10 foo00 foo0
+'
+test1=''
+for test2 in $tst_svc_expect; do
+  VERSION_COMPARE_TEST([$test1], <, [$test2])
+  test1=$test2
+done
+
+AS_EXIT(0)
+]])
+
+AT_CHECK_M4SH
+AT_CHECK([./script])
+
+AT_CLEANUP
+
+
+
+
 ## ----------------------------- ##
 ## Negated classes in globbing.  ##
 ## ----------------------------- ##