integrate Time::HiRes 1.56 from CPAN
[p5sagit/p5-mst-13.2.git] / ext / Time / HiRes / Makefile.PL
1
2 # See lib/ExtUtils/MakeMaker.pm for details of how to influence
3 # the contents of the Makefile that is written.
4 #
5
6 require 5.002;
7
8 use Config;
9 use ExtUtils::MakeMaker;
10 use strict;
11
12 my $VERBOSE = $ENV{VERBOSE};
13 my $DEFINE;
14 my $LIBS = [];
15 my $XSOPT;
16
17 use vars qw($self); # Used in 'sourcing' the hints.
18
19 my $ld_exeext = ($^O eq 'os2' and $Config{ldflags} =~ /-Zexe\b/) ? '.exe' : '';
20
21 unless($ENV{PERL_CORE}) {
22     $ENV{PERL_CORE} = 1 if grep { $_ eq 'PERL_CORE=1' } @ARGV;
23 }
24
25 # Perls 5.002 and 5.003 did not have File::Spec, fake what we need.
26
27 sub my_dirsep {
28     $^O eq 'VMS' ? '.' :
29         $^O =~ /mswin32|netware|djgpp/i ? '\\' :
30             $^O eq 'MacOS' ? ':'
31                 : '/';
32 }
33
34 sub my_catdir {
35     shift;
36     my $catdir = join(my_dirsep, @_);
37     $^O eq 'VMS' ? "[$catdir]" : $catdir;
38 }
39
40 sub my_catfile {
41     shift;
42     return join(my_dirsep, @_) unless $^O eq 'VMS';
43     my $file = pop;
44     return my_catdir (undef, @_) . $file;
45 }
46
47 sub my_updir {
48     shift;
49     $^O eq 'VMS' ? "-" : "..";
50 }
51
52 BEGIN {
53     eval { require File::Spec };
54     if ($@) {
55         *File::Spec::catdir  = \&my_catdir;
56         *File::Spec::updir   = \&my_updir;
57         *File::Spec::catfile = \&my_catfile;
58     }
59 }
60
61 # Avoid 'used only once' warnings.
62 my $nop1 = *File::Spec::catdir;
63 my $nop2 = *File::Spec::updir;
64 my $nop3 = *File::Spec::catfile;
65
66 # if you have 5.004_03 (and some slightly older versions?), xsubpp
67 # tries to generate line numbers in the C code generated from the .xs.
68 # unfortunately, it is a little buggy around #ifdef'd code.
69 # my choice is leave it in and have people with old perls complain
70 # about the "Usage" bug, or leave it out and be unable to compile myself
71 # without changing it, and then I'd always forget to change it before a
72 # release. Sorry, Edward :)
73
74 sub try_compile_and_link {
75     my ($c, %args) = @_;
76
77     my ($ok) = 0;
78     my ($tmp) = "tmp$$";
79     local(*TMPC);
80
81     my $obj_ext = $Config{obj_ext} || ".o";
82     unlink("$tmp.c", "$tmp$obj_ext");
83
84     if (open(TMPC, ">$tmp.c")) {
85         print TMPC $c;
86         close(TMPC);
87
88         my $cccmd = $args{cccmd};
89
90         my $errornull;
91
92         my $COREincdir;
93
94         if ($ENV{PERL_CORE}) {
95             my $updir = File::Spec->updir;
96             $COREincdir = File::Spec->catdir(($updir) x 3);
97         } else {
98             $COREincdir = File::Spec->catdir($Config{'archlibexp'}, 'CORE');
99         }
100
101         my $ccflags = $Config{'ccflags'} . ' ' . "-I$COREincdir";
102
103         if ($^O eq 'VMS') {
104             if ($ENV{PERL_CORE}) {
105                 # Fragile if the extensions change hierachy within
106                 # the Perl core but this should do for now.
107                 $cccmd = "$Config{'cc'} /include=([---]) $tmp.c";
108             } else {
109                 my $perl_core = $Config{'installarchlib'};
110                 $perl_core =~ s/\]$/.CORE]/;
111                 $cccmd = "$Config{'cc'} /include=(perl_root:[000000],$perl_core) $tmp.c";
112             }
113         }
114
115         if ($args{silent} || !$VERBOSE) {
116             $errornull = "2>/dev/null" unless defined $errornull;
117         } else {
118             $errornull = '';
119         }
120
121         $cccmd = "$Config{'cc'} -o $tmp $ccflags $tmp.c @$LIBS $errornull"
122             unless defined $cccmd;
123
124        if ($^O eq 'VMS') {
125             open( CMDFILE, ">$tmp.com" );
126             print CMDFILE "\$ SET MESSAGE/NOFACILITY/NOSEVERITY/NOIDENT/NOTEXT\n";
127             print CMDFILE "\$ $cccmd\n";
128             print CMDFILE "\$ IF \$SEVERITY .NE. 1 THEN EXIT 44\n"; # escalate
129             close CMDFILE;
130             system("\@ $tmp.com");
131             $ok = $?==0;
132             for ("$tmp.c", "$tmp$obj_ext", "$tmp.com", "$tmp$Config{exe_ext}") {
133                 1 while unlink $_;
134             }
135         }
136         else
137         {
138             my $tmp_exe = "$tmp$ld_exeext";
139             printf "cccmd = $cccmd\n" if $VERBOSE;
140             my $res = system($cccmd);
141             $ok = defined($res) && $res==0 && -s $tmp_exe && -x _;
142             unlink("$tmp.c", $tmp_exe);
143         }
144     }
145
146     $ok;
147 }
148
149 sub has_gettimeofday {
150     # confusing but true (if condition true ==> -DHAS_GETTIMEOFDAY already)
151     return 0 if $Config{d_gettimeod} eq 'define';
152     return 1 if try_compile_and_link(<<EOM);
153 #include "EXTERN.h"
154 #include "perl.h"
155 #include "XSUB.h"
156 #ifdef I_SYS_TYPES
157 #   include <sys/types.h>
158 #endif
159
160 #ifdef I_SYS_TIME
161 #   include <sys/time.h>
162 #endif
163
164 #ifdef I_SYS_SELECT
165 #   include <sys/select.h>      /* struct timeval might be hidden in here */
166 #endif
167 static int foo()
168 {
169     struct timeval tv;
170     gettimeofday(&tv, 0);
171 }
172 int main _((int argc, char** argv, char** env))
173 {
174     foo();
175 }
176 EOM
177     return 0;
178 }
179
180 sub has_x {
181     my ($x, %args) = @_;
182
183     return 1 if
184     try_compile_and_link(<<EOM, %args);
185 #include "EXTERN.h"
186 #include "perl.h"
187 #include "XSUB.h"
188
189 #ifdef I_UNISTD
190 #   include <unistd.h>
191 #endif
192
193 #ifdef I_SYS_TYPES
194 #   include <sys/types.h>
195 #endif
196
197 #ifdef I_SYS_TIME
198 #   include <sys/time.h>
199 #endif
200
201 int main _((int argc, char** argv, char** env))
202 {
203         $x;
204 }
205 EOM
206     return 0;
207 }
208
209 sub init {
210     my $hints = File::Spec->catfile("hints", "$^O.pl");
211     if (-f $hints) {
212         print "Using hints $hints...\n";
213         local $self;
214         do $hints;
215         if (exists $self->{LIBS}) {
216             $LIBS = $self->{LIBS};
217             print "Extra libraries: @$LIBS...\n";
218         }
219     }
220
221     $DEFINE = '';
222
223     print "Looking for gettimeofday()... ";
224     my $has_gettimeofday;
225     if (exists $Config{d_gettimeod}) {
226         $has_gettimeofday++ if $Config{d_gettimeod};
227     } elsif (has_gettimeofday()) {
228         $DEFINE .= ' -DHAS_GETTIMEOFDAY';
229         $has_gettimeofday++;
230     }
231
232     if ($has_gettimeofday) {
233         print "found.\n";
234     } else {
235         die <<EOD
236 Your operating system does not seem to have the gettimeofday() function.
237 (or, at least, I cannot find it)
238
239 There is no way Time::HiRes is going to work.
240
241 I am awfully sorry but I cannot go further.
242
243 Aborting configuration.
244
245 EOD
246     }
247
248     print "Looking for setitimer()... ";
249     my $has_setitimer;
250     if (exists $Config{d_setitimer}) {
251         $has_setitimer++ if $Config{d_setitimer};
252     } elsif (has_x("setitimer(ITIMER_REAL, 0, 0)")) {
253         $has_setitimer++;
254         $DEFINE .= ' -DHAS_SETITIMER';
255     }
256
257     if ($has_setitimer) {
258         print "found.\n";
259     } else {
260         print "NOT found.\n";
261     }
262
263     print "Looking for getitimer()... ";
264     my $has_getitimer;
265     if (exists $Config{'d_getitimer'}) {
266         $has_getitimer++ if $Config{'d_getitimer'};
267     } elsif (has_x("getitimer(ITIMER_REAL, 0)")) {
268         $has_getitimer++;
269         $DEFINE .= ' -DHAS_GETITIMER';
270     }
271
272     if ($has_getitimer) {
273         print "found.\n";
274     } else {
275         print "NOT found.\n";
276     }
277
278     if ($has_setitimer && $has_getitimer) {
279         print "You have interval timers (both setitimer and setitimer).\n";
280     } else {
281         print "You do not have interval timers.\n";
282     }
283
284     print "Looking for ualarm()... ";
285     my $has_ualarm;
286     if (exists $Config{d_ualarm}) {
287         $has_ualarm++ if $Config{d_ualarm};
288     } elsif (has_x ("ualarm (0, 0)")) {
289         $has_ualarm++;
290         $DEFINE .= ' -DHAS_UALARM';
291     }
292
293     if ($has_ualarm) {
294         print "found.\n";
295     } else {
296         print "NOT found.\n";
297         if ($has_setitimer) {
298             print "But you have setitimer().\n";
299             print "We can make a Time::HiRes::ualarm().\n";
300         }
301     }
302
303     print "Looking for usleep()... ";
304     my $has_usleep;
305     if (exists $Config{d_usleep}) {
306         $has_usleep++ if $Config{d_usleep};
307     } elsif (has_x ("usleep (0)")) {
308         $has_usleep++;
309         $DEFINE .= ' -DHAS_USLEEP';
310     }
311
312     if ($has_usleep) {
313         print "found.\n";
314     } else {
315         print "NOT found.\n";
316         print "Let's see if you have select()... ";
317         if ($Config{'d_select'} eq 'define') {
318             print "found.\n";
319             print "We can make a Time::HiRes::usleep().\n";
320         } else {
321             print "NOT found.\n";
322             print "You won't have a Time::HiRes::usleep().\n";
323         }
324     }
325
326     print "Looking for nanosleep()... ";
327     my $has_nanosleep;
328     if (exists $Config{d_nanosleep}) {
329         if ($Config{d_nanosleep}) {
330             $has_nanosleep++;
331             $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
332         }
333     } elsif ($^O ne 'mpeix' && # MPE/iX falsely finds nanosleep.
334              has_x ("nanosleep (NULL, NULL)")) {
335         $has_nanosleep++;
336         $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
337     }
338
339     if ($has_nanosleep) {
340         print "found.\n";
341         print "You can mix subsecond sleeps with signals.\n";
342     } else {
343         print "NOT found.\n";
344         my $nt = ($^O eq 'os2' ? '' : 'not');
345         print "You can$nt mix subsecond sleeps with signals.\n";
346     }
347
348     if ($DEFINE) {
349         $DEFINE =~ s/^\s+//;
350         if (open(XDEFINE, ">xdefine")) {
351             print XDEFINE $DEFINE, "\n";
352             close(XDEFINE);
353         }
354     }
355 }
356
357 sub doMakefile {
358     my @makefileopts = ();
359
360     if ($] >= 5.005) {
361         push (@makefileopts,
362             'AUTHOR'    => 'Jarkko Hietaniemi <jhi@iki.fi>',
363             'ABSTRACT_FROM' => 'HiRes.pm',
364         );
365         $DEFINE .= " -DATLEASTFIVEOHOHFIVE";
366     }
367
368     push (@makefileopts,
369         'NAME'  => 'Time::HiRes',
370         'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
371         'LIBS'  => $LIBS,   # e.g., '-lm'
372         'DEFINE'        => $DEFINE,     # e.g., '-DHAS_SOMETHING'
373         'XSOPT' => $XSOPT,
374     # do not even think about 'INC' => '-I/usr/ucbinclude', Solaris will avenge.
375         'INC'   => '',     # e.g., '-I/usr/include/other'
376         'INSTALLDIRS' => 'perl',
377         'dist'      => {
378             'CI'       => 'ci -l',
379             'COMPRESS' => 'gzip -9f',
380             'SUFFIX'   => 'gz',
381         },
382         clean => { FILES => "xdefine" },
383         realclean => { FILES=> 'const-c.inc const-xs.inc' },
384     );
385
386     if ($ENV{PERL_CORE}) {
387         push @makefileopts, MAN3PODS => {};
388     }
389
390     WriteMakefile(@makefileopts);
391 }
392
393 sub doConstants {
394     if (eval {require ExtUtils::Constant; 1}) {
395         my @names = (qw(ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF
396                         ITIMER_REALPROF));
397         foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
398                      d_nanosleep)) {
399             my $macro = $_;
400             if ($macro eq 'd_nanosleep') {
401                 $macro =~ s/d_(.*)/TIME_HIRES_\U$1/;
402             } else {
403                 $macro =~ s/d_(.*)/HAS_\U$1/;
404             }
405             push @names, {name => $_, macro => $macro, value => 1,
406                           default => ["IV", "0"]};
407         }
408         ExtUtils::Constant::WriteConstants(
409                                            NAME => 'Time::HiRes',
410                                            NAMES => \@names,
411                                           );
412     } else {
413         foreach my $file ('const-c.inc', 'const-xs.inc') {
414             my $fallback = File::Spec->catfile('fallback', $file);
415             local $/;
416             open IN, "<$fallback" or die "Can't open $fallback: $!";
417             open OUT, ">$file" or die "Can't open $file: $!";
418             print OUT <IN> or die $!;
419             close OUT or die "Can't close $file: $!";
420             close IN or die "Can't close $fallback: $!";
421         }
422     }
423 }
424
425 sub main {
426     print "Configuring Time::HiRes...\n";
427
428     if ($^O =~ /Win32/i) {
429       $DEFINE = '-DSELECT_IS_BROKEN';
430       $LIBS = [];
431     } else {
432       init();
433     }
434     doMakefile;
435     doConstants;
436     my $make = $Config{'make'} || "make";
437     unless ($ENV{PERL_CORE}) {
438         print  <<EOM;
439 Now you may issue '$make'.  Do not forget also '$make test'.
440
441 EOM
442        if ($ENV{LC_ALL}   =~ /utf-?8/i ||
443            $ENV{LC_CTYPE} =~ /utf-?8/i ||
444            $ENV{LANG}     =~ /utf-?8/i) {
445             print  <<EOM;
446 NOTE: if you get an error like this (the line number may vary):
447 Makefile:91: *** missing separator
448 then set the environment variable LC_ALL to "C" and retry
449 from scratch (re-run perl "Makefile.PL").
450
451 EOM
452         }
453     }
454 }
455
456 &main;
457
458 # EOF