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