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