PVBMs mustn't enter anything that turns SVIVisUV_on().
[p5sagit/p5-mst-13.2.git] / ext / Time / HiRes / Makefile.PL
CommitLineData
56c1b3bd 1#!/usr/bin/perl
2#
3# In general we trust %Config, but for nanosleep() this trust
3d0346a5 4# may be misplaced (it may be linkable but not really functional).
56c1b3bd 5# Use $ENV{FORCE_NANOSLEEP_SCAN} to force rescanning whether there
6# really is hope.
7
3f2ee006 8require 5.002;
9
10use Config;
dcf686c9 11use ExtUtils::MakeMaker;
98b50af3 12use strict;
3f2ee006 13
14my $VERBOSE = $ENV{VERBOSE};
98b50af3 15my $DEFINE;
046e3f33 16my $LIBS = [];
56c1b3bd 17my $XSOPT = '';
ced84e60 18my $SYSCALL_H;
98b50af3 19
046e3f33 20use vars qw($self); # Used in 'sourcing' the hints.
21
4ed0e2d4 22my $ld_exeext = ($^O eq 'cygwin' ||
23 $^O eq 'os2' && $Config{ldflags} =~ /-Zexe\b/) ? '.exe' : '';
622913ab 24
12724655 25unless($ENV{PERL_CORE}) {
26 $ENV{PERL_CORE} = 1 if grep { $_ eq 'PERL_CORE=1' } @ARGV;
98b50af3 27}
28
29# Perls 5.002 and 5.003 did not have File::Spec, fake what we need.
3f2ee006 30
31sub my_dirsep {
32 $^O eq 'VMS' ? '.' :
33 $^O =~ /mswin32|netware|djgpp/i ? '\\' :
34 $^O eq 'MacOS' ? ':'
35 : '/';
36}
37
38sub my_catdir {
39 shift;
40 my $catdir = join(my_dirsep, @_);
98b50af3 41 $^O eq 'VMS' ? "[$catdir]" : $catdir;
42}
43
44sub my_catfile {
45 shift;
46 return join(my_dirsep, @_) unless $^O eq 'VMS';
47 my $file = pop;
48 return my_catdir (undef, @_) . $file;
3f2ee006 49}
50
51sub my_updir {
52 shift;
53 $^O eq 'VMS' ? "-" : "..";
54}
55
56BEGIN {
57 eval { require File::Spec };
58 if ($@) {
25ca0970 59 *File::Spec::catdir = \&my_catdir;
60 *File::Spec::updir = \&my_updir;
98b50af3 61 *File::Spec::catfile = \&my_catfile;
3f2ee006 62 }
63}
64
98b50af3 65# Avoid 'used only once' warnings.
66my $nop1 = *File::Spec::catdir;
67my $nop2 = *File::Spec::updir;
68my $nop3 = *File::Spec::catfile;
69
3f2ee006 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.
5d899d7e 73# my choice is leave it in and have people with old perls complain
3f2ee006 74# about the "Usage" bug, or leave it out and be unable to compile myself
5d899d7e 75# without changing it, and then I'd always forget to change it before a
3f2ee006 76# release. Sorry, Edward :)
77
3f2ee006 78sub try_compile_and_link {
79 my ($c, %args) = @_;
80
81 my ($ok) = 0;
2359510d 82 my ($tmp) = "tmp$$";
3f2ee006 83 local(*TMPC);
84
85 my $obj_ext = $Config{obj_ext} || ".o";
86 unlink("$tmp.c", "$tmp$obj_ext");
87
88 if (open(TMPC, ">$tmp.c")) {
89 print TMPC $c;
90 close(TMPC);
91
98b50af3 92 my $cccmd = $args{cccmd};
3f2ee006 93
94 my $errornull;
95
96 my $COREincdir;
98b50af3 97
3f2ee006 98 if ($ENV{PERL_CORE}) {
99 my $updir = File::Spec->updir;
100 $COREincdir = File::Spec->catdir(($updir) x 3);
101 } else {
102 $COREincdir = File::Spec->catdir($Config{'archlibexp'}, 'CORE');
103 }
98b50af3 104
9e000d5b 105 if ($ENV{PERL_CORE}) {
106 unless (-f File::Spec->catfile($COREincdir, "EXTERN.h")) {
107 die <<__EOD__;
108Your environment variable PERL_CORE is '$ENV{PERL_CORE}' but there
109is no EXTERN.h in $COREincdir.
110Cannot continue, aborting.
111__EOD__
112 }
113 }
114
3f2ee006 115 my $ccflags = $Config{'ccflags'} . ' ' . "-I$COREincdir";
98b50af3 116
3f2ee006 117 if ($^O eq 'VMS') {
118 if ($ENV{PERL_CORE}) {
3d036c2b 119 # Fragile if the extensions change hierarchy within
6a20eacc 120 # the Perl core but this should do for now.
121 $cccmd = "$Config{'cc'} /include=([---]) $tmp.c";
3f2ee006 122 } else {
123 my $perl_core = $Config{'installarchlib'};
124 $perl_core =~ s/\]$/.CORE]/;
98b50af3 125 $cccmd = "$Config{'cc'} /include=(perl_root:[000000],$perl_core) $tmp.c";
3f2ee006 126 }
127 }
128
129 if ($args{silent} || !$VERBOSE) {
130 $errornull = "2>/dev/null" unless defined $errornull;
131 } else {
132 $errornull = '';
133 }
134
98b50af3 135 $cccmd = "$Config{'cc'} -o $tmp $ccflags $tmp.c @$LIBS $errornull"
3f2ee006 136 unless defined $cccmd;
98b50af3 137
c1363767 138 if ($^O eq 'VMS') {
3f2ee006 139 open( CMDFILE, ">$tmp.com" );
140 print CMDFILE "\$ SET MESSAGE/NOFACILITY/NOSEVERITY/NOIDENT/NOTEXT\n";
141 print CMDFILE "\$ $cccmd\n";
98b50af3 142 print CMDFILE "\$ IF \$SEVERITY .NE. 1 THEN EXIT 44\n"; # escalate
3f2ee006 143 close CMDFILE;
144 system("\@ $tmp.com");
145 $ok = $?==0;
5d899d7e 146 for ("$tmp.c", "$tmp$obj_ext", "$tmp.com", "$tmp$Config{exe_ext}") {
98b50af3 147 1 while unlink $_;
3f2ee006 148 }
149 }
150 else
151 {
622913ab 152 my $tmp_exe = "$tmp$ld_exeext";
3f2ee006 153 printf "cccmd = $cccmd\n" if $VERBOSE;
622913ab 154 my $res = system($cccmd);
ced84e60 155 $ok = defined($res) && $res == 0 && -s $tmp_exe && -x _;
26e22fd9 156
157 if ( $ok && exists $args{run} && $args{run}) {
06252d99 158 my $tmp_exe =
159 File::Spec->catfile(File::Spec->curdir, $tmp_exe);
160 printf "Running $tmp_exe..." if $VERBOSE;
161 if (system($tmp_exe) == 0) {
56c1b3bd 162 $ok = 1;
26e22fd9 163 } else {
56c1b3bd 164 $ok = 0;
ced84e60 165 my $errno = $? >> 8;
166 local $! = $errno;
167 printf <<EOF;
168
169*** The test run of '$tmp_exe' failed: status $?
170*** (the status means: errno = $errno or '$!')
171*** DO NOT PANIC: this just means that *some* functionality will be missing.
172EOF
26e22fd9 173 }
174 }
622913ab 175 unlink("$tmp.c", $tmp_exe);
3f2ee006 176 }
177 }
5d899d7e 178
26e22fd9 179 return $ok;
3f2ee006 180}
181
75d5269b 182my $TIME_HEADERS = <<EOH;
5d899d7e 183#include "EXTERN.h"
184#include "perl.h"
185#include "XSUB.h"
186#ifdef I_SYS_TYPES
3f2ee006 187# include <sys/types.h>
188#endif
3f2ee006 189#ifdef I_SYS_TIME
190# include <sys/time.h>
191#endif
3f2ee006 192#ifdef I_SYS_SELECT
193# include <sys/select.h> /* struct timeval might be hidden in here */
194#endif
75d5269b 195EOH
196
197sub has_gettimeofday {
198 # confusing but true (if condition true ==> -DHAS_GETTIMEOFDAY already)
199 return 0 if $Config{d_gettimeod};
200 return 1 if try_compile_and_link(<<EOM);
201$TIME_HEADERS
3f2ee006 202static int foo()
203{
204 struct timeval tv;
205 gettimeofday(&tv, 0);
206}
207int main _((int argc, char** argv, char** env))
208{
209 foo();
210}
211EOM
212 return 0;
213}
214
215sub has_x {
98b50af3 216 my ($x, %args) = @_;
3f2ee006 217
218 return 1 if
219 try_compile_and_link(<<EOM, %args);
220#include "EXTERN.h"
221#include "perl.h"
222#include "XSUB.h"
223
224#ifdef I_UNISTD
225# include <unistd.h>
226#endif
227
228#ifdef I_SYS_TYPES
229# include <sys/types.h>
230#endif
231
232#ifdef I_SYS_TIME
233# include <sys/time.h>
234#endif
235
236int main _((int argc, char** argv, char** env))
237{
238 $x;
239}
240EOM
241 return 0;
242}
243
26e22fd9 244sub has_nanosleep {
3d0346a5 245 print "testing... ";
26e22fd9 246 return 1 if
247 try_compile_and_link(<<EOM, run => 1);
248#include <time.h>
249#include <sys/time.h>
250#include <stdio.h>
251#include <stdlib.h>
252#include <errno.h>
253
254/* int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); */
255
71ac5476 256int main _((int argc, char** argv, char** env)) {
26e22fd9 257 struct timespec ts1, ts2;
56c1b3bd 258 int ret;
26e22fd9 259 ts1.tv_sec = 0;
260 ts1.tv_nsec = 750000000;
261 ts2.tv_sec = 0;
262 ts2.tv_nsec = 0;
56c1b3bd 263 errno = 0;
ced84e60 264 ret = nanosleep(&ts1, &ts2); /* E.g. in AIX nanosleep() fails and sets errno to ENOSYS. */
56c1b3bd 265 ret == 0 ? exit(0) : exit(errno ? errno : -1);
26e22fd9 266}
267EOM
268}
269
4ed0e2d4 270sub has_include {
271 my ($inc) = @_;
272 return 1 if
273 try_compile_and_link(<<EOM);
274#include "EXTERN.h"
275#include "perl.h"
276#include "XSUB.h"
277
278#include <$inc>
279int main _((int argc, char** argv, char** env))
280{
281 return 0;
282}
283EOM
284 return 0;
285}
286
170c5524 287sub has_clock_xxx_syscall {
ced84e60 288 my $x = shift;
289 return 0 unless defined $SYSCALL_H;
290 return 1 if
291 try_compile_and_link(<<EOM, run => 1);
292#include "EXTERN.h"
293#include "perl.h"
294#include "XSUB.h"
295#include <$SYSCALL_H>
296int main _((int argc, char** argv, char** env))
297{
298 struct timespec ts;
299 /* Many Linuxes get ENOSYS even though the syscall exists. */
300 /* All implementations are supposed to support CLOCK_REALTIME. */
301 int ret = syscall(SYS_clock_$x, CLOCK_REALTIME, &ts);
302 ret == 0 ? exit(0) : exit(errno ? errno : -1);
303}
304EOM
305}
306
170c5524 307sub has_clock_xxx {
308 my $xxx = shift;
ced84e60 309 return 1 if
310 try_compile_and_link(<<EOM, run => 1);
311#include "EXTERN.h"
312#include "perl.h"
313#include "XSUB.h"
314int main _((int argc, char** argv, char** env))
315{
316 struct timespec ts;
170c5524 317 int ret = clock_$xxx(CLOCK_REALTIME, &ts); /* Many Linuxes get ENOSYS. */
ced84e60 318 /* All implementations are supposed to support CLOCK_REALTIME. */
319 ret == 0 ? exit(0) : exit(errno ? errno : -1);
320}
321EOM
322}
323
170c5524 324sub has_clock {
325 return 1 if
326 try_compile_and_link(<<EOM, run => 1);
327#include "EXTERN.h"
328#include "perl.h"
329#include "XSUB.h"
330int main _((int argc, char** argv, char** env))
331{
332 clock_t tictoc;
333 clock_t ret = clock();
334 ret == (clock_t)-1 ? exit(errno ? errno : -1) : exit(0);
335}
336EOM
337}
338
339sub has_clock_nanosleep {
340 return 1 if
341 try_compile_and_link(<<EOM, run => 1);
342#include "EXTERN.h"
343#include "perl.h"
344#include "XSUB.h"
345int main _((int argc, char** argv, char** env))
346{
347 int ret;
348 struct timerspec ts1;
349 struct timerspec ts2;
350 ts1.tv_sec = 0;
351 ts1.tv_nsec = 750000000;;
352 ret = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts1, &ts2);
353 ret == 0 ? exit(0) : exit(errno ? errno : -1);
354}
355EOM
356}
357
046e3f33 358sub init {
359 my $hints = File::Spec->catfile("hints", "$^O.pl");
360 if (-f $hints) {
361 print "Using hints $hints...\n";
362 local $self;
363 do $hints;
364 if (exists $self->{LIBS}) {
365 $LIBS = $self->{LIBS};
366 print "Extra libraries: @$LIBS...\n";
3f2ee006 367 }
368 }
046e3f33 369
370 $DEFINE = '';
3f2ee006 371
ced84e60 372 if ($Config{d_syscall}) {
a5929ff3 373 print "Have syscall()... looking for syscall.h... ";
ced84e60 374 if (has_include('syscall.h')) {
375 $SYSCALL_H = 'syscall.h';
376 } elsif (has_include('sys/syscall.h')) {
377 $SYSCALL_H = 'sys/syscall.h';
378 }
a5929ff3 379 } else {
380 print "No syscall()...\n";
ced84e60 381 }
382
a5929ff3 383 if ($Config{d_syscall}) {
384 if (defined $SYSCALL_H) {
385 print "found <$SYSCALL_H>.\n";
386 } else {
387 print "NOT found.\n";
388 }
ced84e60 389 }
390
98b50af3 391 print "Looking for gettimeofday()... ";
3f2ee006 392 my $has_gettimeofday;
5d899d7e 393 if (exists $Config{d_gettimeod}) {
394 $has_gettimeofday++ if $Config{d_gettimeod};
3f2ee006 395 } elsif (has_gettimeofday()) {
396 $DEFINE .= ' -DHAS_GETTIMEOFDAY';
397 $has_gettimeofday++;
398 }
399
400 if ($has_gettimeofday) {
98b50af3 401 print "found.\n";
3f2ee006 402 } else {
403 die <<EOD
404Your operating system does not seem to have the gettimeofday() function.
405(or, at least, I cannot find it)
406
407There is no way Time::HiRes is going to work.
408
409I am awfully sorry but I cannot go further.
410
411Aborting configuration.
412
413EOD
414 }
415
98b50af3 416 print "Looking for setitimer()... ";
3f2ee006 417 my $has_setitimer;
5d899d7e 418 if (exists $Config{d_setitimer}) {
419 $has_setitimer++ if $Config{d_setitimer};
3f2ee006 420 } elsif (has_x("setitimer(ITIMER_REAL, 0, 0)")) {
421 $has_setitimer++;
422 $DEFINE .= ' -DHAS_SETITIMER';
423 }
424
425 if ($has_setitimer) {
98b50af3 426 print "found.\n";
3f2ee006 427 } else {
98b50af3 428 print "NOT found.\n";
3f2ee006 429 }
430
98b50af3 431 print "Looking for getitimer()... ";
3f2ee006 432 my $has_getitimer;
5d899d7e 433 if (exists $Config{'d_getitimer'}) {
434 $has_getitimer++ if $Config{'d_getitimer'};
3f2ee006 435 } elsif (has_x("getitimer(ITIMER_REAL, 0)")) {
436 $has_getitimer++;
437 $DEFINE .= ' -DHAS_GETITIMER';
438 }
439
440 if ($has_getitimer) {
98b50af3 441 print "found.\n";
3f2ee006 442 } else {
98b50af3 443 print "NOT found.\n";
3f2ee006 444 }
445
446 if ($has_setitimer && $has_getitimer) {
4ed0e2d4 447 print "You have interval timers (both setitimer and getitimer).\n";
3f2ee006 448 } else {
98b50af3 449 print "You do not have interval timers.\n";
3f2ee006 450 }
451
98b50af3 452 print "Looking for ualarm()... ";
453 my $has_ualarm;
5d899d7e 454 if (exists $Config{d_ualarm}) {
455 $has_ualarm++ if $Config{d_ualarm};
3f2ee006 456 } elsif (has_x ("ualarm (0, 0)")) {
457 $has_ualarm++;
458 $DEFINE .= ' -DHAS_UALARM';
459 }
460
461 if ($has_ualarm) {
98b50af3 462 print "found.\n";
3f2ee006 463 } else {
98b50af3 464 print "NOT found.\n";
465 if ($has_setitimer) {
466 print "But you have setitimer().\n";
467 print "We can make a Time::HiRes::ualarm().\n";
3f2ee006 468 }
469 }
470
98b50af3 471 print "Looking for usleep()... ";
3f2ee006 472 my $has_usleep;
5d899d7e 473 if (exists $Config{d_usleep}) {
474 $has_usleep++ if $Config{d_usleep};
3f2ee006 475 } elsif (has_x ("usleep (0)")) {
476 $has_usleep++;
477 $DEFINE .= ' -DHAS_USLEEP';
478 }
479
480 if ($has_usleep) {
98b50af3 481 print "found.\n";
3f2ee006 482 } else {
98b50af3 483 print "NOT found.\n";
484 print "Let's see if you have select()... ";
56c1b3bd 485 if ($Config{'d_select'}) {
98b50af3 486 print "found.\n";
487 print "We can make a Time::HiRes::usleep().\n";
3f2ee006 488 } else {
98b50af3 489 print "NOT found.\n";
490 print "You won't have a Time::HiRes::usleep().\n";
3f2ee006 491 }
492 }
493
98b50af3 494 print "Looking for nanosleep()... ";
3f2ee006 495 my $has_nanosleep;
3d0346a5 496 if ($ENV{FORCE_NANOSLEEP_SCAN}) {
497 print "forced scan... ";
498 if (has_nanosleep()) {
499 $has_nanosleep++;
500 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
501 }
502 }
503 elsif (exists $Config{d_nanosleep}) {
504 print "believing \$Config{d_nanosleep}... ";
c1363767 505 if ($Config{d_nanosleep}) {
506 $has_nanosleep++;
507 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
508 }
3d0346a5 509 } elsif ($^O =~ /^(mpeix)$/) {
510 # MPE/iX falsely finds nanosleep from its libc equivalent.
511 print "skipping because in $^O... ";
512 } else {
513 if (has_nanosleep()) {
514 $has_nanosleep++;
515 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
516 }
3f2ee006 517 }
518
519 if ($has_nanosleep) {
98b50af3 520 print "found.\n";
4ed0e2d4 521 print "You can mix subsecond sleeps with signals, if you want to.\n";
522 print "(It's still not portable, though.)\n";
3f2ee006 523 } else {
98b50af3 524 print "NOT found.\n";
622913ab 525 my $nt = ($^O eq 'os2' ? '' : 'not');
526 print "You can$nt mix subsecond sleeps with signals.\n";
4ed0e2d4 527 print "(It would not be portable anyway.)\n";
528 }
529
ced84e60 530 print "Looking for clock_gettime()... ";
531 my $has_clock_gettime;
532 if (exists $Config{d_clock_gettime}) {
533 $has_clock_gettime++ if $Config{d_clock_gettime}; # Unlikely...
170c5524 534 } elsif (has_clock_xxx('gettime')) {
ced84e60 535 $has_clock_gettime++;
536 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME';
170c5524 537 } elsif (defined $SYSCALL_H && has_clock_xxx_syscall('gettime')) {
ced84e60 538 $has_clock_gettime++;
539 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME -DTIME_HIRES_CLOCK_GETTIME_SYSCALL';
540 }
541
542 if ($has_clock_gettime) {
543 if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETTIME_SYSCALL/) {
544 print "found (via syscall).\n";
545 } else {
546 print "found.\n";
547 }
548 } else {
549 print "NOT found.\n";
550 }
551
552 print "Looking for clock_getres()... ";
553 my $has_clock_getres;
554 if (exists $Config{d_clock_getres}) {
555 $has_clock_getres++ if $Config{d_clock_getres}; # Unlikely...
170c5524 556 } elsif (has_clock_xxx('getres')) {
ced84e60 557 $has_clock_getres++;
558 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES';
170c5524 559 } elsif (defined $SYSCALL_H && has_clock_xxx_syscall('getres')) {
ced84e60 560 $has_clock_getres++;
561 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES -DTIME_HIRES_CLOCK_GETRES_SYSCALL';
562 }
563
564 if ($has_clock_getres) {
565 if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETRES_SYSCALL/) {
566 print "found (via syscall).\n";
567 } else {
568 print "found.\n";
569 }
570 } else {
571 print "NOT found.\n";
572 }
573
170c5524 574 print "Looking for clock_nanosleep()... ";
575 my $has_clock_nanosleep;
576 if (exists $Config{d_clock_nanosleep}) {
577 $has_clock_nanosleep++ if $Config{d_clock_nanosleep}; # Unlikely...
578 } elsif (has_clock_nanosleep()) {
579 $has_clock_nanosleep++;
580 $DEFINE .= ' -DTIME_HIRES_CLOCK_NANOSLEEP';
581 }
582
583 if ($has_clock_nanosleep) {
584 print "found.\n";
585 } else {
586 print "NOT found.\n";
587 }
588
589 print "Looking for clock()... ";
590 my $has_clock;
591 if (exists $Config{d_clock}) {
592 $has_clock++ if $Config{d_clock}; # Unlikely...
593 } elsif (has_clock()) {
594 $has_clock++;
595 $DEFINE .= ' -DTIME_HIRES_CLOCK';
596 }
597
598 if ($has_clock) {
599 print "found.\n";
600 } else {
601 print "NOT found.\n";
602 }
603
75d5269b 604 print "Trying struct stat st_atimespec.tv_nsec...";
605 my $has_stat_st_xtimespec;
606 if (try_compile_and_link(<<EOM)) {
607$TIME_HEADERS
608#include <sys/stat.h>
71ac5476 609int main _((int argc, char** argv, char** env)) {
75d5269b 610 struct stat st;
611 st.st_atimespec.tv_nsec = 0;
612}
613EOM
614 $has_stat_st_xtimespec++;
615 $DEFINE .= ' -DTIME_HIRES_STAT=1';
616 }
617
618 if ($has_stat_st_xtimespec) {
619 print "found.\n";
620 } else {
621 print "NOT found.\n";
622 }
623
624 print "Trying struct stat st_atimensec...";
625 my $has_stat_st_xtimensec;
626 if (try_compile_and_link(<<EOM)) {
627$TIME_HEADERS
628#include <sys/stat.h>
71ac5476 629int main _((int argc, char** argv, char** env)) {
75d5269b 630 struct stat st;
631 st.st_atimensec = 0;
632}
633EOM
634 $has_stat_st_xtimensec++;
635 $DEFINE .= ' -DTIME_HIRES_STAT=2';
636 }
637
638 if ($has_stat_st_xtimensec) {
639 print "found.\n";
640 } else {
641 print "NOT found.\n";
642 }
643
644 print "Trying struct stat st_atime_n...";
645 my $has_stat_st_xtime_n;
646 if (try_compile_and_link(<<EOM)) {
647$TIME_HEADERS
648#include <sys/stat.h>
71ac5476 649int main _((int argc, char** argv, char** env)) {
75d5269b 650 struct stat st;
651 st.st_atime_n = 0;
652}
653EOM
654 $has_stat_st_xtime_n++;
655 $DEFINE .= ' -DTIME_HIRES_STAT=3';
656 }
657
658 if ($has_stat_st_xtime_n) {
659 print "found.\n";
660 } else {
661 print "NOT found.\n";
662 }
663
664 print "Trying struct stat st_atim.tv_nsec...";
665 my $has_stat_st_xtim;
666 if (try_compile_and_link(<<EOM)) {
667$TIME_HEADERS
668#include <sys/stat.h>
71ac5476 669int main _((int argc, char** argv, char** env)) {
75d5269b 670 struct stat st;
671 st.st_atim.tv_nsec = 0;
672}
673EOM
674 $has_stat_st_xtim++;
675 $DEFINE .= ' -DTIME_HIRES_STAT=4';
676 }
677
678 if ($has_stat_st_xtim) {
679 print "found.\n";
680 } else {
681 print "NOT found.\n";
682 }
683
684 print "Trying struct stat st_uatime...";
685 my $has_stat_st_uxtime;
686 if (try_compile_and_link(<<EOM)) {
687$TIME_HEADERS
688#include <sys/stat.h>
71ac5476 689int main _((int argc, char** argv, char** env)) {
75d5269b 690 struct stat st;
691 st.st_uatime = 0;
692}
693EOM
694 $has_stat_st_uxtime++;
695 $DEFINE .= ' -DTIME_HIRES_STAT=5';
696 }
697
698 if ($has_stat_st_uxtime) {
699 print "found.\n";
700 } else {
701 print "NOT found.\n";
702 }
703
704 if ($DEFINE =~ /-DTIME_HIRES_STAT=\d+/) {
705 print "You seem to have stat subsecond timestamps.\n";
706 print "(Your struct stat has them, but the filesystems must help.)\n";
707 } else {
708 print "You do not seem to have stat subsecond timestamps.\n";
709 }
710
4ed0e2d4 711 my $has_w32api_windows_h;
170c5524 712
4ed0e2d4 713 if ($^O eq 'cygwin') {
714 print "Looking for <w32api/windows.h>... ";
715 if (has_include('w32api/windows.h')) {
716 $has_w32api_windows_h++;
717 $DEFINE .= ' -DHAS_W32API_WINDOWS_H';
718 }
719 if ($has_w32api_windows_h) {
720 print "found.\n";
721 } else {
722 print "NOT found.\n";
723 }
3f2ee006 724 }
725
726 if ($DEFINE) {
727 $DEFINE =~ s/^\s+//;
728 if (open(XDEFINE, ">xdefine")) {
729 print XDEFINE $DEFINE, "\n";
730 close(XDEFINE);
731 }
732 }
733}
734
735sub doMakefile {
98b50af3 736 my @makefileopts = ();
3f2ee006 737
738 if ($] >= 5.005) {
739 push (@makefileopts,
740 'AUTHOR' => 'Jarkko Hietaniemi <jhi@iki.fi>',
741 'ABSTRACT_FROM' => 'HiRes.pm',
742 );
743 $DEFINE .= " -DATLEASTFIVEOHOHFIVE";
744 }
745
746 push (@makefileopts,
747 'NAME' => 'Time::HiRes',
748 'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
5d899d7e 749 'LIBS' => $LIBS, # e.g., '-lm'
750 'DEFINE' => $DEFINE, # e.g., '-DHAS_SOMETHING'
3f2ee006 751 'XSOPT' => $XSOPT,
ced84e60 752 # Do not even think about 'INC' => '-I/usr/ucbinclude',
753 # Solaris will avenge.
5d899d7e 754 'INC' => '', # e.g., '-I/usr/include/other'
9649b81b 755 'INSTALLDIRS' => ($] >= 5.008 ? 'perl' : 'site'),
3f2ee006 756 'dist' => {
757 'CI' => 'ci -l',
5d899d7e 758 'COMPRESS' => 'gzip -9f',
3f2ee006 759 'SUFFIX' => 'gz',
760 },
761 clean => { FILES => "xdefine" },
1caec985 762 realclean => { FILES=> 'const-c.inc const-xs.inc' },
3f2ee006 763 );
764
9ac5eb64 765 if ($ENV{PERL_CORE}) {
766 push @makefileopts, MAN3PODS => {};
767 }
768
3f2ee006 769 WriteMakefile(@makefileopts);
770}
771
98b50af3 772sub doConstants {
773 if (eval {require ExtUtils::Constant; 1}) {
75d5269b 774 my @names = qw(CLOCK_HIGHRES CLOCK_MONOTONIC
775 CLOCK_PROCESS_CPUTIME_ID
776 CLOCK_REALTIME
777 CLOCK_SOFTTIME
778 CLOCK_THREAD_CPUTIME_ID
779 CLOCK_TIMEOFDAY
780 CLOCKS_PER_SEC
781 ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF
782 ITIMER_REALPROF
783 TIMER_ABSTIME);
98b50af3 784 foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
170c5524 785 d_nanosleep d_clock_gettime d_clock_getres
75d5269b 786 d_clock d_clock_nanosleep d_hires_stat)) {
98b50af3 787 my $macro = $_;
170c5524 788 if ($macro =~ /^(d_nanosleep|d_clock_gettime|d_clock_getres|d_clock|d_clock_nanosleep)$/) {
82cbdcc3 789 $macro =~ s/^d_(.+)/TIME_HIRES_\U$1/;
75d5269b 790 } elsif ($macro =~ /^(d_hires_stat)$/) {
791 my $d_hires_stat = 0;
792 $d_hires_stat = $1 if ($DEFINE =~ /-DTIME_HIRES_STAT=(\d+)/);
793 push @names, {name => $_, macro => "TIME_HIRES_STAT", value => $d_hires_stat,
794 default => ["IV", "0"]};
795 next;
672f6857 796 } else {
82cbdcc3 797 $macro =~ s/^d_(.+)/HAS_\U$1/;
672f6857 798 }
98b50af3 799 push @names, {name => $_, macro => $macro, value => 1,
800 default => ["IV", "0"]};
801 }
802 ExtUtils::Constant::WriteConstants(
803 NAME => 'Time::HiRes',
804 NAMES => \@names,
805 );
806 } else {
1fbb4de4 807 my $file;
808 foreach $file ('const-c.inc', 'const-xs.inc') {
98b50af3 809 my $fallback = File::Spec->catfile('fallback', $file);
810 local $/;
811 open IN, "<$fallback" or die "Can't open $fallback: $!";
812 open OUT, ">$file" or die "Can't open $file: $!";
813 print OUT <IN> or die $!;
814 close OUT or die "Can't close $file: $!";
815 close IN or die "Can't close $fallback: $!";
816 }
817 }
818}
3f2ee006 819
98b50af3 820sub main {
821 print "Configuring Time::HiRes...\n";
1fbb4de4 822 if ($] == 5.007002) {
823 die "Cannot Configure Time::HiRes for Perl $], aborting.\n";
824 }
3f2ee006 825
826 if ($^O =~ /Win32/i) {
827 $DEFINE = '-DSELECT_IS_BROKEN';
046e3f33 828 $LIBS = [];
a5929ff3 829 print "System is $^O, skipping full configure...\n";
3f2ee006 830 } else {
046e3f33 831 init();
3f2ee006 832 }
3f2ee006 833 doMakefile;
98b50af3 834 doConstants;
3f2ee006 835 my $make = $Config{'make'} || "make";
1fbb4de4 836 unless (exists $ENV{PERL_CORE} && $ENV{PERL_CORE}) {
3f2ee006 837 print <<EOM;
3f2ee006 838Now you may issue '$make'. Do not forget also '$make test'.
dfffa540 839EOM
1fbb4de4 840 if ((exists $ENV{LC_ALL} && $ENV{LC_ALL} =~ /utf-?8/i) ||
841 (exists $ENV{LC_CTYPE} && $ENV{LC_CTYPE} =~ /utf-?8/i) ||
842 (exists $ENV{LANG} && $ENV{LANG} =~ /utf-?8/i)) {
dfffa540 843 print <<EOM;
ced84e60 844NOTE: if you get an error like this (the Makefile line number may vary):
d7358e6a 845Makefile:91: *** missing separator
1caec985 846then set the environment variable LC_ALL to "C" and retry
847from scratch (re-run perl "Makefile.PL").
3f2ee006 848EOM
dfffa540 849 }
3f2ee006 850 }
851}
852
853&main;
dcf686c9 854
3f2ee006 855# EOF