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