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 sub has_gettimeofday {
173 # confusing but true (if condition true ==> -DHAS_GETTIMEOFDAY already)
174 return 0 if $Config{d_gettimeod};
175 return 1 if try_compile_and_link(<<EOM);
180 # include <sys/types.h>
184 # include <sys/time.h>
188 # include <sys/select.h> /* struct timeval might be hidden in here */
193 gettimeofday(&tv, 0);
195 int main _((int argc, char** argv, char** env))
207 try_compile_and_link(<<EOM, %args);
217 # include <sys/types.h>
221 # include <sys/time.h>
224 int main _((int argc, char** argv, char** env))
235 try_compile_and_link(<<EOM, run => 1);
237 #include <sys/time.h>
242 /* int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); */
245 struct timespec ts1, ts2;
248 ts1.tv_nsec = 750000000;
252 ret = nanosleep(&ts1, &ts2); /* E.g. in AIX nanosleep() fails and sets errno to ENOSYS. */
253 ret == 0 ? exit(0) : exit(errno ? errno : -1);
261 try_compile_and_link(<<EOM);
267 int main _((int argc, char** argv, char** env))
275 sub has_clock_xxx_syscall {
277 return 0 unless defined $SYSCALL_H;
279 try_compile_and_link(<<EOM, run => 1);
283 #include <$SYSCALL_H>
284 int main _((int argc, char** argv, char** env))
287 /* Many Linuxes get ENOSYS even though the syscall exists. */
288 /* All implementations are supposed to support CLOCK_REALTIME. */
289 int ret = syscall(SYS_clock_$x, CLOCK_REALTIME, &ts);
290 ret == 0 ? exit(0) : exit(errno ? errno : -1);
298 try_compile_and_link(<<EOM, run => 1);
302 int main _((int argc, char** argv, char** env))
305 int ret = clock_$xxx(CLOCK_REALTIME, &ts); /* Many Linuxes get ENOSYS. */
306 /* All implementations are supposed to support CLOCK_REALTIME. */
307 ret == 0 ? exit(0) : exit(errno ? errno : -1);
314 try_compile_and_link(<<EOM, run => 1);
318 int main _((int argc, char** argv, char** env))
321 clock_t ret = clock();
322 ret == (clock_t)-1 ? exit(errno ? errno : -1) : exit(0);
327 sub has_clock_nanosleep {
329 try_compile_and_link(<<EOM, run => 1);
333 int main _((int argc, char** argv, char** env))
336 struct timerspec ts1;
337 struct timerspec ts2;
339 ts1.tv_nsec = 750000000;;
340 ret = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts1, &ts2);
341 ret == 0 ? exit(0) : exit(errno ? errno : -1);
347 my $hints = File::Spec->catfile("hints", "$^O.pl");
349 print "Using hints $hints...\n";
352 if (exists $self->{LIBS}) {
353 $LIBS = $self->{LIBS};
354 print "Extra libraries: @$LIBS...\n";
360 if ($Config{d_syscall}) {
361 print "Have syscall()... looking for syscall.h... ";
362 if (has_include('syscall.h')) {
363 $SYSCALL_H = 'syscall.h';
364 } elsif (has_include('sys/syscall.h')) {
365 $SYSCALL_H = 'sys/syscall.h';
368 print "No syscall()...\n";
371 if ($Config{d_syscall}) {
372 if (defined $SYSCALL_H) {
373 print "found <$SYSCALL_H>.\n";
375 print "NOT found.\n";
379 print "Looking for gettimeofday()... ";
380 my $has_gettimeofday;
381 if (exists $Config{d_gettimeod}) {
382 $has_gettimeofday++ if $Config{d_gettimeod};
383 } elsif (has_gettimeofday()) {
384 $DEFINE .= ' -DHAS_GETTIMEOFDAY';
388 if ($has_gettimeofday) {
392 Your operating system does not seem to have the gettimeofday() function.
393 (or, at least, I cannot find it)
395 There is no way Time::HiRes is going to work.
397 I am awfully sorry but I cannot go further.
399 Aborting configuration.
404 print "Looking for setitimer()... ";
406 if (exists $Config{d_setitimer}) {
407 $has_setitimer++ if $Config{d_setitimer};
408 } elsif (has_x("setitimer(ITIMER_REAL, 0, 0)")) {
410 $DEFINE .= ' -DHAS_SETITIMER';
413 if ($has_setitimer) {
416 print "NOT found.\n";
419 print "Looking for getitimer()... ";
421 if (exists $Config{'d_getitimer'}) {
422 $has_getitimer++ if $Config{'d_getitimer'};
423 } elsif (has_x("getitimer(ITIMER_REAL, 0)")) {
425 $DEFINE .= ' -DHAS_GETITIMER';
428 if ($has_getitimer) {
431 print "NOT found.\n";
434 if ($has_setitimer && $has_getitimer) {
435 print "You have interval timers (both setitimer and getitimer).\n";
437 print "You do not have interval timers.\n";
440 print "Looking for ualarm()... ";
442 if (exists $Config{d_ualarm}) {
443 $has_ualarm++ if $Config{d_ualarm};
444 } elsif (has_x ("ualarm (0, 0)")) {
446 $DEFINE .= ' -DHAS_UALARM';
452 print "NOT found.\n";
453 if ($has_setitimer) {
454 print "But you have setitimer().\n";
455 print "We can make a Time::HiRes::ualarm().\n";
459 print "Looking for usleep()... ";
461 if (exists $Config{d_usleep}) {
462 $has_usleep++ if $Config{d_usleep};
463 } elsif (has_x ("usleep (0)")) {
465 $DEFINE .= ' -DHAS_USLEEP';
471 print "NOT found.\n";
472 print "Let's see if you have select()... ";
473 if ($Config{'d_select'}) {
475 print "We can make a Time::HiRes::usleep().\n";
477 print "NOT found.\n";
478 print "You won't have a Time::HiRes::usleep().\n";
482 print "Looking for nanosleep()... ";
484 if ($ENV{FORCE_NANOSLEEP_SCAN}) {
485 print "forced scan... ";
486 if (has_nanosleep()) {
488 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
491 elsif (exists $Config{d_nanosleep}) {
492 print "believing \$Config{d_nanosleep}... ";
493 if ($Config{d_nanosleep}) {
495 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
497 } elsif ($^O =~ /^(mpeix)$/) {
498 # MPE/iX falsely finds nanosleep from its libc equivalent.
499 print "skipping because in $^O... ";
501 if (has_nanosleep()) {
503 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
507 if ($has_nanosleep) {
509 print "You can mix subsecond sleeps with signals, if you want to.\n";
510 print "(It's still not portable, though.)\n";
512 print "NOT found.\n";
513 my $nt = ($^O eq 'os2' ? '' : 'not');
514 print "You can$nt mix subsecond sleeps with signals.\n";
515 print "(It would not be portable anyway.)\n";
518 print "Looking for clock_gettime()... ";
519 my $has_clock_gettime;
520 if (exists $Config{d_clock_gettime}) {
521 $has_clock_gettime++ if $Config{d_clock_gettime}; # Unlikely...
522 } elsif (has_clock_xxx('gettime')) {
523 $has_clock_gettime++;
524 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME';
525 } elsif (defined $SYSCALL_H && has_clock_xxx_syscall('gettime')) {
526 $has_clock_gettime++;
527 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME -DTIME_HIRES_CLOCK_GETTIME_SYSCALL';
530 if ($has_clock_gettime) {
531 if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETTIME_SYSCALL/) {
532 print "found (via syscall).\n";
537 print "NOT found.\n";
540 print "Looking for clock_getres()... ";
541 my $has_clock_getres;
542 if (exists $Config{d_clock_getres}) {
543 $has_clock_getres++ if $Config{d_clock_getres}; # Unlikely...
544 } elsif (has_clock_xxx('getres')) {
546 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES';
547 } elsif (defined $SYSCALL_H && has_clock_xxx_syscall('getres')) {
549 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES -DTIME_HIRES_CLOCK_GETRES_SYSCALL';
552 if ($has_clock_getres) {
553 if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETRES_SYSCALL/) {
554 print "found (via syscall).\n";
559 print "NOT found.\n";
562 print "Looking for clock_nanosleep()... ";
563 my $has_clock_nanosleep;
564 if (exists $Config{d_clock_nanosleep}) {
565 $has_clock_nanosleep++ if $Config{d_clock_nanosleep}; # Unlikely...
566 } elsif (has_clock_nanosleep()) {
567 $has_clock_nanosleep++;
568 $DEFINE .= ' -DTIME_HIRES_CLOCK_NANOSLEEP';
571 if ($has_clock_nanosleep) {
574 print "NOT found.\n";
577 print "Looking for clock()... ";
579 if (exists $Config{d_clock}) {
580 $has_clock++ if $Config{d_clock}; # Unlikely...
581 } elsif (has_clock()) {
583 $DEFINE .= ' -DTIME_HIRES_CLOCK';
589 print "NOT found.\n";
592 my $has_w32api_windows_h;
594 if ($^O eq 'cygwin') {
595 print "Looking for <w32api/windows.h>... ";
596 if (has_include('w32api/windows.h')) {
597 $has_w32api_windows_h++;
598 $DEFINE .= ' -DHAS_W32API_WINDOWS_H';
600 if ($has_w32api_windows_h) {
603 print "NOT found.\n";
609 if (open(XDEFINE, ">xdefine")) {
610 print XDEFINE $DEFINE, "\n";
617 my @makefileopts = ();
621 'AUTHOR' => 'Jarkko Hietaniemi <jhi@iki.fi>',
622 'ABSTRACT_FROM' => 'HiRes.pm',
624 $DEFINE .= " -DATLEASTFIVEOHOHFIVE";
628 'NAME' => 'Time::HiRes',
629 'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
630 'LIBS' => $LIBS, # e.g., '-lm'
631 'DEFINE' => $DEFINE, # e.g., '-DHAS_SOMETHING'
633 # Do not even think about 'INC' => '-I/usr/ucbinclude',
634 # Solaris will avenge.
635 'INC' => '', # e.g., '-I/usr/include/other'
636 'INSTALLDIRS' => ($] >= 5.008 ? 'perl' : 'site'),
639 'COMPRESS' => 'gzip -9f',
642 clean => { FILES => "xdefine" },
643 realclean => { FILES=> 'const-c.inc const-xs.inc' },
646 if ($ENV{PERL_CORE}) {
647 push @makefileopts, MAN3PODS => {};
650 WriteMakefile(@makefileopts);
654 if (eval {require ExtUtils::Constant; 1}) {
655 my @names = (qw(CLOCK_HIGHRES CLOCK_MONOTONIC
656 CLOCK_PROCESS_CPUTIME_ID
659 CLOCK_THREAD_CPUTIME_ID
662 ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF
665 foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
666 d_nanosleep d_clock_gettime d_clock_getres
667 d_clock d_clock_nanosleep)) {
669 if ($macro =~ /^(d_nanosleep|d_clock_gettime|d_clock_getres|d_clock|d_clock_nanosleep)$/) {
670 $macro =~ s/^d_(.+)/TIME_HIRES_\U$1/;
672 $macro =~ s/^d_(.+)/HAS_\U$1/;
674 push @names, {name => $_, macro => $macro, value => 1,
675 default => ["IV", "0"]};
677 ExtUtils::Constant::WriteConstants(
678 NAME => 'Time::HiRes',
683 foreach $file ('const-c.inc', 'const-xs.inc') {
684 my $fallback = File::Spec->catfile('fallback', $file);
686 open IN, "<$fallback" or die "Can't open $fallback: $!";
687 open OUT, ">$file" or die "Can't open $file: $!";
688 print OUT <IN> or die $!;
689 close OUT or die "Can't close $file: $!";
690 close IN or die "Can't close $fallback: $!";
696 print "Configuring Time::HiRes...\n";
697 if ($] == 5.007002) {
698 die "Cannot Configure Time::HiRes for Perl $], aborting.\n";
701 if ($^O =~ /Win32/i) {
702 $DEFINE = '-DSELECT_IS_BROKEN';
704 print "System is $^O, skipping full configure...\n";
710 my $make = $Config{'make'} || "make";
711 unless (exists $ENV{PERL_CORE} && $ENV{PERL_CORE}) {
713 Now you may issue '$make'. Do not forget also '$make test'.
715 if ((exists $ENV{LC_ALL} && $ENV{LC_ALL} =~ /utf-?8/i) ||
716 (exists $ENV{LC_CTYPE} && $ENV{LC_CTYPE} =~ /utf-?8/i) ||
717 (exists $ENV{LANG} && $ENV{LANG} =~ /utf-?8/i)) {
719 NOTE: if you get an error like this (the Makefile line number may vary):
720 Makefile:91: *** missing separator
721 then set the environment variable LC_ALL to "C" and retry
722 from scratch (re-run perl "Makefile.PL").