1package MakeUtil ;
2package main ;
3
4use strict ;
5
6use Config qw(%Config);
7use File::Copy;
8
9my $VERSION = '1.0';
10
11
12BEGIN
13{
14    eval { require File::Spec::Functions ; File::Spec::Functions->import() } ;
15    if ($@)
16    {
17        *catfile = sub { return "$_[0]/$_[1]" }
18    }
19}
20
21require VMS::Filespec if $^O eq 'VMS';
22
23
24unless($ENV{PERL_CORE}) {
25    $ENV{PERL_CORE} = 1 if grep { $_ eq 'PERL_CORE=1' } @ARGV;
26}
27
28$ENV{SKIP_FOR_CORE} = 1 if $ENV{PERL_CORE} || $ENV{MY_PERL_CORE} ;
29
30
31
32sub MY::libscan
33{
34    my $self = shift;
35    my $path = shift;
36
37    return undef
38        if $path =~ /^(?:RCS|CVS|SCCS|\.svn|_darcs)$/ ||
39           $path =~ /(~|\.bak|_bak)$/ ||
40           $path =~ /\..*\.sw(o|p)$/  ||
41           $path =~ /\B\.svn\b/;
42
43    return $path;
44}
45
46sub MY::postamble
47{
48    return ''
49        if $ENV{PERL_CORE} ;
50
51    my @files = getPerlFiles('MANIFEST');
52
53    # Note: Once you remove all the layers of shell/makefile escaping
54    # the regular expression below reads
55    #
56    #    /^\s*local\s*\(\s*\$^W\s*\)/
57    #
58    my $postamble = '
59
60MyTrebleCheck:
61	@echo Checking for $$^W in files: '. "@files" . '
62	perl -ne \'						\
63	    exit 1 if /^\s*local\s*\(\s*\$$\^W\s*\)/; \'		\
64         ' . " @files || " . '				\
65	(echo found unexpected $$^W ; exit 1)
66	@echo All is ok.
67
68';
69
70    return $postamble;
71}
72
73sub getPerlFiles
74{
75    my @manifests = @_ ;
76
77    my @files = ();
78
79    for my $manifest (@manifests)
80    {
81        my $prefix = './';
82
83        $prefix = $1
84            if $manifest =~ m#^(.*/)#;
85
86        open M, "<$manifest"
87            or die "Cannot open '$manifest': $!\n";
88        while (<M>)
89        {
90            chomp ;
91            next if /^\s*#/ || /^\s*$/ ;
92
93            s/^\s+//;
94            s/\s+$//;
95
96            /^(\S+)\s*(.*)$/;
97
98            my ($file, $rest) = ($1, $2);
99
100            if ($file =~ /\.(pm|pl|t)$/ and $file !~ /MakeUtil.pm/)
101            {
102                push @files, "$prefix$file";
103            }
104            elsif ($rest =~ /perl/i)
105            {
106                push @files, "$prefix$file";
107            }
108
109        }
110        close M;
111    }
112
113    return @files;
114}
115
116sub UpDowngrade
117{
118    return if defined $ENV{TipTop};
119
120    my @files = @_ ;
121
122    # our and use bytes/utf8 is stable from 5.6.0 onward
123    # warnings is stable from 5.6.1 onward
124
125    # Note: this code assumes that each statement it modifies is not
126    #       split across multiple lines.
127
128
129    my $warn_sub = '';
130    my $our_sub = '' ;
131
132    my $upgrade ;
133    my $downgrade ;
134    my $do_downgrade ;
135
136    my $caller = (caller(1))[3] || '';
137
138    if ($caller =~ /downgrade/)
139    {
140        $downgrade = 1;
141    }
142    elsif ($caller =~ /upgrade/)
143    {
144        $upgrade = 1;
145    }
146    else
147    {
148        $do_downgrade = 1
149            if $] < 5.006001 ;
150    }
151
152#    else
153#    {
154#        my $opt = shift @ARGV || '' ;
155#        $upgrade = ($opt =~ /^-upgrade/i);
156#        $downgrade = ($opt =~ /^-downgrade/i);
157#        push @ARGV, $opt unless $downgrade || $upgrade;
158#    }
159
160
161    if ($downgrade || $do_downgrade) {
162        # From: use|no warnings "blah"
163        # To:   local ($^W) = 1; # use|no warnings "blah"
164        $warn_sub = sub {
165                            s/^(\s*)(no\s+warnings)/${1}local (\$^W) = 0; #$2/ ;
166                            s/^(\s*)(use\s+warnings)/${1}local (\$^W) = 1; #$2/ ;
167                        };
168    }
169    #elsif ($] >= 5.006001 || $upgrade) {
170    elsif ($upgrade) {
171        # From: local ($^W) = 1; # use|no warnings "blah"
172        # To:   use|no warnings "blah"
173        $warn_sub = sub {
174            s/^(\s*)local\s*\(\$\^W\)\s*=\s*\d+\s*;\s*#\s*((no|use)\s+warnings.*)/$1$2/ ;
175          };
176    }
177
178    if ($downgrade || $do_downgrade) {
179        $our_sub = sub {
180	    if ( /^(\s*)our\s+\(\s*([^)]+\s*)\)/ ) {
181                my $indent = $1;
182                my $vars = join ' ', split /\s*,\s*/, $2;
183                $_ = "${indent}use vars qw($vars);\n";
184            }
185	    elsif ( /^(\s*)((use|no)\s+(bytes|utf8)\s*;.*)$/)
186            {
187                $_ = "$1# $2\n";
188            }
189          };
190    }
191    #elsif ($] >= 5.006000 || $upgrade) {
192    elsif ($upgrade) {
193        $our_sub = sub {
194	    if ( /^(\s*)use\s+vars\s+qw\((.*?)\)/ ) {
195                my $indent = $1;
196                my $vars = join ', ', split ' ', $2;
197                $_ = "${indent}our ($vars);\n";
198            }
199	    elsif ( /^(\s*)#\s*((use|no)\s+(bytes|utf8)\s*;.*)$/)
200            {
201                $_ = "$1$2\n";
202            }
203          };
204    }
205
206    if (! $our_sub && ! $warn_sub) {
207        warn "Up/Downgrade not needed.\n";
208	if ($upgrade || $downgrade)
209          { exit 0 }
210        else
211          { return }
212    }
213
214    foreach (@files) {
215        #if (-l $_ )
216          { doUpDown($our_sub, $warn_sub, $_) }
217          #else
218          #{ doUpDownViaCopy($our_sub, $warn_sub, $_) }
219    }
220
221    warn "Up/Downgrade complete.\n" ;
222    exit 0 if $upgrade || $downgrade;
223
224}
225
226
227sub doUpDown
228{
229    my $our_sub = shift;
230    my $warn_sub = shift;
231
232    return if -d $_[0];
233
234    local ($^I) = ($^O eq 'VMS') ? "_bak" : ".bak";
235    local (@ARGV) = shift;
236
237    while (<>)
238    {
239        print, last if /^__(END|DATA)__/ ;
240
241        &{ $our_sub }() if $our_sub ;
242        &{ $warn_sub }() if $warn_sub ;
243        print ;
244    }
245
246    return if eof ;
247
248    while (<>)
249      { print }
250}
251
252sub doUpDownViaCopy
253{
254    my $our_sub = shift;
255    my $warn_sub = shift;
256    my $file     = shift ;
257
258    use File::Copy ;
259
260    return if -d $file ;
261
262    my $backup = $file . ($^O eq 'VMS') ? "_bak" : ".bak";
263
264    copy($file, $backup)
265        or die "Cannot copy $file to $backup: $!";
266
267    my @keep = ();
268
269    {
270        open F, "<$file"
271            or die "Cannot open $file: $!\n" ;
272        while (<F>)
273        {
274            if (/^__(END|DATA)__/)
275            {
276                push @keep, $_;
277                last ;
278            }
279
280            &{ $our_sub }() if $our_sub ;
281            &{ $warn_sub }() if $warn_sub ;
282            push @keep, $_;
283        }
284
285        if (! eof F)
286        {
287            while (<F>)
288              { push @keep, $_ }
289        }
290        close F;
291    }
292
293    {
294        open F, ">$file"
295            or die "Cannot open $file: $!\n";
296        print F @keep ;
297        close F;
298    }
299}
300
301
302sub FindBrokenDependencies
303{
304    my $version = shift ;
305    my %thisModule = map { $_ => 1} @_;
306
307    my @modules = qw(
308                    IO::Compress::Base
309                    IO::Compress::Base::Common
310                    IO::Uncompress::Base
311
312                    Compress::Raw::Zlib
313                    Compress::Raw::Bzip2
314
315                    IO::Compress::RawDeflate
316                    IO::Uncompress::RawInflate
317                    IO::Compress::Deflate
318                    IO::Uncompress::Inflate
319                    IO::Compress::Gzip
320                    IO::Compress::Gzip::Constants
321                    IO::Uncompress::Gunzip
322                    IO::Compress::Zip
323                    IO::Uncompress::Unzip
324
325                    IO::Compress::Bzip2
326                    IO::Uncompress::Bunzip2
327
328                    IO::Compress::Lzf
329                    IO::Uncompress::UnLzf
330
331                    IO::Compress::Lzop
332                    IO::Uncompress::UnLzop
333
334                    Compress::Zlib
335                    );
336
337    my @broken = ();
338
339    foreach my $module ( grep { ! $thisModule{$_} } @modules)
340    {
341        my $hasVersion = getInstalledVersion($module);
342
343        # No need to upgrade if the module isn't installed at all
344        next
345            if ! defined $hasVersion;
346
347        # If already have C::Z version 1, then an upgrade to any of the
348        # IO::Compress modules will not break it.
349        next
350            if $module eq 'Compress::Zlib' && $hasVersion < 2;
351
352        if ($hasVersion < $version)
353        {
354            push @broken, $module
355        }
356    }
357
358    return @broken;
359}
360
361sub getInstalledVersion
362{
363    my $module = shift;
364    my $version;
365
366    eval " require $module; ";
367
368    if ($@ eq '')
369    {
370        no strict 'refs';
371        $version = ${ $module . "::VERSION" };
372        $version = 0
373    }
374
375    return $version;
376}
377
378package MakeUtil ;
379
3801;
381
382
383