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