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_x_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_$x(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);
313 my $hints = File::Spec->catfile("hints", "$^O.pl");
315 print "Using hints $hints...\n";
318 if (exists $self->{LIBS}) {
319 $LIBS = $self->{LIBS};
320 print "Extra libraries: @$LIBS...\n";
326 if ($Config{d_syscall}) {
327 print "Have syscall(), looking for syscall.h... ";
328 if (has_include('syscall.h')) {
329 $SYSCALL_H = 'syscall.h';
330 } elsif (has_include('sys/syscall.h')) {
331 $SYSCALL_H = 'sys/syscall.h';
335 if (defined $SYSCALL_H) {
336 print "found <$SYSCALL_H>.\n";
338 print "NOT found.\n";
341 print "Looking for gettimeofday()... ";
342 my $has_gettimeofday;
343 if (exists $Config{d_gettimeod}) {
344 $has_gettimeofday++ if $Config{d_gettimeod};
345 } elsif (has_gettimeofday()) {
346 $DEFINE .= ' -DHAS_GETTIMEOFDAY';
350 if ($has_gettimeofday) {
354 Your operating system does not seem to have the gettimeofday() function.
355 (or, at least, I cannot find it)
357 There is no way Time::HiRes is going to work.
359 I am awfully sorry but I cannot go further.
361 Aborting configuration.
366 print "Looking for setitimer()... ";
368 if (exists $Config{d_setitimer}) {
369 $has_setitimer++ if $Config{d_setitimer};
370 } elsif (has_x("setitimer(ITIMER_REAL, 0, 0)")) {
372 $DEFINE .= ' -DHAS_SETITIMER';
375 if ($has_setitimer) {
378 print "NOT found.\n";
381 print "Looking for getitimer()... ";
383 if (exists $Config{'d_getitimer'}) {
384 $has_getitimer++ if $Config{'d_getitimer'};
385 } elsif (has_x("getitimer(ITIMER_REAL, 0)")) {
387 $DEFINE .= ' -DHAS_GETITIMER';
390 if ($has_getitimer) {
393 print "NOT found.\n";
396 if ($has_setitimer && $has_getitimer) {
397 print "You have interval timers (both setitimer and getitimer).\n";
399 print "You do not have interval timers.\n";
402 print "Looking for ualarm()... ";
404 if (exists $Config{d_ualarm}) {
405 $has_ualarm++ if $Config{d_ualarm};
406 } elsif (has_x ("ualarm (0, 0)")) {
408 $DEFINE .= ' -DHAS_UALARM';
414 print "NOT found.\n";
415 if ($has_setitimer) {
416 print "But you have setitimer().\n";
417 print "We can make a Time::HiRes::ualarm().\n";
421 print "Looking for usleep()... ";
423 if (exists $Config{d_usleep}) {
424 $has_usleep++ if $Config{d_usleep};
425 } elsif (has_x ("usleep (0)")) {
427 $DEFINE .= ' -DHAS_USLEEP';
433 print "NOT found.\n";
434 print "Let's see if you have select()... ";
435 if ($Config{'d_select'}) {
437 print "We can make a Time::HiRes::usleep().\n";
439 print "NOT found.\n";
440 print "You won't have a Time::HiRes::usleep().\n";
444 print "Looking for nanosleep()... ";
446 if ($ENV{FORCE_NANOSLEEP_SCAN}) {
447 print "forced scan... ";
448 if (has_nanosleep()) {
450 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
453 elsif (exists $Config{d_nanosleep}) {
454 print "believing \$Config{d_nanosleep}... ";
455 if ($Config{d_nanosleep}) {
457 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
459 } elsif ($^O =~ /^(mpeix)$/) {
460 # MPE/iX falsely finds nanosleep from its libc equivalent.
461 print "skipping because in $^O... ";
463 if (has_nanosleep()) {
465 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
469 if ($has_nanosleep) {
471 print "You can mix subsecond sleeps with signals, if you want to.\n";
472 print "(It's still not portable, though.)\n";
474 print "NOT found.\n";
475 my $nt = ($^O eq 'os2' ? '' : 'not');
476 print "You can$nt mix subsecond sleeps with signals.\n";
477 print "(It would not be portable anyway.)\n";
480 print "Looking for clock_gettime()... ";
481 my $has_clock_gettime;
482 if (exists $Config{d_clock_gettime}) {
483 $has_clock_gettime++ if $Config{d_clock_gettime}; # Unlikely...
484 } elsif (has_clock_x('gettime')) {
485 $has_clock_gettime++;
486 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME';
487 } elsif (defined $SYSCALL_H && has_clock_x_syscall('gettime')) {
488 $has_clock_gettime++;
489 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME -DTIME_HIRES_CLOCK_GETTIME_SYSCALL';
492 if ($has_clock_gettime) {
493 if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETTIME_SYSCALL/) {
494 print "found (via syscall).\n";
499 print "NOT found.\n";
502 print "Looking for clock_getres()... ";
503 my $has_clock_getres;
504 if (exists $Config{d_clock_getres}) {
505 $has_clock_getres++ if $Config{d_clock_getres}; # Unlikely...
506 } elsif (has_clock_x('getres')) {
508 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES';
509 } elsif (defined $SYSCALL_H && has_clock_x_syscall('getres')) {
511 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES -DTIME_HIRES_CLOCK_GETRES_SYSCALL';
514 if ($has_clock_getres) {
515 if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETRES_SYSCALL/) {
516 print "found (via syscall).\n";
521 print "NOT found.\n";
524 my $has_w32api_windows_h;
525 if ($^O eq 'cygwin') {
526 print "Looking for <w32api/windows.h>... ";
527 if (has_include('w32api/windows.h')) {
528 $has_w32api_windows_h++;
529 $DEFINE .= ' -DHAS_W32API_WINDOWS_H';
531 if ($has_w32api_windows_h) {
534 print "NOT found.\n";
540 if (open(XDEFINE, ">xdefine")) {
541 print XDEFINE $DEFINE, "\n";
548 my @makefileopts = ();
552 'AUTHOR' => 'Jarkko Hietaniemi <jhi@iki.fi>',
553 'ABSTRACT_FROM' => 'HiRes.pm',
555 $DEFINE .= " -DATLEASTFIVEOHOHFIVE";
559 'NAME' => 'Time::HiRes',
560 'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
561 'LIBS' => $LIBS, # e.g., '-lm'
562 'DEFINE' => $DEFINE, # e.g., '-DHAS_SOMETHING'
564 # Do not even think about 'INC' => '-I/usr/ucbinclude',
565 # Solaris will avenge.
566 'INC' => '', # e.g., '-I/usr/include/other'
567 'INSTALLDIRS' => ($] >= 5.008 ? 'perl' : 'site'),
570 'COMPRESS' => 'gzip -9f',
573 clean => { FILES => "xdefine" },
574 realclean => { FILES=> 'const-c.inc const-xs.inc' },
577 if ($ENV{PERL_CORE}) {
578 push @makefileopts, MAN3PODS => {};
581 WriteMakefile(@makefileopts);
585 if (eval {require ExtUtils::Constant; 1}) {
586 my @names = (qw(CLOCK_HIGHRES CLOCK_MONOTONIC
587 CLOCK_PROCESS_CPUTIME_ID
589 CLOCK_THREAD_CPUTIME_ID
591 ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF
593 foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
594 d_nanosleep d_clock_gettime d_clock_getres)) {
596 if ($macro =~ /^(d_nanosleep|d_clock_gettime|d_clock_getres)$/) {
597 $macro =~ s/d_(.*)/TIME_HIRES_\U$1/;
599 $macro =~ s/d_(.*)/HAS_\U$1/;
601 push @names, {name => $_, macro => $macro, value => 1,
602 default => ["IV", "0"]};
604 ExtUtils::Constant::WriteConstants(
605 NAME => 'Time::HiRes',
610 foreach $file ('const-c.inc', 'const-xs.inc') {
611 my $fallback = File::Spec->catfile('fallback', $file);
613 open IN, "<$fallback" or die "Can't open $fallback: $!";
614 open OUT, ">$file" or die "Can't open $file: $!";
615 print OUT <IN> or die $!;
616 close OUT or die "Can't close $file: $!";
617 close IN or die "Can't close $fallback: $!";
623 print "Configuring Time::HiRes...\n";
624 if ($] == 5.007002) {
625 die "Cannot Configure Time::HiRes for Perl $], aborting.\n";
628 if ($^O =~ /Win32/i) {
629 $DEFINE = '-DSELECT_IS_BROKEN';
636 my $make = $Config{'make'} || "make";
637 unless (exists $ENV{PERL_CORE} && $ENV{PERL_CORE}) {
639 Now you may issue '$make'. Do not forget also '$make test'.
641 if ((exists $ENV{LC_ALL} && $ENV{LC_ALL} =~ /utf-?8/i) ||
642 (exists $ENV{LC_CTYPE} && $ENV{LC_CTYPE} =~ /utf-?8/i) ||
643 (exists $ENV{LANG} && $ENV{LANG} =~ /utf-?8/i)) {
645 NOTE: if you get an error like this (the Makefile line number may vary):
646 Makefile:91: *** missing separator
647 then set the environment variable LC_ALL to "C" and retry
648 from scratch (re-run perl "Makefile.PL").