Subversion Repositories public

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
43 root 1
#!/usr/bin/env perl
2
 
3
# Try to locate best version of auto*
4
# By Michael Pyne <michael.pyne@kdemail.net>
5
#
6
# Copyright (c) 2005.
7
# This code is public domain.  You may use it however you like (including
8
# relicensing).
9
 
10
# Emulate the 'which' program.
11
sub which
12
{
13
    my $prog = shift;
14
    my @paths = split(/:/, $ENV{'PATH'});
15
 
16
    for $path (@paths)
17
    {
18
	return "$path/$prog" if -x "$path/$prog";
19
    }
20
 
21
    return "";
22
}
23
 
24
# Subroutine to determine the highest installed version of the given program,
25
# searching from the given paths.
26
sub findBest
27
{
28
    my ($program, @paths) = @_;
29
    my $best_version_found = '0'; # Deliberately a string.
30
    my %versions;
31
    my %minimumVersions = (
32
 	'autoconf' => '2.5',
33
	'automake' => '1.6',
34
    );
35
 
36
    # Allow user to use environment variable to override search.
37
    return $ENV{uc $program} if $ENV{uc $program};
38
 
39
    for $prefix (@paths)
40
    {
41
	@files = glob "$prefix/$program*";
42
	for $file (@files)
43
	{
44
	    # Don't check non-executable scripts.
45
	    next unless -x $file;
46
 
47
	    ($version) = $file =~ /$prefix\/$program-?(.*)$/;
48
	    $version =~ s/-|\.//g;
49
 
50
	    # Special case some programs to make sure it has a minimum version.
51
	    if (not $version and exists $minimumVersions{$program})
52
	    {
53
		my $min_version = $minimumVersions{$program};
54
		my $versionOutput = `$program --version 2>/dev/null | head -n 1`;
55
 
56
		# If we can't run the script to get the version it likely won't work later.
57
		next unless $versionOutput; 
58
 
59
		# Use number.number for version (we don't need the excess in general).
60
		($versionOutput) = ($versionOutput =~ /(\d\.\d)/);
61
 
62
		# Use lt to do lexicographical comparison of strings (which should be
63
		# equivalent and doesn't involve issues with floating point conversions).
64
		if (not $versionOutput or $versionOutput lt $min_version)
65
		{
66
		    next;
67
		}
68
	    }
69
 
70
	    # If no version suffix then use it in favor of a versioned autotool
71
	    # since the ever-popular WANT_AUTOFOO should then work (in theory).
72
	    return $file unless $version;
73
 
74
	    # Emulate 'which', and abort if we've already seen this version.
75
	    next if exists $versions{$version};
76
 
77
	    # Save filename of program.
78
	    $versions{$version} = $file;
79
 
80
	    # Use string comparison so that e.g. 253a will be > 253 but < 254.
81
	    if ($version gt $best_version_found)
82
	    {
83
		$best_version_found = $version;
84
	    }
85
	}
86
    }
87
 
88
    return $versions{$best_version_found};
89
}
90
 
91
# Find an appropriate "which" program for later use by the shell script calling
92
# us.
93
sub findWhich
94
{
95
    for $candidate ('type -p', 'which', 'type')
96
    {
97
	$test = `$candidate sh 2>/dev/null`;
98
	chomp $test;
99
 
100
	return $candidate if -x $test;
101
    }
102
}
103
 
104
# Uses which() to find a program unless the user provided its path in the
105
# environment (the upper case program name is searched).
106
sub findProgram
107
{
108
    $suffix = ""; # For use if @_ has only one param.
109
    my ($program, $suffix) = @_;
110
 
111
    return $ENV{uc $program} if $ENV{uc $program};
112
    return which("$program$suffix");
113
}
114
 
115
# SCRIPT STARTS.
116
 
117
# Search in path.
118
@paths = split(/:/, $ENV{'PATH'});
119
 
120
# Make sure at least /usr/bin and /usr/local/bin are in this search.
121
unshift @paths, '/usr/local/bin' unless grep $_ eq '/usr/local/bin', @paths;
122
unshift @paths, '/usr/bin' unless grep $_ eq '/usr/bin', @paths;
123
 
124
$autoconf = findBest('autoconf', @paths);
125
($autoconf_suffix) = $autoconf =~ /.*autoconf(.*)$/;
126
 
127
# Find matching autoconf companions.
128
$autoheader = findProgram('autoheader', $autoconf_suffix);
129
$autom4te = findProgram('autom4te', $autoconf_suffix);
130
 
131
# Get best automake, and look for unsermake to possibly override it.
132
$automake = findBest('automake', @paths);
133
$unsermake = "";
134
# backward compatible: if $UNSERMAKE points to a path, use it
135
$unsermake = findProgram('unsermake') if (defined($ENV{'UNSERMAKE'}) and $ENV{'UNSERMAKE'} =~ /\//);
136
# new compatible: if it says 'yes', use the one from path
137
$unsermake = which('unsermake') if ($ENV{'UNSERMAKE'} ne 'no');
138
 
139
($automake_suffix) = $automake =~ /.*automake(.*)$/;
140
 
141
# Use unsermake if we found it.
142
$automake = "$unsermake -c" if $unsermake;
143
 
144
# Find matching automake companions.
145
$aclocal = findProgram('aclocal', $automake_suffix);
146
 
147
$which = findWhich();
148
 
149
# Make sure we have all of the needed programs.
150
for $i (qw'autoconf autoheader autom4te automake aclocal')
151
{
152
    unless(${$i})
153
    {
154
	print "# Unable to find $i!!\n";
155
	exit 1;
156
    }
157
}
158
 
159
# Print results in eval-able form.
160
print <<EOF;
161
AUTOCONF="$autoconf"
162
AUTOHEADER="$autoheader"
163
AUTOM4TE="$autom4te"
164
 
165
AUTOMAKE="$automake"
166
ACLOCAL="$aclocal"
167
 
168
WHICH="$which"
169
 
170
export AUTOCONF AUTOHEADER AUTOM4TE AUTOMAKE ACLOCAL WHICH
171
EOF
172
 
173
exit 0;