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