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