Upgrade to Time::HiRes 1.83
[p5sagit/p5-mst-13.2.git] / ext / Time / HiRes / Makefile.PL
1 #!/usr/bin/perl
2 #
3 # In general we trust %Config, but for nanosleep() this trust
4 # may be misplaced (it may be linkable but not really functional).
5 # Use $ENV{FORCE_NANOSLEEP_SCAN} to force rescanning whether there
6 # really is hope.
7
8 require 5.002;
9
10 use Config;
11 use ExtUtils::MakeMaker;
12 use strict;
13
14 my $VERBOSE = $ENV{VERBOSE};
15 my $DEFINE;
16 my $LIBS = [];
17 my $XSOPT = '';
18 my $SYSCALL_H;
19
20 use vars qw($self); # Used in 'sourcing' the hints.
21
22 my $ld_exeext = ($^O eq 'cygwin' ||
23                  $^O eq 'os2' && $Config{ldflags} =~ /-Zexe\b/) ? '.exe' : '';
24
25 unless($ENV{PERL_CORE}) {
26     $ENV{PERL_CORE} = 1 if grep { $_ eq 'PERL_CORE=1' } @ARGV;
27 }
28
29 # Perls 5.002 and 5.003 did not have File::Spec, fake what we need.
30
31 sub my_dirsep {
32     $^O eq 'VMS' ? '.' :
33         $^O =~ /mswin32|netware|djgpp/i ? '\\' :
34             $^O eq 'MacOS' ? ':'
35                 : '/';
36 }
37
38 sub my_catdir {
39     shift;
40     my $catdir = join(my_dirsep, @_);
41     $^O eq 'VMS' ? "[$catdir]" : $catdir;
42 }
43
44 sub my_catfile {
45     shift;
46     return join(my_dirsep, @_) unless $^O eq 'VMS';
47     my $file = pop;
48     return my_catdir (undef, @_) . $file;
49 }
50
51 sub my_updir {
52     shift;
53     $^O eq 'VMS' ? "-" : "..";
54 }
55
56 BEGIN {
57     eval { require File::Spec };
58     if ($@) {
59         *File::Spec::catdir  = \&my_catdir;
60         *File::Spec::updir   = \&my_updir;
61         *File::Spec::catfile = \&my_catfile;
62     }
63 }
64
65 # Avoid 'used only once' warnings.
66 my $nop1 = *File::Spec::catdir;
67 my $nop2 = *File::Spec::updir;
68 my $nop3 = *File::Spec::catfile;
69
70 # if you have 5.004_03 (and some slightly older versions?), xsubpp
71 # tries to generate line numbers in the C code generated from the .xs.
72 # unfortunately, it is a little buggy around #ifdef'd code.
73 # my choice is leave it in and have people with old perls complain
74 # about the "Usage" bug, or leave it out and be unable to compile myself
75 # without changing it, and then I'd always forget to change it before a
76 # release. Sorry, Edward :)
77
78 sub try_compile_and_link {
79     my ($c, %args) = @_;
80
81     my ($ok) = 0;
82     my ($tmp) = "tmp$$";
83     local(*TMPC);
84
85     my $obj_ext = $Config{obj_ext} || ".o";
86     unlink("$tmp.c", "$tmp$obj_ext");
87
88     if (open(TMPC, ">$tmp.c")) {
89         print TMPC $c;
90         close(TMPC);
91
92         my $cccmd = $args{cccmd};
93
94         my $errornull;
95
96         my $COREincdir;
97
98         if ($ENV{PERL_CORE}) {
99             my $updir = File::Spec->updir;
100             $COREincdir = File::Spec->catdir(($updir) x 3);
101         } else {
102             $COREincdir = File::Spec->catdir($Config{'archlibexp'}, 'CORE');
103         }
104
105         my $ccflags = $Config{'ccflags'} . ' ' . "-I$COREincdir";
106
107         if ($^O eq 'VMS') {
108             if ($ENV{PERL_CORE}) {
109                 # Fragile if the extensions change hierarchy within
110                 # the Perl core but this should do for now.
111                 $cccmd = "$Config{'cc'} /include=([---]) $tmp.c";
112             } else {
113                 my $perl_core = $Config{'installarchlib'};
114                 $perl_core =~ s/\]$/.CORE]/;
115                 $cccmd = "$Config{'cc'} /include=(perl_root:[000000],$perl_core) $tmp.c";
116             }
117         }
118
119         if ($args{silent} || !$VERBOSE) {
120             $errornull = "2>/dev/null" unless defined $errornull;
121         } else {
122             $errornull = '';
123         }
124
125         $cccmd = "$Config{'cc'} -o $tmp $ccflags $tmp.c @$LIBS $errornull"
126             unless defined $cccmd;
127
128        if ($^O eq 'VMS') {
129             open( CMDFILE, ">$tmp.com" );
130             print CMDFILE "\$ SET MESSAGE/NOFACILITY/NOSEVERITY/NOIDENT/NOTEXT\n";
131             print CMDFILE "\$ $cccmd\n";
132             print CMDFILE "\$ IF \$SEVERITY .NE. 1 THEN EXIT 44\n"; # escalate
133             close CMDFILE;
134             system("\@ $tmp.com");
135             $ok = $?==0;
136             for ("$tmp.c", "$tmp$obj_ext", "$tmp.com", "$tmp$Config{exe_ext}") {
137                 1 while unlink $_;
138             }
139         }
140         else
141         {
142             my $tmp_exe = "$tmp$ld_exeext";
143             printf "cccmd = $cccmd\n" if $VERBOSE;
144             my $res = system($cccmd);
145             $ok = defined($res) && $res == 0 && -s $tmp_exe && -x _;
146
147             if ( $ok && exists $args{run} && $args{run}) {
148                 my $tmp_exe =
149                     File::Spec->catfile(File::Spec->curdir, $tmp_exe);
150                 printf "Running $tmp_exe..." if $VERBOSE;
151                 if (system($tmp_exe) == 0) {
152                     $ok = 1;
153                 } else {
154                     $ok = 0;
155                     my $errno = $? >> 8;
156                     local $! = $errno;
157                     printf <<EOF;
158
159 *** The test run of '$tmp_exe' failed: status $?
160 *** (the status means: errno = $errno or '$!')
161 *** DO NOT PANIC: this just means that *some* functionality will be missing.
162 EOF
163                 }
164             }
165             unlink("$tmp.c", $tmp_exe);
166         }
167     }
168
169     return $ok;
170 }
171
172 sub has_gettimeofday {
173     # confusing but true (if condition true ==> -DHAS_GETTIMEOFDAY already)
174     return 0 if $Config{d_gettimeod};
175     return 1 if try_compile_and_link(<<EOM);
176 #include "EXTERN.h"
177 #include "perl.h"
178 #include "XSUB.h"
179 #ifdef I_SYS_TYPES
180 #   include <sys/types.h>
181 #endif
182
183 #ifdef I_SYS_TIME
184 #   include <sys/time.h>
185 #endif
186
187 #ifdef I_SYS_SELECT
188 #   include <sys/select.h>      /* struct timeval might be hidden in here */
189 #endif
190 static int foo()
191 {
192     struct timeval tv;
193     gettimeofday(&tv, 0);
194 }
195 int main _((int argc, char** argv, char** env))
196 {
197     foo();
198 }
199 EOM
200     return 0;
201 }
202
203 sub has_x {
204     my ($x, %args) = @_;
205
206     return 1 if
207     try_compile_and_link(<<EOM, %args);
208 #include "EXTERN.h"
209 #include "perl.h"
210 #include "XSUB.h"
211
212 #ifdef I_UNISTD
213 #   include <unistd.h>
214 #endif
215
216 #ifdef I_SYS_TYPES
217 #   include <sys/types.h>
218 #endif
219
220 #ifdef I_SYS_TIME
221 #   include <sys/time.h>
222 #endif
223
224 int main _((int argc, char** argv, char** env))
225 {
226         $x;
227 }
228 EOM
229     return 0;
230 }
231
232 sub has_nanosleep {
233     print "testing... ";
234     return 1 if
235     try_compile_and_link(<<EOM, run => 1);
236 #include <time.h>
237 #include <sys/time.h>
238 #include <stdio.h>
239 #include <stdlib.h>
240 #include <errno.h>
241
242 /* int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); */
243
244 int main() {
245     struct timespec ts1, ts2;
246     int ret;
247     ts1.tv_sec  = 0;
248     ts1.tv_nsec = 750000000;
249     ts2.tv_sec  = 0;
250     ts2.tv_nsec = 0;
251     errno = 0;
252     ret = nanosleep(&ts1, &ts2); /* E.g. in AIX nanosleep() fails and sets errno to ENOSYS. */
253     ret == 0 ? exit(0) : exit(errno ? errno : -1);
254 }
255 EOM
256 }
257
258 sub has_include {
259     my ($inc) = @_;
260     return 1 if
261     try_compile_and_link(<<EOM);
262 #include "EXTERN.h"
263 #include "perl.h"
264 #include "XSUB.h"
265
266 #include <$inc>
267 int main _((int argc, char** argv, char** env))
268 {
269         return 0;
270 }
271 EOM
272     return 0;
273 }
274
275 sub has_clock_x_syscall {
276     my $x = shift;
277     return 0 unless defined $SYSCALL_H;
278     return 1 if
279     try_compile_and_link(<<EOM, run => 1);
280 #include "EXTERN.h"
281 #include "perl.h"
282 #include "XSUB.h"
283 #include <$SYSCALL_H>
284 int main _((int argc, char** argv, char** env))
285 {
286     struct timespec ts;
287     /* Many Linuxes get ENOSYS even though the syscall exists. */
288     /* All implementations are supposed to support CLOCK_REALTIME. */
289     int ret = syscall(SYS_clock_$x, CLOCK_REALTIME, &ts);
290     ret == 0 ? exit(0) : exit(errno ? errno : -1);
291 }
292 EOM
293 }
294
295 sub has_clock_x {
296     my $x = shift;
297     return 1 if
298     try_compile_and_link(<<EOM, run => 1);
299 #include "EXTERN.h"
300 #include "perl.h"
301 #include "XSUB.h"
302 int main _((int argc, char** argv, char** env))
303 {
304     struct timespec ts;
305     int ret = clock_$x(CLOCK_REALTIME, &ts); /* Many Linuxes get ENOSYS. */
306     /* All implementations are supposed to support CLOCK_REALTIME. */
307     ret == 0 ? exit(0) : exit(errno ? errno : -1);
308 }
309 EOM
310 }
311
312 sub init {
313     my $hints = File::Spec->catfile("hints", "$^O.pl");
314     if (-f $hints) {
315         print "Using hints $hints...\n";
316         local $self;
317         do $hints;
318         if (exists $self->{LIBS}) {
319             $LIBS = $self->{LIBS};
320             print "Extra libraries: @$LIBS...\n";
321         }
322     }
323
324     $DEFINE = '';
325
326     if ($Config{d_syscall}) {
327         print "Have syscall()... looking for syscall.h... ";
328         if (has_include('syscall.h')) {
329             $SYSCALL_H = 'syscall.h';
330         } elsif (has_include('sys/syscall.h')) {
331             $SYSCALL_H = 'sys/syscall.h';
332         }
333     } else {
334         print "No syscall()...\n";
335     }
336
337     if ($Config{d_syscall}) {
338         if (defined $SYSCALL_H) {
339             print "found <$SYSCALL_H>.\n";
340         } else {
341             print "NOT found.\n";
342         }
343     }
344
345     print "Looking for gettimeofday()... ";
346     my $has_gettimeofday;
347     if (exists $Config{d_gettimeod}) {
348         $has_gettimeofday++ if $Config{d_gettimeod};
349     } elsif (has_gettimeofday()) {
350         $DEFINE .= ' -DHAS_GETTIMEOFDAY';
351         $has_gettimeofday++;
352     }
353
354     if ($has_gettimeofday) {
355         print "found.\n";
356     } else {
357         die <<EOD
358 Your operating system does not seem to have the gettimeofday() function.
359 (or, at least, I cannot find it)
360
361 There is no way Time::HiRes is going to work.
362
363 I am awfully sorry but I cannot go further.
364
365 Aborting configuration.
366
367 EOD
368     }
369
370     print "Looking for setitimer()... ";
371     my $has_setitimer;
372     if (exists $Config{d_setitimer}) {
373         $has_setitimer++ if $Config{d_setitimer};
374     } elsif (has_x("setitimer(ITIMER_REAL, 0, 0)")) {
375         $has_setitimer++;
376         $DEFINE .= ' -DHAS_SETITIMER';
377     }
378
379     if ($has_setitimer) {
380         print "found.\n";
381     } else {
382         print "NOT found.\n";
383     }
384
385     print "Looking for getitimer()... ";
386     my $has_getitimer;
387     if (exists $Config{'d_getitimer'}) {
388         $has_getitimer++ if $Config{'d_getitimer'};
389     } elsif (has_x("getitimer(ITIMER_REAL, 0)")) {
390         $has_getitimer++;
391         $DEFINE .= ' -DHAS_GETITIMER';
392     }
393
394     if ($has_getitimer) {
395         print "found.\n";
396     } else {
397         print "NOT found.\n";
398     }
399
400     if ($has_setitimer && $has_getitimer) {
401         print "You have interval timers (both setitimer and getitimer).\n";
402     } else {
403         print "You do not have interval timers.\n";
404     }
405
406     print "Looking for ualarm()... ";
407     my $has_ualarm;
408     if (exists $Config{d_ualarm}) {
409         $has_ualarm++ if $Config{d_ualarm};
410     } elsif (has_x ("ualarm (0, 0)")) {
411         $has_ualarm++;
412         $DEFINE .= ' -DHAS_UALARM';
413     }
414
415     if ($has_ualarm) {
416         print "found.\n";
417     } else {
418         print "NOT found.\n";
419         if ($has_setitimer) {
420             print "But you have setitimer().\n";
421             print "We can make a Time::HiRes::ualarm().\n";
422         }
423     }
424
425     print "Looking for usleep()... ";
426     my $has_usleep;
427     if (exists $Config{d_usleep}) {
428         $has_usleep++ if $Config{d_usleep};
429     } elsif (has_x ("usleep (0)")) {
430         $has_usleep++;
431         $DEFINE .= ' -DHAS_USLEEP';
432     }
433
434     if ($has_usleep) {
435         print "found.\n";
436     } else {
437         print "NOT found.\n";
438         print "Let's see if you have select()... ";
439         if ($Config{'d_select'}) {
440             print "found.\n";
441             print "We can make a Time::HiRes::usleep().\n";
442         } else {
443             print "NOT found.\n";
444             print "You won't have a Time::HiRes::usleep().\n";
445         }
446     }
447
448     print "Looking for nanosleep()... ";
449     my $has_nanosleep;
450     if ($ENV{FORCE_NANOSLEEP_SCAN}) {
451         print "forced scan... ";
452         if (has_nanosleep()) {
453             $has_nanosleep++;
454             $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
455         }
456     }
457     elsif (exists $Config{d_nanosleep}) {
458         print "believing \$Config{d_nanosleep}... ";
459         if ($Config{d_nanosleep}) {
460             $has_nanosleep++;
461             $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
462         }
463     } elsif ($^O =~ /^(mpeix)$/) {
464         # MPE/iX falsely finds nanosleep from its libc equivalent.
465         print "skipping because in $^O... ";
466     } else {
467         if (has_nanosleep()) {
468             $has_nanosleep++;
469             $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
470         }
471     }
472
473     if ($has_nanosleep) {
474         print "found.\n";
475         print "You can mix subsecond sleeps with signals, if you want to.\n";
476         print "(It's still not portable, though.)\n";
477     } else {
478         print "NOT found.\n";
479         my $nt = ($^O eq 'os2' ? '' : 'not');
480         print "You can$nt mix subsecond sleeps with signals.\n";
481         print "(It would not be portable anyway.)\n";
482     }
483
484     print "Looking for clock_gettime()... ";
485     my $has_clock_gettime;
486     if (exists $Config{d_clock_gettime}) {
487         $has_clock_gettime++ if $Config{d_clock_gettime}; # Unlikely...
488     } elsif (has_clock_x('gettime')) {
489         $has_clock_gettime++;
490         $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME';
491     } elsif (defined $SYSCALL_H && has_clock_x_syscall('gettime')) {
492         $has_clock_gettime++;
493         $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME -DTIME_HIRES_CLOCK_GETTIME_SYSCALL';
494     }
495
496     if ($has_clock_gettime) {
497         if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETTIME_SYSCALL/) {
498             print "found (via syscall).\n";
499         } else {
500             print "found.\n";
501         }
502     } else {
503         print "NOT found.\n";
504     }
505
506     print "Looking for clock_getres()... ";
507     my $has_clock_getres;
508     if (exists $Config{d_clock_getres}) {
509         $has_clock_getres++ if $Config{d_clock_getres}; # Unlikely...
510     } elsif (has_clock_x('getres')) {
511         $has_clock_getres++;
512         $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES';
513     } elsif (defined $SYSCALL_H && has_clock_x_syscall('getres')) {
514         $has_clock_getres++;
515         $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES -DTIME_HIRES_CLOCK_GETRES_SYSCALL';
516     }
517
518     if ($has_clock_getres) {
519         if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETRES_SYSCALL/) {
520             print "found (via syscall).\n";
521         } else {
522             print "found.\n";
523         }
524     } else {
525         print "NOT found.\n";
526     }
527
528     my $has_w32api_windows_h;
529     if ($^O eq 'cygwin') {
530         print "Looking for <w32api/windows.h>... ";
531         if (has_include('w32api/windows.h')) {
532             $has_w32api_windows_h++;
533             $DEFINE .= ' -DHAS_W32API_WINDOWS_H';
534         }
535         if ($has_w32api_windows_h) {
536             print "found.\n";
537         } else {
538             print "NOT found.\n";
539         }
540     }
541
542     if ($DEFINE) {
543         $DEFINE =~ s/^\s+//;
544         if (open(XDEFINE, ">xdefine")) {
545             print XDEFINE $DEFINE, "\n";
546             close(XDEFINE);
547         }
548     }
549 }
550
551 sub doMakefile {
552     my @makefileopts = ();
553
554     if ($] >= 5.005) {
555         push (@makefileopts,
556             'AUTHOR'    => 'Jarkko Hietaniemi <jhi@iki.fi>',
557             'ABSTRACT_FROM' => 'HiRes.pm',
558         );
559         $DEFINE .= " -DATLEASTFIVEOHOHFIVE";
560     }
561
562     push (@makefileopts,
563         'NAME'  => 'Time::HiRes',
564         'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
565         'LIBS'  => $LIBS,   # e.g., '-lm'
566         'DEFINE'        => $DEFINE,     # e.g., '-DHAS_SOMETHING'
567         'XSOPT' => $XSOPT,
568           # Do not even think about 'INC' => '-I/usr/ucbinclude',
569           # Solaris will avenge.
570         'INC'   => '',     # e.g., '-I/usr/include/other'
571         'INSTALLDIRS' => ($] >= 5.008 ? 'perl' : 'site'),
572         'dist'      => {
573             'CI'       => 'ci -l',
574             'COMPRESS' => 'gzip -9f',
575             'SUFFIX'   => 'gz',
576         },
577         clean => { FILES => "xdefine" },
578         realclean => { FILES=> 'const-c.inc const-xs.inc' },
579     );
580
581     if ($ENV{PERL_CORE}) {
582         push @makefileopts, MAN3PODS => {};
583     }
584
585     WriteMakefile(@makefileopts);
586 }
587
588 sub doConstants {
589     if (eval {require ExtUtils::Constant; 1}) {
590         my @names = (qw(CLOCK_HIGHRES CLOCK_MONOTONIC
591                         CLOCK_PROCESS_CPUTIME_ID
592                         CLOCK_REALTIME
593                         CLOCK_THREAD_CPUTIME_ID
594                         CLOCK_TIMEOFDAY
595                         ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF
596                         ITIMER_REALPROF));
597         foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
598                      d_nanosleep d_clock_gettime d_clock_getres)) {
599             my $macro = $_;
600             if ($macro =~ /^(d_nanosleep|d_clock_gettime|d_clock_getres)$/) {
601                 $macro =~ s/^d_(.+)/TIME_HIRES_\U$1/;
602             } else {
603                 $macro =~ s/^d_(.+)/HAS_\U$1/;
604             }
605             push @names, {name => $_, macro => $macro, value => 1,
606                           default => ["IV", "0"]};
607         }
608         ExtUtils::Constant::WriteConstants(
609                                            NAME => 'Time::HiRes',
610                                            NAMES => \@names,
611                                           );
612     } else {
613         my $file;
614         foreach $file ('const-c.inc', 'const-xs.inc') {
615             my $fallback = File::Spec->catfile('fallback', $file);
616             local $/;
617             open IN, "<$fallback" or die "Can't open $fallback: $!";
618             open OUT, ">$file" or die "Can't open $file: $!";
619             print OUT <IN> or die $!;
620             close OUT or die "Can't close $file: $!";
621             close IN or die "Can't close $fallback: $!";
622         }
623     }
624 }
625
626 sub main {
627     print "Configuring Time::HiRes...\n";
628     if ($] == 5.007002) {
629         die "Cannot Configure Time::HiRes for Perl $], aborting.\n";
630     }
631
632     if ($^O =~ /Win32/i) {
633       $DEFINE = '-DSELECT_IS_BROKEN';
634       $LIBS = [];
635       print "System is $^O, skipping full configure...\n";
636     } else {
637       init();
638     }
639     doMakefile;
640     doConstants;
641     my $make = $Config{'make'} || "make";
642     unless (exists $ENV{PERL_CORE} && $ENV{PERL_CORE}) {
643         print  <<EOM;
644 Now you may issue '$make'.  Do not forget also '$make test'.
645 EOM
646        if ((exists $ENV{LC_ALL}   && $ENV{LC_ALL}   =~ /utf-?8/i) ||
647            (exists $ENV{LC_CTYPE} && $ENV{LC_CTYPE} =~ /utf-?8/i) ||
648            (exists $ENV{LANG}     && $ENV{LANG}     =~ /utf-?8/i)) {
649             print  <<EOM;
650 NOTE: if you get an error like this (the Makefile line number may vary):
651 Makefile:91: *** missing separator
652 then set the environment variable LC_ALL to "C" and retry
653 from scratch (re-run perl "Makefile.PL").
654 EOM
655         }
656     }
657 }
658
659 &main;
660
661 # EOF