acinclude.m4 6.4 KB

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