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