9ff02ccf2e9eda653d3f345a8f73d4cdbbbccdfc
[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     # r/w: p5sagit@git.shadowcat.co.uk:JSON-MaybeXS.git
15       repository => {
16         url => 'git://git.shadowcat.co.uk/p5sagit/JSON-MaybeXS.git',
17         web => 'http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit/JSON-MaybeXS.git',
18         type => 'git',
19       },
20       bugtracker => {
21         mailto => 'bug-JSON-MaybeXS@rt.cpan.org',
22         web => 'https://rt.cpan.org/Public/Dist/Display.html?Name=JSON-MaybeXS',
23       },
24     },
25     x_contributors => [ # manually added, from git shortlog -e -s -n
26       'Clinton Gormley <develop@traveljury.com>',
27       'Graham Knop <haarg@haarg.org>',
28       'John SJ Anderson <genehack@genehack.org>',
29       'Karen Etheridge <ether@cpan.org>',
30       'Kieren Diment <diment@gmail.com>',
31       'Matt S Trout <mst@shadowcat.co.uk>',
32     ],
33     keywords => [ qw(json serializer serialiser data) ],
34   },
35
36   META_ADD => {
37     prereqs => {
38       configure => {
39         requires => {
40           'ExtUtils::MakeMaker' => '0',
41           'ExtUtils::CBuilder' => '0.27',
42           'File::Spec' => '0',
43           'File::Temp' => '0',
44         },
45       },
46       runtime => {
47         requires => {
48           'Scalar::Util' => '0',
49           'Carp' => '0',
50           'JSON::PP' => '2.27300',
51           # we may also add a runtime prereq for Cpanel::JSON::XS, on the
52           # installer's machine
53           perl => '5.006',
54         },
55         recommends => { 'Cpanel::JSON::XS' => '2.3310' },
56       },
57       test => {
58         requires => {
59           'Test::Without::Module' => '0.17',
60           'Test::More' => '0.88',
61         },
62       },
63     },
64   },
65 );
66
67 my $eumm_version  = eval $ExtUtils::MakeMaker::VERSION;
68
69 for (qw(configure build test runtime)) {
70   my $key = $_ eq 'runtime' ? 'PREREQ_PM' : uc $_.'_REQUIRES';
71   next unless exists $WriteMakefileArgs{META_ADD}{prereqs}{$_}
72            or exists $WriteMakefileArgs{$key};
73   my $r = $WriteMakefileArgs{$key} = {
74     %{$WriteMakefileArgs{META_ADD}{prereqs}{$_}{requires} || {}},
75     %{delete $WriteMakefileArgs{$key} || {}},
76   };
77   defined $r->{$_} or delete $r->{$_} for keys %$r;
78 }
79
80 # dynamic prereqs get added here.
81
82 my $args = parse_args();
83
84 # we require Cpanel::JSON::XS, except if JSON::XS is already installed.
85 # (we also always recommend Cpanel::JSON::XS, just to make sure.)
86 $WriteMakefileArgs{PREREQ_PM}{'Cpanel::JSON::XS'} = '2.3310'
87     if not $args->{PUREPERL_ONLY}
88         and ((eval { require Cpanel::JSON::XS; 1 } and not eval { Cpanel::JSON::XS->VERSION('2.3310'); 1 })
89             or (not eval { require JSON::XS; 1; } and can_xs()));
90
91 # avoid "JSON::XS::Boolean::* redefined" warnings caused by incompatibilities
92 # between JSON::XS 2.x and 3.0 --
93 # if JSON::XS is installed and < 3.0
94 # and Cpanel::JSON::XS is (or is about to be) >= 3.0,
95 # then update JSON::XS to eliminate the incompatibility
96 $WriteMakefileArgs{PREREQ_PM}{'JSON::XS'} = '3.00'
97     if not $args->{PUREPERL_ONLY}
98         and eval { require JSON::XS; 1 } and not eval { JSON::XS->VERSION('3.0'); 1 }
99         and (eval { require Cpanel::JSON::XS; Cpanel::JSON::XS->VERSION('3.0'); 1 }
100             # we presume here that if we are installing Cpanel::JSON::XS, we
101             # are installing the latest version
102             or exists $WriteMakefileArgs{PREREQ_PM}{'Cpanel::JSON::XS'});
103
104 $WriteMakefileArgs{MIN_PERL_VERSION} = delete $WriteMakefileArgs{PREREQ_PM}{perl} || 0;
105
106 die 'attention developer: you need to do a sane meta merge here!'
107   if keys %{$WriteMakefileArgs{BUILD_REQUIRES}};
108
109 $WriteMakefileArgs{BUILD_REQUIRES} = {
110     %{$WriteMakefileArgs{BUILD_REQUIRES} || {}},
111     %{delete $WriteMakefileArgs{TEST_REQUIRES}}
112 } if $eumm_version < 6.63_03;
113
114 $WriteMakefileArgs{PREREQ_PM} = {
115     %{$WriteMakefileArgs{PREREQ_PM}},
116     %{delete $WriteMakefileArgs{BUILD_REQUIRES}}
117 } if $eumm_version < 6.55_01;
118
119 delete $WriteMakefileArgs{CONFIGURE_REQUIRES}
120   if $eumm_version < 6.51_03;
121
122 delete $WriteMakefileArgs{MIN_PERL_VERSION}
123   if $eumm_version < 6.48;
124
125 delete @WriteMakefileArgs{qw(META_ADD META_MERGE)}
126   if $eumm_version < 6.46;
127
128 delete $WriteMakefileArgs{LICENSE}
129   if $eumm_version < 6.31;
130
131 WriteMakefile(%WriteMakefileArgs);
132
133
134 sub parse_args {
135   # copied from EUMM
136   require ExtUtils::MakeMaker;
137   require Text::ParseWords;
138   ExtUtils::MakeMaker::parse_args(
139     my $tmp = {},
140     Text::ParseWords::shellwords($ENV{PERL_MM_OPT} || ''),
141     @ARGV,
142   );
143   return $tmp->{ARGS} || {};
144 }
145
146 # can we locate a (the) C compiler
147 sub can_cc {
148   my @chunks = split(/ /, $Config::Config{cc}) or return;
149
150   # $Config{cc} may contain args; try to find out the program part
151   while (@chunks) {
152     return can_run("@chunks") || (pop(@chunks), next);
153   }
154
155   return;
156 }
157
158 # check if we can run some command
159 sub can_run {
160   my ($cmd) = @_;
161
162   return $cmd if -x $cmd;
163   if (my $found_cmd = MM->maybe_command($cmd)) {
164     return $found_cmd;
165   }
166
167   require File::Spec;
168   for my $dir ((split /$Config::Config{path_sep}/, $ENV{PATH}), '.') {
169     next if $dir eq '';
170     my $abs = File::Spec->catfile($dir, $cmd);
171     return $abs if (-x $abs or $abs = MM->maybe_command($abs));
172   }
173
174   return;
175 }
176
177 # Can our C compiler environment build XS files
178 sub can_xs {
179   # Do we have the configure_requires checker?
180   local $@;
181   eval "require ExtUtils::CBuilder; ExtUtils::CBuilder->VERSION(0.27)";
182   if ( $@ ) {
183     # They don't obey configure_requires, so it is
184     # someone old and delicate. Try to avoid hurting
185     # them by falling back to an older simpler test.
186     return can_cc();
187   }
188
189   # Do we have a working C compiler
190   my $builder = ExtUtils::CBuilder->new(
191     quiet => 1,
192   );
193   unless ( $builder->have_compiler ) {
194     # No working C compiler
195     return 0;
196   }
197
198   # Write a C file representative of what XS becomes
199   require File::Temp;
200   my ( $FH, $tmpfile ) = File::Temp::tempfile(
201     "compilexs-XXXXX",
202     SUFFIX => '.c',
203   );
204   binmode $FH;
205   print $FH <<'END_C';
206 #include "EXTERN.h"
207 #include "perl.h"
208 #include "XSUB.h"
209
210 int main(int argc, char **argv) {
211     return 0;
212 }
213
214 int boot_sanexs() {
215     return 1;
216 }
217
218 END_C
219   close $FH;
220
221   # Can the C compiler access the same headers XS does
222   my @libs   = ();
223   my $object = undef;
224   eval {
225     local $^W = 0;
226     $object = $builder->compile(
227       source => $tmpfile,
228     );
229     @libs = $builder->link(
230       objects     => $object,
231       module_name => 'sanexs',
232     );
233   };
234   my $result = $@ ? 0 : 1;
235
236   # Clean up all the build files
237   foreach ( $tmpfile, $object, @libs ) {
238     next unless defined $_;
239     1 while unlink;
240   }
241
242   return $result;
243 }