Don't require Sub::Name if we don't find a compiler.
[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 # If undefined, try our best, if true, require XS, if false, never do
11 # XS
12 my $force_xs;
13
14 for (@ARGV) {
15     /^--pm/ and $force_xs = 0;
16     /^--xs/ and $force_xs = 1;
17 }
18
19 my $has_compiler = $force_xs;
20 unless ( defined $force_xs ) {
21     $has_compiler = check_for_compiler()
22         or no_cc();
23 }
24
25 my %prereqs = (
26     'Scalar::Util'  => '1.18',
27     'Sub::Name'     => '0.02',
28     'Sub::Identify' => '0.03',
29     'MRO::Compat'   => '0.05',
30     'Carp'          => 0,
31 );
32
33 delete $prereqs{'Sub::Name'}
34     unless $has_compiler;
35
36 write_makefile();
37
38 sub write_makefile {
39     my $ccflags = -d '.svn' || $ENV{MAINTAINER_MODE} ? '-Wall' : '';
40
41     WriteMakefile(
42         VERSION_FROM  => 'lib/Class/MOP.pm',
43         NAME          => 'Class::MOP',
44         PREREQ_PM     => \%prereqs,
45         CONFIGURE     => \&init,
46         CCFLAGS       => $ccflags,
47         clean         => { FILES => 'test.c test.o' },
48         ABSTRACT_FROM => 'lib/Class/MOP.pm',
49         AUTHOR        => 'Stevan Little <stevan@iinteractive.com>',
50         LICENSE       => 'perl',
51     );
52 }
53
54 sub no_cc {
55     print <<'EOF';
56
57  I cannot determine if you have a C compiler
58  so I will install a perl-only implementation
59
60  You can force installation of the XS version with
61
62     perl Makefile.PL --xs
63
64 EOF
65 }
66
67 sub check_for_compiler {
68     print "Testing if you have a C compiler\n";
69
70     eval { require ExtUtils::CBuilder };
71     if ($@) {
72         return _check_for_compiler_manually();
73     }
74     else {
75         return _check_for_compiler_with_cbuilder();
76     }
77 }
78
79 sub _check_for_compiler_with_cbuilder {
80     my $cb = ExtUtils::CBuilder->new( quiet => 1 );
81
82     return $cb->have_compiler();
83 }
84
85 sub _check_for_compiler_manually {
86     unless ( open F, '>', 'test.c' ) {
87         warn
88             "Cannot write test.c, skipping test compilation and installing pure Perl version.\n";
89         return 0;
90     }
91
92     print F <<'EOF';
93 int main() { return 0; }
94 EOF
95
96     close F or return 0;
97
98     my $cc = $Config{cc};
99     if ( $cc =~ /cl(\.exe)?$/ ) {
100
101         # stupid stupid MSVC compiler hack tacken from version.pm's
102         # Makefile.PL
103         $cc .= ' -c';    # prevent it from calling the linker
104     }
105
106     system("$cc -o test$Config{obj_ext} test.c") and return 0;
107
108     unlink $_ for grep {-f} 'test.c', "test$Config{obj_ext}";
109
110     return 1;
111 }
112
113 # This is EUMM voodoo
114 sub init {
115     my $hash = $_[1];
116
117     unless ($has_compiler) {
118         @{$hash}{ 'XS', 'C' } = ( {}, [] );
119     }
120
121     $hash;
122 }