Upgrade to Time::HiRes 1.71
[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 misplaces (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
19 use vars qw($self); # Used in 'sourcing' the hints.
20
21 my $ld_exeext = ($^O eq 'cygwin' ||
22                  $^O eq 'os2' && $Config{ldflags} =~ /-Zexe\b/) ? '.exe' : '';
23
24 unless($ENV{PERL_CORE}) {
25     $ENV{PERL_CORE} = 1 if grep { $_ eq 'PERL_CORE=1' } @ARGV;
26 }
27
28 # Perls 5.002 and 5.003 did not have File::Spec, fake what we need.
29
30 sub my_dirsep {
31     $^O eq 'VMS' ? '.' :
32         $^O =~ /mswin32|netware|djgpp/i ? '\\' :
33             $^O eq 'MacOS' ? ':'
34                 : '/';
35 }
36
37 sub my_catdir {
38     shift;
39     my $catdir = join(my_dirsep, @_);
40     $^O eq 'VMS' ? "[$catdir]" : $catdir;
41 }
42
43 sub my_catfile {
44     shift;
45     return join(my_dirsep, @_) unless $^O eq 'VMS';
46     my $file = pop;
47     return my_catdir (undef, @_) . $file;
48 }
49
50 sub my_updir {
51     shift;
52     $^O eq 'VMS' ? "-" : "..";
53 }
54
55 BEGIN {
56     eval { require File::Spec };
57     if ($@) {
58         *File::Spec::catdir  = \&my_catdir;
59         *File::Spec::updir   = \&my_updir;
60         *File::Spec::catfile = \&my_catfile;
61     }
62 }
63
64 # Avoid 'used only once' warnings.
65 my $nop1 = *File::Spec::catdir;
66 my $nop2 = *File::Spec::updir;
67 my $nop3 = *File::Spec::catfile;
68
69 # if you have 5.004_03 (and some slightly older versions?), xsubpp
70 # tries to generate line numbers in the C code generated from the .xs.
71 # unfortunately, it is a little buggy around #ifdef'd code.
72 # my choice is leave it in and have people with old perls complain
73 # about the "Usage" bug, or leave it out and be unable to compile myself
74 # without changing it, and then I'd always forget to change it before a
75 # release. Sorry, Edward :)
76
77 sub try_compile_and_link {
78     my ($c, %args) = @_;
79
80     my ($ok) = 0;
81     my ($tmp) = "tmp$$";
82     local(*TMPC);
83
84     my $obj_ext = $Config{obj_ext} || ".o";
85     unlink("$tmp.c", "$tmp$obj_ext");
86
87     if (open(TMPC, ">$tmp.c")) {
88         print TMPC $c;
89         close(TMPC);
90
91         my $cccmd = $args{cccmd};
92
93         my $errornull;
94
95         my $COREincdir;
96
97         if ($ENV{PERL_CORE}) {
98             my $updir = File::Spec->updir;
99             $COREincdir = File::Spec->catdir(($updir) x 3);
100         } else {
101             $COREincdir = File::Spec->catdir($Config{'archlibexp'}, 'CORE');
102         }
103
104         my $ccflags = $Config{'ccflags'} . ' ' . "-I$COREincdir";
105
106         if ($^O eq 'VMS') {
107             if ($ENV{PERL_CORE}) {
108                 # Fragile if the extensions change hierarchy within
109                 # the Perl core but this should do for now.
110                 $cccmd = "$Config{'cc'} /include=([---]) $tmp.c";
111             } else {
112                 my $perl_core = $Config{'installarchlib'};
113                 $perl_core =~ s/\]$/.CORE]/;
114                 $cccmd = "$Config{'cc'} /include=(perl_root:[000000],$perl_core) $tmp.c";
115             }
116         }
117
118         if ($args{silent} || !$VERBOSE) {
119             $errornull = "2>/dev/null" unless defined $errornull;
120         } else {
121             $errornull = '';
122         }
123
124         $cccmd = "$Config{'cc'} -o $tmp $ccflags $tmp.c @$LIBS $errornull"
125             unless defined $cccmd;
126
127        if ($^O eq 'VMS') {
128             open( CMDFILE, ">$tmp.com" );
129             print CMDFILE "\$ SET MESSAGE/NOFACILITY/NOSEVERITY/NOIDENT/NOTEXT\n";
130             print CMDFILE "\$ $cccmd\n";
131             print CMDFILE "\$ IF \$SEVERITY .NE. 1 THEN EXIT 44\n"; # escalate
132             close CMDFILE;
133             system("\@ $tmp.com");
134             $ok = $?==0;
135             for ("$tmp.c", "$tmp$obj_ext", "$tmp.com", "$tmp$Config{exe_ext}") {
136                 1 while unlink $_;
137             }
138         }
139         else
140         {
141             my $tmp_exe = "$tmp$ld_exeext";
142             printf "cccmd = $cccmd\n" if $VERBOSE;
143             my $res = system($cccmd);
144             $ok = defined($res) && $res==0 && -s $tmp_exe && -x _;
145
146             if ( $ok && exists $args{run} && $args{run}) {
147                 my $abs_tmp_exe =
148                     File::Spec->
149                         catfile(File::Spec->rel2abs(File::Spec->curdir),
150                                 $tmp_exe);
151                 printf "Running $abs_tmp_exe..." if $VERBOSE;
152                 if (system($abs_tmp_exe) == 0) {
153                     $ok = 1;
154                 } else {
155                     $ok = 0;
156                     print "system('$abs_tmp_exe') failed: $?\n";
157                 }
158             }
159             unlink("$tmp.c", $tmp_exe);
160         }
161     }
162
163     return $ok;
164 }
165
166 sub has_gettimeofday {
167     # confusing but true (if condition true ==> -DHAS_GETTIMEOFDAY already)
168     return 0 if $Config{d_gettimeod};
169     return 1 if try_compile_and_link(<<EOM);
170 #include "EXTERN.h"
171 #include "perl.h"
172 #include "XSUB.h"
173 #ifdef I_SYS_TYPES
174 #   include <sys/types.h>
175 #endif
176
177 #ifdef I_SYS_TIME
178 #   include <sys/time.h>
179 #endif
180
181 #ifdef I_SYS_SELECT
182 #   include <sys/select.h>      /* struct timeval might be hidden in here */
183 #endif
184 static int foo()
185 {
186     struct timeval tv;
187     gettimeofday(&tv, 0);
188 }
189 int main _((int argc, char** argv, char** env))
190 {
191     foo();
192 }
193 EOM
194     return 0;
195 }
196
197 sub has_x {
198     my ($x, %args) = @_;
199
200     return 1 if
201     try_compile_and_link(<<EOM, %args);
202 #include "EXTERN.h"
203 #include "perl.h"
204 #include "XSUB.h"
205
206 #ifdef I_UNISTD
207 #   include <unistd.h>
208 #endif
209
210 #ifdef I_SYS_TYPES
211 #   include <sys/types.h>
212 #endif
213
214 #ifdef I_SYS_TIME
215 #   include <sys/time.h>
216 #endif
217
218 int main _((int argc, char** argv, char** env))
219 {
220         $x;
221 }
222 EOM
223     return 0;
224 }
225
226 sub has_nanosleep {
227     print "Trying out nanosleep... ";
228     return 1 if
229     try_compile_and_link(<<EOM, run => 1);
230 #include <time.h>
231 #include <sys/time.h>
232 #include <stdio.h>
233 #include <stdlib.h>
234 #include <errno.h>
235
236 /* int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); */
237
238 int main() {
239     struct timespec ts1, ts2;
240     int ret;
241     ts1.tv_sec  = 0;
242     ts1.tv_nsec = 750000000;
243     ts2.tv_sec  = 0;
244     ts2.tv_nsec = 0;
245     errno = 0;
246     ret = nanosleep(&ts1, &ts2); /* E.g. in AIX nanosleep() fail and set errno to ENOSYS. */
247     ret == 0 ? exit(0) : exit(errno ? errno : -1);
248 }
249 EOM
250 }
251
252 sub has_include {
253     my ($inc) = @_;
254     return 1 if
255     try_compile_and_link(<<EOM);
256 #include "EXTERN.h"
257 #include "perl.h"
258 #include "XSUB.h"
259
260 #include <$inc>
261 int main _((int argc, char** argv, char** env))
262 {
263         return 0;
264 }
265 EOM
266     return 0;
267 }
268
269 sub init {
270     my $hints = File::Spec->catfile("hints", "$^O.pl");
271     if (-f $hints) {
272         print "Using hints $hints...\n";
273         local $self;
274         do $hints;
275         if (exists $self->{LIBS}) {
276             $LIBS = $self->{LIBS};
277             print "Extra libraries: @$LIBS...\n";
278         }
279     }
280
281     $DEFINE = '';
282
283     print "Looking for gettimeofday()... ";
284     my $has_gettimeofday;
285     if (exists $Config{d_gettimeod}) {
286         $has_gettimeofday++ if $Config{d_gettimeod};
287     } elsif (has_gettimeofday()) {
288         $DEFINE .= ' -DHAS_GETTIMEOFDAY';
289         $has_gettimeofday++;
290     }
291
292     if ($has_gettimeofday) {
293         print "found.\n";
294     } else {
295         die <<EOD
296 Your operating system does not seem to have the gettimeofday() function.
297 (or, at least, I cannot find it)
298
299 There is no way Time::HiRes is going to work.
300
301 I am awfully sorry but I cannot go further.
302
303 Aborting configuration.
304
305 EOD
306     }
307
308     print "Looking for setitimer()... ";
309     my $has_setitimer;
310     if (exists $Config{d_setitimer}) {
311         $has_setitimer++ if $Config{d_setitimer};
312     } elsif (has_x("setitimer(ITIMER_REAL, 0, 0)")) {
313         $has_setitimer++;
314         $DEFINE .= ' -DHAS_SETITIMER';
315     }
316
317     if ($has_setitimer) {
318         print "found.\n";
319     } else {
320         print "NOT found.\n";
321     }
322
323     print "Looking for getitimer()... ";
324     my $has_getitimer;
325     if (exists $Config{'d_getitimer'}) {
326         $has_getitimer++ if $Config{'d_getitimer'};
327     } elsif (has_x("getitimer(ITIMER_REAL, 0)")) {
328         $has_getitimer++;
329         $DEFINE .= ' -DHAS_GETITIMER';
330     }
331
332     if ($has_getitimer) {
333         print "found.\n";
334     } else {
335         print "NOT found.\n";
336     }
337
338     if ($has_setitimer && $has_getitimer) {
339         print "You have interval timers (both setitimer and getitimer).\n";
340     } else {
341         print "You do not have interval timers.\n";
342     }
343
344     print "Looking for ualarm()... ";
345     my $has_ualarm;
346     if (exists $Config{d_ualarm}) {
347         $has_ualarm++ if $Config{d_ualarm};
348     } elsif (has_x ("ualarm (0, 0)")) {
349         $has_ualarm++;
350         $DEFINE .= ' -DHAS_UALARM';
351     }
352
353     if ($has_ualarm) {
354         print "found.\n";
355     } else {
356         print "NOT found.\n";
357         if ($has_setitimer) {
358             print "But you have setitimer().\n";
359             print "We can make a Time::HiRes::ualarm().\n";
360         }
361     }
362
363     print "Looking for usleep()... ";
364     my $has_usleep;
365     if (exists $Config{d_usleep}) {
366         $has_usleep++ if $Config{d_usleep};
367     } elsif (has_x ("usleep (0)")) {
368         $has_usleep++;
369         $DEFINE .= ' -DHAS_USLEEP';
370     }
371
372     if ($has_usleep) {
373         print "found.\n";
374     } else {
375         print "NOT found.\n";
376         print "Let's see if you have select()... ";
377         if ($Config{'d_select'}) {
378             print "found.\n";
379             print "We can make a Time::HiRes::usleep().\n";
380         } else {
381             print "NOT found.\n";
382             print "You won't have a Time::HiRes::usleep().\n";
383         }
384     }
385
386     print "Looking for nanosleep()... ";
387     my $has_nanosleep;
388     if (exists $Config{d_nanosleep} && !$ENV{FORCE_NANOSLEEP_SCAN}) {
389         # Believe $Config{d_nanosleep}.
390         if ($Config{d_nanosleep}) {
391             $has_nanosleep++;
392             $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
393         }
394     } elsif ($^O ne 'mpeix' && # MPE/iX falsely finds nanosleep.
395              has_nanosleep()) {
396         $has_nanosleep++;
397         $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
398     }
399
400     if ($has_nanosleep) {
401         print "found.\n";
402         print "You can mix subsecond sleeps with signals, if you want to.\n";
403         print "(It's still not portable, though.)\n";
404     } else {
405         print "NOT found.\n";
406         my $nt = ($^O eq 'os2' ? '' : 'not');
407         print "You can$nt mix subsecond sleeps with signals.\n";
408         print "(It would not be portable anyway.)\n";
409     }
410
411     my $has_w32api_windows_h;
412     if ($^O eq 'cygwin') {
413         print "Looking for <w32api/windows.h>... ";
414         if (has_include('w32api/windows.h')) {
415             $has_w32api_windows_h++;
416             $DEFINE .= ' -DHAS_W32API_WINDOWS_H';
417         }
418         if ($has_w32api_windows_h) {
419             print "found.\n";
420         } else {
421             print "NOT found.\n";
422         }
423     }
424
425     if ($DEFINE) {
426         $DEFINE =~ s/^\s+//;
427         if (open(XDEFINE, ">xdefine")) {
428             print XDEFINE $DEFINE, "\n";
429             close(XDEFINE);
430         }
431     }
432 }
433
434 sub doMakefile {
435     my @makefileopts = ();
436
437     if ($] >= 5.005) {
438         push (@makefileopts,
439             'AUTHOR'    => 'Jarkko Hietaniemi <jhi@iki.fi>',
440             'ABSTRACT_FROM' => 'HiRes.pm',
441         );
442         $DEFINE .= " -DATLEASTFIVEOHOHFIVE";
443     }
444
445     push (@makefileopts,
446         'NAME'  => 'Time::HiRes',
447         'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
448         'LIBS'  => $LIBS,   # e.g., '-lm'
449         'DEFINE'        => $DEFINE,     # e.g., '-DHAS_SOMETHING'
450         'XSOPT' => $XSOPT,
451     # do not even think about 'INC' => '-I/usr/ucbinclude', Solaris will avenge.
452         'INC'   => '',     # e.g., '-I/usr/include/other'
453         'INSTALLDIRS' => 'perl',
454         'dist'      => {
455             'CI'       => 'ci -l',
456             'COMPRESS' => 'gzip -9f',
457             'SUFFIX'   => 'gz',
458         },
459         clean => { FILES => "xdefine" },
460         realclean => { FILES=> 'const-c.inc const-xs.inc' },
461     );
462
463     if ($ENV{PERL_CORE}) {
464         push @makefileopts, MAN3PODS => {};
465     }
466
467     WriteMakefile(@makefileopts);
468 }
469
470 sub doConstants {
471     if (eval {require ExtUtils::Constant; 1}) {
472         my @names = (qw(ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF
473                         ITIMER_REALPROF));
474         foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
475                      d_nanosleep)) {
476             my $macro = $_;
477             if ($macro eq 'd_nanosleep') {
478                 $macro =~ s/d_(.*)/TIME_HIRES_\U$1/;
479             } else {
480                 $macro =~ s/d_(.*)/HAS_\U$1/;
481             }
482             push @names, {name => $_, macro => $macro, value => 1,
483                           default => ["IV", "0"]};
484         }
485         ExtUtils::Constant::WriteConstants(
486                                            NAME => 'Time::HiRes',
487                                            NAMES => \@names,
488                                           );
489     } else {
490         my $file;
491         foreach $file ('const-c.inc', 'const-xs.inc') {
492             my $fallback = File::Spec->catfile('fallback', $file);
493             local $/;
494             open IN, "<$fallback" or die "Can't open $fallback: $!";
495             open OUT, ">$file" or die "Can't open $file: $!";
496             print OUT <IN> or die $!;
497             close OUT or die "Can't close $file: $!";
498             close IN or die "Can't close $fallback: $!";
499         }
500     }
501 }
502
503 sub main {
504     print "Configuring Time::HiRes...\n";
505     if ($] == 5.007002) {
506         die "Cannot Configure Time::HiRes for Perl $], aborting.\n";
507     }
508
509     if ($^O =~ /Win32/i) {
510       $DEFINE = '-DSELECT_IS_BROKEN';
511       $LIBS = [];
512     } else {
513       init();
514     }
515     doMakefile;
516     doConstants;
517     my $make = $Config{'make'} || "make";
518     unless (exists $ENV{PERL_CORE} && $ENV{PERL_CORE}) {
519         print  <<EOM;
520 Now you may issue '$make'.  Do not forget also '$make test'.
521 EOM
522        if ((exists $ENV{LC_ALL}   && $ENV{LC_ALL}   =~ /utf-?8/i) ||
523            (exists $ENV{LC_CTYPE} && $ENV{LC_CTYPE} =~ /utf-?8/i) ||
524            (exists $ENV{LANG}     && $ENV{LANG}     =~ /utf-?8/i)) {
525             print  <<EOM;
526 NOTE: if you get an error like this (the line number may vary):
527 Makefile:91: *** missing separator
528 then set the environment variable LC_ALL to "C" and retry
529 from scratch (re-run perl "Makefile.PL").
530 EOM
531         }
532     }
533 }
534
535 &main;
536
537 # EOF