don't load CPAN.pm to check its version
[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 my $support_configure_requires
44   = $ENV{PERL5_CPANM_IS_RUNNING}
45   || !$ENV{PERL5_CPAN_IS_RUNNING}
46   || do {
47     my ($cpanpm) = grep { -e } map { "$_/CPAN.pm" } @INC;
48     my $v = $cpanpm && MM->parse_version($cpanpm);
49     ($v and $v = eval $v) ? $v > 1.94_55 : 1;
50   };
51
52
53 WriteMakefile(
54   NAME => 'strictures',
55   VERSION_FROM => 'lib/strictures.pm',
56   MIN_PERL_VERSION => '5.006',
57
58   META_MERGE => {
59     'meta-spec' => { version => 2 },
60     dynamic_config => 1,
61
62     resources => {
63       # r/w: p5sagit@git.shadowcat.co.uk:strictures.git
64       repository => {
65         url => 'git://git.shadowcat.co.uk/p5sagit/strictures.git',
66         web => 'http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit/strictures.git',
67         type => 'git',
68       },
69       bugtracker => {
70           mailto => 'bug-strictures@rt.cpan.org',
71           web => 'https://rt.cpan.org/Public/Dist/Display.html?Name=strictures',
72       },
73     },
74
75     prereqs => {
76       configure => {
77         requires => {
78           'ExtUtils::CBuilder' => 0,
79         },
80       },
81       runtime => {
82         ( $] >= 5.008004 && !($have_compiler && $support_configure_requires)
83           ? ( recommends => \%extra_prereqs ) : () ),
84       },
85     },
86   },
87
88   ($] >= 5.008004 && $have_compiler && $support_configure_requires
89     ? ( PREREQ_PM => \%extra_prereqs ) : () ),
90 );
91
92 # can we locate a (the) C compiler
93 sub can_cc {
94   my @chunks = split(/ /, $Config::Config{cc}) or return;
95
96   # $Config{cc} may contain args; try to find out the program part
97   while (@chunks) {
98     return can_run("@chunks") || (pop(@chunks), next);
99   }
100
101   return;
102 }
103
104 # check if we can run some command
105 sub can_run {
106   my ($cmd) = @_;
107
108   return $cmd if -x $cmd;
109   if (my $found_cmd = MM->maybe_command($cmd)) {
110     return $found_cmd;
111   }
112
113   require File::Spec;
114   for my $dir ((split /$Config::Config{path_sep}/, $ENV{PATH}), '.') {
115     next if $dir eq '';
116     my $abs = File::Spec->catfile($dir, $cmd);
117     return $abs if (-x $abs or $abs = MM->maybe_command($abs));
118   }
119
120   return;
121 }
122
123 # Can our C compiler environment build XS files
124 sub can_xs {
125   # Do we have the configure_requires checker?
126   local $@;
127   eval "require ExtUtils::CBuilder; ExtUtils::CBuilder->VERSION(0.27)";
128   if ( $@ ) {
129     # They don't obey configure_requires, so it is
130     # someone old and delicate. Try to avoid hurting
131     # them by falling back to an older simpler test.
132     return can_cc();
133   }
134
135   # Do we have a working C compiler
136   my $builder = ExtUtils::CBuilder->new(
137     quiet => 1,
138   );
139   unless ( $builder->have_compiler ) {
140     # No working C compiler
141     return 0;
142   }
143
144   # Write a C file representative of what XS becomes
145   require File::Temp;
146   my ( $FH, $tmpfile ) = File::Temp::tempfile(
147     "compilexs-XXXXX",
148     SUFFIX => '.c',
149   );
150   binmode $FH;
151   print $FH <<'END_C';
152 #include "EXTERN.h"
153 #include "perl.h"
154 #include "XSUB.h"
155
156 int main(int argc, char **argv) {
157     return 0;
158 }
159
160 int boot_sanexs() {
161     return 1;
162 }
163
164 END_C
165   close $FH;
166
167   # Can the C compiler access the same headers XS does
168   my @libs   = ();
169   my $object = undef;
170   eval {
171     local $^W = 0;
172     $object = $builder->compile(
173       source => $tmpfile,
174     );
175     @libs = $builder->link(
176       objects     => $object,
177       module_name => 'sanexs',
178     );
179   };
180   my $result = $@ ? 0 : 1;
181
182   # Clean up all the build files
183   foreach ( $tmpfile, $object, @libs ) {
184     next unless defined $_;
185     1 while unlink;
186   }
187
188   return $result;
189 }