3 # Try to locate best version of auto*
4 # By Michael Pyne <michael.pyne@kdemail.net>
7 # This code is public domain. You may use it however you like (including
10 # Emulate the 'which' program.
14 my @paths = split(/:/, $ENV{'PATH'});
18 return "$path/$prog" if -x
"$path/$prog";
24 # Subroutine to lexicographically compare two version strings, a and b.
25 # If a > b, 1 is returned.
26 # If a == b, 0 is returned.
27 # If a < b, -1 is returned.
29 # If the strings are of uneven number length then the shorter string is
30 # prepended by enough zeroes to make the two string lengths equal in order to
31 # allow an accurate comparison. Note that the zero-padding only occurs in
32 # between version separators (i.e. 1.6 and 1.10, results in 1.06 vs. 1.10).
33 # Parts of the version ending in -foo (or any other text) are not considered
34 # when doing the compare. (i.e. 2.53a vs 2.53 doesn't end up in 2.53a vs.
40 # Split the strings up by '.' (version separator) and start comparing digit
43 my @aParts = split(/\./, $a);
44 my @bParts = split(/\./, $b);
46 # Make the arrays equal in length by adding missing zeroes to the end of the
48 push @aParts, '0' while scalar @aParts < scalar @bParts;
49 push @bParts, '0' while scalar @bParts < scalar @aParts;
51 # Now compare each individual portion.
52 for (my $i = 0; $i < scalar @aParts; ++$i)
54 # Make sure that any portion that has numbers is contiguous. I'm sure
55 # there's a technique for saving stuff like 2.52a2 but I don't feel
56 # like implementing it.
57 if ($aParts[$i] !~ /^[^\d]*\d+[^\d]*$/ or
58 $bParts[$i] !~ /^[^\d]*\d+[^\d]*$/)
60 die "Not able to compare $a to $b!\n";
63 my ($aDigits) = ($aParts[$i] =~ /(\d+)/);
64 my ($bDigits) = ($bParts[$i] =~ /(\d+)/);
66 # Perl is $MODERATELY_INSULTING_TERM, don't remove the parentheses in
67 # the delta calculation below.
68 my $delta = (length $aDigits) - (length $bDigits);
69 if ($delta < 0) # b is longer
71 my $replacement = ('0' x
(-$delta)) . $aDigits;
72 $aParts[$i] =~ s/$aDigits/$replacement/;
74 elsif ($delta > 0) # a is longer
76 my $replacement = ('0' x
$delta) . $bDigits;
77 $bParts[$i] =~ s/$bDigits/$replacement/;
81 # Arrays now have standardized version components, let's re-merge them
82 # to strings to do the compare.
83 my $newA = join('.', @aParts);
84 my $newB = join('.', @bParts);
86 return 1 if ($newA gt $newB);
87 return -1 if ($newA lt $newB);
91 # Subroutine to determine the highest installed version of the given program,
92 # searching from the given paths.
95 my ($program, @paths) = @_;
96 my $best_version_found = '0'; # Deliberately a string.
98 my %minimumVersions = (
102 my $sgn; # Used for compareVersions results.
104 # Allow user to use environment variable to override search.
105 return $ENV{uc $program} if $ENV{uc $program};
109 @files = glob "$prefix/$program*";
112 # Don't check non-executable scripts.
113 next unless -x
$file;
115 ($version) = $file =~ /$prefix\/$program-?
(.*)$/;
117 # Don't check the -wrapper ones (or any other non program one).
118 # The real deal should start with a version number, or have no
120 next if $version =~ /^[^\d]/;
122 # Special case some programs to make sure it has a minimum version.
123 if (not $version and exists $minimumVersions{$program})
125 my $min_version = $minimumVersions{$program};
126 my $versionOutput = `$program --version 2>/dev/null | head -n 1`;
128 # If we can't run the script to get the version it likely won't work later.
129 next unless $versionOutput;
131 # Use number.number for version (we don't need the excess in general).
132 ($versionOutput) = ($versionOutput =~ /(\d+\.\d+)/);
134 # compareVersions returns -1 if the left argument is less than
135 # the right argument. It can also die for invalid input so
138 $sgn = compareVersions
($versionOutput, $min_version);
141 # $@ would be set if an error was encountered.
142 if ($@
or not $versionOutput or $sgn == -1) {
147 # If no version suffix then use it in favor of a versioned autotool
148 # since the ever-popular WANT_AUTOFOO should then work (in theory).
149 return $file unless $version;
151 # Emulate 'which', and abort if we've already seen this version.
152 next if exists $versions{$version};
154 # Save filename of program.
155 $versions{$version} = $file;
157 # Use string comparison so that e.g. 253a will be > 253 but < 254.
158 # See above about the need for eval.
160 $sgn = compareVersions
($version, $best_version_found);
163 if (not $@
and $sgn == 1)
165 $best_version_found = $version;
170 return $versions{$best_version_found};
173 # Find an appropriate "which" program for later use by the shell script calling
177 for $candidate ('type -p', 'which', 'type')
179 $test = `$candidate sh 2>/dev/null`;
182 return $candidate if -x
$test;
186 # Uses which() to find a program unless the user provided its path in the
187 # environment (the upper case program name is searched).
190 $suffix = ""; # For use if @_ has only one param.
191 my ($program, $suffix) = @_;
193 return $ENV{uc $program} if $ENV{uc $program};
194 return which
("$program$suffix");
200 @paths = split(/:/, $ENV{'PATH'});
202 # Make sure at least /usr/bin and /usr/local/bin are in this search.
203 unshift @paths, '/usr/local/bin' unless grep $_ eq '/usr/local/bin', @paths;
204 unshift @paths, '/usr/bin' unless grep $_ eq '/usr/bin', @paths;
206 $autoconf = findBest
('autoconf', @paths);
207 ($autoconf_suffix) = $autoconf =~ /.*autoconf(.*)$/;
209 # Find matching autoconf companions.
210 $autoheader = findProgram
('autoheader', $autoconf_suffix);
211 $autom4te = findProgram
('autom4te', $autoconf_suffix);
213 # Get best automake, and look for unsermake to possibly override it.
214 $automake = findBest
('automake', @paths);
216 # backward compatible: if $UNSERMAKE points to a path, use it
217 $unsermake = findProgram
('unsermake') if (defined($ENV{'UNSERMAKE'}) and $ENV{'UNSERMAKE'} =~ /\//);
218 # new compatible: if it says 'yes', use the one from path
219 $unsermake = which
('unsermake') if ($ENV{'UNSERMAKE'} ne 'no');
221 ($automake_suffix) = $automake =~ /.*automake(.*)$/;
223 # Use unsermake if we found it.
224 $automake = "$unsermake -c" if $unsermake;
226 # Find matching automake companions.
227 $aclocal = findProgram
('aclocal', $automake_suffix);
229 $which = findWhich
();
231 # Make sure we have all of the needed programs.
232 for $i (qw
'autoconf autoheader autom4te automake aclocal')
236 print "# Unable to find $i!!\n";
241 # Print results in eval-able form.
244 AUTOHEADER="$autoheader"
252 export AUTOCONF AUTOHEADER AUTOM4TE AUTOMAKE ACLOCAL WHICH
257 # vim: set noet ts=8 sw=4: