Introduce a new keyword, state, for state variables.
[p5sagit/p5-mst-13.2.git] / ext / Time / HiRes / Makefile.PL
index f7d62fd..5e54b49 100644 (file)
@@ -1,7 +1,7 @@
 #!/usr/bin/perl
 #
 # In general we trust %Config, but for nanosleep() this trust
-# may be misplaces (it may be linkable but not really functional).
+# may be misplaced (it may be linkable but not really functional).
 # Use $ENV{FORCE_NANOSLEEP_SCAN} to force rescanning whether there
 # really is hope.
 
@@ -15,6 +15,7 @@ my $VERBOSE = $ENV{VERBOSE};
 my $DEFINE;
 my $LIBS = [];
 my $XSOPT = '';
+my $SYSCALL_H;
 
 use vars qw($self); # Used in 'sourcing' the hints.
 
@@ -141,19 +142,24 @@ sub try_compile_and_link {
            my $tmp_exe = "$tmp$ld_exeext";
            printf "cccmd = $cccmd\n" if $VERBOSE;
            my $res = system($cccmd);
-           $ok = defined($res) && $res==0 && -s $tmp_exe && -x _;
+           $ok = defined($res) && $res == 0 && -s $tmp_exe && -x _;
 
            if ( $ok && exists $args{run} && $args{run}) {
-               my $abs_tmp_exe =
-                   File::Spec->
-                       catfile(File::Spec->rel2abs(File::Spec->curdir),
-                               $tmp_exe);
-               printf "Running $abs_tmp_exe..." if $VERBOSE;
-               if (system($abs_tmp_exe) == 0) {
+               my $tmp_exe =
+                   File::Spec->catfile(File::Spec->curdir, $tmp_exe);
+               printf "Running $tmp_exe..." if $VERBOSE;
+               if (system($tmp_exe) == 0) {
                    $ok = 1;
                } else {
                    $ok = 0;
-                   print "[ system('$abs_tmp_exe') failed: status $? ] ";
+                   my $errno = $? >> 8;
+                   local $! = $errno;
+                   printf <<EOF;
+
+*** The test run of '$tmp_exe' failed: status $?
+*** (the status means: errno = $errno or '$!')
+*** DO NOT PANIC: this just means that *some* functionality will be missing.
+EOF
                }
            }
            unlink("$tmp.c", $tmp_exe);
@@ -224,7 +230,7 @@ EOM
 }
 
 sub has_nanosleep {
-    print "Trying out nanosleep... ";
+    print "testing... ";
     return 1 if
     try_compile_and_link(<<EOM, run => 1);
 #include <time.h>
@@ -243,7 +249,7 @@ int main() {
     ts2.tv_sec  = 0;
     ts2.tv_nsec = 0;
     errno = 0;
-    ret = nanosleep(&ts1, &ts2); /* E.g. in AIX nanosleep() fail and set errno to ENOSYS. */
+    ret = nanosleep(&ts1, &ts2); /* E.g. in AIX nanosleep() fails and sets errno to ENOSYS. */
     ret == 0 ? exit(0) : exit(errno ? errno : -1);
 }
 EOM
@@ -266,6 +272,77 @@ EOM
     return 0;
 }
 
+sub has_clock_xxx_syscall {
+    my $x = shift;
+    return 0 unless defined $SYSCALL_H;
+    return 1 if
+    try_compile_and_link(<<EOM, run => 1);
+#include "EXTERN.h"
+#include "perl.h"
+#include "XSUB.h"
+#include <$SYSCALL_H>
+int main _((int argc, char** argv, char** env))
+{
+    struct timespec ts;
+    /* Many Linuxes get ENOSYS even though the syscall exists. */
+    /* All implementations are supposed to support CLOCK_REALTIME. */
+    int ret = syscall(SYS_clock_$x, CLOCK_REALTIME, &ts);
+    ret == 0 ? exit(0) : exit(errno ? errno : -1);
+}
+EOM
+}
+
+sub has_clock_xxx {
+    my $xxx = shift;
+    return 1 if
+    try_compile_and_link(<<EOM, run => 1);
+#include "EXTERN.h"
+#include "perl.h"
+#include "XSUB.h"
+int main _((int argc, char** argv, char** env))
+{
+    struct timespec ts;
+    int ret = clock_$xxx(CLOCK_REALTIME, &ts); /* Many Linuxes get ENOSYS. */
+    /* All implementations are supposed to support CLOCK_REALTIME. */
+    ret == 0 ? exit(0) : exit(errno ? errno : -1);
+}
+EOM
+}
+
+sub has_clock {
+    return 1 if
+    try_compile_and_link(<<EOM, run => 1);
+#include "EXTERN.h"
+#include "perl.h"
+#include "XSUB.h"
+int main _((int argc, char** argv, char** env))
+{
+    clock_t tictoc;
+    clock_t ret = clock();
+    ret == (clock_t)-1 ? exit(errno ? errno : -1) : exit(0);
+}
+EOM
+}
+
+sub has_clock_nanosleep {
+    return 1 if
+    try_compile_and_link(<<EOM, run => 1);
+#include "EXTERN.h"
+#include "perl.h"
+#include "XSUB.h"
+int main _((int argc, char** argv, char** env))
+{
+    int ret;
+    struct timerspec ts1;
+    struct timerspec ts2;
+    ts1.tv_sec  = 0;
+    ts1.tv_nsec = 750000000;;
+    ret = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts1, &ts2);
+    ret == 0 ? exit(0) : exit(errno ? errno : -1);
+}
+EOM
+}
+
 sub init {
     my $hints = File::Spec->catfile("hints", "$^O.pl");
     if (-f $hints) {
@@ -280,6 +357,25 @@ sub init {
 
     $DEFINE = '';
 
+    if ($Config{d_syscall}) {
+       print "Have syscall()... looking for syscall.h... ";
+       if (has_include('syscall.h')) {
+           $SYSCALL_H = 'syscall.h';
+       } elsif (has_include('sys/syscall.h')) {
+           $SYSCALL_H = 'sys/syscall.h';
+       }
+    } else {
+       print "No syscall()...\n";
+    }
+
+    if ($Config{d_syscall}) {
+       if (defined $SYSCALL_H) {
+           print "found <$SYSCALL_H>.\n";
+       } else {
+           print "NOT found.\n";
+       }
+    }
+
     print "Looking for gettimeofday()... ";
     my $has_gettimeofday;
     if (exists $Config{d_gettimeod}) {
@@ -385,16 +481,27 @@ EOD
 
     print "Looking for nanosleep()... ";
     my $has_nanosleep;
-    if (exists $Config{d_nanosleep} && !$ENV{FORCE_NANOSLEEP_SCAN}) {
-        # Believe $Config{d_nanosleep}.
+    if ($ENV{FORCE_NANOSLEEP_SCAN}) {
+       print "forced scan... ";
+       if (has_nanosleep()) {
+           $has_nanosleep++;
+           $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
+       }
+    }
+    elsif (exists $Config{d_nanosleep}) {
+       print "believing \$Config{d_nanosleep}... ";
        if ($Config{d_nanosleep}) {
            $has_nanosleep++;
            $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
        }
-    } elsif ($^O ne 'mpeix' && # MPE/iX falsely finds nanosleep.
-             has_nanosleep()) {
-       $has_nanosleep++;
-       $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
+    } elsif ($^O =~ /^(mpeix)$/) {
+       # MPE/iX falsely finds nanosleep from its libc equivalent.
+       print "skipping because in $^O... ";
+    } else {
+       if (has_nanosleep()) {
+           $has_nanosleep++;
+           $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
+       }
     }
 
     if ($has_nanosleep) {
@@ -408,7 +515,82 @@ EOD
         print "(It would not be portable anyway.)\n";
     }
 
+    print "Looking for clock_gettime()... ";
+    my $has_clock_gettime;
+    if (exists $Config{d_clock_gettime}) {
+        $has_clock_gettime++ if $Config{d_clock_gettime}; # Unlikely...
+    } elsif (has_clock_xxx('gettime')) {
+        $has_clock_gettime++;
+       $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME';
+    } elsif (defined $SYSCALL_H && has_clock_xxx_syscall('gettime')) {
+        $has_clock_gettime++;
+       $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME -DTIME_HIRES_CLOCK_GETTIME_SYSCALL';
+    }
+
+    if ($has_clock_gettime) {
+        if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETTIME_SYSCALL/) {
+           print "found (via syscall).\n";
+       } else {
+           print "found.\n";
+       }
+    } else {
+       print "NOT found.\n";
+    }
+
+    print "Looking for clock_getres()... ";
+    my $has_clock_getres;
+    if (exists $Config{d_clock_getres}) {
+        $has_clock_getres++ if $Config{d_clock_getres}; # Unlikely...
+    } elsif (has_clock_xxx('getres')) {
+        $has_clock_getres++;
+       $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES';
+    } elsif (defined $SYSCALL_H && has_clock_xxx_syscall('getres')) {
+        $has_clock_getres++;
+       $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES -DTIME_HIRES_CLOCK_GETRES_SYSCALL';
+    }
+
+    if ($has_clock_getres) {
+        if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETRES_SYSCALL/) {
+           print "found (via syscall).\n";
+       } else {
+           print "found.\n";
+       }
+    } else {
+       print "NOT found.\n";
+    }
+
+    print "Looking for clock_nanosleep()... ";
+    my $has_clock_nanosleep;
+    if (exists $Config{d_clock_nanosleep}) {
+        $has_clock_nanosleep++ if $Config{d_clock_nanosleep}; # Unlikely...
+    } elsif (has_clock_nanosleep()) {
+        $has_clock_nanosleep++;
+       $DEFINE .= ' -DTIME_HIRES_CLOCK_NANOSLEEP';
+    }
+
+    if ($has_clock_nanosleep) {
+        print "found.\n";
+    } else {
+       print "NOT found.\n";
+    }
+
+    print "Looking for clock()... ";
+    my $has_clock;
+    if (exists $Config{d_clock}) {
+        $has_clock++ if $Config{d_clock}; # Unlikely...
+    } elsif (has_clock()) {
+        $has_clock++;
+       $DEFINE .= ' -DTIME_HIRES_CLOCK';
+    }
+
+    if ($has_clock) {
+        print "found.\n";
+    } else {
+       print "NOT found.\n";
+    }
+
     my $has_w32api_windows_h;
+
     if ($^O eq 'cygwin') {
         print "Looking for <w32api/windows.h>... ";
         if (has_include('w32api/windows.h')) {
@@ -448,9 +630,10 @@ sub doMakefile {
        'LIBS'  => $LIBS,   # e.g., '-lm'
        'DEFINE'        => $DEFINE,     # e.g., '-DHAS_SOMETHING'
        'XSOPT' => $XSOPT,
-    # do not even think about 'INC' => '-I/usr/ucbinclude', Solaris will avenge.
+         # Do not even think about 'INC' => '-I/usr/ucbinclude',
+         # Solaris will avenge.
        'INC'   => '',     # e.g., '-I/usr/include/other'
-       'INSTALLDIRS' => 'perl',
+       'INSTALLDIRS' => ($] >= 5.008 ? 'perl' : 'site'),
        'dist'      => {
            'CI'       => 'ci -l',
            'COMPRESS' => 'gzip -9f',
@@ -469,15 +652,24 @@ sub doMakefile {
 
 sub doConstants {
     if (eval {require ExtUtils::Constant; 1}) {
-       my @names = (qw(ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF
-                       ITIMER_REALPROF));
+       my @names = (qw(CLOCK_HIGHRES CLOCK_MONOTONIC
+                       CLOCK_PROCESS_CPUTIME_ID
+                       CLOCK_REALTIME
+                       CLOCK_SOFTTIME
+                       CLOCK_THREAD_CPUTIME_ID
+                       CLOCK_TIMEOFDAY
+                       CLOCKS_PER_SEC
+                       ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF
+                       ITIMER_REALPROF
+                       TIMER_ABSTIME));
        foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
-                    d_nanosleep)) {
+                    d_nanosleep d_clock_gettime d_clock_getres
+                    d_clock d_clock_nanosleep)) {
            my $macro = $_;
-           if ($macro eq 'd_nanosleep') {
-               $macro =~ s/d_(.*)/TIME_HIRES_\U$1/;
+           if ($macro =~ /^(d_nanosleep|d_clock_gettime|d_clock_getres|d_clock|d_clock_nanosleep)$/) {
+               $macro =~ s/^d_(.+)/TIME_HIRES_\U$1/;
            } else {
-               $macro =~ s/d_(.*)/HAS_\U$1/;
+               $macro =~ s/^d_(.+)/HAS_\U$1/;
            }
            push @names, {name => $_, macro => $macro, value => 1,
                          default => ["IV", "0"]};
@@ -509,6 +701,7 @@ sub main {
     if ($^O =~ /Win32/i) {
       $DEFINE = '-DSELECT_IS_BROKEN';
       $LIBS = [];
+      print "System is $^O, skipping full configure...\n";
     } else {
       init();
     }
@@ -523,7 +716,7 @@ EOM
            (exists $ENV{LC_CTYPE} && $ENV{LC_CTYPE} =~ /utf-?8/i) ||
            (exists $ENV{LANG}     && $ENV{LANG}     =~ /utf-?8/i)) {
             print  <<EOM;
-NOTE: if you get an error like this (the line number may vary):
+NOTE: if you get an error like this (the Makefile line number may vary):
 Makefile:91: *** missing separator
 then set the environment variable LC_ALL to "C" and retry
 from scratch (re-run perl "Makefile.PL").