Depend on Task::Weaken so we know we have an XS Scalar::Util
[gitmo/Class-MOP.git] / Makefile.PL
CommitLineData
f125b31e 1# The perl/C checking voodoo is mostly stolen from Graham Barr's
2# Scalar-List-Utils distribution.
591a9381 3use strict;
4use warnings;
591a9381 5
f125b31e 6use ExtUtils::MakeMaker;
7use Config qw(%Config);
8use File::Spec;
591a9381 9
9ea663b0 10use 5.008;
11
f125b31e 12# If undefined, try our best, if true, require XS, if false, never do
13# XS
14my $force_xs;
591a9381 15
f125b31e 16for (@ARGV) {
17 /^--pm/ and $force_xs = 0;
18 /^--xs/ and $force_xs = 1;
19}
591a9381 20
f125b31e 21my $has_compiler = $force_xs;
22unless ( defined $force_xs ) {
23 $has_compiler = check_for_compiler()
24 or no_cc();
25}
26
e896822d 27my %prereqs = (
9ea663b0 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',
a0cbc21f 37 'Task::Weaken' => '0',
e896822d 38);
39
9ad4163c 40delete @prereqs{qw(Sub::Name Devel::GlobalDestruction)}
e896822d 41 unless $has_compiler;
42
3c5aa6e2 43if ($has_compiler && is_maintainer()) {
44 create_pp_tests();
45}
46
f125b31e 47write_makefile();
48
49sub write_makefile {
50 my $ccflags = -d '.svn' || $ENV{MAINTAINER_MODE} ? '-Wall' : '';
51
52 WriteMakefile(
e896822d 53 VERSION_FROM => 'lib/Class/MOP.pm',
54 NAME => 'Class::MOP',
55 PREREQ_PM => \%prereqs,
f125b31e 56 CONFIGURE => \&init,
57 CCFLAGS => $ccflags,
3c5aa6e2 58 clean => { FILES => 'test.c test.o t/pp*' },
f125b31e 59 ABSTRACT_FROM => 'lib/Class/MOP.pm',
60 AUTHOR => 'Stevan Little <stevan@iinteractive.com>',
61 LICENSE => 'perl',
62 );
63}
64
65sub 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
75EOF
76}
77
78sub 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
90sub _check_for_compiler_with_cbuilder {
91 my $cb = ExtUtils::CBuilder->new( quiet => 1 );
92
93 return $cb->have_compiler();
94}
95
96sub _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';
104int main() { return 0; }
105EOF
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
3c5aa6e2 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.
127sub is_maintainer {
128 return 0 if $ENV{PERL5_CPAN_IS_RUNNING} || $ENV{PERL5_CPANPLUS_IS_RUNNING};
129
130 return 1;
131}
132
133sub 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
f125b31e 161# This is EUMM voodoo
162sub init {
163 my $hash = $_[1];
164
165 unless ($has_compiler) {
166 @{$hash}{ 'XS', 'C' } = ( {}, [] );
167 }
168
169 $hash;
170}