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