support PUREPERL_ONLY
[p5sagit/Devel-GlobalDestruction.git] / Makefile.PL
1 use strict;
2 use warnings;
3
4 require 5.006;
5
6 use ExtUtils::MakeMaker;
7 BEGIN { if ( $^O eq 'cygwin' ) {
8   require ExtUtils::MM_Cygwin;
9   require ExtUtils::MM_Win32;
10   if ( ! defined(&ExtUtils::MM_Cygwin::maybe_command) ) {
11     *ExtUtils::MM_Cygwin::maybe_command = sub {
12       my ($self, $file) = @_;
13       if ($file =~ m{^/cygdrive/}i and ExtUtils::MM_Win32->can('maybe_command')) {
14         ExtUtils::MM_Win32->maybe_command($file);
15       } else {
16         ExtUtils::MM_Unix->maybe_command($file);
17       }
18     }
19   }
20 }}
21
22 my $pureperl;
23 for my $i (0..$#ARGV) {
24   if ($ARGV[$i] =~ /^PUREPERL_ONLY=(.*)/) {
25     $pureperl = $1;
26     splice @ARGV, $i, 1;
27     last;
28   }
29 }
30
31 my $mymeta_works = eval { ExtUtils::MakeMaker->VERSION('6.5707'); 1 };
32 my $mymeta = $mymeta_works || eval { ExtUtils::MakeMaker->VERSION('6.5702'); 1 };
33
34 my %META_BITS = (
35 );
36 my %RUN_DEPS = (
37   'Sub::Exporter::Progressive' => '0.001006',
38   ( (defined ${^GLOBAL_PHASE} or $pureperl or !can_xs() )
39     ? ()
40     : ('Devel::GlobalDestruction::XS' => 0)
41   ),
42 );
43
44 my %WriteMakefileArgs = (
45   NAME                => 'Devel::GlobalDestruction',
46   VERSION_FROM        => 'lib/Devel/GlobalDestruction.pm',
47   LICENSE             => 'perl',
48   INSTALLDIRS         => 'site',
49   PL_FILES            => { },
50   MIN_PERL_VERSION    => '5.006',
51   PREREQ_PM           => \%RUN_DEPS,
52   CONFIGURE_REQUIRES  => { 'ExtUtils::CBuilder' => 0.27 },
53   META_ADD => {
54     resources => {
55       homepage => 'http://search.cpan.org/dist/Devel-GlobalDestruction',
56       repository => 'git://git.shadowcat.co.uk/p5sagit/Devel-GlobalDestruction.git',
57       bugtracker => 'http://rt.cpan.org/Public/Dist/Display.html?Name=Devel-GlobalDestruction',
58     },
59     requires => \%RUN_DEPS,
60   },
61   ($mymeta and !$mymeta_works) ? ( 'NO_MYMETA' => 1 ) : (),
62 );
63
64 unless ( eval { ExtUtils::MakeMaker->VERSION('6.56') } ) {
65   my $br = delete $WriteMakefileArgs{BUILD_REQUIRES};
66   my $pp = $WriteMakefileArgs{PREREQ_PM};
67   for my $mod ( keys %$br ) {
68     if ( exists $pp->{$mod} ) {
69       $pp->{$mod} = $br->{$mod} if $br->{$mod} > $pp->{$mod};
70     }
71     else {
72       $pp->{$mod} = $br->{$mod};
73     }
74   }
75 }
76
77 if (eval { require Devel::GlobalDestruction }
78     && Devel::GlobalDestruction->VERSION < 0.10) {
79   package MY;
80   no warnings 'once';
81
82   *install = sub {
83     my $self = shift;
84     return '
85 pure_site_install ::
86         $(NOECHO) $(RM_F) ' . $self->quote_literal(
87       $self->catfile('$(DESTINSTALLSITEARCH)', 'Devel', 'GlobalDestruction.pm')
88     ) . "\n" . $self->SUPER::install;
89   };
90 }
91
92 delete $WriteMakefileArgs{CONFIGURE_REQUIRES}
93   unless eval { ExtUtils::MakeMaker->VERSION('6.52') };
94
95 WriteMakefile(%WriteMakefileArgs);
96
97 # can we locate a (the) C compiler
98 sub can_cc {
99   my @chunks = split(/ /, $Config::Config{cc}) or return;
100
101   # $Config{cc} may contain args; try to find out the program part
102   while (@chunks) {
103     return can_run("@chunks") || (pop(@chunks), next);
104   }
105
106   return;
107 }
108
109 # check if we can run some command
110 sub can_run {
111   my ($cmd) = @_;
112
113   return $cmd if -x $cmd;
114   if (my $found_cmd = MM->maybe_command($cmd)) {
115     return $found_cmd;
116   }
117
118   require File::Spec;
119   for my $dir ((split /$Config::Config{path_sep}/, $ENV{PATH}), '.') {
120     next if $dir eq '';
121     my $abs = File::Spec->catfile($dir, $cmd);
122     return $abs if (-x $abs or $abs = MM->maybe_command($abs));
123   }
124
125   return;
126 }
127
128 # Can our C compiler environment build XS files
129 sub can_xs {
130   # Do we have the configure_requires checker?
131   local $@;
132   eval "require ExtUtils::CBuilder; ExtUtils::CBuilder->VERSION(0.27)";
133   if ( $@ ) {
134     # They don't obey configure_requires, so it is
135     # someone old and delicate. Try to avoid hurting
136     # them by falling back to an older simpler test.
137     return can_cc();
138   }
139
140   # Do we have a working C compiler
141   my $builder = ExtUtils::CBuilder->new(
142     quiet => 1,
143   );
144   unless ( $builder->have_compiler ) {
145     # No working C compiler
146     return 0;
147   }
148
149   # Write a C file representative of what XS becomes
150   require File::Temp;
151   my ( $FH, $tmpfile ) = File::Temp::tempfile(
152     "compilexs-XXXXX",
153     SUFFIX => '.c',
154   );
155   binmode $FH;
156   print $FH <<'END_C';
157 #include "EXTERN.h"
158 #include "perl.h"
159 #include "XSUB.h"
160
161 int main(int argc, char **argv) {
162     return 0;
163 }
164
165 int boot_sanexs() {
166     return 1;
167 }
168
169 END_C
170   close $FH;
171
172   # Can the C compiler access the same headers XS does
173   my @libs   = ();
174   my $object = undef;
175   eval {
176     local $^W = 0;
177     $object = $builder->compile(
178       source => $tmpfile,
179     );
180     @libs = $builder->link(
181       objects     => $object,
182       module_name => 'sanexs',
183     );
184   };
185   my $result = $@ ? 0 : 1;
186
187   # Clean up all the build files
188   foreach ( $tmpfile, $object, @libs ) {
189     next unless defined $_;
190     1 while unlink;
191   }
192
193   return $result;
194 }