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};
19 use vars qw($self); # Used in 'sourcing' the hints.
21 my $ld_exeext = ($^O eq 'cygwin' ||
22 $^O eq 'os2' && $Config{ldflags} =~ /-Zexe\b/) ? '.exe' : '';
24 unless($ENV{PERL_CORE}) {
25 $ENV{PERL_CORE} = 1 if grep { $_ eq 'PERL_CORE=1' } @ARGV;
28 # Perls 5.002 and 5.003 did not have File::Spec, fake what we need.
32 $^O =~ /mswin32|netware|djgpp/i ? '\\' :
39 my $catdir = join(my_dirsep, @_);
40 $^O eq 'VMS' ? "[$catdir]" : $catdir;
45 return join(my_dirsep, @_) unless $^O eq 'VMS';
47 return my_catdir (undef, @_) . $file;
52 $^O eq 'VMS' ? "-" : "..";
56 eval { require File::Spec };
58 *File::Spec::catdir = \&my_catdir;
59 *File::Spec::updir = \&my_updir;
60 *File::Spec::catfile = \&my_catfile;
64 # Avoid 'used only once' warnings.
65 my $nop1 = *File::Spec::catdir;
66 my $nop2 = *File::Spec::updir;
67 my $nop3 = *File::Spec::catfile;
69 # if you have 5.004_03 (and some slightly older versions?), xsubpp
70 # tries to generate line numbers in the C code generated from the .xs.
71 # unfortunately, it is a little buggy around #ifdef'd code.
72 # my choice is leave it in and have people with old perls complain
73 # about the "Usage" bug, or leave it out and be unable to compile myself
74 # without changing it, and then I'd always forget to change it before a
75 # release. Sorry, Edward :)
77 sub try_compile_and_link {
84 my $obj_ext = $Config{obj_ext} || ".o";
85 unlink("$tmp.c", "$tmp$obj_ext");
87 if (open(TMPC, ">$tmp.c")) {
91 my $cccmd = $args{cccmd};
97 if ($ENV{PERL_CORE}) {
98 my $updir = File::Spec->updir;
99 $COREincdir = File::Spec->catdir(($updir) x 3);
101 $COREincdir = File::Spec->catdir($Config{'archlibexp'}, 'CORE');
104 my $ccflags = $Config{'ccflags'} . ' ' . "-I$COREincdir";
107 if ($ENV{PERL_CORE}) {
108 # Fragile if the extensions change hierarchy within
109 # the Perl core but this should do for now.
110 $cccmd = "$Config{'cc'} /include=([---]) $tmp.c";
112 my $perl_core = $Config{'installarchlib'};
113 $perl_core =~ s/\]$/.CORE]/;
114 $cccmd = "$Config{'cc'} /include=(perl_root:[000000],$perl_core) $tmp.c";
118 if ($args{silent} || !$VERBOSE) {
119 $errornull = "2>/dev/null" unless defined $errornull;
124 $cccmd = "$Config{'cc'} -o $tmp $ccflags $tmp.c @$LIBS $errornull"
125 unless defined $cccmd;
128 open( CMDFILE, ">$tmp.com" );
129 print CMDFILE "\$ SET MESSAGE/NOFACILITY/NOSEVERITY/NOIDENT/NOTEXT\n";
130 print CMDFILE "\$ $cccmd\n";
131 print CMDFILE "\$ IF \$SEVERITY .NE. 1 THEN EXIT 44\n"; # escalate
133 system("\@ $tmp.com");
135 for ("$tmp.c", "$tmp$obj_ext", "$tmp.com", "$tmp$Config{exe_ext}") {
141 my $tmp_exe = "$tmp$ld_exeext";
142 printf "cccmd = $cccmd\n" if $VERBOSE;
143 my $res = system($cccmd);
144 $ok = defined($res) && $res==0 && -s $tmp_exe && -x _;
146 if ( $ok && exists $args{run} && $args{run}) {
148 File::Spec->catfile(File::Spec->curdir, $tmp_exe);
149 printf "Running $tmp_exe..." if $VERBOSE;
150 if (system($tmp_exe) == 0) {
154 print "[ system('$tmp_exe') failed: status $? ] ";
157 unlink("$tmp.c", $tmp_exe);
164 sub has_gettimeofday {
165 # confusing but true (if condition true ==> -DHAS_GETTIMEOFDAY already)
166 return 0 if $Config{d_gettimeod};
167 return 1 if try_compile_and_link(<<EOM);
172 # include <sys/types.h>
176 # include <sys/time.h>
180 # include <sys/select.h> /* struct timeval might be hidden in here */
185 gettimeofday(&tv, 0);
187 int main _((int argc, char** argv, char** env))
199 try_compile_and_link(<<EOM, %args);
209 # include <sys/types.h>
213 # include <sys/time.h>
216 int main _((int argc, char** argv, char** env))
227 try_compile_and_link(<<EOM, run => 1);
229 #include <sys/time.h>
234 /* int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); */
237 struct timespec ts1, ts2;
240 ts1.tv_nsec = 750000000;
244 ret = nanosleep(&ts1, &ts2); /* E.g. in AIX nanosleep() fail and set errno to ENOSYS. */
245 ret == 0 ? exit(0) : exit(errno ? errno : -1);
253 try_compile_and_link(<<EOM);
259 int main _((int argc, char** argv, char** env))
268 my $hints = File::Spec->catfile("hints", "$^O.pl");
270 print "Using hints $hints...\n";
273 if (exists $self->{LIBS}) {
274 $LIBS = $self->{LIBS};
275 print "Extra libraries: @$LIBS...\n";
281 print "Looking for gettimeofday()... ";
282 my $has_gettimeofday;
283 if (exists $Config{d_gettimeod}) {
284 $has_gettimeofday++ if $Config{d_gettimeod};
285 } elsif (has_gettimeofday()) {
286 $DEFINE .= ' -DHAS_GETTIMEOFDAY';
290 if ($has_gettimeofday) {
294 Your operating system does not seem to have the gettimeofday() function.
295 (or, at least, I cannot find it)
297 There is no way Time::HiRes is going to work.
299 I am awfully sorry but I cannot go further.
301 Aborting configuration.
306 print "Looking for setitimer()... ";
308 if (exists $Config{d_setitimer}) {
309 $has_setitimer++ if $Config{d_setitimer};
310 } elsif (has_x("setitimer(ITIMER_REAL, 0, 0)")) {
312 $DEFINE .= ' -DHAS_SETITIMER';
315 if ($has_setitimer) {
318 print "NOT found.\n";
321 print "Looking for getitimer()... ";
323 if (exists $Config{'d_getitimer'}) {
324 $has_getitimer++ if $Config{'d_getitimer'};
325 } elsif (has_x("getitimer(ITIMER_REAL, 0)")) {
327 $DEFINE .= ' -DHAS_GETITIMER';
330 if ($has_getitimer) {
333 print "NOT found.\n";
336 if ($has_setitimer && $has_getitimer) {
337 print "You have interval timers (both setitimer and getitimer).\n";
339 print "You do not have interval timers.\n";
342 print "Looking for ualarm()... ";
344 if (exists $Config{d_ualarm}) {
345 $has_ualarm++ if $Config{d_ualarm};
346 } elsif (has_x ("ualarm (0, 0)")) {
348 $DEFINE .= ' -DHAS_UALARM';
354 print "NOT found.\n";
355 if ($has_setitimer) {
356 print "But you have setitimer().\n";
357 print "We can make a Time::HiRes::ualarm().\n";
361 print "Looking for usleep()... ";
363 if (exists $Config{d_usleep}) {
364 $has_usleep++ if $Config{d_usleep};
365 } elsif (has_x ("usleep (0)")) {
367 $DEFINE .= ' -DHAS_USLEEP';
373 print "NOT found.\n";
374 print "Let's see if you have select()... ";
375 if ($Config{'d_select'}) {
377 print "We can make a Time::HiRes::usleep().\n";
379 print "NOT found.\n";
380 print "You won't have a Time::HiRes::usleep().\n";
384 print "Looking for nanosleep()... ";
386 if ($ENV{FORCE_NANOSLEEP_SCAN}) {
387 print "forced scan... ";
388 if (has_nanosleep()) {
390 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
393 elsif (exists $Config{d_nanosleep}) {
394 print "believing \$Config{d_nanosleep}... ";
395 if ($Config{d_nanosleep}) {
397 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
399 } elsif ($^O =~ /^(mpeix)$/) {
400 # MPE/iX falsely finds nanosleep from its libc equivalent.
401 print "skipping because in $^O... ";
403 if (has_nanosleep()) {
405 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
409 if ($has_nanosleep) {
411 print "You can mix subsecond sleeps with signals, if you want to.\n";
412 print "(It's still not portable, though.)\n";
414 print "NOT found.\n";
415 my $nt = ($^O eq 'os2' ? '' : 'not');
416 print "You can$nt mix subsecond sleeps with signals.\n";
417 print "(It would not be portable anyway.)\n";
420 my $has_w32api_windows_h;
421 if ($^O eq 'cygwin') {
422 print "Looking for <w32api/windows.h>... ";
423 if (has_include('w32api/windows.h')) {
424 $has_w32api_windows_h++;
425 $DEFINE .= ' -DHAS_W32API_WINDOWS_H';
427 if ($has_w32api_windows_h) {
430 print "NOT found.\n";
436 if (open(XDEFINE, ">xdefine")) {
437 print XDEFINE $DEFINE, "\n";
444 my @makefileopts = ();
448 'AUTHOR' => 'Jarkko Hietaniemi <jhi@iki.fi>',
449 'ABSTRACT_FROM' => 'HiRes.pm',
451 $DEFINE .= " -DATLEASTFIVEOHOHFIVE";
455 'NAME' => 'Time::HiRes',
456 'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
457 'LIBS' => $LIBS, # e.g., '-lm'
458 'DEFINE' => $DEFINE, # e.g., '-DHAS_SOMETHING'
460 # do not even think about 'INC' => '-I/usr/ucbinclude', Solaris will avenge.
461 'INC' => '', # e.g., '-I/usr/include/other'
462 'INSTALLDIRS' => ($] >= 5.008 ? 'perl' : 'site'),
465 'COMPRESS' => 'gzip -9f',
468 clean => { FILES => "xdefine" },
469 realclean => { FILES=> 'const-c.inc const-xs.inc' },
472 if ($ENV{PERL_CORE}) {
473 push @makefileopts, MAN3PODS => {};
476 WriteMakefile(@makefileopts);
480 if (eval {require ExtUtils::Constant; 1}) {
481 my @names = (qw(ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF
483 foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
486 if ($macro eq 'd_nanosleep') {
487 $macro =~ s/d_(.*)/TIME_HIRES_\U$1/;
489 $macro =~ s/d_(.*)/HAS_\U$1/;
491 push @names, {name => $_, macro => $macro, value => 1,
492 default => ["IV", "0"]};
494 ExtUtils::Constant::WriteConstants(
495 NAME => 'Time::HiRes',
500 foreach $file ('const-c.inc', 'const-xs.inc') {
501 my $fallback = File::Spec->catfile('fallback', $file);
503 open IN, "<$fallback" or die "Can't open $fallback: $!";
504 open OUT, ">$file" or die "Can't open $file: $!";
505 print OUT <IN> or die $!;
506 close OUT or die "Can't close $file: $!";
507 close IN or die "Can't close $fallback: $!";
513 print "Configuring Time::HiRes...\n";
514 if ($] == 5.007002) {
515 die "Cannot Configure Time::HiRes for Perl $], aborting.\n";
518 if ($^O =~ /Win32/i) {
519 $DEFINE = '-DSELECT_IS_BROKEN';
526 my $make = $Config{'make'} || "make";
527 unless (exists $ENV{PERL_CORE} && $ENV{PERL_CORE}) {
529 Now you may issue '$make'. Do not forget also '$make test'.
531 if ((exists $ENV{LC_ALL} && $ENV{LC_ALL} =~ /utf-?8/i) ||
532 (exists $ENV{LC_CTYPE} && $ENV{LC_CTYPE} =~ /utf-?8/i) ||
533 (exists $ENV{LANG} && $ENV{LANG} =~ /utf-?8/i)) {
535 NOTE: if you get an error like this (the line number may vary):
536 Makefile:91: *** missing separator
537 then set the environment variable LC_ALL to "C" and retry
538 from scratch (re-run perl "Makefile.PL").