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