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