"Fossies" - the Fresh Open Source Software Archive

Member "automake-1.16.3/lib/Automake/Version.pm" (19 Nov 2020, 3901 Bytes) of package /linux/misc/automake-1.16.3.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Version.pm" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.16.2_vs_1.16.3.

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