2caeffdcd01a01d83bd1e6b57943219b7b7e4652
[p5sagit/strictures.git] / Makefile.PL
1 use strict;
2 use warnings FATAL => 'all';
3 use ExtUtils::MakeMaker;
4 BEGIN { if ( $^O eq 'cygwin' ) {
5   require ExtUtils::MM_Cygwin;
6   require ExtUtils::MM_Win32;
7   if ( ! defined(&ExtUtils::MM_Cygwin::maybe_command) ) {
8     *ExtUtils::MM_Cygwin::maybe_command = sub {
9       my ($self, $file) = @_;
10       if ($file =~ m{^/cygdrive/}i and ExtUtils::MM_Win32->can('maybe_command')) {
11         ExtUtils::MM_Win32->maybe_command($file);
12       } else {
13         ExtUtils::MM_Unix->maybe_command($file);
14       }
15     }
16   }
17 }}
18
19
20 (do 'maint/Makefile.PL.include' or die $@) unless -f 'META.yml';
21
22 my %extra_prereqs = (
23   indirect => 0,
24   multidimensional => 0,
25   'bareword::filehandles' => 0,
26 );
27
28 use Text::ParseWords;
29 sub parse_args {
30   # copied from EUMM
31   ExtUtils::MakeMaker::parse_args(
32     my $tmp = {},
33     Text::ParseWords::shellwords($ENV{PERL_MM_OPT} || ''),
34     @ARGV,
35   );
36   return $tmp->{ARGS} || {};
37 }
38
39 my $have_compiler
40   = ! parse_args()->{PUREPERL_ONLY}
41   && can_xs();
42
43 WriteMakefile(
44   NAME => 'strictures',
45   VERSION_FROM => 'lib/strictures.pm',
46   MIN_PERL_VERSION => '5.006',
47
48   META_MERGE => {
49     'meta-spec' => { version => 2 },
50     dynamic_config => 1,
51
52     resources => {
53       # r/w: p5sagit@git.shadowcat.co.uk:strictures.git
54       repository => {
55         url => 'git://git.shadowcat.co.uk/p5sagit/strictures.git',
56         web => 'http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit/strictures.git',
57         type => 'git',
58       },
59       bugtracker => {
60           mailto => 'bug-strictures@rt.cpan.org',
61           web => 'https://rt.cpan.org/Public/Dist/Display.html?Name=strictures',
62       },
63     },
64
65     prereqs => {
66       configure => {
67         requires => {
68           'ExtUtils::CBuilder' => 0,
69         },
70       },
71       runtime => {
72         ( $] >= 5.008004 && !$have_compiler
73           ? ( recommends => \%extra_prereqs ) : () ),
74       },
75     },
76   },
77
78   ($] >= 5.008004 && $have_compiler
79     ? ( PREREQ_PM => \%extra_prereqs ) : () ),
80 );
81
82 # can we locate a (the) C compiler
83 sub can_cc {
84   my @chunks = split(/ /, $Config::Config{cc}) or return;
85
86   # $Config{cc} may contain args; try to find out the program part
87   while (@chunks) {
88     return can_run("@chunks") || (pop(@chunks), next);
89   }
90
91   return;
92 }
93
94 # check if we can run some command
95 sub can_run {
96   my ($cmd) = @_;
97
98   return $cmd if -x $cmd;
99   if (my $found_cmd = MM->maybe_command($cmd)) {
100     return $found_cmd;
101   }
102
103   require File::Spec;
104   for my $dir ((split /$Config::Config{path_sep}/, $ENV{PATH}), '.') {
105     next if $dir eq '';
106     my $abs = File::Spec->catfile($dir, $cmd);
107     return $abs if (-x $abs or $abs = MM->maybe_command($abs));
108   }
109
110   return;
111 }
112
113 # Can our C compiler environment build XS files
114 sub can_xs {
115   # Do we have the configure_requires checker?
116   local $@;
117   eval "require ExtUtils::CBuilder; ExtUtils::CBuilder->VERSION(0.27)";
118   if ( $@ ) {
119     # They don't obey configure_requires, so it is
120     # someone old and delicate. Try to avoid hurting
121     # them by falling back to an older simpler test.
122     return can_cc();
123   }
124
125   # Do we have a working C compiler
126   my $builder = ExtUtils::CBuilder->new(
127     quiet => 1,
128   );
129   unless ( $builder->have_compiler ) {
130     # No working C compiler
131     return 0;
132   }
133
134   # Write a C file representative of what XS becomes
135   require File::Temp;
136   my ( $FH, $tmpfile ) = File::Temp::tempfile(
137     "compilexs-XXXXX",
138     SUFFIX => '.c',
139   );
140   binmode $FH;
141   print $FH <<'END_C';
142 #include "EXTERN.h"
143 #include "perl.h"
144 #include "XSUB.h"
145
146 int main(int argc, char **argv) {
147     return 0;
148 }
149
150 int boot_sanexs() {
151     return 1;
152 }
153
154 END_C
155   close $FH;
156
157   # Can the C compiler access the same headers XS does
158   my @libs   = ();
159   my $object = undef;
160   eval {
161     local $^W = 0;
162     $object = $builder->compile(
163       source => $tmpfile,
164     );
165     @libs = $builder->link(
166       objects     => $object,
167       module_name => 'sanexs',
168     );
169   };
170   my $result = $@ ? 0 : 1;
171
172   # Clean up all the build files
173   foreach ( $tmpfile, $object, @libs ) {
174     next unless defined $_;
175     1 while unlink;
176   }
177
178   return $result;
179 }