This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade Parse::CPAN::Meta from version 1.4417 to 1.4421
[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
91 if (open(TMPC, ">$tmp.c")) {
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') {
3f2ee006
HS
135 open( CMDFILE, ">$tmp.com" );
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"
293#include <$SYSCALL_H>
f3370602 294int main(int argc, char** argv)
ced84e60
SP
295{
296 struct timespec ts;
297 /* Many Linuxes get ENOSYS even though the syscall exists. */
298 /* All implementations are supposed to support CLOCK_REALTIME. */
299 int ret = syscall(SYS_clock_$x, CLOCK_REALTIME, &ts);
300 ret == 0 ? exit(0) : exit(errno ? errno : -1);
301}
302EOM
303}
304
170c5524
SP
305sub has_clock_xxx {
306 my $xxx = shift;
ced84e60
SP
307 return 1 if
308 try_compile_and_link(<<EOM, run => 1);
309#include "EXTERN.h"
310#include "perl.h"
311#include "XSUB.h"
f3370602 312int main(int argc, char** argv)
ced84e60
SP
313{
314 struct timespec ts;
170c5524 315 int ret = clock_$xxx(CLOCK_REALTIME, &ts); /* Many Linuxes get ENOSYS. */
ced84e60
SP
316 /* All implementations are supposed to support CLOCK_REALTIME. */
317 ret == 0 ? exit(0) : exit(errno ? errno : -1);
318}
319EOM
320}
321
170c5524
SP
322sub has_clock {
323 return 1 if
324 try_compile_and_link(<<EOM, run => 1);
325#include "EXTERN.h"
326#include "perl.h"
327#include "XSUB.h"
f3370602 328int main(int argc, char** argv)
170c5524
SP
329{
330 clock_t tictoc;
331 clock_t ret = clock();
332 ret == (clock_t)-1 ? exit(errno ? errno : -1) : exit(0);
333}
334EOM
335}
336
337sub has_clock_nanosleep {
338 return 1 if
339 try_compile_and_link(<<EOM, run => 1);
340#include "EXTERN.h"
341#include "perl.h"
342#include "XSUB.h"
a8fb48f7 343#include <time.h>
f3370602 344int main(int argc, char** argv)
170c5524
SP
345{
346 int ret;
a8fb48f7
SP
347 struct timespec ts1;
348 struct timespec ts2;
170c5524
SP
349 ts1.tv_sec = 0;
350 ts1.tv_nsec = 750000000;;
351 ret = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts1, &ts2);
352 ret == 0 ? exit(0) : exit(errno ? errno : -1);
353}
354EOM
355}
356
a8fb48f7
SP
357sub DEFINE {
358 my ($def, $val) = @_;
359 my $define = defined $val ? "$def=$val" : $def ;
360 unless ($DEFINE =~ /(?:^| )-D\Q$define\E(?: |$)/) {
361 $DEFINE .= " -D$define";
362 }
363}
364
046e3f33
JH
365sub init {
366 my $hints = File::Spec->catfile("hints", "$^O.pl");
367 if (-f $hints) {
368 print "Using hints $hints...\n";
369 local $self;
370 do $hints;
371 if (exists $self->{LIBS}) {
372 $LIBS = $self->{LIBS};
373 print "Extra libraries: @$LIBS...\n";
3f2ee006
HS
374 }
375 }
046e3f33
JH
376
377 $DEFINE = '';
3f2ee006 378
ced84e60 379 if ($Config{d_syscall}) {
a5929ff3 380 print "Have syscall()... looking for syscall.h... ";
ced84e60
SP
381 if (has_include('syscall.h')) {
382 $SYSCALL_H = 'syscall.h';
383 } elsif (has_include('sys/syscall.h')) {
384 $SYSCALL_H = 'sys/syscall.h';
385 }
a5929ff3
RGS
386 } else {
387 print "No syscall()...\n";
ced84e60
SP
388 }
389
a5929ff3
RGS
390 if ($Config{d_syscall}) {
391 if (defined $SYSCALL_H) {
392 print "found <$SYSCALL_H>.\n";
393 } else {
394 print "NOT found.\n";
395 }
ced84e60
SP
396 }
397
98b50af3 398 print "Looking for gettimeofday()... ";
3f2ee006 399 my $has_gettimeofday;
5d899d7e
JH
400 if (exists $Config{d_gettimeod}) {
401 $has_gettimeofday++ if $Config{d_gettimeod};
3f2ee006
HS
402 } elsif (has_gettimeofday()) {
403 $DEFINE .= ' -DHAS_GETTIMEOFDAY';
404 $has_gettimeofday++;
405 }
406
407 if ($has_gettimeofday) {
98b50af3 408 print "found.\n";
3f2ee006
HS
409 } else {
410 die <<EOD
411Your operating system does not seem to have the gettimeofday() function.
412(or, at least, I cannot find it)
413
414There is no way Time::HiRes is going to work.
415
416I am awfully sorry but I cannot go further.
417
418Aborting configuration.
419
420EOD
421 }
422
98b50af3 423 print "Looking for setitimer()... ";
3f2ee006 424 my $has_setitimer;
5d899d7e
JH
425 if (exists $Config{d_setitimer}) {
426 $has_setitimer++ if $Config{d_setitimer};
3f2ee006
HS
427 } elsif (has_x("setitimer(ITIMER_REAL, 0, 0)")) {
428 $has_setitimer++;
429 $DEFINE .= ' -DHAS_SETITIMER';
430 }
431
432 if ($has_setitimer) {
98b50af3 433 print "found.\n";
3f2ee006 434 } else {
98b50af3 435 print "NOT found.\n";
3f2ee006
HS
436 }
437
98b50af3 438 print "Looking for getitimer()... ";
3f2ee006 439 my $has_getitimer;
5d899d7e
JH
440 if (exists $Config{'d_getitimer'}) {
441 $has_getitimer++ if $Config{'d_getitimer'};
3f2ee006
HS
442 } elsif (has_x("getitimer(ITIMER_REAL, 0)")) {
443 $has_getitimer++;
444 $DEFINE .= ' -DHAS_GETITIMER';
445 }
446
447 if ($has_getitimer) {
98b50af3 448 print "found.\n";
3f2ee006 449 } else {
98b50af3 450 print "NOT found.\n";
3f2ee006
HS
451 }
452
453 if ($has_setitimer && $has_getitimer) {
4ed0e2d4 454 print "You have interval timers (both setitimer and getitimer).\n";
3f2ee006 455 } else {
98b50af3 456 print "You do not have interval timers.\n";
3f2ee006
HS
457 }
458
98b50af3
JH
459 print "Looking for ualarm()... ";
460 my $has_ualarm;
5d899d7e
JH
461 if (exists $Config{d_ualarm}) {
462 $has_ualarm++ if $Config{d_ualarm};
3f2ee006
HS
463 } elsif (has_x ("ualarm (0, 0)")) {
464 $has_ualarm++;
465 $DEFINE .= ' -DHAS_UALARM';
466 }
467
468 if ($has_ualarm) {
98b50af3 469 print "found.\n";
3f2ee006 470 } else {
98b50af3
JH
471 print "NOT found.\n";
472 if ($has_setitimer) {
473 print "But you have setitimer().\n";
474 print "We can make a Time::HiRes::ualarm().\n";
3f2ee006
HS
475 }
476 }
477
98b50af3 478 print "Looking for usleep()... ";
3f2ee006 479 my $has_usleep;
5d899d7e
JH
480 if (exists $Config{d_usleep}) {
481 $has_usleep++ if $Config{d_usleep};
3f2ee006
HS
482 } elsif (has_x ("usleep (0)")) {
483 $has_usleep++;
484 $DEFINE .= ' -DHAS_USLEEP';
485 }
486
487 if ($has_usleep) {
98b50af3 488 print "found.\n";
3f2ee006 489 } else {
98b50af3
JH
490 print "NOT found.\n";
491 print "Let's see if you have select()... ";
56c1b3bd 492 if ($Config{'d_select'}) {
98b50af3
JH
493 print "found.\n";
494 print "We can make a Time::HiRes::usleep().\n";
3f2ee006 495 } else {
98b50af3
JH
496 print "NOT found.\n";
497 print "You won't have a Time::HiRes::usleep().\n";
3f2ee006
HS
498 }
499 }
500
98b50af3 501 print "Looking for nanosleep()... ";
3f2ee006 502 my $has_nanosleep;
3d0346a5
SP
503 if ($ENV{FORCE_NANOSLEEP_SCAN}) {
504 print "forced scan... ";
505 if (has_nanosleep()) {
506 $has_nanosleep++;
507 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
508 }
509 }
510 elsif (exists $Config{d_nanosleep}) {
511 print "believing \$Config{d_nanosleep}... ";
c1363767
JH
512 if ($Config{d_nanosleep}) {
513 $has_nanosleep++;
514 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
515 }
3d0346a5
SP
516 } elsif ($^O =~ /^(mpeix)$/) {
517 # MPE/iX falsely finds nanosleep from its libc equivalent.
518 print "skipping because in $^O... ";
519 } else {
520 if (has_nanosleep()) {
521 $has_nanosleep++;
522 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
523 }
3f2ee006
HS
524 }
525
526 if ($has_nanosleep) {
98b50af3 527 print "found.\n";
4ed0e2d4
RGS
528 print "You can mix subsecond sleeps with signals, if you want to.\n";
529 print "(It's still not portable, though.)\n";
3f2ee006 530 } else {
98b50af3 531 print "NOT found.\n";
622913ab
IZ
532 my $nt = ($^O eq 'os2' ? '' : 'not');
533 print "You can$nt mix subsecond sleeps with signals.\n";
4ed0e2d4
RGS
534 print "(It would not be portable anyway.)\n";
535 }
536
ced84e60
SP
537 print "Looking for clock_gettime()... ";
538 my $has_clock_gettime;
1e845d0c 539 my $has_clock_gettime_emulation;
ced84e60
SP
540 if (exists $Config{d_clock_gettime}) {
541 $has_clock_gettime++ if $Config{d_clock_gettime}; # Unlikely...
170c5524 542 } elsif (has_clock_xxx('gettime')) {
ced84e60
SP
543 $has_clock_gettime++;
544 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME';
170c5524 545 } elsif (defined $SYSCALL_H && has_clock_xxx_syscall('gettime')) {
ced84e60
SP
546 $has_clock_gettime++;
547 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME -DTIME_HIRES_CLOCK_GETTIME_SYSCALL';
1e845d0c
JH
548 } elsif ($^O eq 'darwin') {
549 $has_clock_gettime_emulation++;
550 $has_clock_gettime++;
551 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME';
ced84e60
SP
552 }
553
554 if ($has_clock_gettime) {
555 if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETTIME_SYSCALL/) {
556 print "found (via syscall).\n";
1e845d0c
JH
557 } elsif ($has_clock_gettime_emulation) {
558 print "found (via emulation).\n";
ced84e60
SP
559 } else {
560 print "found.\n";
561 }
562 } else {
563 print "NOT found.\n";
564 }
565
566 print "Looking for clock_getres()... ";
567 my $has_clock_getres;
1e845d0c 568 my $has_clock_getres_emulation;
ced84e60
SP
569 if (exists $Config{d_clock_getres}) {
570 $has_clock_getres++ if $Config{d_clock_getres}; # Unlikely...
170c5524 571 } elsif (has_clock_xxx('getres')) {
ced84e60
SP
572 $has_clock_getres++;
573 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES';
170c5524 574 } elsif (defined $SYSCALL_H && has_clock_xxx_syscall('getres')) {
ced84e60
SP
575 $has_clock_getres++;
576 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES -DTIME_HIRES_CLOCK_GETRES_SYSCALL';
1e845d0c
JH
577 } elsif ($^O eq 'darwin') {
578 $has_clock_getres_emulation++;
579 $has_clock_getres++;
580 $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES';
ced84e60
SP
581 }
582
583 if ($has_clock_getres) {
584 if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETRES_SYSCALL/) {
585 print "found (via syscall).\n";
1e845d0c
JH
586 } elsif ($has_clock_getres_emulation) {
587 print "found (via emulation).\n";
ced84e60
SP
588 } else {
589 print "found.\n";
590 }
591 } else {
592 print "NOT found.\n";
593 }
594
170c5524
SP
595 print "Looking for clock_nanosleep()... ";
596 my $has_clock_nanosleep;
3b614a59 597 my $has_clock_nanosleep_emulation;
170c5524
SP
598 if (exists $Config{d_clock_nanosleep}) {
599 $has_clock_nanosleep++ if $Config{d_clock_nanosleep}; # Unlikely...
600 } elsif (has_clock_nanosleep()) {
601 $has_clock_nanosleep++;
602 $DEFINE .= ' -DTIME_HIRES_CLOCK_NANOSLEEP';
3b614a59
JH
603 } elsif ($^O eq 'darwin') {
604 $has_clock_nanosleep++;
605 $has_clock_nanosleep_emulation++;
606 $DEFINE .= ' -DTIME_HIRES_CLOCK_NANOSLEEP';
170c5524
SP
607 }
608
609 if ($has_clock_nanosleep) {
3b614a59
JH
610 if ($has_clock_nanosleep_emulation) {
611 print "found (via emulation).\n";
612 } else {
613 print "found.\n";
614 }
170c5524
SP
615 } else {
616 print "NOT found.\n";
617 }
618
619 print "Looking for clock()... ";
620 my $has_clock;
621 if (exists $Config{d_clock}) {
622 $has_clock++ if $Config{d_clock}; # Unlikely...
623 } elsif (has_clock()) {
624 $has_clock++;
625 $DEFINE .= ' -DTIME_HIRES_CLOCK';
626 }
627
628 if ($has_clock) {
629 print "found.\n";
630 } else {
631 print "NOT found.\n";
632 }
633
f3370602
SP
634 print "Looking for stat() subsecond timestamps...\n";
635
75d5269b
SP
636 print "Trying struct stat st_atimespec.tv_nsec...";
637 my $has_stat_st_xtimespec;
638 if (try_compile_and_link(<<EOM)) {
639$TIME_HEADERS
640#include <sys/stat.h>
f3370602 641int main(int argc, char** argv) {
75d5269b
SP
642 struct stat st;
643 st.st_atimespec.tv_nsec = 0;
644}
645EOM
646 $has_stat_st_xtimespec++;
c0c2b302 647 DEFINE('TIME_HIRES_STAT', 1);
75d5269b
SP
648 }
649
650 if ($has_stat_st_xtimespec) {
651 print "found.\n";
652 } else {
653 print "NOT found.\n";
654 }
655
656 print "Trying struct stat st_atimensec...";
657 my $has_stat_st_xtimensec;
658 if (try_compile_and_link(<<EOM)) {
659$TIME_HEADERS
660#include <sys/stat.h>
f3370602 661int main(int argc, char** argv) {
75d5269b
SP
662 struct stat st;
663 st.st_atimensec = 0;
664}
665EOM
666 $has_stat_st_xtimensec++;
c0c2b302 667 DEFINE('TIME_HIRES_STAT', 2);
75d5269b
SP
668 }
669
670 if ($has_stat_st_xtimensec) {
671 print "found.\n";
672 } else {
673 print "NOT found.\n";
674 }
675
676 print "Trying struct stat st_atime_n...";
677 my $has_stat_st_xtime_n;
678 if (try_compile_and_link(<<EOM)) {
679$TIME_HEADERS
680#include <sys/stat.h>
f3370602 681int main(int argc, char** argv) {
75d5269b
SP
682 struct stat st;
683 st.st_atime_n = 0;
684}
685EOM
686 $has_stat_st_xtime_n++;
c0c2b302 687 DEFINE('TIME_HIRES_STAT', 3);
75d5269b
SP
688 }
689
690 if ($has_stat_st_xtime_n) {
691 print "found.\n";
692 } else {
693 print "NOT found.\n";
694 }
695
696 print "Trying struct stat st_atim.tv_nsec...";
697 my $has_stat_st_xtim;
698 if (try_compile_and_link(<<EOM)) {
699$TIME_HEADERS
700#include <sys/stat.h>
f3370602 701int main(int argc, char** argv) {
75d5269b
SP
702 struct stat st;
703 st.st_atim.tv_nsec = 0;
704}
705EOM
706 $has_stat_st_xtim++;
c0c2b302 707 DEFINE('TIME_HIRES_STAT', 4);
75d5269b
SP
708 }
709
710 if ($has_stat_st_xtim) {
711 print "found.\n";
712 } else {
713 print "NOT found.\n";
714 }
715
716 print "Trying struct stat st_uatime...";
717 my $has_stat_st_uxtime;
718 if (try_compile_and_link(<<EOM)) {
719$TIME_HEADERS
720#include <sys/stat.h>
f3370602 721int main(int argc, char** argv) {
75d5269b
SP
722 struct stat st;
723 st.st_uatime = 0;
724}
725EOM
726 $has_stat_st_uxtime++;
c0c2b302 727 DEFINE('TIME_HIRES_STAT', 5);
75d5269b
SP
728 }
729
730 if ($has_stat_st_uxtime) {
731 print "found.\n";
732 } else {
733 print "NOT found.\n";
734 }
735
736 if ($DEFINE =~ /-DTIME_HIRES_STAT=\d+/) {
f3370602 737 print "You seem to have stat() subsecond timestamps.\n";
75d5269b
SP
738 print "(Your struct stat has them, but the filesystems must help.)\n";
739 } else {
740 print "You do not seem to have stat subsecond timestamps.\n";
741 }
742
4ed0e2d4 743 my $has_w32api_windows_h;
170c5524 744
4ed0e2d4
RGS
745 if ($^O eq 'cygwin') {
746 print "Looking for <w32api/windows.h>... ";
747 if (has_include('w32api/windows.h')) {
748 $has_w32api_windows_h++;
a8fb48f7 749 DEFINE('HAS_W32API_WINDOWS_H');
4ed0e2d4
RGS
750 }
751 if ($has_w32api_windows_h) {
752 print "found.\n";
753 } else {
754 print "NOT found.\n";
755 }
3f2ee006
HS
756 }
757
758 if ($DEFINE) {
759 $DEFINE =~ s/^\s+//;
760 if (open(XDEFINE, ">xdefine")) {
761 print XDEFINE $DEFINE, "\n";
762 close(XDEFINE);
763 }
764 }
765}
766
767sub doMakefile {
98b50af3 768 my @makefileopts = ();
3f2ee006
HS
769
770 if ($] >= 5.005) {
771 push (@makefileopts,
772 'AUTHOR' => 'Jarkko Hietaniemi <jhi@iki.fi>',
773 'ABSTRACT_FROM' => 'HiRes.pm',
774 );
a8fb48f7 775 DEFINE('ATLEASTFIVEOHOHFIVE');
3f2ee006
HS
776 }
777
778 push (@makefileopts,
779 'NAME' => 'Time::HiRes',
780 'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
5d899d7e
JH
781 'LIBS' => $LIBS, # e.g., '-lm'
782 'DEFINE' => $DEFINE, # e.g., '-DHAS_SOMETHING'
3f2ee006 783 'XSOPT' => $XSOPT,
ced84e60
SP
784 # Do not even think about 'INC' => '-I/usr/ucbinclude',
785 # Solaris will avenge.
5d899d7e 786 'INC' => '', # e.g., '-I/usr/include/other'
0f0eae2c 787 'INSTALLDIRS' => ($] >= 5.008 && $] < 5.011 ? 'perl' : 'site'),
90e44bf6
Z
788 'PREREQ_PM' => {
789 'Carp' => 0,
790 'Config' => 0,
791 'DynaLoader' => 0,
792 'Exporter' => 0,
793 'ExtUtils::MakeMaker' => 0,
794 'Test::More' => "0.82",
795 'strict' => 0,
796 },
3f2ee006
HS
797 'dist' => {
798 'CI' => 'ci -l',
5d899d7e 799 'COMPRESS' => 'gzip -9f',
3f2ee006
HS
800 'SUFFIX' => 'gz',
801 },
802 clean => { FILES => "xdefine" },
1caec985 803 realclean => { FILES=> 'const-c.inc const-xs.inc' },
3f2ee006
HS
804 );
805
91a2e9f6
Z
806 if ($^O eq "MSWin32" && !(grep { /\ALD[A-Z]*=/ } @ARGV)) {
807 my $libperl = $Config{libperl} || "";
808 my $gccversion = $Config{gccversion} || "";
809 if ($gccversion =~ /\A3\.4\.[0-9]+/ and $libperl =~ /\.lib\z/) {
810 # Avoid broken linkage with ActivePerl, by linking directly
811 # against the Perl DLL rather than the import library.
812 (my $llibperl = "-l$libperl") =~ s/\.lib\z//;
813 my $lddlflags = $Config{lddlflags} || "";
814 my $ldflags = $Config{ldflags} || "";
815 s/-L(?:".*?"|\S+)//g foreach $lddlflags, $ldflags;
816 my $libdirs = join ' ',
817 map { s/(?<!\\)((?:\\\\)*")/\\$1/g; qq[-L"$_"] }
818 @Config{qw/bin sitebin/};
819 push @makefileopts, macro => {
820 LDDLFLAGS => "$lddlflags $libdirs $llibperl",
821 LDFLAGS => "$ldflags $libdirs $llibperl",
822 PERL_ARCHIVE => "",
823 };
824 }
825 }
826
9ac5eb64
JH
827 if ($ENV{PERL_CORE}) {
828 push @makefileopts, MAN3PODS => {};
829 }
830
4451fd4e
SH
831 if ($ExtUtils::MakeMaker::VERSION >= 6.48) {
832 push @makefileopts, (MIN_PERL_VERSION => '5.008',);
833 }
834
835 if ($ExtUtils::MakeMaker::VERSION >= 6.31) {
836 push @makefileopts, (LICENSE => 'perl_5');
837 }
838
3f2ee006
HS
839 WriteMakefile(@makefileopts);
840}
841
98b50af3
JH
842sub doConstants {
843 if (eval {require ExtUtils::Constant; 1}) {
6a6012c3
JH
844 my @names = qw(
845 CLOCKS_PER_SEC
105140f6 846 CLOCK_BOOTTIME
6a6012c3
JH
847 CLOCK_HIGHRES
848 CLOCK_MONOTONIC
105140f6 849 CLOCK_MONOTONIC_COARSE
774a3049 850 CLOCK_MONOTONIC_PRECISE
105140f6 851 CLOCK_MONOTONIC_RAW
6a6012c3
JH
852 CLOCK_PROCESS_CPUTIME_ID
853 CLOCK_REALTIME
105140f6 854 CLOCK_REALTIME_COARSE
774a3049
JH
855 CLOCK_REALTIME_FAST
856 CLOCK_REALTIME_PRECISE
857 CLOCK_SECOND
6a6012c3
JH
858 CLOCK_SOFTTIME
859 CLOCK_THREAD_CPUTIME_ID
860 CLOCK_TIMEOFDAY
774a3049
JH
861 CLOCK_UPTIME
862 CLOCK_UPTIME_FAST
863 CLOCK_UPTIME_PRECISE
6a6012c3
JH
864 ITIMER_PROF
865 ITIMER_REAL
866 ITIMER_REALPROF
867 ITIMER_VIRTUAL
868 TIMER_ABSTIME
869 );
98b50af3 870 foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
170c5524 871 d_nanosleep d_clock_gettime d_clock_getres
c0c2b302 872 d_clock d_clock_nanosleep d_hires_stat)) {
98b50af3 873 my $macro = $_;
3b614a59 874 if ($macro =~ /^(d_nanosleep|d_clock)$/) {
82cbdcc3 875 $macro =~ s/^d_(.+)/TIME_HIRES_\U$1/;
75d5269b
SP
876 } elsif ($macro =~ /^(d_hires_stat)$/) {
877 my $d_hires_stat = 0;
878 $d_hires_stat = $1 if ($DEFINE =~ /-DTIME_HIRES_STAT=(\d+)/);
879 push @names, {name => $_, macro => "TIME_HIRES_STAT", value => $d_hires_stat,
880 default => ["IV", "0"]};
881 next;
3b614a59 882 } elsif ($macro =~ /^(d_clock_gettime|d_clock_getres|d_clock_nanosleep)$/) {
1e845d0c
JH
883 $macro =~ s/^d_(.+)/TIME_HIRES_\U$1/;
884 my $val = ($DEFINE =~ /-D$macro\b/) ? 1 : 0;
885 push @names, {name => $_, macro => $macro, value => $val,
886 default => ["IV", "0"]};
887 next;
672f6857 888 } else {
82cbdcc3 889 $macro =~ s/^d_(.+)/HAS_\U$1/;
672f6857 890 }
98b50af3
JH
891 push @names, {name => $_, macro => $macro, value => 1,
892 default => ["IV", "0"]};
893 }
894 ExtUtils::Constant::WriteConstants(
895 NAME => 'Time::HiRes',
896 NAMES => \@names,
897 );
898 } else {
1fbb4de4
RGS
899 my $file;
900 foreach $file ('const-c.inc', 'const-xs.inc') {
98b50af3
JH
901 my $fallback = File::Spec->catfile('fallback', $file);
902 local $/;
903 open IN, "<$fallback" or die "Can't open $fallback: $!";
904 open OUT, ">$file" or die "Can't open $file: $!";
905 print OUT <IN> or die $!;
906 close OUT or die "Can't close $file: $!";
907 close IN or die "Can't close $fallback: $!";
908 }
909 }
910}
3f2ee006 911
98b50af3 912sub main {
68c5b4db 913 if (-f "xdefine" && !(@ARGV && $ARGV[0] eq '--configure')) {
bf8300de 914 print qq[$0: The "xdefine" exists, skipping the configure step.\n];
e5620114 915 print qq[("$^X $0 --configure" to force the configure step)\n];
3f2ee006 916 } else {
bf8300de
RGS
917 print "Configuring Time::HiRes...\n";
918 1 while unlink("define");
919 if ($^O =~ /Win32/i) {
920 DEFINE('SELECT_IS_BROKEN');
921 $LIBS = [];
922 print "System is $^O, skipping full configure...\n";
923 open(XDEFINE, ">xdefine") or die "$0: Cannot create xdefine: $!\n";
924 close(XDEFINE);
925 } else {
926 init();
927 }
928 doMakefile;
929 doConstants;
3f2ee006 930 }
3f2ee006 931 my $make = $Config{'make'} || "make";
1fbb4de4 932 unless (exists $ENV{PERL_CORE} && $ENV{PERL_CORE}) {
3f2ee006 933 print <<EOM;
3f2ee006 934Now you may issue '$make'. Do not forget also '$make test'.
dfffa540 935EOM
806b8cc2
SP
936 if ($] == 5.008 &&
937 ((exists $ENV{LC_ALL} && $ENV{LC_ALL} =~ /utf-?8/i) ||
938 (exists $ENV{LC_CTYPE} && $ENV{LC_CTYPE} =~ /utf-?8/i) ||
939 (exists $ENV{LANG} && $ENV{LANG} =~ /utf-?8/i))) {
22149ad2
RGS
940 print <<EOM;
941
ced84e60 942NOTE: if you get an error like this (the Makefile line number may vary):
d7358e6a 943Makefile:91: *** missing separator
1caec985
DM
944then set the environment variable LC_ALL to "C" and retry
945from scratch (re-run perl "Makefile.PL").
806b8cc2 946(And consider upgrading your Perl to, say, at least Perl 5.8.8.)
50a6311a
RGS
947(You got this message because you seem to have
948 an UTF-8 locale active in your shell environment, this used
806b8cc2 949 to cause broken Makefiles to be created from Makefile.PLs)
3f2ee006 950EOM
dfffa540 951 }
3f2ee006
HS
952 }
953}
954
955&main;
dcf686c9 956
3f2ee006 957# EOF