This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Regression test for 34394ecd - SVs that were only on the tmps stack leaked.
[perl5.git] / Porting / cmpVERSION.pl
1 #!/usr/bin/perl -w
2
3 #
4 # cmpVERSION - compare two Perl source trees for modules
5 # that have identical version numbers but different contents.
6 #
7 # with -d option, output the diffs too
8 # with -x option, exclude dual-life modules (after all, there are tools
9 #                 like core-cpan-diff that can already deal with them)
10 #                 With this option, one of the directories must be '.'.
11 #
12 # Original by slaven@rezic.de, modified by jhi.
13 #
14
15 use strict;
16
17 use ExtUtils::MakeMaker;
18 use File::Compare;
19 use File::Find;
20 use File::Spec::Functions qw(rel2abs abs2rel catfile catdir curdir);
21 use Getopt::Std;
22
23 use lib 'Porting';
24 use Maintainers;
25
26 sub usage {
27 die <<'EOF';
28 usage: $0 [ -d -x ] source_dir1 source_dir2
29 EOF
30 }
31
32 my %opts;
33 getopts('dx', \%opts) or usage;
34 @ARGV == 2 or usage;
35
36 for (@ARGV[0, 1]) {
37     die "$0: '$_' does not look like Perl directory\n"
38         unless -f catfile($_, "perl.h") && -d catdir($_, "Porting");
39 }
40
41 my %dual_files;
42 if ($opts{x}) {
43     die "With -x, one of the directories must be '.'\n"
44         unless $ARGV[0] eq '.' or  $ARGV[1] eq '.';
45     for my $m (grep $Maintainers::Modules{$_}{CPAN},
46                                 keys %Maintainers::Modules)
47     {
48
49         $dual_files{"./$_"} = 1 for Maintainers::get_module_files($m);
50     }
51 }
52
53 my $dir2 = rel2abs($ARGV[1]);
54 chdir $ARGV[0] or die "$0: chdir '$ARGV[0]' failed: $!\n";
55
56 # Files to skip from the check for one reason or another,
57 # usually because they pull in their version from some other file.
58 my %skip;
59 @skip{
60     './lib/Carp/Heavy.pm',
61     './lib/Config.pm',          # no version number but contents will vary
62     './lib/Exporter/Heavy.pm',
63     './win32/FindExt.pm',
64 } = ();
65 my $skip_dirs = qr|^\./t/lib|;
66
67 my @wanted;
68 my @diffs;
69 find(
70      sub { /\.pm$/ &&
71                $File::Find::dir !~ $skip_dirs &&
72                ! exists $skip{$File::Find::name} &&
73                ! exists $dual_files{$File::Find::name}
74                &&
75                do { my $file2 =
76                         catfile(catdir($dir2, $File::Find::dir), $_);
77                     (my $xs_file1 = $_)     =~ s/\.pm$/.xs/;
78                     (my $xs_file2 = $file2) =~ s/\.pm$/.xs/;
79                     my $eq1 = compare($_, $file2) == 0;
80                     my $eq2 = 1;
81                     if (-e $xs_file1 && -e $xs_file2) {
82                         $eq2 = compare($xs_file1, $xs_file2) == 0;
83                     }
84                     return if $eq1 && $eq2;
85                     my $version1 = eval {MM->parse_version($_)};
86                     my $version2 = eval {MM->parse_version($file2)};
87                     return unless
88                         defined $version1 &&
89                         defined $version2 &&
90                         $version1 eq $version2;
91                     push @wanted, $File::Find::name;
92                     push @diffs, [ "$File::Find::dir/$_", $file2 ] unless $eq1;
93                     push @diffs, [ "$File::Find::dir/$xs_file1", $xs_file2 ]
94                                                                    unless $eq2;
95                 } }, curdir);
96 for (sort @wanted) {
97     print "$_\n";
98 }
99 exit unless $opts{d};
100 for (sort { $a->[0] cmp $b->[0] } @diffs) {
101     print "\n";
102     system "diff -du '$_->[0]' '$_->[1]'";
103 }
104