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
11 use ExtUtils::MakeMaker;
14 my $VERBOSE = $ENV{VERBOSE};
20 our $self; # Used in 'sourcing' the hints.
22 # TBD: Can we just use $Config(exe_ext) here instead of this complex
24 my $ld_exeext = ($^O eq 'cygwin' ||
25 $^O eq 'os2' && $Config{ldflags} =~ /-Zexe\b/) ? '.exe' :
26 (($^O eq 'vos') ? $Config{exe_ext} : '');
28 unless($ENV{PERL_CORE}) {
29 $ENV{PERL_CORE} = 1 if grep { $_ eq 'PERL_CORE=1' } @ARGV;
32 # Perls 5.002 and 5.003 did not have File::Spec, fake what we need.
36 $^O =~ /mswin32|netware|djgpp/i ? '\\' :
43 my $catdir = join(my_dirsep, @_);
44 $^O eq 'VMS' ? "[$catdir]" : $catdir;
49 return join(my_dirsep, @_) unless $^O eq 'VMS';
51 return my_catdir (undef, @_) . $file;
56 $^O eq 'VMS' ? "-" : "..";
60 eval { require File::Spec };
62 *File::Spec::catdir = \&my_catdir;
63 *File::Spec::updir = \&my_updir;
64 *File::Spec::catfile = \&my_catfile;
68 # Avoid 'used only once' warnings.
69 my $nop1 = *File::Spec::catdir;
70 my $nop2 = *File::Spec::updir;
71 my $nop3 = *File::Spec::catfile;
73 # if you have 5.004_03 (and some slightly older versions?), xsubpp
74 # tries to generate line numbers in the C code generated from the .xs.
75 # unfortunately, it is a little buggy around #ifdef'd code.
76 # my choice is leave it in and have people with old perls complain
77 # about the "Usage" bug, or leave it out and be unable to compile myself
78 # without changing it, and then I'd always forget to change it before a
79 # release. Sorry, Edward :)
81 sub try_compile_and_link {
88 my $obj_ext = $Config{obj_ext} || ".o";
89 unlink("$tmp.c", "$tmp$obj_ext");
91 if (open(TMPC, ">$tmp.c")) {
95 my $cccmd = $args{cccmd};
101 if ($ENV{PERL_CORE}) {
102 my $updir = File::Spec->updir;
103 $COREincdir = File::Spec->catdir(($updir) x 2);
105 $COREincdir = File::Spec->catdir($Config{'archlibexp'}, 'CORE');
108 if ($ENV{PERL_CORE}) {
109 unless (-f File::Spec->catfile($COREincdir, "EXTERN.h")) {
111 Your environment variable PERL_CORE is '$ENV{PERL_CORE}' but there
112 is no EXTERN.h in $COREincdir.
113 Cannot continue, aborting.
118 my $ccflags = $Config{'ccflags'} . ' ' . "-I$COREincdir"
119 . ' -DPERL_NO_INLINE_FUNCTIONS';
122 $cccmd = "$Config{'cc'} /include=($COREincdir) $tmp.c";
125 if ($args{silent} || !$VERBOSE) {
126 $errornull = "2>/dev/null" unless defined $errornull;
131 $cccmd = "$Config{'cc'} -o $tmp $ccflags $tmp.c @$LIBS $errornull"
132 unless defined $cccmd;
135 open( CMDFILE, ">$tmp.com" );
136 print CMDFILE "\$ SET MESSAGE/NOFACILITY/NOSEVERITY/NOIDENT/NOTEXT\n";
137 print CMDFILE "\$ $cccmd\n";
138 print CMDFILE "\$ IF \$SEVERITY .NE. 1 THEN EXIT 44\n"; # escalate
140 system("\@ $tmp.com");
142 for ("$tmp.c", "$tmp$obj_ext", "$tmp.com", "$tmp$Config{exe_ext}") {
148 my $tmp_exe = "$tmp$ld_exeext";
149 printf "cccmd = $cccmd\n" if $VERBOSE;
150 my $res = system($cccmd);
151 $ok = defined($res) && $res == 0 && -s $tmp_exe && -x _;
153 if ( $ok && exists $args{run} && $args{run}) {
155 File::Spec->catfile(File::Spec->curdir, $tmp_exe);
156 printf "Running $tmp_exe..." if $VERBOSE;
157 if (system($tmp_exe) == 0) {
165 *** The test run of '$tmp_exe' failed: status $?
166 *** (the status means: errno = $errno or '$!')
167 *** DO NOT PANIC: this just means that *some* functionality will be missing.
171 unlink("$tmp.c", $tmp_exe);
178 my $TIME_HEADERS = <<EOH;
183 # include <sys/types.h>
186 # include <sys/time.h>
189 # include <sys/select.h> /* struct timeval might be hidden in here */
193 sub has_gettimeofday {
194 # confusing but true (if condition true ==> -DHAS_GETTIMEOFDAY already)
195 return 0 if $Config{d_gettimeod};
196 return 1 if try_compile_and_link(<<EOM);
201 gettimeofday(&tv, 0);
203 int main(int argc, char** argv)
215 try_compile_and_link(<<EOM, %args);
225 # include <sys/types.h>
229 # include <sys/time.h>
232 int main(int argc, char** argv)
243 try_compile_and_link(<<EOM, run => 1);
245 #include <sys/time.h>
250 /* int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); */
252 int main(int argc, char** argv) {
253 struct timespec ts1, ts2;
256 ts1.tv_nsec = 750000000;
260 ret = nanosleep(&ts1, &ts2); /* E.g. in AIX nanosleep() fails and sets errno to ENOSYS. */
261 ret == 0 ? exit(0) : exit(errno ? errno : -1);
269 try_compile_and_link(<<EOM);
275 int main(int argc, char** argv)
283 sub has_clock_xxx_syscall {
285 return 0 unless defined $SYSCALL_H;
287 try_compile_and_link(<<EOM, run => 1);
291 #include <$SYSCALL_H>
292 int main(int argc, char** argv)
295 /* Many Linuxes get ENOSYS even though the syscall exists. */
296 /* All implementations are supposed to support CLOCK_REALTIME. */
297 int ret = syscall(SYS_clock_$x, CLOCK_REALTIME, &ts);
298 ret == 0 ? exit(0) : exit(errno ? errno : -1);
306 try_compile_and_link(<<EOM, run => 1);
310 int main(int argc, char** argv)
313 int ret = clock_$xxx(CLOCK_REALTIME, &ts); /* Many Linuxes get ENOSYS. */
314 /* All implementations are supposed to support CLOCK_REALTIME. */
315 ret == 0 ? exit(0) : exit(errno ? errno : -1);
322 try_compile_and_link(<<EOM, run => 1);
326 int main(int argc, char** argv)
329 clock_t ret = clock();
330 ret == (clock_t)-1 ? exit(errno ? errno : -1) : exit(0);
335 sub has_clock_nanosleep {
337 try_compile_and_link(<<EOM, run => 1);
342 int main(int argc, char** argv)
348 ts1.tv_nsec = 750000000;;
349 ret = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts1, &ts2);
350 ret == 0 ? exit(0) : exit(errno ? errno : -1);
356 my ($def, $val) = @_;
357 my $define = defined $val ? "$def=$val" : $def ;
358 unless ($DEFINE =~ /(?:^| )-D\Q$define\E(?: |$)/) {
359 $DEFINE .= " -D$define";
364 my $hints = File::Spec->catfile("hints", "$^O.pl");
366 print "Using hints $hints...\n";
369 if (exists $self->{LIBS}) {
370 $LIBS = $self->{LIBS};
371 print "Extra libraries: @$LIBS...\n";
377 if ($Config{d_syscall}) {
378 print "Have syscall()... looking for syscall.h... ";
379 if (has_include('syscall.h')) {
380 $SYSCALL_H = 'syscall.h';
381 } elsif (has_include('sys/syscall.h')) {
382 $SYSCALL_H = 'sys/syscall.h';
385 print "No syscall()...\n";
388 if ($Config{d_syscall}) {
389 if (defined $SYSCALL_H) {
390 print "found <$SYSCALL_H>.\n";
392 print "NOT found.\n";
396 print "Looking for gettimeofday()... ";
397 my $has_gettimeofday;
398 if (exists $Config{d_gettimeod}) {
399 $has_gettimeofday++ if $Config{d_gettimeod};
400 } elsif (has_gettimeofday()) {
401 $DEFINE .= ' -DHAS_GETTIMEOFDAY';
405 if ($has_gettimeofday) {
409 Your operating system does not seem to have the gettimeofday() function.
410 (or, at least, I cannot find it)
412 There is no way Time::HiRes is going to work.
414 I am awfully sorry but I cannot go further.
416 Aborting configuration.
421 print "Looking for setitimer()... ";
423 if (exists $Config{d_setitimer}) {
424 $has_setitimer++ if $Config{d_setitimer};
425 } elsif (has_x("setitimer(ITIMER_REAL, 0, 0)")) {
427 $DEFINE .= ' -DHAS_SETITIMER';
430 if ($has_setitimer) {
433 print "NOT found.\n";
436 print "Looking for getitimer()... ";
438 if (exists $Config{'d_getitimer'}) {
439 $has_getitimer++ if $Config{'d_getitimer'};
440 } elsif (has_x("getitimer(ITIMER_REAL, 0)")) {
442 $DEFINE .= ' -DHAS_GETITIMER';
445 if ($has_getitimer) {
448 print "NOT found.\n";
451 if ($has_setitimer && $has_getitimer) {
452 print "You have interval timers (both setitimer and getitimer).\n";
454 print "You do not have interval timers.\n";
457 print "Looking for ualarm()... ";
459 if (exists $Config{d_ualarm}) {
460 $has_ualarm++ if $Config{d_ualarm};
461 } elsif (has_x ("ualarm (0, 0)")) {
463 $DEFINE .= ' -DHAS_UALARM';
469 print "NOT found.\n";
470 if ($has_setitimer) {
471 print "But you have setitimer().\n";
472 print "We can make a Time::HiRes::ualarm().\n";
476 print "Looking for usleep()... ";
478 if (exists $Config{d_usleep}) {
479 $has_usleep++ if $Config{d_usleep};
480 } elsif (has_x ("usleep (0)")) {
482 $DEFINE .= ' -DHAS_USLEEP';
488 print "NOT found.\n";
489 print "Let's see if you have select()... ";
490 if ($Config{'d_select'}) {
492 print "We can make a Time::HiRes::usleep().\n";
494 print "NOT found.\n";
495 print "You won't have a Time::HiRes::usleep().\n";
499 print "Looking for nanosleep()... ";
501 if ($ENV{FORCE_NANOSLEEP_SCAN}) {
502 print "forced scan... ";
503 if (has_nanosleep()) {
505 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
508 elsif (exists $Config{d_nanosleep}) {
509 print "believing \$Config{d_nanosleep}... ";
510 if ($Config{d_nanosleep}) {
512 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
514 } elsif ($^O =~ /^(mpeix)$/) {
515 # MPE/iX falsely finds nanosleep from its libc equivalent.
516 print "skipping because in $^O... ";
518 if (has_nanosleep()) {
520 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
524 if ($has_nanosleep) {
526 print "You can mix subsecond sleeps with signals, if you want to.\n";
527 print "(It's still not portable, though.)\n";
529 print "NOT found.\n";
530 my $nt = ($^O eq 'os2' ? '' : 'not');
531 print "You can$nt mix subsecond sleeps with signals.\n";
532 print "(It would not be portable anyway.)\n";
535 print "Looking for clock_gettime()... ";
536 my $has_clock_gettime;
537 if (exists $Config{d_clock_gettime}) {
538 $has_clock_gettime++ if $Config{d_clock_gettime}; # Unlikely...
539 } elsif (has_clock_xxx('gettime')) {
540 $has_clock_gettime++;
541 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME';
542 } elsif (defined $SYSCALL_H && has_clock_xxx_syscall('gettime')) {
543 $has_clock_gettime++;
544 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME -DTIME_HIRES_CLOCK_GETTIME_SYSCALL';
547 if ($has_clock_gettime) {
548 if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETTIME_SYSCALL/) {
549 print "found (via syscall).\n";
554 print "NOT found.\n";
557 print "Looking for clock_getres()... ";
558 my $has_clock_getres;
559 if (exists $Config{d_clock_getres}) {
560 $has_clock_getres++ if $Config{d_clock_getres}; # Unlikely...
561 } elsif (has_clock_xxx('getres')) {
563 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES';
564 } elsif (defined $SYSCALL_H && has_clock_xxx_syscall('getres')) {
566 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES -DTIME_HIRES_CLOCK_GETRES_SYSCALL';
569 if ($has_clock_getres) {
570 if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETRES_SYSCALL/) {
571 print "found (via syscall).\n";
576 print "NOT found.\n";
579 print "Looking for clock_nanosleep()... ";
580 my $has_clock_nanosleep;
581 if (exists $Config{d_clock_nanosleep}) {
582 $has_clock_nanosleep++ if $Config{d_clock_nanosleep}; # Unlikely...
583 } elsif (has_clock_nanosleep()) {
584 $has_clock_nanosleep++;
585 $DEFINE .= ' -DTIME_HIRES_CLOCK_NANOSLEEP';
588 if ($has_clock_nanosleep) {
591 print "NOT found.\n";
594 print "Looking for clock()... ";
596 if (exists $Config{d_clock}) {
597 $has_clock++ if $Config{d_clock}; # Unlikely...
598 } elsif (has_clock()) {
600 $DEFINE .= ' -DTIME_HIRES_CLOCK';
606 print "NOT found.\n";
609 print "Looking for stat() subsecond timestamps...\n";
611 print "Trying struct stat st_atimespec.tv_nsec...";
612 my $has_stat_st_xtimespec;
613 if (try_compile_and_link(<<EOM)) {
615 #include <sys/stat.h>
616 int main(int argc, char** argv) {
618 st.st_atimespec.tv_nsec = 0;
621 $has_stat_st_xtimespec++;
622 DEFINE('TIME_HIRES_STAT', 1);
625 if ($has_stat_st_xtimespec) {
628 print "NOT found.\n";
631 print "Trying struct stat st_atimensec...";
632 my $has_stat_st_xtimensec;
633 if (try_compile_and_link(<<EOM)) {
635 #include <sys/stat.h>
636 int main(int argc, char** argv) {
641 $has_stat_st_xtimensec++;
642 DEFINE('TIME_HIRES_STAT', 2);
645 if ($has_stat_st_xtimensec) {
648 print "NOT found.\n";
651 print "Trying struct stat st_atime_n...";
652 my $has_stat_st_xtime_n;
653 if (try_compile_and_link(<<EOM)) {
655 #include <sys/stat.h>
656 int main(int argc, char** argv) {
661 $has_stat_st_xtime_n++;
662 DEFINE('TIME_HIRES_STAT', 3);
665 if ($has_stat_st_xtime_n) {
668 print "NOT found.\n";
671 print "Trying struct stat st_atim.tv_nsec...";
672 my $has_stat_st_xtim;
673 if (try_compile_and_link(<<EOM)) {
675 #include <sys/stat.h>
676 int main(int argc, char** argv) {
678 st.st_atim.tv_nsec = 0;
682 DEFINE('TIME_HIRES_STAT', 4);
685 if ($has_stat_st_xtim) {
688 print "NOT found.\n";
691 print "Trying struct stat st_uatime...";
692 my $has_stat_st_uxtime;
693 if (try_compile_and_link(<<EOM)) {
695 #include <sys/stat.h>
696 int main(int argc, char** argv) {
701 $has_stat_st_uxtime++;
702 DEFINE('TIME_HIRES_STAT', 5);
705 if ($has_stat_st_uxtime) {
708 print "NOT found.\n";
711 if ($DEFINE =~ /-DTIME_HIRES_STAT=\d+/) {
712 print "You seem to have stat() subsecond timestamps.\n";
713 print "(Your struct stat has them, but the filesystems must help.)\n";
715 print "You do not seem to have stat subsecond timestamps.\n";
718 my $has_w32api_windows_h;
720 if ($^O eq 'cygwin') {
721 print "Looking for <w32api/windows.h>... ";
722 if (has_include('w32api/windows.h')) {
723 $has_w32api_windows_h++;
724 DEFINE('HAS_W32API_WINDOWS_H');
726 if ($has_w32api_windows_h) {
729 print "NOT found.\n";
735 if (open(XDEFINE, ">xdefine")) {
736 print XDEFINE $DEFINE, "\n";
743 my @makefileopts = ();
747 'AUTHOR' => 'Jarkko Hietaniemi <jhi@iki.fi>',
748 'ABSTRACT_FROM' => 'HiRes.pm',
750 DEFINE('ATLEASTFIVEOHOHFIVE');
754 'NAME' => 'Time::HiRes',
755 'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
756 'LIBS' => $LIBS, # e.g., '-lm'
757 'DEFINE' => $DEFINE, # e.g., '-DHAS_SOMETHING'
759 # Do not even think about 'INC' => '-I/usr/ucbinclude',
760 # Solaris will avenge.
761 'INC' => '', # e.g., '-I/usr/include/other'
762 'INSTALLDIRS' => ($] >= 5.008 && $] < 5.011 ? 'perl' : 'site'),
768 'ExtUtils::MakeMaker' => 0,
769 'Test::More' => "0.82",
774 'COMPRESS' => 'gzip -9f',
777 clean => { FILES => "xdefine" },
778 realclean => { FILES=> 'const-c.inc const-xs.inc' },
781 if ($^O eq "MSWin32" && !(grep { /\ALD[A-Z]*=/ } @ARGV)) {
782 my $libperl = $Config{libperl} || "";
783 my $gccversion = $Config{gccversion} || "";
784 if ($gccversion =~ /\A3\.4\.[0-9]+/ and $libperl =~ /\.lib\z/) {
785 # Avoid broken linkage with ActivePerl, by linking directly
786 # against the Perl DLL rather than the import library.
787 (my $llibperl = "-l$libperl") =~ s/\.lib\z//;
788 my $lddlflags = $Config{lddlflags} || "";
789 my $ldflags = $Config{ldflags} || "";
790 s/-L(?:".*?"|\S+)//g foreach $lddlflags, $ldflags;
791 my $libdirs = join ' ',
792 map { s/(?<!\\)((?:\\\\)*")/\\$1/g; qq[-L"$_"] }
793 @Config{qw/bin sitebin/};
794 push @makefileopts, macro => {
795 LDDLFLAGS => "$lddlflags $libdirs $llibperl",
796 LDFLAGS => "$ldflags $libdirs $llibperl",
802 if ($ENV{PERL_CORE}) {
803 push @makefileopts, MAN3PODS => {};
806 WriteMakefile(@makefileopts);
810 if (eval {require ExtUtils::Constant; 1}) {
811 my @names = qw(CLOCK_HIGHRES CLOCK_MONOTONIC
812 CLOCK_PROCESS_CPUTIME_ID
815 CLOCK_THREAD_CPUTIME_ID
818 ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF
821 foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
822 d_nanosleep d_clock_gettime d_clock_getres
823 d_clock d_clock_nanosleep d_hires_stat)) {
825 if ($macro =~ /^(d_nanosleep|d_clock_gettime|d_clock_getres|d_clock|d_clock_nanosleep)$/) {
826 $macro =~ s/^d_(.+)/TIME_HIRES_\U$1/;
827 } elsif ($macro =~ /^(d_hires_stat)$/) {
828 my $d_hires_stat = 0;
829 $d_hires_stat = $1 if ($DEFINE =~ /-DTIME_HIRES_STAT=(\d+)/);
830 push @names, {name => $_, macro => "TIME_HIRES_STAT", value => $d_hires_stat,
831 default => ["IV", "0"]};
834 $macro =~ s/^d_(.+)/HAS_\U$1/;
836 push @names, {name => $_, macro => $macro, value => 1,
837 default => ["IV", "0"]};
839 ExtUtils::Constant::WriteConstants(
840 NAME => 'Time::HiRes',
845 foreach $file ('const-c.inc', 'const-xs.inc') {
846 my $fallback = File::Spec->catfile('fallback', $file);
848 open IN, "<$fallback" or die "Can't open $fallback: $!";
849 open OUT, ">$file" or die "Can't open $file: $!";
850 print OUT <IN> or die $!;
851 close OUT or die "Can't close $file: $!";
852 close IN or die "Can't close $fallback: $!";
858 if (-f "xdefine" && !(@ARGV && $ARGV[0] eq '--configure')) {
859 print qq[$0: The "xdefine" exists, skipping the configure step.\n];
860 print qq[("$^X $0 --configure" to force the configure step)\n];
862 print "Configuring Time::HiRes...\n";
863 1 while unlink("define");
864 if ($^O =~ /Win32/i) {
865 DEFINE('SELECT_IS_BROKEN');
867 print "System is $^O, skipping full configure...\n";
868 open(XDEFINE, ">xdefine") or die "$0: Cannot create xdefine: $!\n";
876 my $make = $Config{'make'} || "make";
877 unless (exists $ENV{PERL_CORE} && $ENV{PERL_CORE}) {
879 Now you may issue '$make'. Do not forget also '$make test'.
882 ((exists $ENV{LC_ALL} && $ENV{LC_ALL} =~ /utf-?8/i) ||
883 (exists $ENV{LC_CTYPE} && $ENV{LC_CTYPE} =~ /utf-?8/i) ||
884 (exists $ENV{LANG} && $ENV{LANG} =~ /utf-?8/i))) {
887 NOTE: if you get an error like this (the Makefile line number may vary):
888 Makefile:91: *** missing separator
889 then set the environment variable LC_ALL to "C" and retry
890 from scratch (re-run perl "Makefile.PL").
891 (And consider upgrading your Perl to, say, at least Perl 5.8.8.)
892 (You got this message because you seem to have
893 an UTF-8 locale active in your shell environment, this used
894 to cause broken Makefiles to be created from Makefile.PLs)