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