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