add StartTouch and EndTouch signals to Plugin class; make PluginInsert handle these...
[ardour.git] / libs / taglib / admin / detect-autoconf.pl
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 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.
28 #
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.
35 # 2.053)
36 sub compareVersions
37 {
38     my ($a, $b) = @_;
39
40     # Split the strings up by '.' (version separator) and start comparing digit
41     # length.
42
43     my @aParts = split(/\./, $a);
44     my @bParts = split(/\./, $b);
45
46     # Make the arrays equal in length by adding missing zeroes to the end of the
47     # version.
48     push @aParts, '0' while scalar @aParts < scalar @bParts;
49     push @bParts, '0' while scalar @bParts < scalar @aParts;
50
51     # Now compare each individual portion.
52     for (my $i = 0; $i < scalar @aParts; ++$i)
53     {
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]*$/)
59         {
60             die "Not able to compare $a to $b!\n";
61         }
62
63         my ($aDigits) = ($aParts[$i] =~ /(\d+)/);
64         my ($bDigits) = ($bParts[$i] =~ /(\d+)/);
65
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
70         {
71             my $replacement = ('0' x (-$delta)) . $aDigits;
72             $aParts[$i] =~ s/$aDigits/$replacement/;
73         }
74         elsif ($delta > 0) # a is longer
75         {
76             my $replacement = ('0' x $delta) . $bDigits;
77             $bParts[$i] =~ s/$bDigits/$replacement/;
78         }
79     }
80
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);
85
86     return 1 if ($newA gt $newB);
87     return -1 if ($newA lt $newB);
88     return 0;
89 }
90
91 # Subroutine to determine the highest installed version of the given program,
92 # searching from the given paths.
93 sub findBest
94 {
95     my ($program, @paths) = @_;
96     my $best_version_found = '0'; # Deliberately a string.
97     my %versions;
98     my %minimumVersions = (
99         'autoconf' => '2.5',
100         'automake' => '1.6',
101     );
102     my $sgn; # Used for compareVersions results.
103
104     # Allow user to use environment variable to override search.
105     return $ENV{uc $program} if $ENV{uc $program};
106
107     for $prefix (@paths)
108     {
109         @files = glob "$prefix/$program*";
110         for $file (@files)
111         {
112             # Don't check non-executable scripts.
113             next unless -x $file;
114
115             ($version) = $file =~ /$prefix\/$program-?(.*)$/;
116
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
119             # suffix at all.
120             next if $version =~ /^[^\d]/;
121
122             # Special case some programs to make sure it has a minimum version.
123             if (not $version and exists $minimumVersions{$program})
124             {
125                 my $min_version = $minimumVersions{$program};
126                 my $versionOutput = `$program --version 2>/dev/null | head -n 1`;
127
128                 # If we can't run the script to get the version it likely won't work later.
129                 next unless $versionOutput;
130
131                 # Use number.number for version (we don't need the excess in general).
132                 ($versionOutput) = ($versionOutput =~ /(\d+\.\d+)/);
133
134                 # compareVersions returns -1 if the left argument is less than
135                 # the right argument.  It can also die for invalid input so
136                 # wrap with eval.
137                 eval {
138                     $sgn = compareVersions($versionOutput, $min_version);
139                 };
140
141                 # $@ would be set if an error was encountered.
142                 if ($@ or not $versionOutput or $sgn == -1) {
143                     next;
144                 }
145             }
146
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;
150
151             # Emulate 'which', and abort if we've already seen this version.
152             next if exists $versions{$version};
153
154             # Save filename of program.
155             $versions{$version} = $file;
156
157             # Use string comparison so that e.g. 253a will be > 253 but < 254.
158             # See above about the need for eval.
159             eval {
160                 $sgn = compareVersions($version, $best_version_found);
161             };
162
163             if (not $@ and $sgn == 1)
164             {
165                 $best_version_found = $version;
166             }
167         }
168     }
169
170     return $versions{$best_version_found};
171 }
172
173 # Find an appropriate "which" program for later use by the shell script calling
174 # us.
175 sub findWhich
176 {
177     for $candidate ('type -p', 'which', 'type')
178     {
179         $test = `$candidate sh 2>/dev/null`;
180         chomp $test;
181
182         return $candidate if -x $test;
183     }
184 }
185
186 # Uses which() to find a program unless the user provided its path in the
187 # environment (the upper case program name is searched).
188 sub findProgram
189 {
190     $suffix = ""; # For use if @_ has only one param.
191     my ($program, $suffix) = @_;
192
193     return $ENV{uc $program} if $ENV{uc $program};
194     return which("$program$suffix");
195 }
196
197 # SCRIPT STARTS.
198
199 # Search in path.
200 @paths = split(/:/, $ENV{'PATH'});
201
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;
205
206 $autoconf = findBest('autoconf', @paths);
207 ($autoconf_suffix) = $autoconf =~ /.*autoconf(.*)$/;
208
209 # Find matching autoconf companions.
210 $autoheader = findProgram('autoheader', $autoconf_suffix);
211 $autom4te = findProgram('autom4te', $autoconf_suffix);
212
213 # Get best automake, and look for unsermake to possibly override it.
214 $automake = findBest('automake', @paths);
215 $unsermake = "";
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');
220
221 ($automake_suffix) = $automake =~ /.*automake(.*)$/;
222
223 # Use unsermake if we found it.
224 $automake = "$unsermake -c" if $unsermake;
225
226 # Find matching automake companions.
227 $aclocal = findProgram('aclocal', $automake_suffix);
228
229 $which = findWhich();
230
231 # Make sure we have all of the needed programs.
232 for $i (qw'autoconf autoheader autom4te automake aclocal')
233 {
234     unless(${$i})
235     {
236         print "# Unable to find $i!!\n";
237         exit 1;
238     }
239 }
240
241 # Print results in eval-able form.
242 print <<EOF;
243 AUTOCONF="$autoconf"
244 AUTOHEADER="$autoheader"
245 AUTOM4TE="$autom4te"
246
247 AUTOMAKE="$automake"
248 ACLOCAL="$aclocal"
249
250 WHICH="$which"
251
252 export AUTOCONF AUTOHEADER AUTOM4TE AUTOMAKE ACLOCAL WHICH
253 EOF
254
255 exit 0;
256
257 # vim: set noet ts=8 sw=4: