3 # In general we trust %Config, but for nanosleep() this trust
4 # may be misplaces (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}) {
149 catfile(File::Spec->rel2abs(File::Spec->curdir),
151 printf "Running $abs_tmp_exe..." if $VERBOSE;
152 if (system($abs_tmp_exe) == 0) {
156 print "[ system('$abs_tmp_exe') failed: status $? ] ";
159 unlink("$tmp.c", $tmp_exe);
166 sub has_gettimeofday {
167 # confusing but true (if condition true ==> -DHAS_GETTIMEOFDAY already)
168 return 0 if $Config{d_gettimeod};
169 return 1 if try_compile_and_link(<<EOM);
174 # include <sys/types.h>
178 # include <sys/time.h>
182 # include <sys/select.h> /* struct timeval might be hidden in here */
187 gettimeofday(&tv, 0);
189 int main _((int argc, char** argv, char** env))
201 try_compile_and_link(<<EOM, %args);
211 # include <sys/types.h>
215 # include <sys/time.h>
218 int main _((int argc, char** argv, char** env))
227 print "Trying out nanosleep... ";
229 try_compile_and_link(<<EOM, run => 1);
231 #include <sys/time.h>
236 /* int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); */
239 struct timespec ts1, ts2;
242 ts1.tv_nsec = 750000000;
246 ret = nanosleep(&ts1, &ts2); /* E.g. in AIX nanosleep() fail and set errno to ENOSYS. */
247 ret == 0 ? exit(0) : exit(errno ? errno : -1);
255 try_compile_and_link(<<EOM);
261 int main _((int argc, char** argv, char** env))
270 my $hints = File::Spec->catfile("hints", "$^O.pl");
272 print "Using hints $hints...\n";
275 if (exists $self->{LIBS}) {
276 $LIBS = $self->{LIBS};
277 print "Extra libraries: @$LIBS...\n";
283 print "Looking for gettimeofday()... ";
284 my $has_gettimeofday;
285 if (exists $Config{d_gettimeod}) {
286 $has_gettimeofday++ if $Config{d_gettimeod};
287 } elsif (has_gettimeofday()) {
288 $DEFINE .= ' -DHAS_GETTIMEOFDAY';
292 if ($has_gettimeofday) {
296 Your operating system does not seem to have the gettimeofday() function.
297 (or, at least, I cannot find it)
299 There is no way Time::HiRes is going to work.
301 I am awfully sorry but I cannot go further.
303 Aborting configuration.
308 print "Looking for setitimer()... ";
310 if (exists $Config{d_setitimer}) {
311 $has_setitimer++ if $Config{d_setitimer};
312 } elsif (has_x("setitimer(ITIMER_REAL, 0, 0)")) {
314 $DEFINE .= ' -DHAS_SETITIMER';
317 if ($has_setitimer) {
320 print "NOT found.\n";
323 print "Looking for getitimer()... ";
325 if (exists $Config{'d_getitimer'}) {
326 $has_getitimer++ if $Config{'d_getitimer'};
327 } elsif (has_x("getitimer(ITIMER_REAL, 0)")) {
329 $DEFINE .= ' -DHAS_GETITIMER';
332 if ($has_getitimer) {
335 print "NOT found.\n";
338 if ($has_setitimer && $has_getitimer) {
339 print "You have interval timers (both setitimer and getitimer).\n";
341 print "You do not have interval timers.\n";
344 print "Looking for ualarm()... ";
346 if (exists $Config{d_ualarm}) {
347 $has_ualarm++ if $Config{d_ualarm};
348 } elsif (has_x ("ualarm (0, 0)")) {
350 $DEFINE .= ' -DHAS_UALARM';
356 print "NOT found.\n";
357 if ($has_setitimer) {
358 print "But you have setitimer().\n";
359 print "We can make a Time::HiRes::ualarm().\n";
363 print "Looking for usleep()... ";
365 if (exists $Config{d_usleep}) {
366 $has_usleep++ if $Config{d_usleep};
367 } elsif (has_x ("usleep (0)")) {
369 $DEFINE .= ' -DHAS_USLEEP';
375 print "NOT found.\n";
376 print "Let's see if you have select()... ";
377 if ($Config{'d_select'}) {
379 print "We can make a Time::HiRes::usleep().\n";
381 print "NOT found.\n";
382 print "You won't have a Time::HiRes::usleep().\n";
386 print "Looking for nanosleep()... ";
388 if (exists $Config{d_nanosleep} && !$ENV{FORCE_NANOSLEEP_SCAN}) {
389 # Believe $Config{d_nanosleep}.
390 if ($Config{d_nanosleep}) {
392 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
394 } elsif ($^O ne 'mpeix' && # MPE/iX falsely finds nanosleep.
397 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
400 if ($has_nanosleep) {
402 print "You can mix subsecond sleeps with signals, if you want to.\n";
403 print "(It's still not portable, though.)\n";
405 print "NOT found.\n";
406 my $nt = ($^O eq 'os2' ? '' : 'not');
407 print "You can$nt mix subsecond sleeps with signals.\n";
408 print "(It would not be portable anyway.)\n";
411 my $has_w32api_windows_h;
412 if ($^O eq 'cygwin') {
413 print "Looking for <w32api/windows.h>... ";
414 if (has_include('w32api/windows.h')) {
415 $has_w32api_windows_h++;
416 $DEFINE .= ' -DHAS_W32API_WINDOWS_H';
418 if ($has_w32api_windows_h) {
421 print "NOT found.\n";
427 if (open(XDEFINE, ">xdefine")) {
428 print XDEFINE $DEFINE, "\n";
435 my @makefileopts = ();
439 'AUTHOR' => 'Jarkko Hietaniemi <jhi@iki.fi>',
440 'ABSTRACT_FROM' => 'HiRes.pm',
442 $DEFINE .= " -DATLEASTFIVEOHOHFIVE";
446 'NAME' => 'Time::HiRes',
447 'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
448 'LIBS' => $LIBS, # e.g., '-lm'
449 'DEFINE' => $DEFINE, # e.g., '-DHAS_SOMETHING'
451 # do not even think about 'INC' => '-I/usr/ucbinclude', Solaris will avenge.
452 'INC' => '', # e.g., '-I/usr/include/other'
453 'INSTALLDIRS' => 'perl',
456 'COMPRESS' => 'gzip -9f',
459 clean => { FILES => "xdefine" },
460 realclean => { FILES=> 'const-c.inc const-xs.inc' },
463 if ($ENV{PERL_CORE}) {
464 push @makefileopts, MAN3PODS => {};
467 WriteMakefile(@makefileopts);
471 if (eval {require ExtUtils::Constant; 1}) {
472 my @names = (qw(ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF
474 foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
477 if ($macro eq 'd_nanosleep') {
478 $macro =~ s/d_(.*)/TIME_HIRES_\U$1/;
480 $macro =~ s/d_(.*)/HAS_\U$1/;
482 push @names, {name => $_, macro => $macro, value => 1,
483 default => ["IV", "0"]};
485 ExtUtils::Constant::WriteConstants(
486 NAME => 'Time::HiRes',
491 foreach $file ('const-c.inc', 'const-xs.inc') {
492 my $fallback = File::Spec->catfile('fallback', $file);
494 open IN, "<$fallback" or die "Can't open $fallback: $!";
495 open OUT, ">$file" or die "Can't open $file: $!";
496 print OUT <IN> or die $!;
497 close OUT or die "Can't close $file: $!";
498 close IN or die "Can't close $fallback: $!";
504 print "Configuring Time::HiRes...\n";
505 if ($] == 5.007002) {
506 die "Cannot Configure Time::HiRes for Perl $], aborting.\n";
509 if ($^O =~ /Win32/i) {
510 $DEFINE = '-DSELECT_IS_BROKEN';
517 my $make = $Config{'make'} || "make";
518 unless (exists $ENV{PERL_CORE} && $ENV{PERL_CORE}) {
520 Now you may issue '$make'. Do not forget also '$make test'.
522 if ((exists $ENV{LC_ALL} && $ENV{LC_ALL} =~ /utf-?8/i) ||
523 (exists $ENV{LC_CTYPE} && $ENV{LC_CTYPE} =~ /utf-?8/i) ||
524 (exists $ENV{LANG} && $ENV{LANG} =~ /utf-?8/i)) {
526 NOTE: if you get an error like this (the line number may vary):
527 Makefile:91: *** missing separator
528 then set the environment variable LC_ALL to "C" and retry
529 from scratch (re-run perl "Makefile.PL").