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);
157 unshift @run, $Config{run} if $Config{run} && -e $Config{run};
158 printf "Running $tmp_exe..." if $VERBOSE;
159 if (system(@run) == 0) {
167 *** The test run of '$tmp_exe' failed: status $?
168 *** (the status means: errno = $errno or '$!')
169 *** DO NOT PANIC: this just means that *some* functionality will be missing.
173 unlink("$tmp.c", $tmp_exe);
180 my $TIME_HEADERS = <<EOH;
185 # include <sys/types.h>
188 # include <sys/time.h>
191 # include <sys/select.h> /* struct timeval might be hidden in here */
195 sub has_gettimeofday {
196 # confusing but true (if condition true ==> -DHAS_GETTIMEOFDAY already)
197 return 0 if $Config{d_gettimeod};
198 return 1 if try_compile_and_link(<<EOM);
203 gettimeofday(&tv, 0);
205 int main(int argc, char** argv)
217 try_compile_and_link(<<EOM, %args);
227 # include <sys/types.h>
231 # include <sys/time.h>
234 int main(int argc, char** argv)
245 try_compile_and_link(<<EOM, run => 1);
247 #include <sys/time.h>
252 /* int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); */
254 int main(int argc, char** argv) {
255 struct timespec ts1, ts2;
258 ts1.tv_nsec = 750000000;
262 ret = nanosleep(&ts1, &ts2); /* E.g. in AIX nanosleep() fails and sets errno to ENOSYS. */
263 ret == 0 ? exit(0) : exit(errno ? errno : -1);
271 try_compile_and_link(<<EOM);
277 int main(int argc, char** argv)
285 sub has_clock_xxx_syscall {
287 return 0 unless defined $SYSCALL_H;
289 try_compile_and_link(<<EOM, run => 1);
293 #include <$SYSCALL_H>
294 int main(int argc, char** argv)
297 /* Many Linuxes get ENOSYS even though the syscall exists. */
298 /* All implementations are supposed to support CLOCK_REALTIME. */
299 int ret = syscall(SYS_clock_$x, CLOCK_REALTIME, &ts);
300 ret == 0 ? exit(0) : exit(errno ? errno : -1);
308 try_compile_and_link(<<EOM, run => 1);
312 int main(int argc, char** argv)
315 int ret = clock_$xxx(CLOCK_REALTIME, &ts); /* Many Linuxes get ENOSYS. */
316 /* All implementations are supposed to support CLOCK_REALTIME. */
317 ret == 0 ? exit(0) : exit(errno ? errno : -1);
324 try_compile_and_link(<<EOM, run => 1);
328 int main(int argc, char** argv)
331 clock_t ret = clock();
332 ret == (clock_t)-1 ? exit(errno ? errno : -1) : exit(0);
337 sub has_clock_nanosleep {
339 try_compile_and_link(<<EOM, run => 1);
344 int main(int argc, char** argv)
350 ts1.tv_nsec = 750000000;;
351 ret = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts1, &ts2);
352 ret == 0 ? exit(0) : exit(errno ? errno : -1);
358 my ($def, $val) = @_;
359 my $define = defined $val ? "$def=$val" : $def ;
360 unless ($DEFINE =~ /(?:^| )-D\Q$define\E(?: |$)/) {
361 $DEFINE .= " -D$define";
366 my $hints = File::Spec->catfile("hints", "$^O.pl");
368 print "Using hints $hints...\n";
371 if (exists $self->{LIBS}) {
372 $LIBS = $self->{LIBS};
373 print "Extra libraries: @$LIBS...\n";
379 if ($Config{d_syscall}) {
380 print "Have syscall()... looking for syscall.h... ";
381 if (has_include('syscall.h')) {
382 $SYSCALL_H = 'syscall.h';
383 } elsif (has_include('sys/syscall.h')) {
384 $SYSCALL_H = 'sys/syscall.h';
387 print "No syscall()...\n";
390 if ($Config{d_syscall}) {
391 if (defined $SYSCALL_H) {
392 print "found <$SYSCALL_H>.\n";
394 print "NOT found.\n";
398 print "Looking for gettimeofday()... ";
399 my $has_gettimeofday;
400 if (exists $Config{d_gettimeod}) {
401 $has_gettimeofday++ if $Config{d_gettimeod};
402 } elsif (has_gettimeofday()) {
403 $DEFINE .= ' -DHAS_GETTIMEOFDAY';
407 if ($has_gettimeofday) {
411 Your operating system does not seem to have the gettimeofday() function.
412 (or, at least, I cannot find it)
414 There is no way Time::HiRes is going to work.
416 I am awfully sorry but I cannot go further.
418 Aborting configuration.
423 print "Looking for setitimer()... ";
425 if (exists $Config{d_setitimer}) {
426 $has_setitimer++ if $Config{d_setitimer};
427 } elsif (has_x("setitimer(ITIMER_REAL, 0, 0)")) {
429 $DEFINE .= ' -DHAS_SETITIMER';
432 if ($has_setitimer) {
435 print "NOT found.\n";
438 print "Looking for getitimer()... ";
440 if (exists $Config{'d_getitimer'}) {
441 $has_getitimer++ if $Config{'d_getitimer'};
442 } elsif (has_x("getitimer(ITIMER_REAL, 0)")) {
444 $DEFINE .= ' -DHAS_GETITIMER';
447 if ($has_getitimer) {
450 print "NOT found.\n";
453 if ($has_setitimer && $has_getitimer) {
454 print "You have interval timers (both setitimer and getitimer).\n";
456 print "You do not have interval timers.\n";
459 print "Looking for ualarm()... ";
461 if (exists $Config{d_ualarm}) {
462 $has_ualarm++ if $Config{d_ualarm};
463 } elsif (has_x ("ualarm (0, 0)")) {
465 $DEFINE .= ' -DHAS_UALARM';
471 print "NOT found.\n";
472 if ($has_setitimer) {
473 print "But you have setitimer().\n";
474 print "We can make a Time::HiRes::ualarm().\n";
478 print "Looking for usleep()... ";
480 if (exists $Config{d_usleep}) {
481 $has_usleep++ if $Config{d_usleep};
482 } elsif (has_x ("usleep (0)")) {
484 $DEFINE .= ' -DHAS_USLEEP';
490 print "NOT found.\n";
491 print "Let's see if you have select()... ";
492 if ($Config{'d_select'}) {
494 print "We can make a Time::HiRes::usleep().\n";
496 print "NOT found.\n";
497 print "You won't have a Time::HiRes::usleep().\n";
501 print "Looking for nanosleep()... ";
503 if ($ENV{FORCE_NANOSLEEP_SCAN}) {
504 print "forced scan... ";
505 if (has_nanosleep()) {
507 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
510 elsif (exists $Config{d_nanosleep}) {
511 print "believing \$Config{d_nanosleep}... ";
512 if ($Config{d_nanosleep}) {
514 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
516 } elsif ($^O =~ /^(mpeix)$/) {
517 # MPE/iX falsely finds nanosleep from its libc equivalent.
518 print "skipping because in $^O... ";
520 if (has_nanosleep()) {
522 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
526 if ($has_nanosleep) {
528 print "You can mix subsecond sleeps with signals, if you want to.\n";
529 print "(It's still not portable, though.)\n";
531 print "NOT found.\n";
532 my $nt = ($^O eq 'os2' ? '' : 'not');
533 print "You can$nt mix subsecond sleeps with signals.\n";
534 print "(It would not be portable anyway.)\n";
537 print "Looking for clock_gettime()... ";
538 my $has_clock_gettime;
539 if (exists $Config{d_clock_gettime}) {
540 $has_clock_gettime++ if $Config{d_clock_gettime}; # Unlikely...
541 } elsif (has_clock_xxx('gettime')) {
542 $has_clock_gettime++;
543 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME';
544 } elsif (defined $SYSCALL_H && has_clock_xxx_syscall('gettime')) {
545 $has_clock_gettime++;
546 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME -DTIME_HIRES_CLOCK_GETTIME_SYSCALL';
549 if ($has_clock_gettime) {
550 if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETTIME_SYSCALL/) {
551 print "found (via syscall).\n";
556 print "NOT found.\n";
559 print "Looking for clock_getres()... ";
560 my $has_clock_getres;
561 if (exists $Config{d_clock_getres}) {
562 $has_clock_getres++ if $Config{d_clock_getres}; # Unlikely...
563 } elsif (has_clock_xxx('getres')) {
565 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES';
566 } elsif (defined $SYSCALL_H && has_clock_xxx_syscall('getres')) {
568 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES -DTIME_HIRES_CLOCK_GETRES_SYSCALL';
571 if ($has_clock_getres) {
572 if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETRES_SYSCALL/) {
573 print "found (via syscall).\n";
578 print "NOT found.\n";
581 print "Looking for clock_nanosleep()... ";
582 my $has_clock_nanosleep;
583 if (exists $Config{d_clock_nanosleep}) {
584 $has_clock_nanosleep++ if $Config{d_clock_nanosleep}; # Unlikely...
585 } elsif (has_clock_nanosleep()) {
586 $has_clock_nanosleep++;
587 $DEFINE .= ' -DTIME_HIRES_CLOCK_NANOSLEEP';
590 if ($has_clock_nanosleep) {
593 print "NOT found.\n";
596 print "Looking for clock()... ";
598 if (exists $Config{d_clock}) {
599 $has_clock++ if $Config{d_clock}; # Unlikely...
600 } elsif (has_clock()) {
602 $DEFINE .= ' -DTIME_HIRES_CLOCK';
608 print "NOT found.\n";
611 print "Looking for stat() subsecond timestamps...\n";
613 print "Trying struct stat st_atimespec.tv_nsec...";
614 my $has_stat_st_xtimespec;
615 if (try_compile_and_link(<<EOM)) {
617 #include <sys/stat.h>
618 int main(int argc, char** argv) {
620 st.st_atimespec.tv_nsec = 0;
623 $has_stat_st_xtimespec++;
624 DEFINE('TIME_HIRES_STAT', 1);
627 if ($has_stat_st_xtimespec) {
630 print "NOT found.\n";
633 print "Trying struct stat st_atimensec...";
634 my $has_stat_st_xtimensec;
635 if (try_compile_and_link(<<EOM)) {
637 #include <sys/stat.h>
638 int main(int argc, char** argv) {
643 $has_stat_st_xtimensec++;
644 DEFINE('TIME_HIRES_STAT', 2);
647 if ($has_stat_st_xtimensec) {
650 print "NOT found.\n";
653 print "Trying struct stat st_atime_n...";
654 my $has_stat_st_xtime_n;
655 if (try_compile_and_link(<<EOM)) {
657 #include <sys/stat.h>
658 int main(int argc, char** argv) {
663 $has_stat_st_xtime_n++;
664 DEFINE('TIME_HIRES_STAT', 3);
667 if ($has_stat_st_xtime_n) {
670 print "NOT found.\n";
673 print "Trying struct stat st_atim.tv_nsec...";
674 my $has_stat_st_xtim;
675 if (try_compile_and_link(<<EOM)) {
677 #include <sys/stat.h>
678 int main(int argc, char** argv) {
680 st.st_atim.tv_nsec = 0;
684 DEFINE('TIME_HIRES_STAT', 4);
687 if ($has_stat_st_xtim) {
690 print "NOT found.\n";
693 print "Trying struct stat st_uatime...";
694 my $has_stat_st_uxtime;
695 if (try_compile_and_link(<<EOM)) {
697 #include <sys/stat.h>
698 int main(int argc, char** argv) {
703 $has_stat_st_uxtime++;
704 DEFINE('TIME_HIRES_STAT', 5);
707 if ($has_stat_st_uxtime) {
710 print "NOT found.\n";
713 if ($DEFINE =~ /-DTIME_HIRES_STAT=\d+/) {
714 print "You seem to have stat() subsecond timestamps.\n";
715 print "(Your struct stat has them, but the filesystems must help.)\n";
717 print "You do not seem to have stat subsecond timestamps.\n";
720 my $has_w32api_windows_h;
722 if ($^O eq 'cygwin') {
723 print "Looking for <w32api/windows.h>... ";
724 if (has_include('w32api/windows.h')) {
725 $has_w32api_windows_h++;
726 DEFINE('HAS_W32API_WINDOWS_H');
728 if ($has_w32api_windows_h) {
731 print "NOT found.\n";
737 if (open(XDEFINE, ">xdefine")) {
738 print XDEFINE $DEFINE, "\n";
745 my @makefileopts = ();
749 'AUTHOR' => 'Jarkko Hietaniemi <jhi@iki.fi>',
750 'ABSTRACT_FROM' => 'HiRes.pm',
752 DEFINE('ATLEASTFIVEOHOHFIVE');
756 'NAME' => 'Time::HiRes',
757 'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
758 'LIBS' => $LIBS, # e.g., '-lm'
759 'DEFINE' => $DEFINE, # e.g., '-DHAS_SOMETHING'
761 # Do not even think about 'INC' => '-I/usr/ucbinclude',
762 # Solaris will avenge.
763 'INC' => '', # e.g., '-I/usr/include/other'
764 'INSTALLDIRS' => ($] >= 5.008 && $] < 5.011 ? 'perl' : 'site'),
770 'ExtUtils::MakeMaker' => 0,
771 'Test::More' => "0.82",
776 'COMPRESS' => 'gzip -9f',
779 clean => { FILES => "xdefine" },
780 realclean => { FILES=> 'const-c.inc const-xs.inc' },
783 if ($^O eq "MSWin32" && !(grep { /\ALD[A-Z]*=/ } @ARGV)) {
784 my $libperl = $Config{libperl} || "";
785 my $gccversion = $Config{gccversion} || "";
786 if ($gccversion =~ /\A3\.4\.[0-9]+/ and $libperl =~ /\.lib\z/) {
787 # Avoid broken linkage with ActivePerl, by linking directly
788 # against the Perl DLL rather than the import library.
789 (my $llibperl = "-l$libperl") =~ s/\.lib\z//;
790 my $lddlflags = $Config{lddlflags} || "";
791 my $ldflags = $Config{ldflags} || "";
792 s/-L(?:".*?"|\S+)//g foreach $lddlflags, $ldflags;
793 my $libdirs = join ' ',
794 map { s/(?<!\\)((?:\\\\)*")/\\$1/g; qq[-L"$_"] }
795 @Config{qw/bin sitebin/};
796 push @makefileopts, macro => {
797 LDDLFLAGS => "$lddlflags $libdirs $llibperl",
798 LDFLAGS => "$ldflags $libdirs $llibperl",
804 if ($ENV{PERL_CORE}) {
805 push @makefileopts, MAN3PODS => {};
808 if ($ExtUtils::MakeMaker::VERSION >= 6.48) {
809 push @makefileopts, (MIN_PERL_VERSION => '5.008',);
812 if ($ExtUtils::MakeMaker::VERSION >= 6.31) {
813 push @makefileopts, (LICENSE => 'perl_5');
816 WriteMakefile(@makefileopts);
820 if (eval {require ExtUtils::Constant; 1}) {
826 CLOCK_MONOTONIC_COARSE
827 CLOCK_MONOTONIC_PRECISE
829 CLOCK_PROCESS_CPUTIME_ID
831 CLOCK_REALTIME_COARSE
833 CLOCK_REALTIME_PRECISE
836 CLOCK_THREAD_CPUTIME_ID
847 foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
848 d_nanosleep d_clock_gettime d_clock_getres
849 d_clock d_clock_nanosleep d_hires_stat)) {
851 if ($macro =~ /^(d_nanosleep|d_clock_gettime|d_clock_getres|d_clock|d_clock_nanosleep)$/) {
852 $macro =~ s/^d_(.+)/TIME_HIRES_\U$1/;
853 } elsif ($macro =~ /^(d_hires_stat)$/) {
854 my $d_hires_stat = 0;
855 $d_hires_stat = $1 if ($DEFINE =~ /-DTIME_HIRES_STAT=(\d+)/);
856 push @names, {name => $_, macro => "TIME_HIRES_STAT", value => $d_hires_stat,
857 default => ["IV", "0"]};
860 $macro =~ s/^d_(.+)/HAS_\U$1/;
862 push @names, {name => $_, macro => $macro, value => 1,
863 default => ["IV", "0"]};
865 ExtUtils::Constant::WriteConstants(
866 NAME => 'Time::HiRes',
871 foreach $file ('const-c.inc', 'const-xs.inc') {
872 my $fallback = File::Spec->catfile('fallback', $file);
874 open IN, "<$fallback" or die "Can't open $fallback: $!";
875 open OUT, ">$file" or die "Can't open $file: $!";
876 print OUT <IN> or die $!;
877 close OUT or die "Can't close $file: $!";
878 close IN or die "Can't close $fallback: $!";
884 if (-f "xdefine" && !(@ARGV && $ARGV[0] eq '--configure')) {
885 print qq[$0: The "xdefine" exists, skipping the configure step.\n];
886 print qq[("$^X $0 --configure" to force the configure step)\n];
888 print "Configuring Time::HiRes...\n";
889 1 while unlink("define");
890 if ($^O =~ /Win32/i) {
891 DEFINE('SELECT_IS_BROKEN');
893 print "System is $^O, skipping full configure...\n";
894 open(XDEFINE, ">xdefine") or die "$0: Cannot create xdefine: $!\n";
902 my $make = $Config{'make'} || "make";
903 unless (exists $ENV{PERL_CORE} && $ENV{PERL_CORE}) {
905 Now you may issue '$make'. Do not forget also '$make test'.
908 ((exists $ENV{LC_ALL} && $ENV{LC_ALL} =~ /utf-?8/i) ||
909 (exists $ENV{LC_CTYPE} && $ENV{LC_CTYPE} =~ /utf-?8/i) ||
910 (exists $ENV{LANG} && $ENV{LANG} =~ /utf-?8/i))) {
913 NOTE: if you get an error like this (the Makefile line number may vary):
914 Makefile:91: *** missing separator
915 then set the environment variable LC_ALL to "C" and retry
916 from scratch (re-run perl "Makefile.PL").
917 (And consider upgrading your Perl to, say, at least Perl 5.8.8.)
918 (You got this message because you seem to have
919 an UTF-8 locale active in your shell environment, this used
920 to cause broken Makefiles to be created from Makefile.PLs)