1# This Makefile.PL stolen from Params::Validate
2#
3# The perl/C checking voodoo is stolen from Graham Barr's
4# Scalar-List-Utils distribution.
5
6use strict;
7
8use Config qw(%Config);
9use ExtUtils::MakeMaker;
10use File::Temp qw/tempdir tempfile/;
11
12if ($] < 5.006002) {
13    die <<'EOL'
14Perl 5.006002 required.  Please install version-0.9906 if you need
15to support an earlier Perl release.
16EOL
17}
18
19my ($no_xs, $force_xs);
20
21unlink 'pm_to_blib'; # belts and braces
22
23if ($ENV{PERL_ONLY}) {
24    $no_xs = 1;
25}
26
27for (@ARGV)
28{
29    /^--perl_only/ and $no_xs = 1;
30    /^--perl-only/ and $no_xs = 1;
31    /^--xs/ and $force_xs = 1;
32}
33
34if ($] < 5.010) { # support pure Perl only
35    $no_xs = 1;
36}
37
38unless (defined $no_xs or $force_xs)
39{
40    check_for_compiler()
41        or no_cc();
42
43}
44
45write_makefile();
46
47sub write_makefile
48{
49    my %test_prereq = (
50        'Test::More'            => 0.45,
51        'File::Temp'            => 0.13,
52        'base'                  => 0,
53    );
54
55    WriteMakefile( VERSION         => '0.9929',
56                   NAME            => 'version',
57                   LICENSE         => 'perl',
58                   MIN_PERL_VERSION=> 5.006002,
59                   TEST_REQUIRES   => \%test_prereq,
60                   NORECURS        => $no_xs,
61                   ABSTRACT    => 'Structured version objects',
62                   AUTHOR      => 'John Peacock <jpeacock@cpan.org>',
63                   ( $] >= 5.009001 && $] < 5.011000 ?
64                     ( INSTALLDIRS => 'perl' ) :
65                     ()
66                   ),
67		   ( ($] < 5.012
68		       && ! $ENV{PERL_NO_HIGHLANDER}
69		       && ! ( $ENV{PERL_MM_OPT}
70			   && $ENV{PERL_MM_OPT} =~ /(?:INSTALL_BASE|PREFIX)/ )
71		       && ! grep { /INSTALL_BASE/ || /PREFIX/ } @ARGV ) ?
72		     ( UNINST => 1 ) :
73		     ()
74		   ),
75                   PM              =>
76                       {'lib/version.pm'  => '$(INST_LIBDIR)/version.pm',
77                        'lib/version.pod' => '$(INST_LIBDIR)/version.pod',
78                        'lib/version/regex.pm'  =>
79                            '$(INST_LIBDIR)/version/regex.pm',
80                        'lib/version/Internals.pod' =>
81                            '$(INST_LIBDIR)/version/Internals.pod',
82                        'vperl/vpp.pm' => '$(INST_LIBDIR)/version/vpp.pm',
83                        },
84                   PL_FILES        => {},
85                   C               => [],
86		   ( $no_xs ?
87		       () :
88		       ( DIR       => ['vutil'])
89		   ),
90                   dist            => {
91                       COMPRESS => 'gzip -9f',
92                       SUFFIX => 'gz',
93                   },
94                   META_MERGE => {
95                       "meta-spec" => { version => 2 },
96                       resources => {
97                           repository => {
98                               type => 'git',
99                               url => 'git://github.com/Perl/version.pm.git',
100                               web  => 'https://github.com/Perl/version.pm',
101                           },
102                           bugtracker => {
103                               web => 'https://rt.cpan.org/Public/Dist/Display.html?Name=version',
104                               mailto => 'bug-version@rt.cpan.org',
105                           },
106                       },
107                       no_index => {
108                           package => ['charstar'],
109                       },
110                   },
111                 );
112}
113
114sub no_cc
115{
116    $no_xs = 1;
117    print <<'EOF';
118
119 I cannot determine if you have a C compiler
120 so I will install a perl-only implementation
121
122 You can force installation of the XS version with
123
124    perl Makefile.PL --xs
125
126EOF
127
128}
129
130sub check_for_compiler
131{
132    # IMPORTANT NOTE:  This is NOT used to determine how to compile
133    # extensions properly; EU::MM does that for us.  This is only
134    # intended to see if that is likely to succeed.  We do not try
135    # to do anything here except compile (not even link).  If you
136    # want this to be a full featured test, feel free to submit a
137    # patch or do something useful.
138
139    print "Testing if you have a C compiler\n";
140
141    eval { require ExtUtils::CBuilder };
142    if ($@)
143    {
144        return _check_for_compiler_manually();
145    }
146    else
147    {
148        return _check_for_compiler_with_cbuilder();
149    }
150}
151
152sub _check_for_compiler_with_cbuilder
153{
154    my $cb = ExtUtils::CBuilder->new( quiet => 1 );
155
156    return $cb->have_compiler;
157}
158
159sub _check_for_compiler_manually
160{
161    unless ( open F, ">test.c" )
162    {
163        warn "Cannot write test.c, skipping test compilation and installing pure Perl version.\n";
164        return 0;
165    }
166
167    print F <<'EOF';
168int main() { return 0; }
169EOF
170
171    close F or return 0;
172
173    my ($cc, $ccflags, $obj_ext) = map { $Config{$_} } qw/cc ccflags obj_ext/;
174
175    my $command;
176    if ($^O =~ /(dos|win32)/i && $Config{'cc'} =~ /^cl/) {
177        $command = "$cc $ccflags /c test.c";
178    }
179    elsif ($Config{gccversion}) {
180        $command = "$cc $ccflags -o test$obj_ext test.c";
181    }
182    else {
183        warn "Unsupported system: can't test compiler availability. Patches welcome...";
184        return 0;
185    }
186
187    my $retval = system( $command );
188    map { unlink $_ if -f $_ } ('test.c',"test$obj_ext");
189
190    return not($retval); # system returns -1
191}
192