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