add missing configure requires - ExtUtils::CBuilder is not guaranteed to be at the...
[p5sagit/JSON-MaybeXS.git] / Makefile.PL
1 use strict;
2 use warnings FATAL => 'all';
3 use 5.008001;
4 use ExtUtils::MakeMaker;
5 (do 'maint/Makefile.PL.include' or die $@) unless -f 'META.yml';
6
7 # we require Cpanel::JSON::XS, except if JSON::XS is already installed.
8 # (we also always recommend Cpanel::JSON::XS, just to make sure.)
9 my $require_cpanel_json_xs = can_xs() && !eval { require JSON::XS; 1; };
10
11 my %WriteMakefileArgs = (
12   NAME => 'JSON::MaybeXS',
13   VERSION_FROM => 'lib/JSON/MaybeXS.pm',
14   PREREQ_PM => {
15     'JSON::PP' => '2.27202',
16     ($require_cpanel_json_xs
17       ? ('Cpanel::JSON::XS' => '2.3310')
18       : ()),
19     perl => '5.006',
20   },
21   CONFIGURE_REQUIRES => {
22     'ExtUtils::CBuilder' => '0.27',
23     'File::Spec' => '0',
24     'File::Temp' => '0',
25   },
26   TEST_REQUIRES => {
27     'Test::Without::Module' => '0.17',
28     'Test::More' => '0.88'
29   },
30
31   META_MERGE => {
32     'meta-spec' => { version => 2 },
33     dynamic_config => 1,
34     prereqs => { runtime => { recommends => { 'Cpanel::JSON::XS' => '2.3310' } } },
35
36     resources => {
37       repository => {
38         url => 'https://github.com/karenetheridge/JSON-MaybeXS.git',
39         web => 'https://github.com/karenetheridge/JSON-MaybeXS',
40         type => 'git',
41       },
42       bugtracker => {
43         mailto => 'bug-JSON-MaybeXS@rt.cpan.org',
44         web => 'https://rt.cpan.org/Public/Dist/Display.html?Name=JSON-MaybeXS',
45       },
46     },
47   },
48
49   realclean => { FILES => 'Distar/' },
50 );
51
52 my $eumm_version  = eval $ExtUtils::MakeMaker::VERSION;
53 $WriteMakefileArgs{MIN_PERL_VERSION} = delete $WriteMakefileArgs{PREREQ_PM}{perl} || 0;
54
55 $WriteMakefileArgs{BUILD_REQUIRES} = {
56     %{$WriteMakefileArgs{BUILD_REQUIRES} || {}},
57     %{delete $WriteMakefileArgs{TEST_REQUIRES}}
58 } if $eumm_version < 6.63_03;
59
60 $WriteMakefileArgs{PREREQ_PM} = {
61     %{$WriteMakefileArgs{PREREQ_PM}},
62     %{delete $WriteMakefileArgs{BUILD_REQUIRES}}
63 } if $eumm_version < 6.55_01;
64
65 delete $WriteMakefileArgs{CONFIGURE_REQUIRES}
66   if $eumm_version < 6.51_03;
67
68 WriteMakefile(%WriteMakefileArgs);
69
70 # can we locate a (the) C compiler
71 sub can_cc {
72   my @chunks = split(/ /, $Config::Config{cc}) or return;
73
74   # $Config{cc} may contain args; try to find out the program part
75   while (@chunks) {
76     return can_run("@chunks") || (pop(@chunks), next);
77   }
78
79   return;
80 }
81
82 # check if we can run some command
83 sub can_run {
84   my ($cmd) = @_;
85
86   return $cmd if -x $cmd;
87   if (my $found_cmd = MM->maybe_command($cmd)) {
88     return $found_cmd;
89   }
90
91   require File::Spec;
92   for my $dir ((split /$Config::Config{path_sep}/, $ENV{PATH}), '.') {
93     next if $dir eq '';
94     my $abs = File::Spec->catfile($dir, $cmd);
95     return $abs if (-x $abs or $abs = MM->maybe_command($abs));
96   }
97
98   return;
99 }
100
101 # Can our C compiler environment build XS files
102 sub can_xs {
103   # Do we have the configure_requires checker?
104   local $@;
105   eval "require ExtUtils::CBuilder; ExtUtils::CBuilder->VERSION(0.27)";
106   if ( $@ ) {
107     # They don't obey configure_requires, so it is
108     # someone old and delicate. Try to avoid hurting
109     # them by falling back to an older simpler test.
110     return can_cc();
111   }
112
113   # Do we have a working C compiler
114   my $builder = ExtUtils::CBuilder->new(
115     quiet => 1,
116   );
117   unless ( $builder->have_compiler ) {
118     # No working C compiler
119     return 0;
120   }
121
122   # Write a C file representative of what XS becomes
123   require File::Temp;
124   my ( $FH, $tmpfile ) = File::Temp::tempfile(
125     "compilexs-XXXXX",
126     SUFFIX => '.c',
127   );
128   binmode $FH;
129   print $FH <<'END_C';
130 #include "EXTERN.h"
131 #include "perl.h"
132 #include "XSUB.h"
133
134 int main(int argc, char **argv) {
135     return 0;
136 }
137
138 int boot_sanexs() {
139     return 1;
140 }
141
142 END_C
143   close $FH;
144
145   # Can the C compiler access the same headers XS does
146   my @libs   = ();
147   my $object = undef;
148   eval {
149     local $^W = 0;
150     $object = $builder->compile(
151       source => $tmpfile,
152     );
153     @libs = $builder->link(
154       objects     => $object,
155       module_name => 'sanexs',
156     );
157   };
158   my $result = $@ ? 0 : 1;
159
160   # Clean up all the build files
161   foreach ( $tmpfile, $object, @libs ) {
162     next unless defined $_;
163     1 while unlink;
164   }
165
166   return $result;
167 }