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