summaryrefslogtreecommitdiffstats
path: root/detect-autoconf.pl
blob: 5ce1a0861bba75363cc160f29cf953a5fed9870c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env perl

# Try to locate best version of auto*
# By Michael Pyne <michael.pyne@kdemail.net>
#
# Copyright (c) 2005.
# This code is public domain.  You may use it however you like (including
# relicensing).

# Emulate the 'which' program.
sub which
{
    my $prog = shift;
    my @paths = split(/:/, $ENV{'PATH'});

    for $path (@paths)
    {
	return "$path/$prog" if -x "$path/$prog";
    }

    return "";
}

# Subroutine to lexicographically compare two version strings, a and b.
# If a > b, 1 is returned.
# If a == b, 0 is returned.
# If a < b, -1 is returned.
#
# If the strings are of uneven number length then the shorter string is
# prepended by enough zeroes to make the two string lengths equal in order to
# allow an accurate comparison.  Note that the zero-padding only occurs in
# between version separators (i.e. 1.6 and 1.10, results in 1.06 vs. 1.10).
# Parts of the version ending in -foo (or any other text) are not considered
# when doing the compare. (i.e. 2.53a vs 2.53 doesn't end up in 2.53a vs.
# 2.053)
sub compareVersions
{
    my ($a, $b) = @_;

    # Split the strings up by '.' (version separator) and start comparing digit
    # length.

    my @aParts = split(/\./, $a);
    my @bParts = split(/\./, $b);

    # Make the arrays equal in length by adding missing zeroes to the end of the
    # version.
    push @aParts, '0' while scalar @aParts < scalar @bParts;
    push @bParts, '0' while scalar @bParts < scalar @aParts;

    # Now compare each individual portion.
    for (my $i = 0; $i < scalar @aParts; ++$i)
    {
	# Make sure that any portion that has numbers is contiguous.  I'm sure
	# there's a technique for saving stuff like 2.52a2 but I don't feel
	# like implementing it.
	if ($aParts[$i] !~ /^[^\d]*\d+[^\d]*$/ or
	    $bParts[$i] !~ /^[^\d]*\d+[^\d]*$/)
	{
	    die "Not able to compare $a to $b!\n";
	}

	my ($aDigits) = ($aParts[$i] =~ /(\d+)/);
	my ($bDigits) = ($bParts[$i] =~ /(\d+)/);

	# Perl is $MODERATELY_INSULTING_TERM, don't remove the parentheses in
	# the delta calculation below.
	my $delta = (length $aDigits) - (length $bDigits);
	if ($delta < 0) # b is longer
	{
	    my $replacement = ('0' x (-$delta)) . $aDigits;
	    $aParts[$i] =~ s/$aDigits/$replacement/;
	}
	elsif ($delta > 0) # a is longer
	{
	    my $replacement = ('0' x $delta) . $bDigits;
	    $bParts[$i] =~ s/$bDigits/$replacement/;
	}
    }

    # Arrays now have standardized version components, let's re-merge them
    # to strings to do the compare.
    my $newA = join('.', @aParts);
    my $newB = join('.', @bParts);

    return 1 if ($newA gt $newB);
    return -1 if ($newA lt $newB);
    return 0;
}

# Subroutine to determine the highest installed version of the given program,
# searching from the given paths.
sub findBest
{
    my ($program, @paths) = @_;
    my $best_version_found = '0'; # Deliberately a string.
    my %versions;
    my %minimumVersions = (
	'autoconf' => '2.5',
	'automake' => '1.6',
    );
    my $sgn; # Used for compareVersions results.

    # Allow user to use environment variable to override search.
    return $ENV{uc $program} if $ENV{uc $program};

    for $prefix (@paths)
    {
	@files = glob "$prefix/$program*";
	for $file (@files)
	{
	    # Don't check non-executable scripts.
	    next unless -x $file;

	    ($version) = $file =~ /$prefix\/$program-?(.*)$/;

	    # Don't check the -wrapper ones (or any other non program one).
	    # The real deal should start with a version number, or have no
	    # suffix at all.
	    next if $version =~ /^[^\d]/;

	    # Special case some programs to make sure it has a minimum version.
	    if (not $version and exists $minimumVersions{$program})
	    {
		my $min_version = $minimumVersions{$program};
		my $versionOutput = `$program --version 2>/dev/null | head -n 1`;

		# If we can't run the script to get the version it likely won't work later.
		next unless $versionOutput;

		# Use number.number for version (we don't need the excess in general).
		($versionOutput) = ($versionOutput =~ /(\d+\.\d+)/);

		# compareVersions returns -1 if the left argument is less than
		# the right argument.  It can also die for invalid input so
		# wrap with eval.
		eval {
		    $sgn = compareVersions($versionOutput, $min_version);
		};

		# $@ would be set if an error was encountered.
		if ($@ or not $versionOutput or $sgn == -1) {
		    next;
		}
	    }

	    # If no version suffix then use it in favor of a versioned autotool
	    # since the ever-popular WANT_AUTOFOO should then work (in theory).
	    return $file unless $version;

	    # Emulate 'which', and abort if we've already seen this version.
	    next if exists $versions{$version};

	    # Save filename of program.
	    $versions{$version} = $file;

	    # Use string comparison so that e.g. 253a will be > 253 but < 254.
	    # See above about the need for eval.
	    eval {
		$sgn = compareVersions($version, $best_version_found);
	    };

	    if (not $@ and $sgn == 1)
	    {
		$best_version_found = $version;
	    }
	}
    }

    return $versions{$best_version_found};
}

# Find an appropriate "which" program for later use by the shell script calling
# us.
sub findWhich
{
    for $candidate ('type -p', 'which', 'type')
    {
	$test = `$candidate sh 2>/dev/null`;
	chomp $test;

	return $candidate if -x $test;
    }
}

# Uses which() to find a program unless the user provided its path in the
# environment (the upper case program name is searched).
sub findProgram
{
    $suffix = ""; # For use if @_ has only one param.
    my ($program, $suffix) = @_;

    return $ENV{uc $program} if $ENV{uc $program};
    return which("$program$suffix");
}

# SCRIPT STARTS.

# Search in path.
@paths = split(/:/, $ENV{'PATH'});

# Make sure at least /usr/bin and /usr/local/bin are in this search.
unshift @paths, '/usr/local/bin' unless grep $_ eq '/usr/local/bin', @paths;
unshift @paths, '/usr/bin' unless grep $_ eq '/usr/bin', @paths;

$autoconf = findBest('autoconf', @paths);
($autoconf_suffix) = $autoconf =~ /.*autoconf(.*)$/;

# Find matching autoconf companions.
$autoheader = findProgram('autoheader', $autoconf_suffix);
$autom4te = findProgram('autom4te', $autoconf_suffix);

# Get best automake, and look for unsermake to possibly override it.
$automake = findBest('automake', @paths);
$unsermake = "";
# backward compatible: if $UNSERMAKE points to a path, use it
$unsermake = findProgram('unsermake') if (defined($ENV{'UNSERMAKE'}) and $ENV{'UNSERMAKE'} =~ /\//);
# new compatible: if it says 'yes', use the one from path
$unsermake = which('unsermake') if ($ENV{'UNSERMAKE'} ne 'no');

($automake_suffix) = $automake =~ /.*automake(.*)$/;

# Find matching automake companions.
$aclocal = findProgram('aclocal', $automake_suffix);

# Use unsermake if we found it.
$automake = "$unsermake -c" if ($unsermake and $aclocal);

$which = findWhich();

# Make sure we have all of the needed programs.
for $i (qw'autoconf autoheader autom4te automake aclocal')
{
    unless(${$i})
    {
	print STDERR "# Unable to find $i!!\n";
    }
}

# Print results in eval-able form.
print <<EOF;
AUTOCONF="$autoconf"
AUTOHEADER="$autoheader"
AUTOM4TE="$autom4te"

AUTOMAKE="$automake"
ACLOCAL="$aclocal"

WHICH="$which"

export AUTOCONF AUTOHEADER AUTOM4TE AUTOMAKE ACLOCAL WHICH
EOF

exit 0;

# vim: set noet ts=8 sw=4: