This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make Time/HiRes/Makefile.PL produce a Makefile if one is needed
[perl5.git] / ext / Time / HiRes / Makefile.PL
index bce235e..2fe6cf4 100644 (file)
@@ -19,8 +19,11 @@ my $SYSCALL_H;
 
 use vars qw($self); # Used in 'sourcing' the hints.
 
+# TBD: Can we just use $Config(exe_ext) here instead of this complex
+#      expression?
 my $ld_exeext = ($^O eq 'cygwin' ||
-                 $^O eq 'os2' && $Config{ldflags} =~ /-Zexe\b/) ? '.exe' : '';
+                 $^O eq 'os2' && $Config{ldflags} =~ /-Zexe\b/) ? '.exe' :
+                (($^O eq 'vos') ? $Config{exe_ext} : '');
 
 unless($ENV{PERL_CORE}) {
     $ENV{PERL_CORE} = 1 if grep { $_ eq 'PERL_CORE=1' } @ARGV;
@@ -102,6 +105,16 @@ sub try_compile_and_link {
            $COREincdir = File::Spec->catdir($Config{'archlibexp'}, 'CORE');
        }
 
+       if ($ENV{PERL_CORE}) {
+           unless (-f File::Spec->catfile($COREincdir, "EXTERN.h")) {
+               die <<__EOD__;
+Your environment variable PERL_CORE is '$ENV{PERL_CORE}' but there
+is no EXTERN.h in $COREincdir.
+Cannot continue, aborting.
+__EOD__
+            }
+        }
+
        my $ccflags = $Config{'ccflags'} . ' ' . "-I$COREincdir";
 
        if ($^O eq 'VMS') {
@@ -169,30 +182,32 @@ EOF
     return $ok;
 }
 
-sub has_gettimeofday {
-    # confusing but true (if condition true ==> -DHAS_GETTIMEOFDAY already)
-    return 0 if $Config{d_gettimeod};
-    return 1 if try_compile_and_link(<<EOM);
+my $TIME_HEADERS = <<EOH;
 #include "EXTERN.h"
 #include "perl.h"
 #include "XSUB.h"
 #ifdef I_SYS_TYPES
 #   include <sys/types.h>
 #endif
-
 #ifdef I_SYS_TIME
 #   include <sys/time.h>
 #endif
-
 #ifdef I_SYS_SELECT
 #   include <sys/select.h>     /* struct timeval might be hidden in here */
 #endif
+EOH
+
+sub has_gettimeofday {
+    # confusing but true (if condition true ==> -DHAS_GETTIMEOFDAY already)
+    return 0 if $Config{d_gettimeod};
+    return 1 if try_compile_and_link(<<EOM);
+$TIME_HEADERS
 static int foo()
 {
     struct timeval tv;
     gettimeofday(&tv, 0);
 }
-int main _((int argc, char** argv, char** env))
+int main(int argc, char** argv)
 {
     foo();
 }
@@ -221,7 +236,7 @@ sub has_x {
 #   include <sys/time.h>
 #endif
 
-int main _((int argc, char** argv, char** env))
+int main(int argc, char** argv)
 {
        $x;
 }
@@ -241,7 +256,7 @@ sub has_nanosleep {
 
 /* int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); */
 
-int main() {
+int main(int argc, char** argv) {
     struct timespec ts1, ts2;
     int ret;
     ts1.tv_sec  = 0;
@@ -264,7 +279,7 @@ sub has_include {
 #include "XSUB.h"
 
 #include <$inc>
-int main _((int argc, char** argv, char** env))
+int main(int argc, char** argv)
 {
        return 0;
 }
@@ -272,7 +287,7 @@ EOM
     return 0;
 }
 
-sub has_clock_x_syscall {
+sub has_clock_xxx_syscall {
     my $x = shift;
     return 0 unless defined $SYSCALL_H;
     return 1 if
@@ -281,7 +296,7 @@ sub has_clock_x_syscall {
 #include "perl.h"
 #include "XSUB.h"
 #include <$SYSCALL_H>
-int main _((int argc, char** argv, char** env))
+int main(int argc, char** argv)
 {
     struct timespec ts;
     /* Many Linuxes get ENOSYS even though the syscall exists. */
@@ -292,23 +307,66 @@ int main _((int argc, char** argv, char** env))
 EOM
 }
 
-sub has_clock_x {
-    my $x = shift;
+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))
+int main(int argc, char** argv)
 {
     struct timespec ts;
-    int ret = clock_$x(CLOCK_REALTIME, &ts); /* Many Linuxes get ENOSYS. */
+    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)
+{
+    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"
+#include <time.h>
+int main(int argc, char** argv)
+{
+    int ret;
+    struct timespec ts1;
+    struct timespec 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 DEFINE {
+    my ($def, $val) = @_;
+    my $define = defined $val ? "$def=$val" : $def ;
+    unless ($DEFINE =~ /(?:^| )-D\Q$define\E(?: |$)/) {
+       $DEFINE .= " -D$define";
+    }
+}
+
 sub init {
     my $hints = File::Spec->catfile("hints", "$^O.pl");
     if (-f $hints) {
@@ -485,10 +543,10 @@ EOD
     my $has_clock_gettime;
     if (exists $Config{d_clock_gettime}) {
         $has_clock_gettime++ if $Config{d_clock_gettime}; # Unlikely...
-    } elsif (has_clock_x('gettime')) {
+    } elsif (has_clock_xxx('gettime')) {
         $has_clock_gettime++;
        $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME';
-    } elsif (defined $SYSCALL_H && has_clock_x_syscall('gettime')) {
+    } elsif (defined $SYSCALL_H && has_clock_xxx_syscall('gettime')) {
         $has_clock_gettime++;
        $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME -DTIME_HIRES_CLOCK_GETTIME_SYSCALL';
     }
@@ -507,10 +565,10 @@ EOD
     my $has_clock_getres;
     if (exists $Config{d_clock_getres}) {
         $has_clock_getres++ if $Config{d_clock_getres}; # Unlikely...
-    } elsif (has_clock_x('getres')) {
+    } elsif (has_clock_xxx('getres')) {
         $has_clock_getres++;
        $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES';
-    } elsif (defined $SYSCALL_H && has_clock_x_syscall('getres')) {
+    } elsif (defined $SYSCALL_H && has_clock_xxx_syscall('getres')) {
         $has_clock_getres++;
        $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES -DTIME_HIRES_CLOCK_GETRES_SYSCALL';
     }
@@ -525,12 +583,152 @@ EOD
        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";
+    }
+
+    print "Looking for stat() subsecond timestamps...\n";
+
+    print "Trying struct stat st_atimespec.tv_nsec...";
+    my $has_stat_st_xtimespec;
+    if (try_compile_and_link(<<EOM)) {
+$TIME_HEADERS
+#include <sys/stat.h>
+int main(int argc, char** argv) {
+    struct stat st;
+    st.st_atimespec.tv_nsec = 0;
+}
+EOM
+      $has_stat_st_xtimespec++;
+      DEFINE('TIME_HIRES_STAT', 1);
+    }
+
+    if ($has_stat_st_xtimespec) {
+        print "found.\n";
+    } else {
+       print "NOT found.\n";
+    }
+
+    print "Trying struct stat st_atimensec...";
+    my $has_stat_st_xtimensec;
+    if (try_compile_and_link(<<EOM)) {
+$TIME_HEADERS
+#include <sys/stat.h>
+int main(int argc, char** argv) {
+    struct stat st;
+    st.st_atimensec = 0;
+}
+EOM
+      $has_stat_st_xtimensec++;
+      DEFINE('TIME_HIRES_STAT', 2);
+    }
+
+    if ($has_stat_st_xtimensec) {
+        print "found.\n";
+    } else {
+       print "NOT found.\n";
+    }
+
+    print "Trying struct stat st_atime_n...";
+    my $has_stat_st_xtime_n;
+    if (try_compile_and_link(<<EOM)) {
+$TIME_HEADERS
+#include <sys/stat.h>
+int main(int argc, char** argv) {
+    struct stat st;
+    st.st_atime_n = 0;
+}
+EOM
+      $has_stat_st_xtime_n++;
+      DEFINE('TIME_HIRES_STAT', 3);
+    }
+
+    if ($has_stat_st_xtime_n) {
+        print "found.\n";
+    } else {
+       print "NOT found.\n";
+    }
+
+    print "Trying struct stat st_atim.tv_nsec...";
+    my $has_stat_st_xtim;
+    if (try_compile_and_link(<<EOM)) {
+$TIME_HEADERS
+#include <sys/stat.h>
+int main(int argc, char** argv) {
+    struct stat st;
+    st.st_atim.tv_nsec = 0;
+}
+EOM
+      $has_stat_st_xtim++;
+      DEFINE('TIME_HIRES_STAT', 4);
+    }
+
+    if ($has_stat_st_xtim) {
+        print "found.\n";
+    } else {
+       print "NOT found.\n";
+    }
+
+    print "Trying struct stat st_uatime...";
+    my $has_stat_st_uxtime;
+    if (try_compile_and_link(<<EOM)) {
+$TIME_HEADERS
+#include <sys/stat.h>
+int main(int argc, char** argv) {
+    struct stat st;
+    st.st_uatime = 0;
+}
+EOM
+      $has_stat_st_uxtime++;
+      DEFINE('TIME_HIRES_STAT', 5);
+    }
+
+    if ($has_stat_st_uxtime) {
+        print "found.\n";
+    } else {
+       print "NOT found.\n";
+    }
+
+   if ($DEFINE =~ /-DTIME_HIRES_STAT=\d+/) {
+    print "You seem to have stat() subsecond timestamps.\n";
+    print "(Your struct stat has them, but the filesystems must help.)\n";
+   } else {
+    print "You do not seem to have stat subsecond timestamps.\n";
+   }
+
     my $has_w32api_windows_h;
+
     if ($^O eq 'cygwin') {
         print "Looking for <w32api/windows.h>... ";
         if (has_include('w32api/windows.h')) {
            $has_w32api_windows_h++;
-           $DEFINE .= ' -DHAS_W32API_WINDOWS_H';
+           DEFINE('HAS_W32API_WINDOWS_H');
        }
         if ($has_w32api_windows_h) {
            print "found.\n";
@@ -556,7 +754,7 @@ sub doMakefile {
            'AUTHOR'    => 'Jarkko Hietaniemi <jhi@iki.fi>',
            'ABSTRACT_FROM' => 'HiRes.pm',
        );
-       $DEFINE .= " -DATLEASTFIVEOHOHFIVE";
+       DEFINE('ATLEASTFIVEOHOHFIVE');
     }
 
     push (@makefileopts,
@@ -587,18 +785,28 @@ sub doMakefile {
 
 sub doConstants {
     if (eval {require ExtUtils::Constant; 1}) {
-       my @names = (qw(CLOCK_HIGHRES CLOCK_MONOTONIC
-                       CLOCK_PROCESS_CPUTIME_ID
-                       CLOCK_REALTIME
-                       CLOCK_THREAD_CPUTIME_ID
-                       CLOCK_TIMEOFDAY
-                       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_clock_gettime d_clock_getres)) {
+                    d_nanosleep d_clock_gettime d_clock_getres
+                    d_clock d_clock_nanosleep d_hires_stat)) {
            my $macro = $_;
-           if ($macro =~ /^(d_nanosleep|d_clock_gettime|d_clock_getres)$/) {
+           if ($macro =~ /^(d_nanosleep|d_clock_gettime|d_clock_getres|d_clock|d_clock_nanosleep)$/) {
                $macro =~ s/^d_(.+)/TIME_HIRES_\U$1/;
+           } elsif ($macro =~ /^(d_hires_stat)$/) {
+               my $d_hires_stat = 0;
+               $d_hires_stat = $1 if ($DEFINE =~ /-DTIME_HIRES_STAT=(\d+)/);
+               push @names, {name => $_, macro => "TIME_HIRES_STAT", value => $d_hires_stat,
+                             default => ["IV", "0"]};
+               next;
            } else {
                $macro =~ s/^d_(.+)/HAS_\U$1/;
            }
@@ -624,33 +832,43 @@ sub doConstants {
 }
 
 sub main {
-    print "Configuring Time::HiRes...\n";
-    if ($] == 5.007002) {
-       die "Cannot Configure Time::HiRes for Perl $], aborting.\n";
-    }
-
-    if ($^O =~ /Win32/i) {
-      $DEFINE = '-DSELECT_IS_BROKEN';
-      $LIBS = [];
-      print "System is $^O, skipping full configure...\n";
+    if (-f "Makefile" and -f "xdefine" && !(@ARGV  && $ARGV[0] eq '--configure')) {
+       print qq[$0: The "xdefine" exists, skipping the configure step.\n];
+       print qq[("$^X $0 --configure" to force the configure step)\n];
     } else {
-      init();
+       print "Configuring Time::HiRes...\n";
+       1 while unlink("define");
+       if ($^O =~ /Win32/i) {
+           DEFINE('SELECT_IS_BROKEN');
+           $LIBS = [];
+           print "System is $^O, skipping full configure...\n";
+           open(XDEFINE, ">xdefine") or die "$0: Cannot create xdefine: $!\n";
+           close(XDEFINE);
+       } else {
+           init();
+       }
+       doMakefile;
+       doConstants;
     }
-    doMakefile;
-    doConstants;
     my $make = $Config{'make'} || "make";
     unless (exists $ENV{PERL_CORE} && $ENV{PERL_CORE}) {
        print  <<EOM;
 Now you may issue '$make'.  Do not forget also '$make test'.
 EOM
-       if ((exists $ENV{LC_ALL}   && $ENV{LC_ALL}   =~ /utf-?8/i) ||
-           (exists $ENV{LC_CTYPE} && $ENV{LC_CTYPE} =~ /utf-?8/i) ||
-           (exists $ENV{LANG}     && $ENV{LANG}     =~ /utf-?8/i)) {
-            print  <<EOM;
+       if ($] == 5.008 &&
+          ((exists $ENV{LC_ALL}   && $ENV{LC_ALL}   =~ /utf-?8/i) ||
+           (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 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").
+(And consider upgrading your Perl to, say, at least Perl 5.8.8.)
+(You got this message because you seem to have
+ an UTF-8 locale active in your shell environment, this used
+ to cause broken Makefiles to be created from Makefile.PLs)
 EOM
         }
     }