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