more sophisticated code is needed if we actually have build-phase prereqs
[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   realclean => { FILES => [ 'Distar/', 'MANIFEST*' ] },
55 );
56
57 my $eumm_version  = eval $ExtUtils::MakeMaker::VERSION;
58
59 for (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
70 # dynamic prereqs get added here.
71
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'
75     if can_xs() && !eval { require JSON::XS; 1; };
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 # can we locate a (the) C compiler
107 sub can_cc {
108   my @chunks = split(/ /, $Config::Config{cc}) or return;
109
110   # $Config{cc} may contain args; try to find out the program part
111   while (@chunks) {
112     return can_run("@chunks") || (pop(@chunks), next);
113   }
114
115   return;
116 }
117
118 # check if we can run some command
119 sub can_run {
120   my ($cmd) = @_;
121
122   return $cmd if -x $cmd;
123   if (my $found_cmd = MM->maybe_command($cmd)) {
124     return $found_cmd;
125   }
126
127   require File::Spec;
128   for my $dir ((split /$Config::Config{path_sep}/, $ENV{PATH}), '.') {
129     next if $dir eq '';
130     my $abs = File::Spec->catfile($dir, $cmd);
131     return $abs if (-x $abs or $abs = MM->maybe_command($abs));
132   }
133
134   return;
135 }
136
137 # Can our C compiler environment build XS files
138 sub can_xs {
139   # Do we have the configure_requires checker?
140   local $@;
141   eval "require ExtUtils::CBuilder; ExtUtils::CBuilder->VERSION(0.27)";
142   if ( $@ ) {
143     # They don't obey configure_requires, so it is
144     # someone old and delicate. Try to avoid hurting
145     # them by falling back to an older simpler test.
146     return can_cc();
147   }
148
149   # Do we have a working C compiler
150   my $builder = ExtUtils::CBuilder->new(
151     quiet => 1,
152   );
153   unless ( $builder->have_compiler ) {
154     # No working C compiler
155     return 0;
156   }
157
158   # Write a C file representative of what XS becomes
159   require File::Temp;
160   my ( $FH, $tmpfile ) = File::Temp::tempfile(
161     "compilexs-XXXXX",
162     SUFFIX => '.c',
163   );
164   binmode $FH;
165   print $FH <<'END_C';
166 #include "EXTERN.h"
167 #include "perl.h"
168 #include "XSUB.h"
169
170 int main(int argc, char **argv) {
171     return 0;
172 }
173
174 int boot_sanexs() {
175     return 1;
176 }
177
178 END_C
179   close $FH;
180
181   # Can the C compiler access the same headers XS does
182   my @libs   = ();
183   my $object = undef;
184   eval {
185     local $^W = 0;
186     $object = $builder->compile(
187       source => $tmpfile,
188     );
189     @libs = $builder->link(
190       objects     => $object,
191       module_name => 'sanexs',
192     );
193   };
194   my $result = $@ ? 0 : 1;
195
196   # Clean up all the build files
197   foreach ( $tmpfile, $object, @libs ) {
198     next unless defined $_;
199     1 while unlink;
200   }
201
202   return $result;
203 }