2
0

Version.pm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # Copyright (C) 2001-2017 Free Software Foundation, Inc.
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 2, or (at your option)
  5. # any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. package Automake::Version;
  13. use 5.006;
  14. use strict;
  15. use Automake::ChannelDefs;
  16. =head1 NAME
  17. Automake::Version - version comparison
  18. =head1 SYNOPSIS
  19. use Automake::Version;
  20. print "Version $version is older than required version $required\n"
  21. if Automake::Version::check ($version, $required);
  22. =head1 DESCRIPTION
  23. This module provides support for comparing versions string
  24. as they are used in Automake.
  25. A version is a string that looks like
  26. C<MAJOR.MINOR[.MICRO][ALPHA][-FORK]> where C<MAJOR>, C<MINOR>, and
  27. C<MICRO> are digits, C<ALPHA> is a character, and C<FORK> any
  28. alphanumeric word.
  29. Usually, C<ALPHA> is used to label alpha releases or intermediate
  30. snapshots, C<FORK> is used for git branches or patched releases, and
  31. C<MICRO> is used for bug fixes releases on the C<MAJOR.MINOR> branch.
  32. For the purpose of ordering, C<1.4> is the same as C<1.4.0>, but
  33. C<1.4g> is the same as C<1.4.99g>. The C<FORK> identifier is ignored
  34. in the ordering, except when it looks like C<-pMINOR[ALPHA]>: some
  35. versions were labeled like C<1.4-p3a>, this is the same as an alpha
  36. release labeled C<1.4.3a>. Yes, it's horrible, but Automake did not
  37. support two-dot versions in the past.
  38. =head2 FUNCTIONS
  39. =over 4
  40. =item C<split ($version)>
  41. Split the string C<$version> into the corresponding C<(MAJOR, MINOR,
  42. MICRO, ALPHA, FORK)> tuple. For instance C<'1.4g'> would be split
  43. into C<(1, 4, 99, 'g', '')>. Return C<()> on error.
  44. =cut
  45. sub split ($)
  46. {
  47. my ($ver) = @_;
  48. # Special case for versions like 1.4-p2a.
  49. if ($ver =~ /^(\d+)\.(\d+)(?:-p(\d+)([a-z]+)?)$/)
  50. {
  51. return ($1, $2, $3, $4 || '', '');
  52. }
  53. # Common case.
  54. elsif ($ver =~ /^(\d+)\.(\d+)(?:\.(\d+))?([a-z])?(?:-([A-Za-z0-9]+))?$/)
  55. {
  56. return ($1, $2, $3 || (defined $4 ? 99 : 0), $4 || '', $5 || '');
  57. }
  58. return ();
  59. }
  60. =item C<compare (\@LVERSION, \@RVERSION)>
  61. Compare two version tuples, as returned by C<split>.
  62. Return 1, 0, or -1, if C<LVERSION> is found to be respectively
  63. greater than, equal to, or less than C<RVERSION>.
  64. =cut
  65. sub compare (\@\@)
  66. {
  67. my @l = @{$_[0]};
  68. my @r = @{$_[1]};
  69. for my $i (0, 1, 2)
  70. {
  71. return 1 if ($l[$i] > $r[$i]);
  72. return -1 if ($l[$i] < $r[$i]);
  73. }
  74. for my $i (3, 4)
  75. {
  76. return 1 if ($l[$i] gt $r[$i]);
  77. return -1 if ($l[$i] lt $r[$i]);
  78. }
  79. return 0;
  80. }
  81. =item C<check($VERSION, $REQUIRED)>
  82. Handles the logic of requiring a version number in Automake.
  83. C<$VERSION> should be Automake's version, while C<$REQUIRED>
  84. is the version required by the user input.
  85. Return 0 if the required version is satisfied, 1 otherwise.
  86. =cut
  87. sub check ($$)
  88. {
  89. my ($version, $required) = @_;
  90. my @version = Automake::Version::split ($version);
  91. my @required = Automake::Version::split ($required);
  92. prog_error "version is incorrect: $version"
  93. if $#version == -1;
  94. # This should not happen, because process_option_list and split_version
  95. # use similar regexes.
  96. prog_error "required version is incorrect: $required"
  97. if $#required == -1;
  98. # If we require 3.4n-foo then we require something
  99. # >= 3.4n, with the 'foo' fork identifier.
  100. return 1
  101. if ($required[4] ne '' && $required[4] ne $version[4]);
  102. return 0 > compare (@version, @required);
  103. }
  104. 1;
  105. ### Setup "GNU" style for perl-mode and cperl-mode.
  106. ## Local Variables:
  107. ## perl-indent-level: 2
  108. ## perl-continued-statement-offset: 2
  109. ## perl-continued-brace-offset: 0
  110. ## perl-brace-offset: 0
  111. ## perl-brace-imaginary-offset: 0
  112. ## perl-label-offset: -2
  113. ## cperl-indent-level: 2
  114. ## cperl-brace-offset: 0
  115. ## cperl-continued-brace-offset: 0
  116. ## cperl-label-offset: -2
  117. ## cperl-extra-newline-before-brace: t
  118. ## cperl-merge-trailing-else: nil
  119. ## cperl-continued-statement-offset: 2
  120. ## End: