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