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