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