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 my $ccflags = $Config{'ccflags'} . ' ' . "-I$COREincdir";
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";
113 my $perl_core = $Config{'installarchlib'};
114 $perl_core =~ s/\]$/.CORE]/;
115 $cccmd = "$Config{'cc'} /include=(perl_root:[000000],$perl_core) $tmp.c";
119 if ($args{silent} || !$VERBOSE) {
120 $errornull = "2>/dev/null" unless defined $errornull;
125 $cccmd = "$Config{'cc'} -o $tmp $ccflags $tmp.c @$LIBS $errornull"
126 unless defined $cccmd;
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
134 system("\@ $tmp.com");
136 for ("$tmp.c", "$tmp$obj_ext", "$tmp.com", "$tmp$Config{exe_ext}") {
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 _;
147 if ( $ok && exists $args{run} && $args{run}) {
149 File::Spec->catfile(File::Spec->curdir, $tmp_exe);
150 printf "Running $tmp_exe..." if $VERBOSE;
151 if (system($tmp_exe) == 0) {
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.
165 unlink("$tmp.c", $tmp_exe);
172 my $TIME_HEADERS = <<EOH;
177 # include <sys/types.h>
180 # include <sys/time.h>
183 # include <sys/select.h> /* struct timeval might be hidden in here */
187 sub has_gettimeofday {
188 # confusing but true (if condition true ==> -DHAS_GETTIMEOFDAY already)
189 return 0 if $Config{d_gettimeod};
190 return 1 if try_compile_and_link(<<EOM);
195 gettimeofday(&tv, 0);
197 int main _((int argc, char** argv, char** env))
209 try_compile_and_link(<<EOM, %args);
219 # include <sys/types.h>
223 # include <sys/time.h>
226 int main _((int argc, char** argv, char** env))
237 try_compile_and_link(<<EOM, run => 1);
239 #include <sys/time.h>
244 /* int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); */
247 struct timespec ts1, ts2;
250 ts1.tv_nsec = 750000000;
254 ret = nanosleep(&ts1, &ts2); /* E.g. in AIX nanosleep() fails and sets errno to ENOSYS. */
255 ret == 0 ? exit(0) : exit(errno ? errno : -1);
263 try_compile_and_link(<<EOM);
269 int main _((int argc, char** argv, char** env))
277 sub has_clock_xxx_syscall {
279 return 0 unless defined $SYSCALL_H;
281 try_compile_and_link(<<EOM, run => 1);
285 #include <$SYSCALL_H>
286 int main _((int argc, char** argv, char** env))
289 /* Many Linuxes get ENOSYS even though the syscall exists. */
290 /* All implementations are supposed to support CLOCK_REALTIME. */
291 int ret = syscall(SYS_clock_$x, CLOCK_REALTIME, &ts);
292 ret == 0 ? exit(0) : exit(errno ? errno : -1);
300 try_compile_and_link(<<EOM, run => 1);
304 int main _((int argc, char** argv, char** env))
307 int ret = clock_$xxx(CLOCK_REALTIME, &ts); /* Many Linuxes get ENOSYS. */
308 /* All implementations are supposed to support CLOCK_REALTIME. */
309 ret == 0 ? exit(0) : exit(errno ? errno : -1);
316 try_compile_and_link(<<EOM, run => 1);
320 int main _((int argc, char** argv, char** env))
323 clock_t ret = clock();
324 ret == (clock_t)-1 ? exit(errno ? errno : -1) : exit(0);
329 sub has_clock_nanosleep {
331 try_compile_and_link(<<EOM, run => 1);
335 int main _((int argc, char** argv, char** env))
338 struct timerspec ts1;
339 struct timerspec ts2;
341 ts1.tv_nsec = 750000000;;
342 ret = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts1, &ts2);
343 ret == 0 ? exit(0) : exit(errno ? errno : -1);
349 my $hints = File::Spec->catfile("hints", "$^O.pl");
351 print "Using hints $hints...\n";
354 if (exists $self->{LIBS}) {
355 $LIBS = $self->{LIBS};
356 print "Extra libraries: @$LIBS...\n";
362 if ($Config{d_syscall}) {
363 print "Have syscall()... looking for syscall.h... ";
364 if (has_include('syscall.h')) {
365 $SYSCALL_H = 'syscall.h';
366 } elsif (has_include('sys/syscall.h')) {
367 $SYSCALL_H = 'sys/syscall.h';
370 print "No syscall()...\n";
373 if ($Config{d_syscall}) {
374 if (defined $SYSCALL_H) {
375 print "found <$SYSCALL_H>.\n";
377 print "NOT found.\n";
381 print "Looking for gettimeofday()... ";
382 my $has_gettimeofday;
383 if (exists $Config{d_gettimeod}) {
384 $has_gettimeofday++ if $Config{d_gettimeod};
385 } elsif (has_gettimeofday()) {
386 $DEFINE .= ' -DHAS_GETTIMEOFDAY';
390 if ($has_gettimeofday) {
394 Your operating system does not seem to have the gettimeofday() function.
395 (or, at least, I cannot find it)
397 There is no way Time::HiRes is going to work.
399 I am awfully sorry but I cannot go further.
401 Aborting configuration.
406 print "Looking for setitimer()... ";
408 if (exists $Config{d_setitimer}) {
409 $has_setitimer++ if $Config{d_setitimer};
410 } elsif (has_x("setitimer(ITIMER_REAL, 0, 0)")) {
412 $DEFINE .= ' -DHAS_SETITIMER';
415 if ($has_setitimer) {
418 print "NOT found.\n";
421 print "Looking for getitimer()... ";
423 if (exists $Config{'d_getitimer'}) {
424 $has_getitimer++ if $Config{'d_getitimer'};
425 } elsif (has_x("getitimer(ITIMER_REAL, 0)")) {
427 $DEFINE .= ' -DHAS_GETITIMER';
430 if ($has_getitimer) {
433 print "NOT found.\n";
436 if ($has_setitimer && $has_getitimer) {
437 print "You have interval timers (both setitimer and getitimer).\n";
439 print "You do not have interval timers.\n";
442 print "Looking for ualarm()... ";
444 if (exists $Config{d_ualarm}) {
445 $has_ualarm++ if $Config{d_ualarm};
446 } elsif (has_x ("ualarm (0, 0)")) {
448 $DEFINE .= ' -DHAS_UALARM';
454 print "NOT found.\n";
455 if ($has_setitimer) {
456 print "But you have setitimer().\n";
457 print "We can make a Time::HiRes::ualarm().\n";
461 print "Looking for usleep()... ";
463 if (exists $Config{d_usleep}) {
464 $has_usleep++ if $Config{d_usleep};
465 } elsif (has_x ("usleep (0)")) {
467 $DEFINE .= ' -DHAS_USLEEP';
473 print "NOT found.\n";
474 print "Let's see if you have select()... ";
475 if ($Config{'d_select'}) {
477 print "We can make a Time::HiRes::usleep().\n";
479 print "NOT found.\n";
480 print "You won't have a Time::HiRes::usleep().\n";
484 print "Looking for nanosleep()... ";
486 if ($ENV{FORCE_NANOSLEEP_SCAN}) {
487 print "forced scan... ";
488 if (has_nanosleep()) {
490 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
493 elsif (exists $Config{d_nanosleep}) {
494 print "believing \$Config{d_nanosleep}... ";
495 if ($Config{d_nanosleep}) {
497 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
499 } elsif ($^O =~ /^(mpeix)$/) {
500 # MPE/iX falsely finds nanosleep from its libc equivalent.
501 print "skipping because in $^O... ";
503 if (has_nanosleep()) {
505 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
509 if ($has_nanosleep) {
511 print "You can mix subsecond sleeps with signals, if you want to.\n";
512 print "(It's still not portable, though.)\n";
514 print "NOT found.\n";
515 my $nt = ($^O eq 'os2' ? '' : 'not');
516 print "You can$nt mix subsecond sleeps with signals.\n";
517 print "(It would not be portable anyway.)\n";
520 print "Looking for clock_gettime()... ";
521 my $has_clock_gettime;
522 if (exists $Config{d_clock_gettime}) {
523 $has_clock_gettime++ if $Config{d_clock_gettime}; # Unlikely...
524 } elsif (has_clock_xxx('gettime')) {
525 $has_clock_gettime++;
526 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME';
527 } elsif (defined $SYSCALL_H && has_clock_xxx_syscall('gettime')) {
528 $has_clock_gettime++;
529 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME -DTIME_HIRES_CLOCK_GETTIME_SYSCALL';
532 if ($has_clock_gettime) {
533 if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETTIME_SYSCALL/) {
534 print "found (via syscall).\n";
539 print "NOT found.\n";
542 print "Looking for clock_getres()... ";
543 my $has_clock_getres;
544 if (exists $Config{d_clock_getres}) {
545 $has_clock_getres++ if $Config{d_clock_getres}; # Unlikely...
546 } elsif (has_clock_xxx('getres')) {
548 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES';
549 } elsif (defined $SYSCALL_H && has_clock_xxx_syscall('getres')) {
551 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES -DTIME_HIRES_CLOCK_GETRES_SYSCALL';
554 if ($has_clock_getres) {
555 if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETRES_SYSCALL/) {
556 print "found (via syscall).\n";
561 print "NOT found.\n";
564 print "Looking for clock_nanosleep()... ";
565 my $has_clock_nanosleep;
566 if (exists $Config{d_clock_nanosleep}) {
567 $has_clock_nanosleep++ if $Config{d_clock_nanosleep}; # Unlikely...
568 } elsif (has_clock_nanosleep()) {
569 $has_clock_nanosleep++;
570 $DEFINE .= ' -DTIME_HIRES_CLOCK_NANOSLEEP';
573 if ($has_clock_nanosleep) {
576 print "NOT found.\n";
579 print "Looking for clock()... ";
581 if (exists $Config{d_clock}) {
582 $has_clock++ if $Config{d_clock}; # Unlikely...
583 } elsif (has_clock()) {
585 $DEFINE .= ' -DTIME_HIRES_CLOCK';
591 print "NOT found.\n";
594 print "Trying struct stat st_atimespec.tv_nsec...";
595 my $has_stat_st_xtimespec;
596 if (try_compile_and_link(<<EOM)) {
598 #include <sys/stat.h>
601 st.st_atimespec.tv_nsec = 0;
604 $has_stat_st_xtimespec++;
605 $DEFINE .= ' -DTIME_HIRES_STAT=1';
608 if ($has_stat_st_xtimespec) {
611 print "NOT found.\n";
614 print "Trying struct stat st_atimensec...";
615 my $has_stat_st_xtimensec;
616 if (try_compile_and_link(<<EOM)) {
618 #include <sys/stat.h>
624 $has_stat_st_xtimensec++;
625 $DEFINE .= ' -DTIME_HIRES_STAT=2';
628 if ($has_stat_st_xtimensec) {
631 print "NOT found.\n";
634 print "Trying struct stat st_atime_n...";
635 my $has_stat_st_xtime_n;
636 if (try_compile_and_link(<<EOM)) {
638 #include <sys/stat.h>
644 $has_stat_st_xtime_n++;
645 $DEFINE .= ' -DTIME_HIRES_STAT=3';
648 if ($has_stat_st_xtime_n) {
651 print "NOT found.\n";
654 print "Trying struct stat st_atim.tv_nsec...";
655 my $has_stat_st_xtim;
656 if (try_compile_and_link(<<EOM)) {
658 #include <sys/stat.h>
661 st.st_atim.tv_nsec = 0;
665 $DEFINE .= ' -DTIME_HIRES_STAT=4';
668 if ($has_stat_st_xtim) {
671 print "NOT found.\n";
674 print "Trying struct stat st_uatime...";
675 my $has_stat_st_uxtime;
676 if (try_compile_and_link(<<EOM)) {
678 #include <sys/stat.h>
684 $has_stat_st_uxtime++;
685 $DEFINE .= ' -DTIME_HIRES_STAT=5';
688 if ($has_stat_st_uxtime) {
691 print "NOT found.\n";
694 if ($DEFINE =~ /-DTIME_HIRES_STAT=\d+/) {
695 print "You seem to have stat subsecond timestamps.\n";
696 print "(Your struct stat has them, but the filesystems must help.)\n";
698 print "You do not seem to have stat subsecond timestamps.\n";
701 my $has_w32api_windows_h;
703 if ($^O eq 'cygwin') {
704 print "Looking for <w32api/windows.h>... ";
705 if (has_include('w32api/windows.h')) {
706 $has_w32api_windows_h++;
707 $DEFINE .= ' -DHAS_W32API_WINDOWS_H';
709 if ($has_w32api_windows_h) {
712 print "NOT found.\n";
718 if (open(XDEFINE, ">xdefine")) {
719 print XDEFINE $DEFINE, "\n";
726 my @makefileopts = ();
730 'AUTHOR' => 'Jarkko Hietaniemi <jhi@iki.fi>',
731 'ABSTRACT_FROM' => 'HiRes.pm',
733 $DEFINE .= " -DATLEASTFIVEOHOHFIVE";
737 'NAME' => 'Time::HiRes',
738 'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
739 'LIBS' => $LIBS, # e.g., '-lm'
740 'DEFINE' => $DEFINE, # e.g., '-DHAS_SOMETHING'
742 # Do not even think about 'INC' => '-I/usr/ucbinclude',
743 # Solaris will avenge.
744 'INC' => '', # e.g., '-I/usr/include/other'
745 'INSTALLDIRS' => ($] >= 5.008 ? 'perl' : 'site'),
748 'COMPRESS' => 'gzip -9f',
751 clean => { FILES => "xdefine" },
752 realclean => { FILES=> 'const-c.inc const-xs.inc' },
755 if ($ENV{PERL_CORE}) {
756 push @makefileopts, MAN3PODS => {};
759 WriteMakefile(@makefileopts);
763 if (eval {require ExtUtils::Constant; 1}) {
764 my @names = qw(CLOCK_HIGHRES CLOCK_MONOTONIC
765 CLOCK_PROCESS_CPUTIME_ID
768 CLOCK_THREAD_CPUTIME_ID
771 ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF
774 foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
775 d_nanosleep d_clock_gettime d_clock_getres
776 d_clock d_clock_nanosleep d_hires_stat)) {
778 if ($macro =~ /^(d_nanosleep|d_clock_gettime|d_clock_getres|d_clock|d_clock_nanosleep)$/) {
779 $macro =~ s/^d_(.+)/TIME_HIRES_\U$1/;
780 } elsif ($macro =~ /^(d_hires_stat)$/) {
781 my $d_hires_stat = 0;
782 $d_hires_stat = $1 if ($DEFINE =~ /-DTIME_HIRES_STAT=(\d+)/);
783 push @names, {name => $_, macro => "TIME_HIRES_STAT", value => $d_hires_stat,
784 default => ["IV", "0"]};
787 $macro =~ s/^d_(.+)/HAS_\U$1/;
789 push @names, {name => $_, macro => $macro, value => 1,
790 default => ["IV", "0"]};
792 ExtUtils::Constant::WriteConstants(
793 NAME => 'Time::HiRes',
798 foreach $file ('const-c.inc', 'const-xs.inc') {
799 my $fallback = File::Spec->catfile('fallback', $file);
801 open IN, "<$fallback" or die "Can't open $fallback: $!";
802 open OUT, ">$file" or die "Can't open $file: $!";
803 print OUT <IN> or die $!;
804 close OUT or die "Can't close $file: $!";
805 close IN or die "Can't close $fallback: $!";
811 print "Configuring Time::HiRes...\n";
812 if ($] == 5.007002) {
813 die "Cannot Configure Time::HiRes for Perl $], aborting.\n";
816 if ($^O =~ /Win32/i) {
817 $DEFINE = '-DSELECT_IS_BROKEN';
819 print "System is $^O, skipping full configure...\n";
825 my $make = $Config{'make'} || "make";
826 unless (exists $ENV{PERL_CORE} && $ENV{PERL_CORE}) {
828 Now you may issue '$make'. Do not forget also '$make test'.
830 if ((exists $ENV{LC_ALL} && $ENV{LC_ALL} =~ /utf-?8/i) ||
831 (exists $ENV{LC_CTYPE} && $ENV{LC_CTYPE} =~ /utf-?8/i) ||
832 (exists $ENV{LANG} && $ENV{LANG} =~ /utf-?8/i)) {
834 NOTE: if you get an error like this (the Makefile line number may vary):
835 Makefile:91: *** missing separator
836 then set the environment variable LC_ALL to "C" and retry
837 from scratch (re-run perl "Makefile.PL").