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 use vars qw($self); # Used in 'sourcing' the hints.
22 my $ld_exeext = ($^O eq 'cygwin' ||
23 $^O eq 'os2' && $Config{ldflags} =~ /-Zexe\b/) ? '.exe' : '';
25 unless($ENV{PERL_CORE}) {
26 $ENV{PERL_CORE} = 1 if grep { $_ eq 'PERL_CORE=1' } @ARGV;
29 # Perls 5.002 and 5.003 did not have File::Spec, fake what we need.
33 $^O =~ /mswin32|netware|djgpp/i ? '\\' :
40 my $catdir = join(my_dirsep, @_);
41 $^O eq 'VMS' ? "[$catdir]" : $catdir;
46 return join(my_dirsep, @_) unless $^O eq 'VMS';
48 return my_catdir (undef, @_) . $file;
53 $^O eq 'VMS' ? "-" : "..";
57 eval { require File::Spec };
59 *File::Spec::catdir = \&my_catdir;
60 *File::Spec::updir = \&my_updir;
61 *File::Spec::catfile = \&my_catfile;
65 # Avoid 'used only once' warnings.
66 my $nop1 = *File::Spec::catdir;
67 my $nop2 = *File::Spec::updir;
68 my $nop3 = *File::Spec::catfile;
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 :)
78 sub try_compile_and_link {
85 my $obj_ext = $Config{obj_ext} || ".o";
86 unlink("$tmp.c", "$tmp$obj_ext");
88 if (open(TMPC, ">$tmp.c")) {
92 my $cccmd = $args{cccmd};
98 if ($ENV{PERL_CORE}) {
99 my $updir = File::Spec->updir;
100 $COREincdir = File::Spec->catdir(($updir) x 3);
102 $COREincdir = File::Spec->catdir($Config{'archlibexp'}, 'CORE');
105 if ($ENV{PERL_CORE}) {
106 unless (-f File::Spec->catfile($COREincdir, "EXTERN.h")) {
108 Your environment variable PERL_CORE is '$ENV{PERL_CORE}' but there
109 is no EXTERN.h in $COREincdir.
110 Cannot continue, aborting.
115 my $ccflags = $Config{'ccflags'} . ' ' . "-I$COREincdir";
118 if ($ENV{PERL_CORE}) {
119 # Fragile if the extensions change hierarchy within
120 # the Perl core but this should do for now.
121 $cccmd = "$Config{'cc'} /include=([---]) $tmp.c";
123 my $perl_core = $Config{'installarchlib'};
124 $perl_core =~ s/\]$/.CORE]/;
125 $cccmd = "$Config{'cc'} /include=(perl_root:[000000],$perl_core) $tmp.c";
129 if ($args{silent} || !$VERBOSE) {
130 $errornull = "2>/dev/null" unless defined $errornull;
135 $cccmd = "$Config{'cc'} -o $tmp $ccflags $tmp.c @$LIBS $errornull"
136 unless defined $cccmd;
139 open( CMDFILE, ">$tmp.com" );
140 print CMDFILE "\$ SET MESSAGE/NOFACILITY/NOSEVERITY/NOIDENT/NOTEXT\n";
141 print CMDFILE "\$ $cccmd\n";
142 print CMDFILE "\$ IF \$SEVERITY .NE. 1 THEN EXIT 44\n"; # escalate
144 system("\@ $tmp.com");
146 for ("$tmp.c", "$tmp$obj_ext", "$tmp.com", "$tmp$Config{exe_ext}") {
152 my $tmp_exe = "$tmp$ld_exeext";
153 printf "cccmd = $cccmd\n" if $VERBOSE;
154 my $res = system($cccmd);
155 $ok = defined($res) && $res == 0 && -s $tmp_exe && -x _;
157 if ( $ok && exists $args{run} && $args{run}) {
159 File::Spec->catfile(File::Spec->curdir, $tmp_exe);
160 printf "Running $tmp_exe..." if $VERBOSE;
161 if (system($tmp_exe) == 0) {
169 *** The test run of '$tmp_exe' failed: status $?
170 *** (the status means: errno = $errno or '$!')
171 *** DO NOT PANIC: this just means that *some* functionality will be missing.
175 unlink("$tmp.c", $tmp_exe);
182 my $TIME_HEADERS = <<EOH;
187 # include <sys/types.h>
190 # include <sys/time.h>
193 # include <sys/select.h> /* struct timeval might be hidden in here */
197 sub has_gettimeofday {
198 # confusing but true (if condition true ==> -DHAS_GETTIMEOFDAY already)
199 return 0 if $Config{d_gettimeod};
200 return 1 if try_compile_and_link(<<EOM);
205 gettimeofday(&tv, 0);
207 int main(int argc, char** argv)
219 try_compile_and_link(<<EOM, %args);
229 # include <sys/types.h>
233 # include <sys/time.h>
236 int main(int argc, char** argv)
247 try_compile_and_link(<<EOM, run => 1);
249 #include <sys/time.h>
254 /* int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); */
256 int main(int argc, char** argv) {
257 struct timespec ts1, ts2;
260 ts1.tv_nsec = 750000000;
264 ret = nanosleep(&ts1, &ts2); /* E.g. in AIX nanosleep() fails and sets errno to ENOSYS. */
265 ret == 0 ? exit(0) : exit(errno ? errno : -1);
273 try_compile_and_link(<<EOM);
279 int main(int argc, char** argv)
287 sub has_clock_xxx_syscall {
289 return 0 unless defined $SYSCALL_H;
291 try_compile_and_link(<<EOM, run => 1);
295 #include <$SYSCALL_H>
296 int main(int argc, char** argv)
299 /* Many Linuxes get ENOSYS even though the syscall exists. */
300 /* All implementations are supposed to support CLOCK_REALTIME. */
301 int ret = syscall(SYS_clock_$x, CLOCK_REALTIME, &ts);
302 ret == 0 ? exit(0) : exit(errno ? errno : -1);
310 try_compile_and_link(<<EOM, run => 1);
314 int main(int argc, char** argv)
317 int ret = clock_$xxx(CLOCK_REALTIME, &ts); /* Many Linuxes get ENOSYS. */
318 /* All implementations are supposed to support CLOCK_REALTIME. */
319 ret == 0 ? exit(0) : exit(errno ? errno : -1);
326 try_compile_and_link(<<EOM, run => 1);
330 int main(int argc, char** argv)
333 clock_t ret = clock();
334 ret == (clock_t)-1 ? exit(errno ? errno : -1) : exit(0);
339 sub has_clock_nanosleep {
341 try_compile_and_link(<<EOM, run => 1);
346 int main(int argc, char** argv)
352 ts1.tv_nsec = 750000000;;
353 ret = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts1, &ts2);
354 ret == 0 ? exit(0) : exit(errno ? errno : -1);
360 my ($def, $val) = @_;
361 my $define = defined $val ? "$def=$val" : $def ;
362 unless ($DEFINE =~ /(?:^| )-D\Q$define\E(?: |$)/) {
363 $DEFINE .= " -D$define";
368 my $hints = File::Spec->catfile("hints", "$^O.pl");
370 print "Using hints $hints...\n";
373 if (exists $self->{LIBS}) {
374 $LIBS = $self->{LIBS};
375 print "Extra libraries: @$LIBS...\n";
381 if ($Config{d_syscall}) {
382 print "Have syscall()... looking for syscall.h... ";
383 if (has_include('syscall.h')) {
384 $SYSCALL_H = 'syscall.h';
385 } elsif (has_include('sys/syscall.h')) {
386 $SYSCALL_H = 'sys/syscall.h';
389 print "No syscall()...\n";
392 if ($Config{d_syscall}) {
393 if (defined $SYSCALL_H) {
394 print "found <$SYSCALL_H>.\n";
396 print "NOT found.\n";
400 print "Looking for gettimeofday()... ";
401 my $has_gettimeofday;
402 if (exists $Config{d_gettimeod}) {
403 $has_gettimeofday++ if $Config{d_gettimeod};
404 } elsif (has_gettimeofday()) {
405 $DEFINE .= ' -DHAS_GETTIMEOFDAY';
409 if ($has_gettimeofday) {
413 Your operating system does not seem to have the gettimeofday() function.
414 (or, at least, I cannot find it)
416 There is no way Time::HiRes is going to work.
418 I am awfully sorry but I cannot go further.
420 Aborting configuration.
425 print "Looking for setitimer()... ";
427 if (exists $Config{d_setitimer}) {
428 $has_setitimer++ if $Config{d_setitimer};
429 } elsif (has_x("setitimer(ITIMER_REAL, 0, 0)")) {
431 $DEFINE .= ' -DHAS_SETITIMER';
434 if ($has_setitimer) {
437 print "NOT found.\n";
440 print "Looking for getitimer()... ";
442 if (exists $Config{'d_getitimer'}) {
443 $has_getitimer++ if $Config{'d_getitimer'};
444 } elsif (has_x("getitimer(ITIMER_REAL, 0)")) {
446 $DEFINE .= ' -DHAS_GETITIMER';
449 if ($has_getitimer) {
452 print "NOT found.\n";
455 if ($has_setitimer && $has_getitimer) {
456 print "You have interval timers (both setitimer and getitimer).\n";
458 print "You do not have interval timers.\n";
461 print "Looking for ualarm()... ";
463 if (exists $Config{d_ualarm}) {
464 $has_ualarm++ if $Config{d_ualarm};
465 } elsif (has_x ("ualarm (0, 0)")) {
467 $DEFINE .= ' -DHAS_UALARM';
473 print "NOT found.\n";
474 if ($has_setitimer) {
475 print "But you have setitimer().\n";
476 print "We can make a Time::HiRes::ualarm().\n";
480 print "Looking for usleep()... ";
482 if (exists $Config{d_usleep}) {
483 $has_usleep++ if $Config{d_usleep};
484 } elsif (has_x ("usleep (0)")) {
486 $DEFINE .= ' -DHAS_USLEEP';
492 print "NOT found.\n";
493 print "Let's see if you have select()... ";
494 if ($Config{'d_select'}) {
496 print "We can make a Time::HiRes::usleep().\n";
498 print "NOT found.\n";
499 print "You won't have a Time::HiRes::usleep().\n";
503 print "Looking for nanosleep()... ";
505 if ($ENV{FORCE_NANOSLEEP_SCAN}) {
506 print "forced scan... ";
507 if (has_nanosleep()) {
509 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
512 elsif (exists $Config{d_nanosleep}) {
513 print "believing \$Config{d_nanosleep}... ";
514 if ($Config{d_nanosleep}) {
516 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
518 } elsif ($^O =~ /^(mpeix)$/) {
519 # MPE/iX falsely finds nanosleep from its libc equivalent.
520 print "skipping because in $^O... ";
522 if (has_nanosleep()) {
524 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
528 if ($has_nanosleep) {
530 print "You can mix subsecond sleeps with signals, if you want to.\n";
531 print "(It's still not portable, though.)\n";
533 print "NOT found.\n";
534 my $nt = ($^O eq 'os2' ? '' : 'not');
535 print "You can$nt mix subsecond sleeps with signals.\n";
536 print "(It would not be portable anyway.)\n";
539 print "Looking for clock_gettime()... ";
540 my $has_clock_gettime;
541 if (exists $Config{d_clock_gettime}) {
542 $has_clock_gettime++ if $Config{d_clock_gettime}; # Unlikely...
543 } elsif (has_clock_xxx('gettime')) {
544 $has_clock_gettime++;
545 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME';
546 } elsif (defined $SYSCALL_H && has_clock_xxx_syscall('gettime')) {
547 $has_clock_gettime++;
548 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME -DTIME_HIRES_CLOCK_GETTIME_SYSCALL';
551 if ($has_clock_gettime) {
552 if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETTIME_SYSCALL/) {
553 print "found (via syscall).\n";
558 print "NOT found.\n";
561 print "Looking for clock_getres()... ";
562 my $has_clock_getres;
563 if (exists $Config{d_clock_getres}) {
564 $has_clock_getres++ if $Config{d_clock_getres}; # Unlikely...
565 } elsif (has_clock_xxx('getres')) {
567 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES';
568 } elsif (defined $SYSCALL_H && has_clock_xxx_syscall('getres')) {
570 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES -DTIME_HIRES_CLOCK_GETRES_SYSCALL';
573 if ($has_clock_getres) {
574 if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETRES_SYSCALL/) {
575 print "found (via syscall).\n";
580 print "NOT found.\n";
583 print "Looking for clock_nanosleep()... ";
584 my $has_clock_nanosleep;
585 if (exists $Config{d_clock_nanosleep}) {
586 $has_clock_nanosleep++ if $Config{d_clock_nanosleep}; # Unlikely...
587 } elsif (has_clock_nanosleep()) {
588 $has_clock_nanosleep++;
589 $DEFINE .= ' -DTIME_HIRES_CLOCK_NANOSLEEP';
592 if ($has_clock_nanosleep) {
595 print "NOT found.\n";
598 print "Looking for clock()... ";
600 if (exists $Config{d_clock}) {
601 $has_clock++ if $Config{d_clock}; # Unlikely...
602 } elsif (has_clock()) {
604 $DEFINE .= ' -DTIME_HIRES_CLOCK';
610 print "NOT found.\n";
613 print "Looking for stat() subsecond timestamps...\n";
615 print "Trying struct stat st_atimespec.tv_nsec...";
616 my $has_stat_st_xtimespec;
617 if (try_compile_and_link(<<EOM)) {
619 #include <sys/stat.h>
620 int main(int argc, char** argv) {
622 st.st_atimespec.tv_nsec = 0;
625 $has_stat_st_xtimespec++;
626 DEFINE('TIME_HIRES_STAT', 1);
629 if ($has_stat_st_xtimespec) {
632 print "NOT found.\n";
635 print "Trying struct stat st_atimensec...";
636 my $has_stat_st_xtimensec;
637 if (try_compile_and_link(<<EOM)) {
639 #include <sys/stat.h>
640 int main(int argc, char** argv) {
645 $has_stat_st_xtimensec++;
646 DEFINE('TIME_HIRES_STAT', 2);
649 if ($has_stat_st_xtimensec) {
652 print "NOT found.\n";
655 print "Trying struct stat st_atime_n...";
656 my $has_stat_st_xtime_n;
657 if (try_compile_and_link(<<EOM)) {
659 #include <sys/stat.h>
660 int main(int argc, char** argv) {
665 $has_stat_st_xtime_n++;
666 DEFINE('TIME_HIRES_STAT', 3);
669 if ($has_stat_st_xtime_n) {
672 print "NOT found.\n";
675 print "Trying struct stat st_atim.tv_nsec...";
676 my $has_stat_st_xtim;
677 if (try_compile_and_link(<<EOM)) {
679 #include <sys/stat.h>
680 int main(int argc, char** argv) {
682 st.st_atim.tv_nsec = 0;
686 DEFINE('TIME_HIRES_STAT', 4);
689 if ($has_stat_st_xtim) {
692 print "NOT found.\n";
695 print "Trying struct stat st_uatime...";
696 my $has_stat_st_uxtime;
697 if (try_compile_and_link(<<EOM)) {
699 #include <sys/stat.h>
700 int main(int argc, char** argv) {
705 $has_stat_st_uxtime++;
706 DEFINE('TIME_HIRES_STAT', 5);
709 if ($has_stat_st_uxtime) {
712 print "NOT found.\n";
715 if ($DEFINE =~ /-DTIME_HIRES_STAT=\d+/) {
716 print "You seem to have stat() subsecond timestamps.\n";
717 print "(Your struct stat has them, but the filesystems must help.)\n";
719 print "You do not seem to have stat subsecond timestamps.\n";
722 my $has_w32api_windows_h;
724 if ($^O eq 'cygwin') {
725 print "Looking for <w32api/windows.h>... ";
726 if (has_include('w32api/windows.h')) {
727 $has_w32api_windows_h++;
728 DEFINE('HAS_W32API_WINDOWS_H');
730 if ($has_w32api_windows_h) {
733 print "NOT found.\n";
739 if (open(XDEFINE, ">xdefine")) {
740 print XDEFINE $DEFINE, "\n";
747 my @makefileopts = ();
751 'AUTHOR' => 'Jarkko Hietaniemi <jhi@iki.fi>',
752 'ABSTRACT_FROM' => 'HiRes.pm',
754 DEFINE('ATLEASTFIVEOHOHFIVE');
758 'NAME' => 'Time::HiRes',
759 'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
760 'LIBS' => $LIBS, # e.g., '-lm'
761 'DEFINE' => $DEFINE, # e.g., '-DHAS_SOMETHING'
763 # Do not even think about 'INC' => '-I/usr/ucbinclude',
764 # Solaris will avenge.
765 'INC' => '', # e.g., '-I/usr/include/other'
766 'INSTALLDIRS' => ($] >= 5.008 ? 'perl' : 'site'),
769 'COMPRESS' => 'gzip -9f',
772 clean => { FILES => "xdefine" },
773 realclean => { FILES=> 'const-c.inc const-xs.inc' },
776 if ($ENV{PERL_CORE}) {
777 push @makefileopts, MAN3PODS => {};
780 WriteMakefile(@makefileopts);
784 if (eval {require ExtUtils::Constant; 1}) {
785 my @names = qw(CLOCK_HIGHRES CLOCK_MONOTONIC
786 CLOCK_PROCESS_CPUTIME_ID
789 CLOCK_THREAD_CPUTIME_ID
792 ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF
795 foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
796 d_nanosleep d_clock_gettime d_clock_getres
797 d_clock d_clock_nanosleep d_hires_stat)) {
799 if ($macro =~ /^(d_nanosleep|d_clock_gettime|d_clock_getres|d_clock|d_clock_nanosleep)$/) {
800 $macro =~ s/^d_(.+)/TIME_HIRES_\U$1/;
801 } elsif ($macro =~ /^(d_hires_stat)$/) {
802 my $d_hires_stat = 0;
803 $d_hires_stat = $1 if ($DEFINE =~ /-DTIME_HIRES_STAT=(\d+)/);
804 push @names, {name => $_, macro => "TIME_HIRES_STAT", value => $d_hires_stat,
805 default => ["IV", "0"]};
808 $macro =~ s/^d_(.+)/HAS_\U$1/;
810 push @names, {name => $_, macro => $macro, value => 1,
811 default => ["IV", "0"]};
813 ExtUtils::Constant::WriteConstants(
814 NAME => 'Time::HiRes',
819 foreach $file ('const-c.inc', 'const-xs.inc') {
820 my $fallback = File::Spec->catfile('fallback', $file);
822 open IN, "<$fallback" or die "Can't open $fallback: $!";
823 open OUT, ">$file" or die "Can't open $file: $!";
824 print OUT <IN> or die $!;
825 close OUT or die "Can't close $file: $!";
826 close IN or die "Can't close $fallback: $!";
832 print "Configuring Time::HiRes...\n";
833 if ($] == 5.007002) {
834 die "Cannot Configure Time::HiRes for Perl $], aborting.\n";
837 if ($^O =~ /Win32/i) {
838 DEFINE('SELECT_IS_BROKEN');
840 print "System is $^O, skipping full configure...\n";
846 my $make = $Config{'make'} || "make";
847 unless (exists $ENV{PERL_CORE} && $ENV{PERL_CORE}) {
849 Now you may issue '$make'. Do not forget also '$make test'.
851 if ((exists $ENV{LC_ALL} && $ENV{LC_ALL} =~ /utf-?8/i) ||
852 (exists $ENV{LC_CTYPE} && $ENV{LC_CTYPE} =~ /utf-?8/i) ||
853 (exists $ENV{LANG} && $ENV{LANG} =~ /utf-?8/i)) {
855 NOTE: if you get an error like this (the Makefile line number may vary):
856 Makefile:91: *** missing separator
857 then set the environment variable LC_ALL to "C" and retry
858 from scratch (re-run perl "Makefile.PL").
859 (And consider upgrading your Perl.)