"make realclean" also cleans out Distar/
[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   realclean => { FILES => 'Distar/' },
36 );
37
38 # can we locate a (the) C compiler
39 sub can_cc {
40   my @chunks = split(/ /, $Config::Config{cc}) or return;
41
42   # $Config{cc} may contain args; try to find out the program part
43   while (@chunks) {
44     return can_run("@chunks") || (pop(@chunks), next);
45   }
46
47   return;
48 }
49
50 # check if we can run some command
51 sub can_run {
52   my ($cmd) = @_;
53
54   return $cmd if -x $cmd;
55   if (my $found_cmd = MM->maybe_command($cmd)) {
56     return $found_cmd;
57   }
58
59   require File::Spec;
60   for my $dir ((split /$Config::Config{path_sep}/, $ENV{PATH}), '.') {
61     next if $dir eq '';
62     my $abs = File::Spec->catfile($dir, $cmd);
63     return $abs if (-x $abs or $abs = MM->maybe_command($abs));
64   }
65
66   return;
67 }
68
69 # Can our C compiler environment build XS files
70 sub can_xs {
71   # Do we have the configure_requires checker?
72   local $@;
73   eval "require ExtUtils::CBuilder; ExtUtils::CBuilder->VERSION(0.27)";
74   if ( $@ ) {
75     # They don't obey configure_requires, so it is
76     # someone old and delicate. Try to avoid hurting
77     # them by falling back to an older simpler test.
78     return can_cc();
79   }
80
81   # Do we have a working C compiler
82   my $builder = ExtUtils::CBuilder->new(
83     quiet => 1,
84   );
85   unless ( $builder->have_compiler ) {
86     # No working C compiler
87     return 0;
88   }
89
90   # Write a C file representative of what XS becomes
91   require File::Temp;
92   my ( $FH, $tmpfile ) = File::Temp::tempfile(
93     "compilexs-XXXXX",
94     SUFFIX => '.c',
95   );
96   binmode $FH;
97   print $FH <<'END_C';
98 #include "EXTERN.h"
99 #include "perl.h"
100 #include "XSUB.h"
101
102 int main(int argc, char **argv) {
103     return 0;
104 }
105
106 int boot_sanexs() {
107     return 1;
108 }
109
110 END_C
111   close $FH;
112
113   # Can the C compiler access the same headers XS does
114   my @libs   = ();
115   my $object = undef;
116   eval {
117     local $^W = 0;
118     $object = $builder->compile(
119       source => $tmpfile,
120     );
121     @libs = $builder->link(
122       objects     => $object,
123       module_name => 'sanexs',
124     );
125   };
126   my $result = $@ ? 0 : 1;
127
128   # Clean up all the build files
129   foreach ( $tmpfile, $object, @libs ) {
130     next unless defined $_;
131     1 while unlink;
132   }
133
134   return $result;
135 }