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