bd5408dc3737883e3081c5ce53547b747c50eac4
[p5sagit/JSON-MaybeXS.git] / Makefile.PL
1 use strict;
2 use warnings FATAL => 'all';
3 use 5.008001;
4 use ExtUtils::MakeMaker;
5 (do 'maint/Makefile.PL.include' or die $@) unless -f 'META.yml';
6
7 my %WriteMakefileArgs = (
8   NAME => 'JSON::MaybeXS',
9   VERSION_FROM => 'lib/JSON/MaybeXS.pm',
10   META_ADD => {
11     'meta-spec' => { version => 2 },
12     dynamic_config => 1,
13     prereqs => {
14       configure => {
15         requires => {
16           'ExtUtils::CBuilder' => '0.27',
17           'File::Spec' => '0',
18           'File::Temp' => '0',
19         },
20       },
21       runtime => {
22         requires => {
23           'JSON::PP' => '2.27202',
24           # we may also add a runtime prereq for Cpanel::JSON::XS, on the
25           # installer's machine
26           perl => '5.006',
27         },
28         recommends => { 'Cpanel::JSON::XS' => '2.3310' },
29       },
30       test => {
31         requires => {
32           'Test::Without::Module' => '0.17',
33           'Test::More' => '0.88',
34         },
35       },
36     },
37
38     resources => {
39       repository => {
40         url => 'https://github.com/karenetheridge/JSON-MaybeXS.git',
41         web => 'https://github.com/karenetheridge/JSON-MaybeXS',
42         type => 'git',
43       },
44       bugtracker => {
45         mailto => 'bug-JSON-MaybeXS@rt.cpan.org',
46         web => 'https://rt.cpan.org/Public/Dist/Display.html?Name=JSON-MaybeXS',
47       },
48     },
49   },
50
51   realclean => { FILES => 'Distar/' },
52 );
53
54 my $eumm_version  = eval $ExtUtils::MakeMaker::VERSION;
55
56 for (qw(configure build test runtime)) {
57   my $key = $_ eq 'runtime' ? 'PREREQ_PM' : uc $_.'_REQUIRES';
58   next unless exists $WriteMakefileArgs{META_ADD}{prereqs}{$_}
59            or exists $WriteMakefileArgs{$key};
60   my $r = $WriteMakefileArgs{$key} = {
61     %{$WriteMakefileArgs{META_ADD}{prereqs}{$_}{requires} || {}},
62     %{delete $WriteMakefileArgs{$key} || {}},
63   };
64   defined $r->{$_} or delete $r->{$_} for keys %$r;
65 }
66
67 # we require Cpanel::JSON::XS, except if JSON::XS is already installed.
68 # (we also always recommend Cpanel::JSON::XS, just to make sure.)
69 $WriteMakefileArgs{PREREQ_PM}{'Cpanel::JSON::XS'} = '2.3310'
70     if can_xs() && !eval { require JSON::XS; 1; };
71
72 $WriteMakefileArgs{MIN_PERL_VERSION} = delete $WriteMakefileArgs{PREREQ_PM}{perl} || 0;
73
74 $WriteMakefileArgs{BUILD_REQUIRES} = {
75     %{$WriteMakefileArgs{BUILD_REQUIRES} || {}},
76     %{delete $WriteMakefileArgs{TEST_REQUIRES}}
77 } if $eumm_version < 6.63_03;
78
79 $WriteMakefileArgs{PREREQ_PM} = {
80     %{$WriteMakefileArgs{PREREQ_PM}},
81     %{delete $WriteMakefileArgs{BUILD_REQUIRES}}
82 } if $eumm_version < 6.55_01;
83
84 delete $WriteMakefileArgs{CONFIGURE_REQUIRES}
85   if $eumm_version < 6.51_03;
86
87 WriteMakefile(%WriteMakefileArgs);
88
89 # can we locate a (the) C compiler
90 sub can_cc {
91   my @chunks = split(/ /, $Config::Config{cc}) or return;
92
93   # $Config{cc} may contain args; try to find out the program part
94   while (@chunks) {
95     return can_run("@chunks") || (pop(@chunks), next);
96   }
97
98   return;
99 }
100
101 # check if we can run some command
102 sub can_run {
103   my ($cmd) = @_;
104
105   return $cmd if -x $cmd;
106   if (my $found_cmd = MM->maybe_command($cmd)) {
107     return $found_cmd;
108   }
109
110   require File::Spec;
111   for my $dir ((split /$Config::Config{path_sep}/, $ENV{PATH}), '.') {
112     next if $dir eq '';
113     my $abs = File::Spec->catfile($dir, $cmd);
114     return $abs if (-x $abs or $abs = MM->maybe_command($abs));
115   }
116
117   return;
118 }
119
120 # Can our C compiler environment build XS files
121 sub can_xs {
122   # Do we have the configure_requires checker?
123   local $@;
124   eval "require ExtUtils::CBuilder; ExtUtils::CBuilder->VERSION(0.27)";
125   if ( $@ ) {
126     # They don't obey configure_requires, so it is
127     # someone old and delicate. Try to avoid hurting
128     # them by falling back to an older simpler test.
129     return can_cc();
130   }
131
132   # Do we have a working C compiler
133   my $builder = ExtUtils::CBuilder->new(
134     quiet => 1,
135   );
136   unless ( $builder->have_compiler ) {
137     # No working C compiler
138     return 0;
139   }
140
141   # Write a C file representative of what XS becomes
142   require File::Temp;
143   my ( $FH, $tmpfile ) = File::Temp::tempfile(
144     "compilexs-XXXXX",
145     SUFFIX => '.c',
146   );
147   binmode $FH;
148   print $FH <<'END_C';
149 #include "EXTERN.h"
150 #include "perl.h"
151 #include "XSUB.h"
152
153 int main(int argc, char **argv) {
154     return 0;
155 }
156
157 int boot_sanexs() {
158     return 1;
159 }
160
161 END_C
162   close $FH;
163
164   # Can the C compiler access the same headers XS does
165   my @libs   = ();
166   my $object = undef;
167   eval {
168     local $^W = 0;
169     $object = $builder->compile(
170       source => $tmpfile,
171     );
172     @libs = $builder->link(
173       objects     => $object,
174       module_name => 'sanexs',
175     );
176   };
177   my $result = $@ ? 0 : 1;
178
179   # Clean up all the build files
180   foreach ( $tmpfile, $object, @libs ) {
181     next unless defined $_;
182     1 while unlink;
183   }
184
185   return $result;
186 }