This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update Time-Piece to CPAN version 1.3202
[perl5.git] / dist / Time-HiRes / Makefile.PL
CommitLineData
56c1b3bd
RGS
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
RGS
5# Use $ENV{FORCE_NANOSLEEP_SCAN} to force rescanning whether there
6# really is hope.
7
90e44bf6 8{ use 5.006; }
3f2ee006
HS
9
10use Config;
dcf686c9 11use ExtUtils::MakeMaker;
98b50af3 12use strict;
3f2ee006
HS
13
14my $VERBOSE = $ENV{VERBOSE};
98b50af3 15my $DEFINE;
046e3f33 16my $LIBS = [];
56c1b3bd 17my $XSOPT = '';
ced84e60 18my $SYSCALL_H;
98b50af3 19
90e44bf6 20our $self; # Used in 'sourcing' the hints.
046e3f33 21
3722f0dc
PG
22# TBD: Can we just use $Config(exe_ext) here instead of this complex
23# expression?
4ed0e2d4 24my $ld_exeext = ($^O eq 'cygwin' ||
3722f0dc
PG
25 $^O eq 'os2' && $Config{ldflags} =~ /-Zexe\b/) ? '.exe' :
26 (($^O eq 'vos') ? $Config{exe_ext} : '');
622913ab 27
12724655
IZ
28unless($ENV{PERL_CORE}) {
29 $ENV{PERL_CORE} = 1 if grep { $_ eq 'PERL_CORE=1' } @ARGV;
98b50af3
JH
30}
31
32# Perls 5.002 and 5.003 did not have File::Spec, fake what we need.
3f2ee006
HS
33
34sub my_dirsep {
35 $^O eq 'VMS' ? '.' :
36 $^O =~ /mswin32|netware|djgpp/i ? '\\' :
37 $^O eq 'MacOS' ? ':'
38 : '/';
39}
40
41sub my_catdir {
42 shift;
43 my $catdir = join(my_dirsep, @_);
98b50af3
JH
44 $^O eq 'VMS' ? "[$catdir]" : $catdir;
45}
46
47sub my_catfile {
48 shift;
49 return join(my_dirsep, @_) unless $^O eq 'VMS';
50 my $file = pop;
51 return my_catdir (undef, @_) . $file;
3f2ee006
HS
52}
53
54sub my_updir {
55 shift;
56 $^O eq 'VMS' ? "-" : "..";
57}
58
59BEGIN {
60 eval { require File::Spec };
61 if ($@) {
25ca0970
JH
62 *File::Spec::catdir = \&my_catdir;
63 *File::Spec::updir = \&my_updir;
98b50af3 64 *File::Spec::catfile = \&my_catfile;
3f2ee006
HS
65 }
66}
67
98b50af3
JH
68# Avoid 'used only once' warnings.
69my $nop1 = *File::Spec::catdir;
70my $nop2 = *File::Spec::updir;
71my $nop3 = *File::Spec::catfile;
72
3f2ee006
HS
73# if you have 5.004_03 (and some slightly older versions?), xsubpp
74# tries to generate line numbers in the C code generated from the .xs.
75# unfortunately, it is a little buggy around #ifdef'd code.
5d899d7e 76# my choice is leave it in and have people with old perls complain
3f2ee006 77# about the "Usage" bug, or leave it out and be unable to compile myself
5d899d7e 78# without changing it, and then I'd always forget to change it before a
3f2ee006
HS
79# release. Sorry, Edward :)
80
3f2ee006
HS
81sub try_compile_and_link {
82 my ($c, %args) = @_;
83
84 my ($ok) = 0;
2359510d 85 my ($tmp) = "tmp$$";
3f2ee006
HS
86 local(*TMPC);
87
88 my $obj_ext = $Config{obj_ext} || ".o";
89 unlink("$tmp.c", "$tmp$obj_ext");
90
1ae6ead9 91 if (open(TMPC, '>', "$tmp.c")) {
3f2ee006
HS
92 print TMPC $c;
93 close(TMPC);
94
98b50af3 95 my $cccmd = $args{cccmd};
3f2ee006
HS
96
97 my $errornull;
98
99 my $COREincdir;
98b50af3 100
3f2ee006
HS
101 if ($ENV{PERL_CORE}) {
102 my $updir = File::Spec->updir;
40d04ec4 103 $COREincdir = File::Spec->catdir(($updir) x 2);
3f2ee006
HS
104 } else {
105 $COREincdir = File::Spec->catdir($Config{'archlibexp'}, 'CORE');
106 }
98b50af3 107
9e000d5b
SH
108 if ($ENV{PERL_CORE}) {
109 unless (-f File::Spec->catfile($COREincdir, "EXTERN.h")) {
110 die <<__EOD__;
111Your environment variable PERL_CORE is '$ENV{PERL_CORE}' but there
112is no EXTERN.h in $COREincdir.
113Cannot continue, aborting.
114__EOD__
115 }
116 }
117
c7627e6d
NC
118 my $ccflags = $Config{'ccflags'} . ' ' . "-I$COREincdir"
119 . ' -DPERL_NO_INLINE_FUNCTIONS';
98b50af3 120
3f2ee006 121 if ($^O eq 'VMS') {
4edfaa88 122 $cccmd = "$Config{'cc'} /include=($COREincdir) $tmp.c";
3f2ee006
HS
123 }
124
125 if ($args{silent} || !$VERBOSE) {
126 $errornull = "2>/dev/null" unless defined $errornull;
127 } else {
128 $errornull = '';
129 }
130
98b50af3 131 $cccmd = "$Config{'cc'} -o $tmp $ccflags $tmp.c @$LIBS $errornull"
3f2ee006 132 unless defined $cccmd;
98b50af3 133
c1363767 134 if ($^O eq 'VMS') {
1ae6ead9 135 open( CMDFILE, '>', "$tmp.com" );
3f2ee006
HS
136 print CMDFILE "\$ SET MESSAGE/NOFACILITY/NOSEVERITY/NOIDENT/NOTEXT\n";
137 print CMDFILE "\$ $cccmd\n";
98b50af3 138 print CMDFILE "\$ IF \$SEVERITY .NE. 1 THEN EXIT 44\n"; # escalate
3f2ee006
HS
139 close CMDFILE;
140 system("\@ $tmp.com");
141 $ok = $?==0;
5d899d7e 142 for ("$tmp.c", "$tmp$obj_ext", "$tmp.com", "$tmp$Config{exe_ext}") {
98b50af3 143 1 while unlink $_;
3f2ee006
HS
144 }
145 }
146 else
147 {
622913ab 148 my $tmp_exe = "$tmp$ld_exeext";
3f2ee006 149 printf "cccmd = $cccmd\n" if $VERBOSE;
622913ab 150 my $res = system($cccmd);
ced84e60 151 $ok = defined($res) && $res == 0 && -s $tmp_exe && -x _;
26e22fd9 152
5ac41681 153 if ( $ok && exists $args{run} && $args{run} && !$ENV{TIME_HIRES_DONT_RUN_PROBES} ) {
06252d99
RGS
154 my $tmp_exe =
155 File::Spec->catfile(File::Spec->curdir, $tmp_exe);
120b53f9
RS
156 my @run = $tmp_exe;
157 unshift @run, $Config{run} if $Config{run} && -e $Config{run};
06252d99 158 printf "Running $tmp_exe..." if $VERBOSE;
120b53f9 159 if (system(@run) == 0) {
56c1b3bd 160 $ok = 1;
26e22fd9 161 } else {
56c1b3bd 162 $ok = 0;
ced84e60
SP
163 my $errno = $? >> 8;
164 local $! = $errno;
165 printf <<EOF;
166
167*** The test run of '$tmp_exe' failed: status $?
168*** (the status means: errno = $errno or '$!')
169*** DO NOT PANIC: this just means that *some* functionality will be missing.
170EOF
26e22fd9
RGS
171 }
172 }
622913ab 173 unlink("$tmp.c", $tmp_exe);
3f2ee006
HS
174 }
175 }
5d899d7e 176
26e22fd9 177 return $ok;
3f2ee006
HS
178}
179
75d5269b 180my $TIME_HEADERS = <<EOH;
5d899d7e
JH
181#include "EXTERN.h"
182#include "perl.h"
183#include "XSUB.h"
184#ifdef I_SYS_TYPES
3f2ee006
HS
185# include <sys/types.h>
186#endif
3f2ee006
HS
187#ifdef I_SYS_TIME
188# include <sys/time.h>
189#endif
3f2ee006
HS
190#ifdef I_SYS_SELECT
191# include <sys/select.h> /* struct timeval might be hidden in here */
192#endif
75d5269b
SP
193EOH
194
195sub has_gettimeofday {
196 # confusing but true (if condition true ==> -DHAS_GETTIMEOFDAY already)
197 return 0 if $Config{d_gettimeod};
198 return 1 if try_compile_and_link(<<EOM);
199$TIME_HEADERS
3f2ee006
HS
200static int foo()
201{
202 struct timeval tv;
203 gettimeofday(&tv, 0);
204}
f3370602 205int main(int argc, char** argv)
3f2ee006
HS
206{
207 foo();
208}
209EOM
210 return 0;
211}
212
213sub has_x {
98b50af3 214 my ($x, %args) = @_;
3f2ee006
HS
215
216 return 1 if
217 try_compile_and_link(<<EOM, %args);
218#include "EXTERN.h"
219#include "perl.h"
220#include "XSUB.h"
221
222#ifdef I_UNISTD
223# include <unistd.h>
224#endif
225
226#ifdef I_SYS_TYPES
227# include <sys/types.h>
228#endif
229
230#ifdef I_SYS_TIME
231# include <sys/time.h>
232#endif
233
f3370602 234int main(int argc, char** argv)
3f2ee006
HS
235{
236 $x;
237}
238EOM
239 return 0;
240}
241
26e22fd9 242sub has_nanosleep {
3d0346a5 243 print "testing... ";
26e22fd9
RGS
244 return 1 if
245 try_compile_and_link(<<EOM, run => 1);
246#include <time.h>
247#include <sys/time.h>
248#include <stdio.h>
249#include <stdlib.h>
250#include <errno.h>
251
252/* int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); */
253
f3370602 254int main(int argc, char** argv) {
26e22fd9 255 struct timespec ts1, ts2;
56c1b3bd 256 int ret;
26e22fd9
RGS
257 ts1.tv_sec = 0;
258 ts1.tv_nsec = 750000000;
259 ts2.tv_sec = 0;
260 ts2.tv_nsec = 0;
56c1b3bd 261 errno = 0;
ced84e60 262 ret = nanosleep(&ts1, &ts2); /* E.g. in AIX nanosleep() fails and sets errno to ENOSYS. */
56c1b3bd 263 ret == 0 ? exit(0) : exit(errno ? errno : -1);
26e22fd9
RGS
264}
265EOM
266}
267
4ed0e2d4
RGS
268sub has_include {
269 my ($inc) = @_;
270 return 1 if
271 try_compile_and_link(<<EOM);
272#include "EXTERN.h"
273#include "perl.h"
274#include "XSUB.h"
275
276#include <$inc>
f3370602 277int main(int argc, char** argv)
4ed0e2d4
RGS
278{
279 return 0;
280}
281EOM
282 return 0;
283}
284
170c5524 285sub has_clock_xxx_syscall {
ced84e60
SP
286 my $x = shift;
287 return 0 unless defined $SYSCALL_H;
288 return 1 if
289 try_compile_and_link(<<EOM, run => 1);
290#include "EXTERN.h"
291#include "perl.h"
292#include "XSUB.h"
605b4dbe 293#include <time.h>
ced84e60 294#include <$SYSCALL_H>
f3370602 295int main(int argc, char** argv)
ced84e60
SP
296{
297 struct timespec ts;
298 /* Many Linuxes get ENOSYS even though the syscall exists. */
299 /* All implementations are supposed to support CLOCK_REALTIME. */
300 int ret = syscall(SYS_clock_$x, CLOCK_REALTIME, &ts);
301 ret == 0 ? exit(0) : exit(errno ? errno : -1);
302}
303EOM
304}
305
170c5524
SP
306sub has_clock_xxx {
307 my $xxx = shift;
ced84e60
SP
308 return 1 if
309 try_compile_and_link(<<EOM, run => 1);
310#include "EXTERN.h"
311#include "perl.h"
312#include "XSUB.h"
605b4dbe 313#include <time.h>
f3370602 314int main(int argc, char** argv)
ced84e60
SP
315{
316 struct timespec ts;
170c5524 317 int ret = clock_$xxx(CLOCK_REALTIME, &ts); /* Many Linuxes get ENOSYS. */
ced84e60
SP
318 /* All implementations are supposed to support CLOCK_REALTIME. */
319 ret == 0 ? exit(0) : exit(errno ? errno : -1);
320}
321EOM
322}
323
170c5524
SP
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"
605b4dbe 330#include <time.h>
f3370602 331int main(int argc, char** argv)
170c5524
SP
332{
333 clock_t tictoc;
334 clock_t ret = clock();
335 ret == (clock_t)-1 ? exit(errno ? errno : -1) : exit(0);
336}
337EOM
338}
339
340sub has_clock_nanosleep {
341 return 1 if
342 try_compile_and_link(<<EOM, run => 1);
343#include "EXTERN.h"
344#include "perl.h"
345#include "XSUB.h"
a8fb48f7 346#include <time.h>
f3370602 347int main(int argc, char** argv)
170c5524
SP
348{
349 int ret;
a8fb48f7
SP
350 struct timespec ts1;
351 struct timespec ts2;
170c5524
SP
352 ts1.tv_sec = 0;
353 ts1.tv_nsec = 750000000;;
56ed632c
DIM
354 /* All implementations are supposed to support CLOCK_REALTIME. */
355 ret = clock_nanosleep(CLOCK_REALTIME, 0, &ts1, &ts2);
170c5524
SP
356 ret == 0 ? exit(0) : exit(errno ? errno : -1);
357}
358EOM
359}
360
c4a535af
SH
361sub has_futimens {
362 return 1 if
363 try_compile_and_link(<<EOM);
364#include "EXTERN.h"
365#include "perl.h"
366#include "XSUB.h"
605b4dbe 367#include <sys/stat.h>
c4a535af
SH
368int main(int argc, char** argv)
369{
370 int ret;
371 struct timespec ts[2];
372 ret = futimens(0, ts);
373 ret == 0 ? exit(0) : exit(errno ? errno : -1);
374}
375EOM
376}
377
378sub has_utimensat{
379 return 1 if
380 try_compile_and_link(<<EOM);
381#include "EXTERN.h"
382#include "perl.h"
383#include "XSUB.h"
605b4dbe 384#include <sys/stat.h>
c4a535af
SH
385#include <fcntl.h>
386int main(int argc, char** argv)
387{
388 int ret;
389 struct timespec ts[2];
390 ret = utimensat(AT_FDCWD, 0, ts, 0);
391 ret == 0 ? exit(0) : exit(errno ? errno : -1);
392}
393EOM
394}
395
69d1f2c2
JH
396sub has_clockid_t{
397 return 1 if
398 try_compile_and_link(<<EOM);
399#include "EXTERN.h"
400#include "perl.h"
401#include "XSUB.h"
605b4dbe 402#include <time.h>
69d1f2c2
JH
403int main(int argc, char** argv)
404{
405 clockid_t id = CLOCK_REALTIME;
406 exit(id == CLOCK_REALTIME ? 1 : 0);
407}
408EOM
409}
410
a8fb48f7
SP
411sub DEFINE {
412 my ($def, $val) = @_;
413 my $define = defined $val ? "$def=$val" : $def ;
414 unless ($DEFINE =~ /(?:^| )-D\Q$define\E(?: |$)/) {
415 $DEFINE .= " -D$define";
416 }
417}
418
046e3f33 419sub init {
3172fdbc 420 my $hints = File::Spec->catfile("hints", "$^O.pl");
046e3f33
JH
421 if (-f $hints) {
422 print "Using hints $hints...\n";
423 local $self;
3172fdbc 424 do "./$hints";
046e3f33
JH
425 if (exists $self->{LIBS}) {
426 $LIBS = $self->{LIBS};
427 print "Extra libraries: @$LIBS...\n";
3f2ee006
HS
428 }
429 }
046e3f33
JH
430
431 $DEFINE = '';
3f2ee006 432
ced84e60 433 if ($Config{d_syscall}) {
a5929ff3 434 print "Have syscall()... looking for syscall.h... ";
ced84e60
SP
435 if (has_include('syscall.h')) {
436 $SYSCALL_H = 'syscall.h';
437 } elsif (has_include('sys/syscall.h')) {
438 $SYSCALL_H = 'sys/syscall.h';
439 }
a5929ff3
RGS
440 } else {
441 print "No syscall()...\n";
ced84e60
SP
442 }
443
a5929ff3
RGS
444 if ($Config{d_syscall}) {
445 if (defined $SYSCALL_H) {
446 print "found <$SYSCALL_H>.\n";
447 } else {
448 print "NOT found.\n";
449 }
ced84e60
SP
450 }
451
98b50af3 452 print "Looking for gettimeofday()... ";
3f2ee006 453 my $has_gettimeofday;
5d899d7e
JH
454 if (exists $Config{d_gettimeod}) {
455 $has_gettimeofday++ if $Config{d_gettimeod};
3f2ee006
HS
456 } elsif (has_gettimeofday()) {
457 $DEFINE .= ' -DHAS_GETTIMEOFDAY';
458 $has_gettimeofday++;
459 }
460
461 if ($has_gettimeofday) {
98b50af3 462 print "found.\n";
3f2ee006
HS
463 } else {
464 die <<EOD
465Your operating system does not seem to have the gettimeofday() function.
466(or, at least, I cannot find it)
467
468There is no way Time::HiRes is going to work.
469
470I am awfully sorry but I cannot go further.
471
472Aborting configuration.
473
474EOD
475 }
476
98b50af3 477 print "Looking for setitimer()... ";
3f2ee006 478 my $has_setitimer;
5d899d7e
JH
479 if (exists $Config{d_setitimer}) {
480 $has_setitimer++ if $Config{d_setitimer};
3f2ee006
HS
481 } elsif (has_x("setitimer(ITIMER_REAL, 0, 0)")) {
482 $has_setitimer++;
483 $DEFINE .= ' -DHAS_SETITIMER';
484 }
485
486 if ($has_setitimer) {
98b50af3 487 print "found.\n";
3f2ee006 488 } else {
98b50af3 489 print "NOT found.\n";
3f2ee006
HS
490 }
491
98b50af3 492 print "Looking for getitimer()... ";
3f2ee006 493 my $has_getitimer;
5d899d7e
JH
494 if (exists $Config{'d_getitimer'}) {
495 $has_getitimer++ if $Config{'d_getitimer'};
3f2ee006
HS
496 } elsif (has_x("getitimer(ITIMER_REAL, 0)")) {
497 $has_getitimer++;
498 $DEFINE .= ' -DHAS_GETITIMER';
499 }
500
501 if ($has_getitimer) {
98b50af3 502 print "found.\n";
3f2ee006 503 } else {
98b50af3 504 print "NOT found.\n";
3f2ee006
HS
505 }
506
507 if ($has_setitimer && $has_getitimer) {
4ed0e2d4 508 print "You have interval timers (both setitimer and getitimer).\n";
3f2ee006 509 } else {
98b50af3 510 print "You do not have interval timers.\n";
3f2ee006
HS
511 }
512
98b50af3
JH
513 print "Looking for ualarm()... ";
514 my $has_ualarm;
5d899d7e
JH
515 if (exists $Config{d_ualarm}) {
516 $has_ualarm++ if $Config{d_ualarm};
3f2ee006
HS
517 } elsif (has_x ("ualarm (0, 0)")) {
518 $has_ualarm++;
519 $DEFINE .= ' -DHAS_UALARM';
520 }
521
522 if ($has_ualarm) {
98b50af3 523 print "found.\n";
3f2ee006 524 } else {
98b50af3
JH
525 print "NOT found.\n";
526 if ($has_setitimer) {
527 print "But you have setitimer().\n";
528 print "We can make a Time::HiRes::ualarm().\n";
3f2ee006
HS
529 }
530 }
531
98b50af3 532 print "Looking for usleep()... ";
3f2ee006 533 my $has_usleep;
5d899d7e
JH
534 if (exists $Config{d_usleep}) {
535 $has_usleep++ if $Config{d_usleep};
3f2ee006
HS
536 } elsif (has_x ("usleep (0)")) {
537 $has_usleep++;
538 $DEFINE .= ' -DHAS_USLEEP';
539 }
540
541 if ($has_usleep) {
98b50af3 542 print "found.\n";
3f2ee006 543 } else {
98b50af3
JH
544 print "NOT found.\n";
545 print "Let's see if you have select()... ";
56c1b3bd 546 if ($Config{'d_select'}) {
98b50af3
JH
547 print "found.\n";
548 print "We can make a Time::HiRes::usleep().\n";
3f2ee006 549 } else {
98b50af3
JH
550 print "NOT found.\n";
551 print "You won't have a Time::HiRes::usleep().\n";
3f2ee006
HS
552 }
553 }
554
98b50af3 555 print "Looking for nanosleep()... ";
3f2ee006 556 my $has_nanosleep;
3d0346a5
SP
557 if ($ENV{FORCE_NANOSLEEP_SCAN}) {
558 print "forced scan... ";
559 if (has_nanosleep()) {
560 $has_nanosleep++;
561 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
562 }
563 }
564 elsif (exists $Config{d_nanosleep}) {
565 print "believing \$Config{d_nanosleep}... ";
c1363767
JH
566 if ($Config{d_nanosleep}) {
567 $has_nanosleep++;
568 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
569 }
3d0346a5
SP
570 } elsif ($^O =~ /^(mpeix)$/) {
571 # MPE/iX falsely finds nanosleep from its libc equivalent.
572 print "skipping because in $^O... ";
573 } else {
574 if (has_nanosleep()) {
575 $has_nanosleep++;
576 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
577 }
3f2ee006
HS
578 }
579
580 if ($has_nanosleep) {
98b50af3 581 print "found.\n";
4ed0e2d4
RGS
582 print "You can mix subsecond sleeps with signals, if you want to.\n";
583 print "(It's still not portable, though.)\n";
3f2ee006 584 } else {
98b50af3 585 print "NOT found.\n";
622913ab
IZ
586 my $nt = ($^O eq 'os2' ? '' : 'not');
587 print "You can$nt mix subsecond sleeps with signals.\n";
4ed0e2d4
RGS
588 print "(It would not be portable anyway.)\n";
589 }
590
69d1f2c2
JH
591 print "Looking for clockid_t... ";
592 my $has_clockid_t;
593 if (has_clockid_t()) {
594 print "found.\n";
595 $has_clockid_t++;
596 $DEFINE .= ' -DTIME_HIRES_CLOCKID_T';
597 } else {
598 print "NOT found, will use int.\n";
599 }
600
ced84e60
SP
601 print "Looking for clock_gettime()... ";
602 my $has_clock_gettime;
1e845d0c 603 my $has_clock_gettime_emulation;
ced84e60
SP
604 if (exists $Config{d_clock_gettime}) {
605 $has_clock_gettime++ if $Config{d_clock_gettime}; # Unlikely...
170c5524 606 } elsif (has_clock_xxx('gettime')) {
ced84e60
SP
607 $has_clock_gettime++;
608 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME';
170c5524 609 } elsif (defined $SYSCALL_H && has_clock_xxx_syscall('gettime')) {
ced84e60
SP
610 $has_clock_gettime++;
611 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME -DTIME_HIRES_CLOCK_GETTIME_SYSCALL';
1e845d0c
JH
612 } elsif ($^O eq 'darwin') {
613 $has_clock_gettime_emulation++;
614 $has_clock_gettime++;
c4a535af 615 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME -DTIME_HIRES_CLOCK_GETTIME_EMULATION';
ced84e60
SP
616 }
617
618 if ($has_clock_gettime) {
619 if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETTIME_SYSCALL/) {
620 print "found (via syscall).\n";
1e845d0c
JH
621 } elsif ($has_clock_gettime_emulation) {
622 print "found (via emulation).\n";
ced84e60
SP
623 } else {
624 print "found.\n";
625 }
626 } else {
627 print "NOT found.\n";
628 }
629
630 print "Looking for clock_getres()... ";
631 my $has_clock_getres;
1e845d0c 632 my $has_clock_getres_emulation;
ced84e60
SP
633 if (exists $Config{d_clock_getres}) {
634 $has_clock_getres++ if $Config{d_clock_getres}; # Unlikely...
170c5524 635 } elsif (has_clock_xxx('getres')) {
ced84e60
SP
636 $has_clock_getres++;
637 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES';
170c5524 638 } elsif (defined $SYSCALL_H && has_clock_xxx_syscall('getres')) {
ced84e60
SP
639 $has_clock_getres++;
640 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES -DTIME_HIRES_CLOCK_GETRES_SYSCALL';
1e845d0c
JH
641 } elsif ($^O eq 'darwin') {
642 $has_clock_getres_emulation++;
643 $has_clock_getres++;
c4a535af 644 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES -DTIME_HIRES_CLOCK_GETRES_EMULATION';
ced84e60
SP
645 }
646
647 if ($has_clock_getres) {
648 if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETRES_SYSCALL/) {
649 print "found (via syscall).\n";
1e845d0c
JH
650 } elsif ($has_clock_getres_emulation) {
651 print "found (via emulation).\n";
ced84e60
SP
652 } else {
653 print "found.\n";
654 }
655 } else {
656 print "NOT found.\n";
657 }
658
170c5524
SP
659 print "Looking for clock_nanosleep()... ";
660 my $has_clock_nanosleep;
3b614a59 661 my $has_clock_nanosleep_emulation;
170c5524
SP
662 if (exists $Config{d_clock_nanosleep}) {
663 $has_clock_nanosleep++ if $Config{d_clock_nanosleep}; # Unlikely...
664 } elsif (has_clock_nanosleep()) {
665 $has_clock_nanosleep++;
666 $DEFINE .= ' -DTIME_HIRES_CLOCK_NANOSLEEP';
3b614a59
JH
667 } elsif ($^O eq 'darwin') {
668 $has_clock_nanosleep++;
669 $has_clock_nanosleep_emulation++;
c4a535af 670 $DEFINE .= ' -DTIME_HIRES_CLOCK_NANOSLEEP -DTIME_HIRES_CLOCK_NANOSLEEP_EMULATION';
170c5524
SP
671 }
672
673 if ($has_clock_nanosleep) {
3b614a59
JH
674 if ($has_clock_nanosleep_emulation) {
675 print "found (via emulation).\n";
676 } else {
677 print "found.\n";
678 }
170c5524
SP
679 } else {
680 print "NOT found.\n";
681 }
682
683 print "Looking for clock()... ";
684 my $has_clock;
685 if (exists $Config{d_clock}) {
686 $has_clock++ if $Config{d_clock}; # Unlikely...
687 } elsif (has_clock()) {
688 $has_clock++;
689 $DEFINE .= ' -DTIME_HIRES_CLOCK';
690 }
691
692 if ($has_clock) {
693 print "found.\n";
694 } else {
695 print "NOT found.\n";
696 }
697
c4a535af
SH
698 print "Looking for futimens()... ";
699 my $has_futimens;
700 if (has_futimens()) {
701 $has_futimens++;
702 $DEFINE .= ' -DHAS_FUTIMENS';
703 }
704
705 if ($has_futimens) {
706 print "found.\n";
707 } else {
708 print "NOT found.\n";
709 }
710
711 print "Looking for utimensat()... ";
712 my $has_utimensat;
713 if (has_utimensat()) {
714 $has_utimensat++;
715 $DEFINE .= ' -DHAS_UTIMENSAT';
716 }
717
718 if ($has_utimensat) {
719 print "found.\n";
720 } else {
721 print "NOT found.\n";
722 }
723
724 if ($has_futimens or $has_utimensat) {
725 $DEFINE .= ' -DTIME_HIRES_UTIME';
726 }
727
f3370602
SP
728 print "Looking for stat() subsecond timestamps...\n";
729
75d5269b
SP
730 print "Trying struct stat st_atimespec.tv_nsec...";
731 my $has_stat_st_xtimespec;
732 if (try_compile_and_link(<<EOM)) {
733$TIME_HEADERS
734#include <sys/stat.h>
f3370602 735int main(int argc, char** argv) {
75d5269b
SP
736 struct stat st;
737 st.st_atimespec.tv_nsec = 0;
738}
739EOM
740 $has_stat_st_xtimespec++;
c4a535af 741 DEFINE('TIME_HIRES_STAT_ST_XTIMESPEC'); # 1
75d5269b
SP
742 }
743
744 if ($has_stat_st_xtimespec) {
745 print "found.\n";
746 } else {
747 print "NOT found.\n";
748 }
749
750 print "Trying struct stat st_atimensec...";
751 my $has_stat_st_xtimensec;
752 if (try_compile_and_link(<<EOM)) {
753$TIME_HEADERS
754#include <sys/stat.h>
f3370602 755int main(int argc, char** argv) {
75d5269b
SP
756 struct stat st;
757 st.st_atimensec = 0;
758}
759EOM
760 $has_stat_st_xtimensec++;
c4a535af 761 DEFINE('TIME_HIRES_STAT_ST_XTIMENSEC'); # 2
75d5269b
SP
762 }
763
764 if ($has_stat_st_xtimensec) {
765 print "found.\n";
766 } else {
767 print "NOT found.\n";
768 }
769
770 print "Trying struct stat st_atime_n...";
771 my $has_stat_st_xtime_n;
772 if (try_compile_and_link(<<EOM)) {
773$TIME_HEADERS
774#include <sys/stat.h>
f3370602 775int main(int argc, char** argv) {
75d5269b
SP
776 struct stat st;
777 st.st_atime_n = 0;
778}
779EOM
780 $has_stat_st_xtime_n++;
c4a535af 781 DEFINE('TIME_HIRES_STAT_ST_XTIME_N'); # 3
75d5269b
SP
782 }
783
784 if ($has_stat_st_xtime_n) {
785 print "found.\n";
786 } else {
787 print "NOT found.\n";
788 }
789
790 print "Trying struct stat st_atim.tv_nsec...";
791 my $has_stat_st_xtim;
792 if (try_compile_and_link(<<EOM)) {
793$TIME_HEADERS
794#include <sys/stat.h>
f3370602 795int main(int argc, char** argv) {
75d5269b
SP
796 struct stat st;
797 st.st_atim.tv_nsec = 0;
798}
799EOM
800 $has_stat_st_xtim++;
c4a535af 801 DEFINE('TIME_HIRES_STAT_XTIM'); # 4
75d5269b
SP
802 }
803
804 if ($has_stat_st_xtim) {
805 print "found.\n";
806 } else {
807 print "NOT found.\n";
808 }
809
810 print "Trying struct stat st_uatime...";
811 my $has_stat_st_uxtime;
812 if (try_compile_and_link(<<EOM)) {
813$TIME_HEADERS
814#include <sys/stat.h>
f3370602 815int main(int argc, char** argv) {
75d5269b
SP
816 struct stat st;
817 st.st_uatime = 0;
818}
819EOM
820 $has_stat_st_uxtime++;
c4a535af 821 DEFINE('TIME_HIRES_STAT_ST_UXTIME'); # 5
75d5269b
SP
822 }
823
824 if ($has_stat_st_uxtime) {
825 print "found.\n";
826 } else {
827 print "NOT found.\n";
828 }
829
c4a535af
SH
830 # See HiRes.xs hrstatns()
831 if ($has_stat_st_xtimespec) {
832 DEFINE('TIME_HIRES_STAT', 1);
833 } elsif ($has_stat_st_xtimensec) {
834 DEFINE('TIME_HIRES_STAT', 2);
835 } elsif ($has_stat_st_xtime_n) {
836 DEFINE('TIME_HIRES_STAT', 3);
837 } elsif ($has_stat_st_xtim) {
838 DEFINE('TIME_HIRES_STAT', 4);
839 } elsif ($has_stat_st_uxtime) {
840 DEFINE('TIME_HIRES_STAT', 5);
841 }
842
75d5269b 843 if ($DEFINE =~ /-DTIME_HIRES_STAT=\d+/) {
f3370602 844 print "You seem to have stat() subsecond timestamps.\n";
75d5269b
SP
845 print "(Your struct stat has them, but the filesystems must help.)\n";
846 } else {
847 print "You do not seem to have stat subsecond timestamps.\n";
848 }
849
4ed0e2d4 850 my $has_w32api_windows_h;
170c5524 851
4ed0e2d4
RGS
852 if ($^O eq 'cygwin') {
853 print "Looking for <w32api/windows.h>... ";
854 if (has_include('w32api/windows.h')) {
855 $has_w32api_windows_h++;
a8fb48f7 856 DEFINE('HAS_W32API_WINDOWS_H');
4ed0e2d4
RGS
857 }
858 if ($has_w32api_windows_h) {
859 print "found.\n";
860 } else {
861 print "NOT found.\n";
862 }
3f2ee006
HS
863 }
864
865 if ($DEFINE) {
866 $DEFINE =~ s/^\s+//;
1ae6ead9 867 if (open(XDEFINE, '>', 'xdefine')) {
3f2ee006
HS
868 print XDEFINE $DEFINE, "\n";
869 close(XDEFINE);
870 }
871 }
872}
873
874sub doMakefile {
98b50af3 875 my @makefileopts = ();
3f2ee006
HS
876
877 if ($] >= 5.005) {
878 push (@makefileopts,
879 'AUTHOR' => 'Jarkko Hietaniemi <jhi@iki.fi>',
880 'ABSTRACT_FROM' => 'HiRes.pm',
881 );
a8fb48f7 882 DEFINE('ATLEASTFIVEOHOHFIVE');
3f2ee006
HS
883 }
884
885 push (@makefileopts,
886 'NAME' => 'Time::HiRes',
887 'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
5d899d7e
JH
888 'LIBS' => $LIBS, # e.g., '-lm'
889 'DEFINE' => $DEFINE, # e.g., '-DHAS_SOMETHING'
3f2ee006 890 'XSOPT' => $XSOPT,
ced84e60
SP
891 # Do not even think about 'INC' => '-I/usr/ucbinclude',
892 # Solaris will avenge.
5d899d7e 893 'INC' => '', # e.g., '-I/usr/include/other'
0f0eae2c 894 'INSTALLDIRS' => ($] >= 5.008 && $] < 5.011 ? 'perl' : 'site'),
90e44bf6
Z
895 'PREREQ_PM' => {
896 'Carp' => 0,
897 'Config' => 0,
898 'DynaLoader' => 0,
899 'Exporter' => 0,
900 'ExtUtils::MakeMaker' => 0,
c4a535af 901 'Test::More' => 0,
90e44bf6
Z
902 'strict' => 0,
903 },
3f2ee006
HS
904 'dist' => {
905 'CI' => 'ci -l',
5d899d7e 906 'COMPRESS' => 'gzip -9f',
3f2ee006
HS
907 'SUFFIX' => 'gz',
908 },
909 clean => { FILES => "xdefine" },
1caec985 910 realclean => { FILES=> 'const-c.inc const-xs.inc' },
3f2ee006
HS
911 );
912
91a2e9f6
Z
913 if ($^O eq "MSWin32" && !(grep { /\ALD[A-Z]*=/ } @ARGV)) {
914 my $libperl = $Config{libperl} || "";
915 my $gccversion = $Config{gccversion} || "";
916 if ($gccversion =~ /\A3\.4\.[0-9]+/ and $libperl =~ /\.lib\z/) {
917 # Avoid broken linkage with ActivePerl, by linking directly
918 # against the Perl DLL rather than the import library.
919 (my $llibperl = "-l$libperl") =~ s/\.lib\z//;
920 my $lddlflags = $Config{lddlflags} || "";
921 my $ldflags = $Config{ldflags} || "";
922 s/-L(?:".*?"|\S+)//g foreach $lddlflags, $ldflags;
923 my $libdirs = join ' ',
924 map { s/(?<!\\)((?:\\\\)*")/\\$1/g; qq[-L"$_"] }
925 @Config{qw/bin sitebin/};
926 push @makefileopts, macro => {
927 LDDLFLAGS => "$lddlflags $libdirs $llibperl",
928 LDFLAGS => "$ldflags $libdirs $llibperl",
929 PERL_ARCHIVE => "",
930 };
931 }
932 }
933
9ac5eb64
JH
934 if ($ENV{PERL_CORE}) {
935 push @makefileopts, MAN3PODS => {};
936 }
937
4451fd4e 938 if ($ExtUtils::MakeMaker::VERSION >= 6.48) {
af94b3ac 939 push @makefileopts, (MIN_PERL_VERSION => '5.006',);
4451fd4e
SH
940 }
941
942 if ($ExtUtils::MakeMaker::VERSION >= 6.31) {
943 push @makefileopts, (LICENSE => 'perl_5');
944 }
945
3f2ee006
HS
946 WriteMakefile(@makefileopts);
947}
948
98b50af3
JH
949sub doConstants {
950 if (eval {require ExtUtils::Constant; 1}) {
6a6012c3
JH
951 my @names = qw(
952 CLOCKS_PER_SEC
105140f6 953 CLOCK_BOOTTIME
6a6012c3
JH
954 CLOCK_HIGHRES
955 CLOCK_MONOTONIC
105140f6 956 CLOCK_MONOTONIC_COARSE
774a3049 957 CLOCK_MONOTONIC_PRECISE
105140f6 958 CLOCK_MONOTONIC_RAW
6a6012c3
JH
959 CLOCK_PROCESS_CPUTIME_ID
960 CLOCK_REALTIME
105140f6 961 CLOCK_REALTIME_COARSE
774a3049
JH
962 CLOCK_REALTIME_FAST
963 CLOCK_REALTIME_PRECISE
964 CLOCK_SECOND
6a6012c3
JH
965 CLOCK_SOFTTIME
966 CLOCK_THREAD_CPUTIME_ID
967 CLOCK_TIMEOFDAY
774a3049
JH
968 CLOCK_UPTIME
969 CLOCK_UPTIME_FAST
970 CLOCK_UPTIME_PRECISE
6a6012c3
JH
971 ITIMER_PROF
972 ITIMER_REAL
973 ITIMER_REALPROF
974 ITIMER_VIRTUAL
975 TIMER_ABSTIME
976 );
98b50af3 977 foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
170c5524 978 d_nanosleep d_clock_gettime d_clock_getres
c4a535af
SH
979 d_clock d_clock_nanosleep d_hires_stat
980 d_futimens d_utimensat d_hires_utime)) {
98b50af3 981 my $macro = $_;
3b614a59 982 if ($macro =~ /^(d_nanosleep|d_clock)$/) {
82cbdcc3 983 $macro =~ s/^d_(.+)/TIME_HIRES_\U$1/;
75d5269b
SP
984 } elsif ($macro =~ /^(d_hires_stat)$/) {
985 my $d_hires_stat = 0;
986 $d_hires_stat = $1 if ($DEFINE =~ /-DTIME_HIRES_STAT=(\d+)/);
987 push @names, {name => $_, macro => "TIME_HIRES_STAT", value => $d_hires_stat,
988 default => ["IV", "0"]};
989 next;
c4a535af
SH
990 } elsif ($macro =~ /^(d_hires_utime)$/) {
991 my $d_hires_utime =
992 ($DEFINE =~ /-DHAS_FUTIMENS/ ||
993 $DEFINE =~ /-DHAS_UTIMENSAT/) ? 1 : 0;
994 push @names, {name => $_, macro => "TIME_HIRES_UTIME", value => $d_hires_utime,
995 default => ["IV", "0"]};
996 next;
3b614a59 997 } elsif ($macro =~ /^(d_clock_gettime|d_clock_getres|d_clock_nanosleep)$/) {
1e845d0c
JH
998 $macro =~ s/^d_(.+)/TIME_HIRES_\U$1/;
999 my $val = ($DEFINE =~ /-D$macro\b/) ? 1 : 0;
1000 push @names, {name => $_, macro => $macro, value => $val,
1001 default => ["IV", "0"]};
1002 next;
672f6857 1003 } else {
82cbdcc3 1004 $macro =~ s/^d_(.+)/HAS_\U$1/;
672f6857 1005 }
98b50af3
JH
1006 push @names, {name => $_, macro => $macro, value => 1,
1007 default => ["IV", "0"]};
1008 }
1009 ExtUtils::Constant::WriteConstants(
1010 NAME => 'Time::HiRes',
1011 NAMES => \@names,
1012 );
1013 } else {
1fbb4de4
RGS
1014 my $file;
1015 foreach $file ('const-c.inc', 'const-xs.inc') {
98b50af3
JH
1016 my $fallback = File::Spec->catfile('fallback', $file);
1017 local $/;
1ae6ead9
JL
1018 open IN, '<', $fallback or die "Can't open $fallback: $!";
1019 open OUT, '>', $file or die "Can't open $file: $!";
98b50af3
JH
1020 print OUT <IN> or die $!;
1021 close OUT or die "Can't close $file: $!";
1022 close IN or die "Can't close $fallback: $!";
1023 }
1024 }
1025}
3f2ee006 1026
98b50af3 1027sub main {
68c5b4db 1028 if (-f "xdefine" && !(@ARGV && $ARGV[0] eq '--configure')) {
bf8300de 1029 print qq[$0: The "xdefine" exists, skipping the configure step.\n];
e5620114 1030 print qq[("$^X $0 --configure" to force the configure step)\n];
3f2ee006 1031 } else {
bf8300de
RGS
1032 print "Configuring Time::HiRes...\n";
1033 1 while unlink("define");
1034 if ($^O =~ /Win32/i) {
1035 DEFINE('SELECT_IS_BROKEN');
1036 $LIBS = [];
1037 print "System is $^O, skipping full configure...\n";
1ae6ead9 1038 open(XDEFINE, '>', 'xdefine') or die "$0: Cannot create xdefine: $!\n";
bf8300de
RGS
1039 close(XDEFINE);
1040 } else {
1041 init();
1042 }
1043 doMakefile;
1044 doConstants;
3f2ee006 1045 }
3f2ee006 1046 my $make = $Config{'make'} || "make";
1fbb4de4 1047 unless (exists $ENV{PERL_CORE} && $ENV{PERL_CORE}) {
3f2ee006 1048 print <<EOM;
3f2ee006 1049Now you may issue '$make'. Do not forget also '$make test'.
dfffa540 1050EOM
806b8cc2
SP
1051 if ($] == 5.008 &&
1052 ((exists $ENV{LC_ALL} && $ENV{LC_ALL} =~ /utf-?8/i) ||
1053 (exists $ENV{LC_CTYPE} && $ENV{LC_CTYPE} =~ /utf-?8/i) ||
1054 (exists $ENV{LANG} && $ENV{LANG} =~ /utf-?8/i))) {
22149ad2
RGS
1055 print <<EOM;
1056
ced84e60 1057NOTE: if you get an error like this (the Makefile line number may vary):
d7358e6a 1058Makefile:91: *** missing separator
1caec985
DM
1059then set the environment variable LC_ALL to "C" and retry
1060from scratch (re-run perl "Makefile.PL").
806b8cc2 1061(And consider upgrading your Perl to, say, at least Perl 5.8.8.)
50a6311a
RGS
1062(You got this message because you seem to have
1063 an UTF-8 locale active in your shell environment, this used
806b8cc2 1064 to cause broken Makefiles to be created from Makefile.PLs)
3f2ee006 1065EOM
dfffa540 1066 }
3f2ee006
HS
1067 }
1068}
1069
1070&main;
dcf686c9 1071
3f2ee006 1072# EOF