Run all the tests twice if we have XS, once with XS & once without.
[gitmo/Class-MOP.git] / Makefile.PL
1 # The perl/C checking voodoo is mostly stolen from Graham Barr's
2 # Scalar-List-Utils distribution.
3 use strict;
4 use warnings;
5
6 use ExtUtils::MakeMaker;
7 use Config qw(%Config);
8 use File::Spec;
9
10 use 5.008;
11
12 # If undefined, try our best, if true, require XS, if false, never do
13 # XS
14 my $force_xs;
15
16 for (@ARGV) {
17     /^--pm/ and $force_xs = 0;
18     /^--xs/ and $force_xs = 1;
19 }
20
21 my $has_compiler = $force_xs;
22 unless ( defined $force_xs ) {
23     $has_compiler = check_for_compiler()
24         or no_cc();
25 }
26
27 my %prereqs = (
28     'Scalar::Util'             => '1.18',
29     'Sub::Name'                => '0.04',
30     'Sub::Identify'            => '0.03',
31     'MRO::Compat'              => '0.05',
32     'Test::More'               => '0',
33     'Test::Exception'          => '0',
34     'File::Spec'               => '0',
35     'Carp'                     => '0',
36     'Devel::GlobalDestruction' => '0',
37 );
38
39 delete @prereqs{qw(Sub::Name Devel::GlobalDestruction)}
40     unless $has_compiler;
41
42 if ($has_compiler && is_maintainer()) {
43     create_pp_tests();
44 }
45
46 write_makefile();
47
48 sub write_makefile {
49     my $ccflags = -d '.svn' || $ENV{MAINTAINER_MODE} ? '-Wall' : '';
50
51     WriteMakefile(
52         VERSION_FROM  => 'lib/Class/MOP.pm',
53         NAME          => 'Class::MOP',
54         PREREQ_PM     => \%prereqs,
55         CONFIGURE     => \&init,
56         CCFLAGS       => $ccflags,
57         clean         => { FILES => 'test.c test.o t/pp*' },
58         ABSTRACT_FROM => 'lib/Class/MOP.pm',
59         AUTHOR        => 'Stevan Little <stevan@iinteractive.com>',
60         LICENSE       => 'perl',
61     );
62 }
63
64 sub no_cc {
65     print <<'EOF';
66
67  I cannot determine if you have a C compiler
68  so I will install a perl-only implementation
69
70  You can force installation of the XS version with
71
72     perl Makefile.PL --xs
73
74 EOF
75 }
76
77 sub check_for_compiler {
78     print "Testing if you have a C compiler\n";
79
80     eval { require ExtUtils::CBuilder };
81     if ($@) {
82         return _check_for_compiler_manually();
83     }
84     else {
85         return _check_for_compiler_with_cbuilder();
86     }
87 }
88
89 sub _check_for_compiler_with_cbuilder {
90     my $cb = ExtUtils::CBuilder->new( quiet => 1 );
91
92     return $cb->have_compiler();
93 }
94
95 sub _check_for_compiler_manually {
96     unless ( open F, '>', 'test.c' ) {
97         warn
98             "Cannot write test.c, skipping test compilation and installing pure Perl version.\n";
99         return 0;
100     }
101
102     print F <<'EOF';
103 int main() { return 0; }
104 EOF
105
106     close F or return 0;
107
108     my $cc = $Config{cc};
109     if ( $cc =~ /cl(\.exe)?$/ ) {
110
111         # stupid stupid MSVC compiler hack tacken from version.pm's
112         # Makefile.PL
113         $cc .= ' -c';    # prevent it from calling the linker
114     }
115
116     system("$cc -o test$Config{obj_ext} test.c") and return 0;
117
118     unlink $_ for grep {-f} 'test.c', "test$Config{obj_ext}";
119
120     return 1;
121 }
122
123 # This sucks, but it's the best guess we can make. Since we just use
124 # it to run two sets of tests, it's not big deal if it ends up true
125 # for a non-maintainer.
126 sub is_maintainer {
127     return 0 if $ENV{PERL5_CPAN_IS_RUNNING} || $ENV{PERL5_CPANPLUS_IS_RUNNING};
128
129     return 1;
130 }
131
132 sub create_pp_tests {
133     opendir my $dh, 't' or die "Cannot read t: $!";
134
135     foreach my $file ( grep {/^\d.+\.t$/} readdir $dh ) {
136         next if $file =~ /^99/;
137
138         my $real_file = File::Spec->catfile( 't', $file );
139
140         open my $fh, '<', $real_file
141             or die "Cannot read $real_file: $!";
142
143         my $shbang = <$fh>;
144         my $test = do { local $/; <$fh> };
145
146         close $fh;
147
148         $test = "$shbang\nBEGIN { \$ENV{CLASS_MOP_NO_XS} = 1 }\n\n$test";
149
150         my $new_file = File::Spec->catfile( 't', "pp_$file" );
151         open my $new_fh, '>', $new_file
152             or die "Cannot write to $new_file: $!";
153
154         print $new_fh $test;
155
156         close $new_fh;
157     }
158 }
159
160 # This is EUMM voodoo
161 sub init {
162     my $hash = $_[1];
163
164     unless ($has_compiler) {
165         @{$hash}{ 'XS', 'C' } = ( {}, [] );
166     }
167
168     $hash;
169 }