This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
refer Time::HiRes RT queue to perl core
[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{
8da08029
CBW
370 int ret1, ret2;
371 struct timespec ts1[2], ts2[2];
372 ret1 = futimens(0, ts1);
373 char buf[1];
374 read(0, buf, 0); /* Assuming reading nothing updates atime (the [0]) */
375 ret2 = futimens(0, ts2);
376 ret1 == 0 && ret2 == 0 && (ts1[0].tv_nsec != 0 || ts2[0].tv_nsec != 0) ?
377 exit(0) : exit(errno ? errno : -1);
c4a535af
SH
378}
379EOM
380}
381
382sub has_utimensat{
383 return 1 if
384 try_compile_and_link(<<EOM);
385#include "EXTERN.h"
386#include "perl.h"
387#include "XSUB.h"
605b4dbe 388#include <sys/stat.h>
c4a535af
SH
389#include <fcntl.h>
390int main(int argc, char** argv)
391{
8da08029
CBW
392 int ret1, ret2;
393 struct timespec ts1[2], ts2[2];
394 /* We make the brave but probably foolish assumption that systems
395 * modern enough to have utimensat also have the /dev/stdin. */
396 ret1 = utimensat(AT_FDCWD, "/dev/stdin", ts1, 0);
397 char buf[1];
398 read(0, buf, 0); /* Assuming reading nothing updates atime (the [0]) */
399 ret2 = utimensat(AT_FDCWD, "/dev/stdin", ts2, 0);
400 ret1 == 0 && ret2 == 0 && (ts1[0].tv_nsec != 0 || ts2[0].tv_nsec != 0) ?
401 exit(0) : exit(errno ? errno : -1);
c4a535af
SH
402}
403EOM
404}
405
69d1f2c2
JH
406sub has_clockid_t{
407 return 1 if
408 try_compile_and_link(<<EOM);
409#include "EXTERN.h"
410#include "perl.h"
411#include "XSUB.h"
605b4dbe 412#include <time.h>
69d1f2c2
JH
413int main(int argc, char** argv)
414{
415 clockid_t id = CLOCK_REALTIME;
416 exit(id == CLOCK_REALTIME ? 1 : 0);
417}
418EOM
419}
420
a8fb48f7
SP
421sub DEFINE {
422 my ($def, $val) = @_;
423 my $define = defined $val ? "$def=$val" : $def ;
424 unless ($DEFINE =~ /(?:^| )-D\Q$define\E(?: |$)/) {
425 $DEFINE .= " -D$define";
426 }
427}
428
046e3f33 429sub init {
3172fdbc 430 my $hints = File::Spec->catfile("hints", "$^O.pl");
046e3f33
JH
431 if (-f $hints) {
432 print "Using hints $hints...\n";
433 local $self;
3172fdbc 434 do "./$hints";
046e3f33
JH
435 if (exists $self->{LIBS}) {
436 $LIBS = $self->{LIBS};
437 print "Extra libraries: @$LIBS...\n";
3f2ee006
HS
438 }
439 }
046e3f33
JH
440
441 $DEFINE = '';
3f2ee006 442
ced84e60 443 if ($Config{d_syscall}) {
a5929ff3 444 print "Have syscall()... looking for syscall.h... ";
ced84e60
SP
445 if (has_include('syscall.h')) {
446 $SYSCALL_H = 'syscall.h';
447 } elsif (has_include('sys/syscall.h')) {
448 $SYSCALL_H = 'sys/syscall.h';
449 }
a5929ff3
RGS
450 } else {
451 print "No syscall()...\n";
ced84e60
SP
452 }
453
a5929ff3
RGS
454 if ($Config{d_syscall}) {
455 if (defined $SYSCALL_H) {
456 print "found <$SYSCALL_H>.\n";
457 } else {
458 print "NOT found.\n";
459 }
ced84e60
SP
460 }
461
98b50af3 462 print "Looking for gettimeofday()... ";
3f2ee006 463 my $has_gettimeofday;
5d899d7e
JH
464 if (exists $Config{d_gettimeod}) {
465 $has_gettimeofday++ if $Config{d_gettimeod};
3f2ee006
HS
466 } elsif (has_gettimeofday()) {
467 $DEFINE .= ' -DHAS_GETTIMEOFDAY';
468 $has_gettimeofday++;
469 }
470
471 if ($has_gettimeofday) {
98b50af3 472 print "found.\n";
3f2ee006
HS
473 } else {
474 die <<EOD
475Your operating system does not seem to have the gettimeofday() function.
476(or, at least, I cannot find it)
477
478There is no way Time::HiRes is going to work.
479
480I am awfully sorry but I cannot go further.
481
482Aborting configuration.
483
484EOD
485 }
486
98b50af3 487 print "Looking for setitimer()... ";
3f2ee006 488 my $has_setitimer;
5d899d7e
JH
489 if (exists $Config{d_setitimer}) {
490 $has_setitimer++ if $Config{d_setitimer};
3f2ee006
HS
491 } elsif (has_x("setitimer(ITIMER_REAL, 0, 0)")) {
492 $has_setitimer++;
493 $DEFINE .= ' -DHAS_SETITIMER';
494 }
495
496 if ($has_setitimer) {
98b50af3 497 print "found.\n";
3f2ee006 498 } else {
98b50af3 499 print "NOT found.\n";
3f2ee006
HS
500 }
501
98b50af3 502 print "Looking for getitimer()... ";
3f2ee006 503 my $has_getitimer;
5d899d7e
JH
504 if (exists $Config{'d_getitimer'}) {
505 $has_getitimer++ if $Config{'d_getitimer'};
3f2ee006
HS
506 } elsif (has_x("getitimer(ITIMER_REAL, 0)")) {
507 $has_getitimer++;
508 $DEFINE .= ' -DHAS_GETITIMER';
509 }
510
511 if ($has_getitimer) {
98b50af3 512 print "found.\n";
3f2ee006 513 } else {
98b50af3 514 print "NOT found.\n";
3f2ee006
HS
515 }
516
517 if ($has_setitimer && $has_getitimer) {
4ed0e2d4 518 print "You have interval timers (both setitimer and getitimer).\n";
3f2ee006 519 } else {
8da08029 520 print "You do NOT have interval timers.\n";
3f2ee006
HS
521 }
522
98b50af3
JH
523 print "Looking for ualarm()... ";
524 my $has_ualarm;
5d899d7e
JH
525 if (exists $Config{d_ualarm}) {
526 $has_ualarm++ if $Config{d_ualarm};
3f2ee006
HS
527 } elsif (has_x ("ualarm (0, 0)")) {
528 $has_ualarm++;
529 $DEFINE .= ' -DHAS_UALARM';
530 }
531
532 if ($has_ualarm) {
98b50af3 533 print "found.\n";
3f2ee006 534 } else {
98b50af3
JH
535 print "NOT found.\n";
536 if ($has_setitimer) {
537 print "But you have setitimer().\n";
538 print "We can make a Time::HiRes::ualarm().\n";
3f2ee006
HS
539 }
540 }
541
98b50af3 542 print "Looking for usleep()... ";
3f2ee006 543 my $has_usleep;
5d899d7e
JH
544 if (exists $Config{d_usleep}) {
545 $has_usleep++ if $Config{d_usleep};
3f2ee006
HS
546 } elsif (has_x ("usleep (0)")) {
547 $has_usleep++;
548 $DEFINE .= ' -DHAS_USLEEP';
549 }
550
551 if ($has_usleep) {
98b50af3 552 print "found.\n";
3f2ee006 553 } else {
98b50af3
JH
554 print "NOT found.\n";
555 print "Let's see if you have select()... ";
56c1b3bd 556 if ($Config{'d_select'}) {
98b50af3
JH
557 print "found.\n";
558 print "We can make a Time::HiRes::usleep().\n";
3f2ee006 559 } else {
98b50af3
JH
560 print "NOT found.\n";
561 print "You won't have a Time::HiRes::usleep().\n";
3f2ee006
HS
562 }
563 }
564
98b50af3 565 print "Looking for nanosleep()... ";
3f2ee006 566 my $has_nanosleep;
3d0346a5
SP
567 if ($ENV{FORCE_NANOSLEEP_SCAN}) {
568 print "forced scan... ";
569 if (has_nanosleep()) {
570 $has_nanosleep++;
571 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
572 }
573 }
574 elsif (exists $Config{d_nanosleep}) {
575 print "believing \$Config{d_nanosleep}... ";
c1363767
JH
576 if ($Config{d_nanosleep}) {
577 $has_nanosleep++;
578 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
579 }
3d0346a5
SP
580 } else {
581 if (has_nanosleep()) {
582 $has_nanosleep++;
583 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
584 }
3f2ee006
HS
585 }
586
587 if ($has_nanosleep) {
98b50af3 588 print "found.\n";
4ed0e2d4
RGS
589 print "You can mix subsecond sleeps with signals, if you want to.\n";
590 print "(It's still not portable, though.)\n";
3f2ee006 591 } else {
98b50af3 592 print "NOT found.\n";
622913ab
IZ
593 my $nt = ($^O eq 'os2' ? '' : 'not');
594 print "You can$nt mix subsecond sleeps with signals.\n";
4ed0e2d4
RGS
595 print "(It would not be portable anyway.)\n";
596 }
597
69d1f2c2
JH
598 print "Looking for clockid_t... ";
599 my $has_clockid_t;
600 if (has_clockid_t()) {
601 print "found.\n";
602 $has_clockid_t++;
603 $DEFINE .= ' -DTIME_HIRES_CLOCKID_T';
604 } else {
605 print "NOT found, will use int.\n";
606 }
607
ced84e60
SP
608 print "Looking for clock_gettime()... ";
609 my $has_clock_gettime;
1e845d0c 610 my $has_clock_gettime_emulation;
ced84e60
SP
611 if (exists $Config{d_clock_gettime}) {
612 $has_clock_gettime++ if $Config{d_clock_gettime}; # Unlikely...
170c5524 613 } elsif (has_clock_xxx('gettime')) {
ced84e60
SP
614 $has_clock_gettime++;
615 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME';
170c5524 616 } elsif (defined $SYSCALL_H && has_clock_xxx_syscall('gettime')) {
ced84e60
SP
617 $has_clock_gettime++;
618 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME -DTIME_HIRES_CLOCK_GETTIME_SYSCALL';
1e845d0c
JH
619 } elsif ($^O eq 'darwin') {
620 $has_clock_gettime_emulation++;
621 $has_clock_gettime++;
c4a535af 622 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME -DTIME_HIRES_CLOCK_GETTIME_EMULATION';
ced84e60
SP
623 }
624
625 if ($has_clock_gettime) {
626 if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETTIME_SYSCALL/) {
627 print "found (via syscall).\n";
1e845d0c
JH
628 } elsif ($has_clock_gettime_emulation) {
629 print "found (via emulation).\n";
ced84e60
SP
630 } else {
631 print "found.\n";
632 }
633 } else {
634 print "NOT found.\n";
635 }
636
637 print "Looking for clock_getres()... ";
638 my $has_clock_getres;
1e845d0c 639 my $has_clock_getres_emulation;
ced84e60
SP
640 if (exists $Config{d_clock_getres}) {
641 $has_clock_getres++ if $Config{d_clock_getres}; # Unlikely...
170c5524 642 } elsif (has_clock_xxx('getres')) {
ced84e60
SP
643 $has_clock_getres++;
644 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES';
170c5524 645 } elsif (defined $SYSCALL_H && has_clock_xxx_syscall('getres')) {
ced84e60
SP
646 $has_clock_getres++;
647 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES -DTIME_HIRES_CLOCK_GETRES_SYSCALL';
1e845d0c
JH
648 } elsif ($^O eq 'darwin') {
649 $has_clock_getres_emulation++;
650 $has_clock_getres++;
c4a535af 651 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES -DTIME_HIRES_CLOCK_GETRES_EMULATION';
ced84e60
SP
652 }
653
654 if ($has_clock_getres) {
655 if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETRES_SYSCALL/) {
656 print "found (via syscall).\n";
1e845d0c
JH
657 } elsif ($has_clock_getres_emulation) {
658 print "found (via emulation).\n";
ced84e60
SP
659 } else {
660 print "found.\n";
661 }
662 } else {
663 print "NOT found.\n";
664 }
665
170c5524
SP
666 print "Looking for clock_nanosleep()... ";
667 my $has_clock_nanosleep;
3b614a59 668 my $has_clock_nanosleep_emulation;
170c5524
SP
669 if (exists $Config{d_clock_nanosleep}) {
670 $has_clock_nanosleep++ if $Config{d_clock_nanosleep}; # Unlikely...
671 } elsif (has_clock_nanosleep()) {
672 $has_clock_nanosleep++;
673 $DEFINE .= ' -DTIME_HIRES_CLOCK_NANOSLEEP';
3b614a59
JH
674 } elsif ($^O eq 'darwin') {
675 $has_clock_nanosleep++;
676 $has_clock_nanosleep_emulation++;
c4a535af 677 $DEFINE .= ' -DTIME_HIRES_CLOCK_NANOSLEEP -DTIME_HIRES_CLOCK_NANOSLEEP_EMULATION';
170c5524
SP
678 }
679
680 if ($has_clock_nanosleep) {
3b614a59
JH
681 if ($has_clock_nanosleep_emulation) {
682 print "found (via emulation).\n";
683 } else {
684 print "found.\n";
685 }
170c5524
SP
686 } else {
687 print "NOT found.\n";
688 }
689
690 print "Looking for clock()... ";
691 my $has_clock;
692 if (exists $Config{d_clock}) {
693 $has_clock++ if $Config{d_clock}; # Unlikely...
694 } elsif (has_clock()) {
695 $has_clock++;
696 $DEFINE .= ' -DTIME_HIRES_CLOCK';
697 }
698
699 if ($has_clock) {
700 print "found.\n";
701 } else {
702 print "NOT found.\n";
703 }
704
8da08029 705 print "Looking for working futimens()... ";
c4a535af
SH
706 my $has_futimens;
707 if (has_futimens()) {
708 $has_futimens++;
709 $DEFINE .= ' -DHAS_FUTIMENS';
710 }
711
712 if ($has_futimens) {
713 print "found.\n";
714 } else {
715 print "NOT found.\n";
716 }
717
8da08029 718 print "Looking for working utimensat()... ";
c4a535af
SH
719 my $has_utimensat;
720 if (has_utimensat()) {
721 $has_utimensat++;
722 $DEFINE .= ' -DHAS_UTIMENSAT';
723 }
724
725 if ($has_utimensat) {
726 print "found.\n";
727 } else {
728 print "NOT found.\n";
729 }
730
5dbe8f0a 731 my $has_hires_utime = ($has_futimens && $has_utimensat);
8da08029 732 if ($has_hires_utime) {
c4a535af 733 $DEFINE .= ' -DTIME_HIRES_UTIME';
8da08029
CBW
734 print "You seem to have subsecond timestamp setting.\n";
735 } else {
736 print "You do NOT seem to have subsecond timestamp setting.\n";
c4a535af
SH
737 }
738
f3370602
SP
739 print "Looking for stat() subsecond timestamps...\n";
740
75d5269b
SP
741 print "Trying struct stat st_atimespec.tv_nsec...";
742 my $has_stat_st_xtimespec;
743 if (try_compile_and_link(<<EOM)) {
744$TIME_HEADERS
745#include <sys/stat.h>
f3370602 746int main(int argc, char** argv) {
75d5269b
SP
747 struct stat st;
748 st.st_atimespec.tv_nsec = 0;
749}
750EOM
751 $has_stat_st_xtimespec++;
c4a535af 752 DEFINE('TIME_HIRES_STAT_ST_XTIMESPEC'); # 1
75d5269b
SP
753 }
754
755 if ($has_stat_st_xtimespec) {
756 print "found.\n";
757 } else {
758 print "NOT found.\n";
759 }
760
761 print "Trying struct stat st_atimensec...";
762 my $has_stat_st_xtimensec;
763 if (try_compile_and_link(<<EOM)) {
764$TIME_HEADERS
765#include <sys/stat.h>
f3370602 766int main(int argc, char** argv) {
75d5269b
SP
767 struct stat st;
768 st.st_atimensec = 0;
769}
770EOM
771 $has_stat_st_xtimensec++;
c4a535af 772 DEFINE('TIME_HIRES_STAT_ST_XTIMENSEC'); # 2
75d5269b
SP
773 }
774
775 if ($has_stat_st_xtimensec) {
776 print "found.\n";
777 } else {
778 print "NOT found.\n";
779 }
780
781 print "Trying struct stat st_atime_n...";
782 my $has_stat_st_xtime_n;
783 if (try_compile_and_link(<<EOM)) {
784$TIME_HEADERS
785#include <sys/stat.h>
f3370602 786int main(int argc, char** argv) {
75d5269b
SP
787 struct stat st;
788 st.st_atime_n = 0;
789}
790EOM
791 $has_stat_st_xtime_n++;
c4a535af 792 DEFINE('TIME_HIRES_STAT_ST_XTIME_N'); # 3
75d5269b
SP
793 }
794
795 if ($has_stat_st_xtime_n) {
796 print "found.\n";
797 } else {
798 print "NOT found.\n";
799 }
800
801 print "Trying struct stat st_atim.tv_nsec...";
802 my $has_stat_st_xtim;
803 if (try_compile_and_link(<<EOM)) {
804$TIME_HEADERS
805#include <sys/stat.h>
f3370602 806int main(int argc, char** argv) {
75d5269b
SP
807 struct stat st;
808 st.st_atim.tv_nsec = 0;
809}
810EOM
811 $has_stat_st_xtim++;
c4a535af 812 DEFINE('TIME_HIRES_STAT_XTIM'); # 4
75d5269b
SP
813 }
814
815 if ($has_stat_st_xtim) {
816 print "found.\n";
817 } else {
818 print "NOT found.\n";
819 }
820
821 print "Trying struct stat st_uatime...";
822 my $has_stat_st_uxtime;
823 if (try_compile_and_link(<<EOM)) {
824$TIME_HEADERS
825#include <sys/stat.h>
f3370602 826int main(int argc, char** argv) {
75d5269b
SP
827 struct stat st;
828 st.st_uatime = 0;
829}
830EOM
831 $has_stat_st_uxtime++;
c4a535af 832 DEFINE('TIME_HIRES_STAT_ST_UXTIME'); # 5
75d5269b
SP
833 }
834
835 if ($has_stat_st_uxtime) {
836 print "found.\n";
837 } else {
838 print "NOT found.\n";
839 }
840
c4a535af
SH
841 # See HiRes.xs hrstatns()
842 if ($has_stat_st_xtimespec) {
843 DEFINE('TIME_HIRES_STAT', 1);
844 } elsif ($has_stat_st_xtimensec) {
845 DEFINE('TIME_HIRES_STAT', 2);
846 } elsif ($has_stat_st_xtime_n) {
847 DEFINE('TIME_HIRES_STAT', 3);
848 } elsif ($has_stat_st_xtim) {
849 DEFINE('TIME_HIRES_STAT', 4);
850 } elsif ($has_stat_st_uxtime) {
851 DEFINE('TIME_HIRES_STAT', 5);
8da08029 852 }
c4a535af 853
8da08029
CBW
854 my $has_hires_stat = ($DEFINE =~ /-DTIME_HIRES_STAT=(\d+)/) ? $1 : 0;
855 if ($has_hires_stat) {
856 print "You seem to have subsecond timestamp reading.\n";
857 print "(Your struct stat has them, but the filesystems must help.)\n";
858 unless ($has_hires_utime) {
859 print "However, you do NOT seem to have subsecond timestamp setting.\n";
860 }
861 } else {
862 print "You do NOT seem to have subsecond timestamp reading.\n";
863 }
75d5269b 864
4ed0e2d4 865 my $has_w32api_windows_h;
170c5524 866
4ed0e2d4
RGS
867 if ($^O eq 'cygwin') {
868 print "Looking for <w32api/windows.h>... ";
869 if (has_include('w32api/windows.h')) {
870 $has_w32api_windows_h++;
a8fb48f7 871 DEFINE('HAS_W32API_WINDOWS_H');
4ed0e2d4
RGS
872 }
873 if ($has_w32api_windows_h) {
874 print "found.\n";
875 } else {
876 print "NOT found.\n";
877 }
3f2ee006
HS
878 }
879
880 if ($DEFINE) {
881 $DEFINE =~ s/^\s+//;
1ae6ead9 882 if (open(XDEFINE, '>', 'xdefine')) {
3f2ee006
HS
883 print XDEFINE $DEFINE, "\n";
884 close(XDEFINE);
885 }
886 }
887}
888
889sub doMakefile {
98b50af3 890 my @makefileopts = ();
3f2ee006
HS
891
892 if ($] >= 5.005) {
893 push (@makefileopts,
894 'AUTHOR' => 'Jarkko Hietaniemi <jhi@iki.fi>',
895 'ABSTRACT_FROM' => 'HiRes.pm',
896 );
a8fb48f7 897 DEFINE('ATLEASTFIVEOHOHFIVE');
3f2ee006 898 }
cbc1b415 899 DEFINE('USE_PPPORT_H') unless $ENV{PERL_CORE};
3f2ee006
HS
900
901 push (@makefileopts,
902 'NAME' => 'Time::HiRes',
903 'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
5d899d7e
JH
904 'LIBS' => $LIBS, # e.g., '-lm'
905 'DEFINE' => $DEFINE, # e.g., '-DHAS_SOMETHING'
3f2ee006 906 'XSOPT' => $XSOPT,
ced84e60
SP
907 # Do not even think about 'INC' => '-I/usr/ucbinclude',
908 # Solaris will avenge.
5d899d7e 909 'INC' => '', # e.g., '-I/usr/include/other'
0f0eae2c 910 'INSTALLDIRS' => ($] >= 5.008 && $] < 5.011 ? 'perl' : 'site'),
90e44bf6
Z
911 'PREREQ_PM' => {
912 'Carp' => 0,
913 'Config' => 0,
90e44bf6
Z
914 'Exporter' => 0,
915 'ExtUtils::MakeMaker' => 0,
c4a535af 916 'Test::More' => 0,
8eedd56d 917 'XSLoader' => 0,
90e44bf6
Z
918 'strict' => 0,
919 },
3f2ee006
HS
920 'dist' => {
921 'CI' => 'ci -l',
5d899d7e 922 'COMPRESS' => 'gzip -9f',
3f2ee006
HS
923 'SUFFIX' => 'gz',
924 },
925 clean => { FILES => "xdefine" },
1caec985 926 realclean => { FILES=> 'const-c.inc const-xs.inc' },
3f2ee006
HS
927 );
928
91a2e9f6
Z
929 if ($^O eq "MSWin32" && !(grep { /\ALD[A-Z]*=/ } @ARGV)) {
930 my $libperl = $Config{libperl} || "";
931 my $gccversion = $Config{gccversion} || "";
932 if ($gccversion =~ /\A3\.4\.[0-9]+/ and $libperl =~ /\.lib\z/) {
933 # Avoid broken linkage with ActivePerl, by linking directly
934 # against the Perl DLL rather than the import library.
935 (my $llibperl = "-l$libperl") =~ s/\.lib\z//;
936 my $lddlflags = $Config{lddlflags} || "";
937 my $ldflags = $Config{ldflags} || "";
938 s/-L(?:".*?"|\S+)//g foreach $lddlflags, $ldflags;
939 my $libdirs = join ' ',
940 map { s/(?<!\\)((?:\\\\)*")/\\$1/g; qq[-L"$_"] }
941 @Config{qw/bin sitebin/};
942 push @makefileopts, macro => {
943 LDDLFLAGS => "$lddlflags $libdirs $llibperl",
944 LDFLAGS => "$ldflags $libdirs $llibperl",
945 PERL_ARCHIVE => "",
946 };
947 }
948 }
949
9ac5eb64
JH
950 if ($ENV{PERL_CORE}) {
951 push @makefileopts, MAN3PODS => {};
952 }
953
4451fd4e 954 if ($ExtUtils::MakeMaker::VERSION >= 6.48) {
af94b3ac 955 push @makefileopts, (MIN_PERL_VERSION => '5.006',);
4451fd4e
SH
956 }
957
958 if ($ExtUtils::MakeMaker::VERSION >= 6.31) {
959 push @makefileopts, (LICENSE => 'perl_5');
960 }
961
3a24361c
GK
962 if ($ExtUtils::MakeMaker::VERSION >= 6.46) {
963 push @makefileopts, (
964 META_MERGE => {
965 resources => {
966 repository => 'git://perl5.git.perl.org/perl.git',
967 bugtracker => 'https://rt.perl.org/rt3/',
968 homepage => "http://dev.perl.org/",
969 },
970 },
971 )
972 }
973
3f2ee006
HS
974 WriteMakefile(@makefileopts);
975}
976
98b50af3
JH
977sub doConstants {
978 if (eval {require ExtUtils::Constant; 1}) {
727404d0
TR
979 # More or less this same list is in HiRes.pm. Should unify.
980 my @names = qw(
981 CLOCKS_PER_SEC
982 CLOCK_BOOTTIME
983 CLOCK_HIGHRES
984 CLOCK_MONOTONIC
985 CLOCK_MONOTONIC_COARSE
986 CLOCK_MONOTONIC_FAST
987 CLOCK_MONOTONIC_PRECISE
988 CLOCK_MONOTONIC_RAW
989 CLOCK_PROF
990 CLOCK_PROCESS_CPUTIME_ID
991 CLOCK_REALTIME
992 CLOCK_REALTIME_COARSE
993 CLOCK_REALTIME_FAST
994 CLOCK_REALTIME_PRECISE
995 CLOCK_REALTIME_RAW
996 CLOCK_SECOND
997 CLOCK_SOFTTIME
998 CLOCK_THREAD_CPUTIME_ID
999 CLOCK_TIMEOFDAY
1000 CLOCK_UPTIME
1001 CLOCK_UPTIME_COARSE
1002 CLOCK_UPTIME_FAST
1003 CLOCK_UPTIME_PRECISE
1004 CLOCK_UPTIME_RAW
1005 CLOCK_VIRTUAL
1006 ITIMER_PROF
1007 ITIMER_REAL
1008 ITIMER_REALPROF
1009 ITIMER_VIRTUAL
1010 TIMER_ABSTIME
1011 );
98b50af3 1012 foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
170c5524 1013 d_nanosleep d_clock_gettime d_clock_getres
c4a535af
SH
1014 d_clock d_clock_nanosleep d_hires_stat
1015 d_futimens d_utimensat d_hires_utime)) {
98b50af3 1016 my $macro = $_;
3b614a59 1017 if ($macro =~ /^(d_nanosleep|d_clock)$/) {
82cbdcc3 1018 $macro =~ s/^d_(.+)/TIME_HIRES_\U$1/;
75d5269b 1019 } elsif ($macro =~ /^(d_hires_stat)$/) {
8da08029 1020 my $d_hires_stat = $1 if ($DEFINE =~ /-DTIME_HIRES_STAT=(\d+)/);
de3293c0
CBW
1021 if (defined $d_hires_stat) {
1022 push @names, {name => $_, macro => "TIME_HIRES_STAT", value => $d_hires_stat,
1023 default => ["IV", "0"]};
1024 next;
1025 }
c4a535af
SH
1026 } elsif ($macro =~ /^(d_hires_utime)$/) {
1027 my $d_hires_utime =
8da08029
CBW
1028 ($DEFINE =~ /-DHAS_FUTIMENS/ ||
1029 $DEFINE =~ /-DHAS_UTIMENSAT/);
c4a535af
SH
1030 push @names, {name => $_, macro => "TIME_HIRES_UTIME", value => $d_hires_utime,
1031 default => ["IV", "0"]};
1032 next;
3b614a59 1033 } elsif ($macro =~ /^(d_clock_gettime|d_clock_getres|d_clock_nanosleep)$/) {
1e845d0c
JH
1034 $macro =~ s/^d_(.+)/TIME_HIRES_\U$1/;
1035 my $val = ($DEFINE =~ /-D$macro\b/) ? 1 : 0;
1036 push @names, {name => $_, macro => $macro, value => $val,
1037 default => ["IV", "0"]};
1038 next;
672f6857 1039 } else {
82cbdcc3 1040 $macro =~ s/^d_(.+)/HAS_\U$1/;
672f6857 1041 }
98b50af3
JH
1042 push @names, {name => $_, macro => $macro, value => 1,
1043 default => ["IV", "0"]};
1044 }
1045 ExtUtils::Constant::WriteConstants(
1046 NAME => 'Time::HiRes',
1047 NAMES => \@names,
1048 );
1049 } else {
1fbb4de4
RGS
1050 my $file;
1051 foreach $file ('const-c.inc', 'const-xs.inc') {
98b50af3
JH
1052 my $fallback = File::Spec->catfile('fallback', $file);
1053 local $/;
1ae6ead9
JL
1054 open IN, '<', $fallback or die "Can't open $fallback: $!";
1055 open OUT, '>', $file or die "Can't open $file: $!";
98b50af3
JH
1056 print OUT <IN> or die $!;
1057 close OUT or die "Can't close $file: $!";
1058 close IN or die "Can't close $fallback: $!";
1059 }
1060 }
1061}
3f2ee006 1062
98b50af3 1063sub main {
de3293c0 1064 if (-f "xdefine" && !(@ARGV && $ARGV[0] =~ /^--(?:configure|force)$/)) {
bf8300de 1065 print qq[$0: The "xdefine" exists, skipping the configure step.\n];
de3293c0
CBW
1066 print qq[Use "$^X $0 --configure"\n];
1067 print qq[or: "$^X $0 --force\n];
1068 print qq[to force the configure step.\n];
3f2ee006 1069 } else {
bf8300de
RGS
1070 print "Configuring Time::HiRes...\n";
1071 1 while unlink("define");
1072 if ($^O =~ /Win32/i) {
1073 DEFINE('SELECT_IS_BROKEN');
1074 $LIBS = [];
1075 print "System is $^O, skipping full configure...\n";
1ae6ead9 1076 open(XDEFINE, '>', 'xdefine') or die "$0: Cannot create xdefine: $!\n";
bf8300de
RGS
1077 close(XDEFINE);
1078 } else {
1079 init();
1080 }
1081 doMakefile;
1082 doConstants;
3f2ee006 1083 }
3f2ee006 1084 my $make = $Config{'make'} || "make";
1fbb4de4 1085 unless (exists $ENV{PERL_CORE} && $ENV{PERL_CORE}) {
3f2ee006 1086 print <<EOM;
3f2ee006 1087Now you may issue '$make'. Do not forget also '$make test'.
dfffa540 1088EOM
806b8cc2
SP
1089 if ($] == 5.008 &&
1090 ((exists $ENV{LC_ALL} && $ENV{LC_ALL} =~ /utf-?8/i) ||
1091 (exists $ENV{LC_CTYPE} && $ENV{LC_CTYPE} =~ /utf-?8/i) ||
1092 (exists $ENV{LANG} && $ENV{LANG} =~ /utf-?8/i))) {
22149ad2
RGS
1093 print <<EOM;
1094
ced84e60 1095NOTE: if you get an error like this (the Makefile line number may vary):
d7358e6a 1096Makefile:91: *** missing separator
1caec985
DM
1097then set the environment variable LC_ALL to "C" and retry
1098from scratch (re-run perl "Makefile.PL").
806b8cc2 1099(And consider upgrading your Perl to, say, at least Perl 5.8.8.)
50a6311a
RGS
1100(You got this message because you seem to have
1101 an UTF-8 locale active in your shell environment, this used
806b8cc2 1102 to cause broken Makefiles to be created from Makefile.PLs)
3f2ee006 1103EOM
dfffa540 1104 }
3f2ee006
HS
1105 }
1106}
1107
1108&main;
dcf686c9 1109
3f2ee006 1110# EOF