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