1# The perl/C checking voodoo is stolen from Graham Barr's
2# Scalar-List-Utils distribution.
3use 5.006;
4
5use strict;
6use warnings;
7
8use ExtUtils::MakeMaker;
9use Config qw(%Config);
10use File::Spec;
11
12my $no_xs;
13my $force_xs;
14for (@ARGV)
15{
16    /^--pm/ and $no_xs = 1;
17    /^--xs/ and $no_xs = 0;
18}
19
20if ($no_xs)
21{
22    write_makefile();
23    exit;
24}
25
26unless (defined $no_xs)
27{
28    check_for_compiler()
29        or no_cc();
30
31    if ( -d '.svn' )
32    {
33        local *DIR;
34        opendir DIR, "t" or die "Cannot read t: $!";
35
36        foreach my $file ( grep { /^\d.+\.t$/ } readdir DIR )
37        {
38            next if $file =~ /^99/;
39
40            my $real_file = File::Spec->catfile( 't', $file );
41
42            local *F;
43            open F, "<$real_file" or die "Cannot read $real_file: $!";
44
45            my $shbang = <F>;
46            my $test = do { local $/; <F> };
47
48            close F;
49
50            $test = "$shbang\nBEGIN { \$ENV{PV_TEST_PERL} = 1 }\n\n$test";
51
52            my $new_file = File::Spec->catfile( 't', "zz_$file" );
53            open F, ">$new_file" or die "Cannot write $new_file: $!";
54
55            print F $test;
56
57            close F;
58        }
59    }
60}
61
62write_makefile();
63
64sub write_makefile
65{
66    my %prereq =
67        ( 'Test::More' => 0,
68          'Scalar::Util' => 0,
69        );
70    $prereq{'Attribute::Handlers'} = 0 if $] >= 5.006;
71
72    my $zz = join ' ', glob File::Spec->catfile( 't', 'zz_*.t' );
73
74    my $ccflags = -d '.svn' ? '-Wall' : '';
75
76    WriteMakefile( VERSION_FROM  => "lib/Params/Validate.pm",
77                   NAME          => "Params::Validate",
78                   PREREQ_PM     => \%prereq,
79                   CONFIGURE     => \&init,
80                   CCFLAGS       => $ccflags,
81                   clean         => { FILES => "test.c test.o $zz" },
82                   ABSTRACT_FROM => 'lib/Params/Validate.pm',
83                   AUTHOR        => 'Dave Rolsky, <autarch@urth.org>',
84                   LICENSE       => 'perl',
85                 );
86}
87
88sub init
89{
90    my $hash = $_[1];
91
92    if ($no_xs)
93    {
94        @{ $hash }{ 'XS', 'C' } = ( {}, [] );
95    }
96
97    $hash;
98}
99
100sub no_cc
101{
102    $no_xs = 1;
103    print <<'EOF';
104
105 I cannot determine if you have a C compiler
106 so I will install a perl-only implementation
107
108 You can force installation of the XS version with
109
110    perl Makefile.PL --xs
111
112EOF
113
114    write_makefile();
115    exit;
116}
117
118sub check_for_compiler
119{
120    print "Testing if you have a C compiler\n";
121
122    eval { require ExtUtils::CBuilder };
123    if ($@)
124    {
125        return _check_for_compiler_manually();
126    }
127    else
128    {
129        return _check_for_compiler_with_cbuilder();
130    }
131}
132
133sub _check_for_compiler_with_cbuilder
134{
135    my $cb = ExtUtils::CBuilder->new( quiet => 1 );
136
137    return $cb->have_compiler;
138}
139
140sub _check_for_compiler_manually
141{
142    unless ( open F, ">test.c" )
143    {
144        warn "Cannot write test.c, skipping test compilation and installing pure Perl version.\n";
145        return 0;
146    }
147
148    print F <<'EOF';
149int main() { return 0; }
150EOF
151
152    close F or return 0;
153
154    my $cc = $Config{cc};
155    if ( $cc =~ /cl(\.exe)?$/ )
156    {
157        # stupid stupid MSVC compiler hack tacken from version.pm's
158        # Makefile.PL
159        $cc .= ' -c'; # prevent it from calling the linker
160    }
161
162    system( "$cc -o test$Config{obj_ext} test.c" ) and return 0;
163
164    unlink $_
165        for grep { -f } 'test.c', "test$Config{obj_ext}";
166
167    return 1;
168}
169