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 # 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 3);
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";
121 if ($ENV{PERL_CORE}) {
122 # Fragile if the extensions change hierarchy within
123 # the Perl core but this should do for now.
124 $cccmd = "$Config{'cc'} /include=([---]) $tmp.c";
126 my $perl_core = $Config{'installarchlib'};
127 $perl_core =~ s/\]$/.CORE]/;
128 $cccmd = "$Config{'cc'} /include=(perl_root:[000000],$perl_core) $tmp.c";
132 if ($args{silent} || !$VERBOSE) {
133 $errornull = "2>/dev/null" unless defined $errornull;
138 $cccmd = "$Config{'cc'} -o $tmp $ccflags $tmp.c @$LIBS $errornull"
139 unless defined $cccmd;
142 open( CMDFILE, ">$tmp.com" );
143 print CMDFILE "\$ SET MESSAGE/NOFACILITY/NOSEVERITY/NOIDENT/NOTEXT\n";
144 print CMDFILE "\$ $cccmd\n";
145 print CMDFILE "\$ IF \$SEVERITY .NE. 1 THEN EXIT 44\n"; # escalate
147 system("\@ $tmp.com");
149 for ("$tmp.c", "$tmp$obj_ext", "$tmp.com", "$tmp$Config{exe_ext}") {
155 my $tmp_exe = "$tmp$ld_exeext";
156 printf "cccmd = $cccmd\n" if $VERBOSE;
157 my $res = system($cccmd);
158 $ok = defined($res) && $res == 0 && -s $tmp_exe && -x _;
160 if ( $ok && exists $args{run} && $args{run}) {
162 File::Spec->catfile(File::Spec->curdir, $tmp_exe);
163 printf "Running $tmp_exe..." if $VERBOSE;
164 if (system($tmp_exe) == 0) {
172 *** The test run of '$tmp_exe' failed: status $?
173 *** (the status means: errno = $errno or '$!')
174 *** DO NOT PANIC: this just means that *some* functionality will be missing.
178 unlink("$tmp.c", $tmp_exe);
185 my $TIME_HEADERS = <<EOH;
190 # include <sys/types.h>
193 # include <sys/time.h>
196 # include <sys/select.h> /* struct timeval might be hidden in here */
200 sub has_gettimeofday {
201 # confusing but true (if condition true ==> -DHAS_GETTIMEOFDAY already)
202 return 0 if $Config{d_gettimeod};
203 return 1 if try_compile_and_link(<<EOM);
208 gettimeofday(&tv, 0);
210 int main(int argc, char** argv)
222 try_compile_and_link(<<EOM, %args);
232 # include <sys/types.h>
236 # include <sys/time.h>
239 int main(int argc, char** argv)
250 try_compile_and_link(<<EOM, run => 1);
252 #include <sys/time.h>
257 /* int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); */
259 int main(int argc, char** argv) {
260 struct timespec ts1, ts2;
263 ts1.tv_nsec = 750000000;
267 ret = nanosleep(&ts1, &ts2); /* E.g. in AIX nanosleep() fails and sets errno to ENOSYS. */
268 ret == 0 ? exit(0) : exit(errno ? errno : -1);
276 try_compile_and_link(<<EOM);
282 int main(int argc, char** argv)
290 sub has_clock_xxx_syscall {
292 return 0 unless defined $SYSCALL_H;
294 try_compile_and_link(<<EOM, run => 1);
298 #include <$SYSCALL_H>
299 int main(int argc, char** argv)
302 /* Many Linuxes get ENOSYS even though the syscall exists. */
303 /* All implementations are supposed to support CLOCK_REALTIME. */
304 int ret = syscall(SYS_clock_$x, CLOCK_REALTIME, &ts);
305 ret == 0 ? exit(0) : exit(errno ? errno : -1);
313 try_compile_and_link(<<EOM, run => 1);
317 int main(int argc, char** argv)
320 int ret = clock_$xxx(CLOCK_REALTIME, &ts); /* Many Linuxes get ENOSYS. */
321 /* All implementations are supposed to support CLOCK_REALTIME. */
322 ret == 0 ? exit(0) : exit(errno ? errno : -1);
329 try_compile_and_link(<<EOM, run => 1);
333 int main(int argc, char** argv)
336 clock_t ret = clock();
337 ret == (clock_t)-1 ? exit(errno ? errno : -1) : exit(0);
342 sub has_clock_nanosleep {
344 try_compile_and_link(<<EOM, run => 1);
349 int main(int argc, char** argv)
355 ts1.tv_nsec = 750000000;;
356 ret = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts1, &ts2);
357 ret == 0 ? exit(0) : exit(errno ? errno : -1);
363 my ($def, $val) = @_;
364 my $define = defined $val ? "$def=$val" : $def ;
365 unless ($DEFINE =~ /(?:^| )-D\Q$define\E(?: |$)/) {
366 $DEFINE .= " -D$define";
371 my $hints = File::Spec->catfile("hints", "$^O.pl");
373 print "Using hints $hints...\n";
376 if (exists $self->{LIBS}) {
377 $LIBS = $self->{LIBS};
378 print "Extra libraries: @$LIBS...\n";
384 if ($Config{d_syscall}) {
385 print "Have syscall()... looking for syscall.h... ";
386 if (has_include('syscall.h')) {
387 $SYSCALL_H = 'syscall.h';
388 } elsif (has_include('sys/syscall.h')) {
389 $SYSCALL_H = 'sys/syscall.h';
392 print "No syscall()...\n";
395 if ($Config{d_syscall}) {
396 if (defined $SYSCALL_H) {
397 print "found <$SYSCALL_H>.\n";
399 print "NOT found.\n";
403 print "Looking for gettimeofday()... ";
404 my $has_gettimeofday;
405 if (exists $Config{d_gettimeod}) {
406 $has_gettimeofday++ if $Config{d_gettimeod};
407 } elsif (has_gettimeofday()) {
408 $DEFINE .= ' -DHAS_GETTIMEOFDAY';
412 if ($has_gettimeofday) {
416 Your operating system does not seem to have the gettimeofday() function.
417 (or, at least, I cannot find it)
419 There is no way Time::HiRes is going to work.
421 I am awfully sorry but I cannot go further.
423 Aborting configuration.
428 print "Looking for setitimer()... ";
430 if (exists $Config{d_setitimer}) {
431 $has_setitimer++ if $Config{d_setitimer};
432 } elsif (has_x("setitimer(ITIMER_REAL, 0, 0)")) {
434 $DEFINE .= ' -DHAS_SETITIMER';
437 if ($has_setitimer) {
440 print "NOT found.\n";
443 print "Looking for getitimer()... ";
445 if (exists $Config{'d_getitimer'}) {
446 $has_getitimer++ if $Config{'d_getitimer'};
447 } elsif (has_x("getitimer(ITIMER_REAL, 0)")) {
449 $DEFINE .= ' -DHAS_GETITIMER';
452 if ($has_getitimer) {
455 print "NOT found.\n";
458 if ($has_setitimer && $has_getitimer) {
459 print "You have interval timers (both setitimer and getitimer).\n";
461 print "You do not have interval timers.\n";
464 print "Looking for ualarm()... ";
466 if (exists $Config{d_ualarm}) {
467 $has_ualarm++ if $Config{d_ualarm};
468 } elsif (has_x ("ualarm (0, 0)")) {
470 $DEFINE .= ' -DHAS_UALARM';
476 print "NOT found.\n";
477 if ($has_setitimer) {
478 print "But you have setitimer().\n";
479 print "We can make a Time::HiRes::ualarm().\n";
483 print "Looking for usleep()... ";
485 if (exists $Config{d_usleep}) {
486 $has_usleep++ if $Config{d_usleep};
487 } elsif (has_x ("usleep (0)")) {
489 $DEFINE .= ' -DHAS_USLEEP';
495 print "NOT found.\n";
496 print "Let's see if you have select()... ";
497 if ($Config{'d_select'}) {
499 print "We can make a Time::HiRes::usleep().\n";
501 print "NOT found.\n";
502 print "You won't have a Time::HiRes::usleep().\n";
506 print "Looking for nanosleep()... ";
508 if ($ENV{FORCE_NANOSLEEP_SCAN}) {
509 print "forced scan... ";
510 if (has_nanosleep()) {
512 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
515 elsif (exists $Config{d_nanosleep}) {
516 print "believing \$Config{d_nanosleep}... ";
517 if ($Config{d_nanosleep}) {
519 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
521 } elsif ($^O =~ /^(mpeix)$/) {
522 # MPE/iX falsely finds nanosleep from its libc equivalent.
523 print "skipping because in $^O... ";
525 if (has_nanosleep()) {
527 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
531 if ($has_nanosleep) {
533 print "You can mix subsecond sleeps with signals, if you want to.\n";
534 print "(It's still not portable, though.)\n";
536 print "NOT found.\n";
537 my $nt = ($^O eq 'os2' ? '' : 'not');
538 print "You can$nt mix subsecond sleeps with signals.\n";
539 print "(It would not be portable anyway.)\n";
542 print "Looking for clock_gettime()... ";
543 my $has_clock_gettime;
544 if (exists $Config{d_clock_gettime}) {
545 $has_clock_gettime++ if $Config{d_clock_gettime}; # Unlikely...
546 } elsif (has_clock_xxx('gettime')) {
547 $has_clock_gettime++;
548 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME';
549 } elsif (defined $SYSCALL_H && has_clock_xxx_syscall('gettime')) {
550 $has_clock_gettime++;
551 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME -DTIME_HIRES_CLOCK_GETTIME_SYSCALL';
554 if ($has_clock_gettime) {
555 if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETTIME_SYSCALL/) {
556 print "found (via syscall).\n";
561 print "NOT found.\n";
564 print "Looking for clock_getres()... ";
565 my $has_clock_getres;
566 if (exists $Config{d_clock_getres}) {
567 $has_clock_getres++ if $Config{d_clock_getres}; # Unlikely...
568 } elsif (has_clock_xxx('getres')) {
570 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES';
571 } elsif (defined $SYSCALL_H && has_clock_xxx_syscall('getres')) {
573 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES -DTIME_HIRES_CLOCK_GETRES_SYSCALL';
576 if ($has_clock_getres) {
577 if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETRES_SYSCALL/) {
578 print "found (via syscall).\n";
583 print "NOT found.\n";
586 print "Looking for clock_nanosleep()... ";
587 my $has_clock_nanosleep;
588 if (exists $Config{d_clock_nanosleep}) {
589 $has_clock_nanosleep++ if $Config{d_clock_nanosleep}; # Unlikely...
590 } elsif (has_clock_nanosleep()) {
591 $has_clock_nanosleep++;
592 $DEFINE .= ' -DTIME_HIRES_CLOCK_NANOSLEEP';
595 if ($has_clock_nanosleep) {
598 print "NOT found.\n";
601 print "Looking for clock()... ";
603 if (exists $Config{d_clock}) {
604 $has_clock++ if $Config{d_clock}; # Unlikely...
605 } elsif (has_clock()) {
607 $DEFINE .= ' -DTIME_HIRES_CLOCK';
613 print "NOT found.\n";
616 print "Looking for stat() subsecond timestamps...\n";
618 print "Trying struct stat st_atimespec.tv_nsec...";
619 my $has_stat_st_xtimespec;
620 if (try_compile_and_link(<<EOM)) {
622 #include <sys/stat.h>
623 int main(int argc, char** argv) {
625 st.st_atimespec.tv_nsec = 0;
628 $has_stat_st_xtimespec++;
629 DEFINE('TIME_HIRES_STAT', 1);
632 if ($has_stat_st_xtimespec) {
635 print "NOT found.\n";
638 print "Trying struct stat st_atimensec...";
639 my $has_stat_st_xtimensec;
640 if (try_compile_and_link(<<EOM)) {
642 #include <sys/stat.h>
643 int main(int argc, char** argv) {
648 $has_stat_st_xtimensec++;
649 DEFINE('TIME_HIRES_STAT', 2);
652 if ($has_stat_st_xtimensec) {
655 print "NOT found.\n";
658 print "Trying struct stat st_atime_n...";
659 my $has_stat_st_xtime_n;
660 if (try_compile_and_link(<<EOM)) {
662 #include <sys/stat.h>
663 int main(int argc, char** argv) {
668 $has_stat_st_xtime_n++;
669 DEFINE('TIME_HIRES_STAT', 3);
672 if ($has_stat_st_xtime_n) {
675 print "NOT found.\n";
678 print "Trying struct stat st_atim.tv_nsec...";
679 my $has_stat_st_xtim;
680 if (try_compile_and_link(<<EOM)) {
682 #include <sys/stat.h>
683 int main(int argc, char** argv) {
685 st.st_atim.tv_nsec = 0;
689 DEFINE('TIME_HIRES_STAT', 4);
692 if ($has_stat_st_xtim) {
695 print "NOT found.\n";
698 print "Trying struct stat st_uatime...";
699 my $has_stat_st_uxtime;
700 if (try_compile_and_link(<<EOM)) {
702 #include <sys/stat.h>
703 int main(int argc, char** argv) {
708 $has_stat_st_uxtime++;
709 DEFINE('TIME_HIRES_STAT', 5);
712 if ($has_stat_st_uxtime) {
715 print "NOT found.\n";
718 if ($DEFINE =~ /-DTIME_HIRES_STAT=\d+/) {
719 print "You seem to have stat() subsecond timestamps.\n";
720 print "(Your struct stat has them, but the filesystems must help.)\n";
722 print "You do not seem to have stat subsecond timestamps.\n";
725 my $has_w32api_windows_h;
727 if ($^O eq 'cygwin') {
728 print "Looking for <w32api/windows.h>... ";
729 if (has_include('w32api/windows.h')) {
730 $has_w32api_windows_h++;
731 DEFINE('HAS_W32API_WINDOWS_H');
733 if ($has_w32api_windows_h) {
736 print "NOT found.\n";
742 if (open(XDEFINE, ">xdefine")) {
743 print XDEFINE $DEFINE, "\n";
750 my @makefileopts = ();
754 'AUTHOR' => 'Jarkko Hietaniemi <jhi@iki.fi>',
755 'ABSTRACT_FROM' => 'HiRes.pm',
757 DEFINE('ATLEASTFIVEOHOHFIVE');
761 'NAME' => 'Time::HiRes',
762 'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
763 'LIBS' => $LIBS, # e.g., '-lm'
764 'DEFINE' => $DEFINE, # e.g., '-DHAS_SOMETHING'
766 # Do not even think about 'INC' => '-I/usr/ucbinclude',
767 # Solaris will avenge.
768 'INC' => '', # e.g., '-I/usr/include/other'
769 'INSTALLDIRS' => ($] >= 5.008 ? 'perl' : 'site'),
772 'COMPRESS' => 'gzip -9f',
775 clean => { FILES => "xdefine" },
776 realclean => { FILES=> 'const-c.inc const-xs.inc' },
779 if ($ENV{PERL_CORE}) {
780 push @makefileopts, MAN3PODS => {};
783 WriteMakefile(@makefileopts);
787 if (eval {require ExtUtils::Constant; 1}) {
788 my @names = qw(CLOCK_HIGHRES CLOCK_MONOTONIC
789 CLOCK_PROCESS_CPUTIME_ID
792 CLOCK_THREAD_CPUTIME_ID
795 ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF
798 foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
799 d_nanosleep d_clock_gettime d_clock_getres
800 d_clock d_clock_nanosleep d_hires_stat)) {
802 if ($macro =~ /^(d_nanosleep|d_clock_gettime|d_clock_getres|d_clock|d_clock_nanosleep)$/) {
803 $macro =~ s/^d_(.+)/TIME_HIRES_\U$1/;
804 } elsif ($macro =~ /^(d_hires_stat)$/) {
805 my $d_hires_stat = 0;
806 $d_hires_stat = $1 if ($DEFINE =~ /-DTIME_HIRES_STAT=(\d+)/);
807 push @names, {name => $_, macro => "TIME_HIRES_STAT", value => $d_hires_stat,
808 default => ["IV", "0"]};
811 $macro =~ s/^d_(.+)/HAS_\U$1/;
813 push @names, {name => $_, macro => $macro, value => 1,
814 default => ["IV", "0"]};
816 ExtUtils::Constant::WriteConstants(
817 NAME => 'Time::HiRes',
822 foreach $file ('const-c.inc', 'const-xs.inc') {
823 my $fallback = File::Spec->catfile('fallback', $file);
825 open IN, "<$fallback" or die "Can't open $fallback: $!";
826 open OUT, ">$file" or die "Can't open $file: $!";
827 print OUT <IN> or die $!;
828 close OUT or die "Can't close $file: $!";
829 close IN or die "Can't close $fallback: $!";
835 print "Configuring Time::HiRes...\n";
836 if ($] == 5.007002) {
837 die "Cannot Configure Time::HiRes for Perl $], aborting.\n";
840 if ($^O =~ /Win32/i) {
841 DEFINE('SELECT_IS_BROKEN');
843 print "System is $^O, skipping full configure...\n";
849 my $make = $Config{'make'} || "make";
850 unless (exists $ENV{PERL_CORE} && $ENV{PERL_CORE}) {
852 Now you may issue '$make'. Do not forget also '$make test'.
855 ((exists $ENV{LC_ALL} && $ENV{LC_ALL} =~ /utf-?8/i) ||
856 (exists $ENV{LC_CTYPE} && $ENV{LC_CTYPE} =~ /utf-?8/i) ||
857 (exists $ENV{LANG} && $ENV{LANG} =~ /utf-?8/i))) {
860 NOTE: if you get an error like this (the Makefile line number may vary):
861 Makefile:91: *** missing separator
862 then set the environment variable LC_ALL to "C" and retry
863 from scratch (re-run perl "Makefile.PL").
864 (And consider upgrading your Perl to, say, at least Perl 5.8.8.)
865 (You got this message because you seem to have
866 an UTF-8 locale active in your shell environment, this used
867 to cause broken Makefiles to be created from Makefile.PLs)