Upgrade to Time-HiRes-1.75
[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 $tmp_exe =
148                     File::Spec->catfile(File::Spec->curdir, $tmp_exe);
149                 printf "Running $tmp_exe..." if $VERBOSE;
150                 if (system($tmp_exe) == 0) {
151                     $ok = 1;
152                 } else {
153                     $ok = 0;
154                     print "[ system('$tmp_exe') failed: status $? ] ";
155                 }
156             }
157             unlink("$tmp.c", $tmp_exe);
158         }
159     }
160
161     return $ok;
162 }
163
164 sub has_gettimeofday {
165     # confusing but true (if condition true ==> -DHAS_GETTIMEOFDAY already)
166     return 0 if $Config{d_gettimeod};
167     return 1 if try_compile_and_link(<<EOM);
168 #include "EXTERN.h"
169 #include "perl.h"
170 #include "XSUB.h"
171 #ifdef I_SYS_TYPES
172 #   include <sys/types.h>
173 #endif
174
175 #ifdef I_SYS_TIME
176 #   include <sys/time.h>
177 #endif
178
179 #ifdef I_SYS_SELECT
180 #   include <sys/select.h>      /* struct timeval might be hidden in here */
181 #endif
182 static int foo()
183 {
184     struct timeval tv;
185     gettimeofday(&tv, 0);
186 }
187 int main _((int argc, char** argv, char** env))
188 {
189     foo();
190 }
191 EOM
192     return 0;
193 }
194
195 sub has_x {
196     my ($x, %args) = @_;
197
198     return 1 if
199     try_compile_and_link(<<EOM, %args);
200 #include "EXTERN.h"
201 #include "perl.h"
202 #include "XSUB.h"
203
204 #ifdef I_UNISTD
205 #   include <unistd.h>
206 #endif
207
208 #ifdef I_SYS_TYPES
209 #   include <sys/types.h>
210 #endif
211
212 #ifdef I_SYS_TIME
213 #   include <sys/time.h>
214 #endif
215
216 int main _((int argc, char** argv, char** env))
217 {
218         $x;
219 }
220 EOM
221     return 0;
222 }
223
224 sub has_nanosleep {
225     print "Trying out nanosleep... ";
226     return 1 if
227     try_compile_and_link(<<EOM, run => 1);
228 #include <time.h>
229 #include <sys/time.h>
230 #include <stdio.h>
231 #include <stdlib.h>
232 #include <errno.h>
233
234 /* int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); */
235
236 int main() {
237     struct timespec ts1, ts2;
238     int ret;
239     ts1.tv_sec  = 0;
240     ts1.tv_nsec = 750000000;
241     ts2.tv_sec  = 0;
242     ts2.tv_nsec = 0;
243     errno = 0;
244     ret = nanosleep(&ts1, &ts2); /* E.g. in AIX nanosleep() fail and set errno to ENOSYS. */
245     ret == 0 ? exit(0) : exit(errno ? errno : -1);
246 }
247 EOM
248 }
249
250 sub has_include {
251     my ($inc) = @_;
252     return 1 if
253     try_compile_and_link(<<EOM);
254 #include "EXTERN.h"
255 #include "perl.h"
256 #include "XSUB.h"
257
258 #include <$inc>
259 int main _((int argc, char** argv, char** env))
260 {
261         return 0;
262 }
263 EOM
264     return 0;
265 }
266
267 sub init {
268     my $hints = File::Spec->catfile("hints", "$^O.pl");
269     if (-f $hints) {
270         print "Using hints $hints...\n";
271         local $self;
272         do $hints;
273         if (exists $self->{LIBS}) {
274             $LIBS = $self->{LIBS};
275             print "Extra libraries: @$LIBS...\n";
276         }
277     }
278
279     $DEFINE = '';
280
281     print "Looking for gettimeofday()... ";
282     my $has_gettimeofday;
283     if (exists $Config{d_gettimeod}) {
284         $has_gettimeofday++ if $Config{d_gettimeod};
285     } elsif (has_gettimeofday()) {
286         $DEFINE .= ' -DHAS_GETTIMEOFDAY';
287         $has_gettimeofday++;
288     }
289
290     if ($has_gettimeofday) {
291         print "found.\n";
292     } else {
293         die <<EOD
294 Your operating system does not seem to have the gettimeofday() function.
295 (or, at least, I cannot find it)
296
297 There is no way Time::HiRes is going to work.
298
299 I am awfully sorry but I cannot go further.
300
301 Aborting configuration.
302
303 EOD
304     }
305
306     print "Looking for setitimer()... ";
307     my $has_setitimer;
308     if (exists $Config{d_setitimer}) {
309         $has_setitimer++ if $Config{d_setitimer};
310     } elsif (has_x("setitimer(ITIMER_REAL, 0, 0)")) {
311         $has_setitimer++;
312         $DEFINE .= ' -DHAS_SETITIMER';
313     }
314
315     if ($has_setitimer) {
316         print "found.\n";
317     } else {
318         print "NOT found.\n";
319     }
320
321     print "Looking for getitimer()... ";
322     my $has_getitimer;
323     if (exists $Config{'d_getitimer'}) {
324         $has_getitimer++ if $Config{'d_getitimer'};
325     } elsif (has_x("getitimer(ITIMER_REAL, 0)")) {
326         $has_getitimer++;
327         $DEFINE .= ' -DHAS_GETITIMER';
328     }
329
330     if ($has_getitimer) {
331         print "found.\n";
332     } else {
333         print "NOT found.\n";
334     }
335
336     if ($has_setitimer && $has_getitimer) {
337         print "You have interval timers (both setitimer and getitimer).\n";
338     } else {
339         print "You do not have interval timers.\n";
340     }
341
342     print "Looking for ualarm()... ";
343     my $has_ualarm;
344     if (exists $Config{d_ualarm}) {
345         $has_ualarm++ if $Config{d_ualarm};
346     } elsif (has_x ("ualarm (0, 0)")) {
347         $has_ualarm++;
348         $DEFINE .= ' -DHAS_UALARM';
349     }
350
351     if ($has_ualarm) {
352         print "found.\n";
353     } else {
354         print "NOT found.\n";
355         if ($has_setitimer) {
356             print "But you have setitimer().\n";
357             print "We can make a Time::HiRes::ualarm().\n";
358         }
359     }
360
361     print "Looking for usleep()... ";
362     my $has_usleep;
363     if (exists $Config{d_usleep}) {
364         $has_usleep++ if $Config{d_usleep};
365     } elsif (has_x ("usleep (0)")) {
366         $has_usleep++;
367         $DEFINE .= ' -DHAS_USLEEP';
368     }
369
370     if ($has_usleep) {
371         print "found.\n";
372     } else {
373         print "NOT found.\n";
374         print "Let's see if you have select()... ";
375         if ($Config{'d_select'}) {
376             print "found.\n";
377             print "We can make a Time::HiRes::usleep().\n";
378         } else {
379             print "NOT found.\n";
380             print "You won't have a Time::HiRes::usleep().\n";
381         }
382     }
383
384     print "Looking for nanosleep()... ";
385     my $has_nanosleep;
386     if (exists $Config{d_nanosleep} && !$ENV{FORCE_NANOSLEEP_SCAN}) {
387         # Believe $Config{d_nanosleep}.
388         if ($Config{d_nanosleep}) {
389             $has_nanosleep++;
390             $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
391         }
392     } elsif ($^O ne 'mpeix' && # MPE/iX falsely finds nanosleep.
393              has_nanosleep()) {
394         $has_nanosleep++;
395         $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
396     }
397
398     if ($has_nanosleep) {
399         print "found.\n";
400         print "You can mix subsecond sleeps with signals, if you want to.\n";
401         print "(It's still not portable, though.)\n";
402     } else {
403         print "NOT found.\n";
404         my $nt = ($^O eq 'os2' ? '' : 'not');
405         print "You can$nt mix subsecond sleeps with signals.\n";
406         print "(It would not be portable anyway.)\n";
407     }
408
409     my $has_w32api_windows_h;
410     if ($^O eq 'cygwin') {
411         print "Looking for <w32api/windows.h>... ";
412         if (has_include('w32api/windows.h')) {
413             $has_w32api_windows_h++;
414             $DEFINE .= ' -DHAS_W32API_WINDOWS_H';
415         }
416         if ($has_w32api_windows_h) {
417             print "found.\n";
418         } else {
419             print "NOT found.\n";
420         }
421     }
422
423     if ($DEFINE) {
424         $DEFINE =~ s/^\s+//;
425         if (open(XDEFINE, ">xdefine")) {
426             print XDEFINE $DEFINE, "\n";
427             close(XDEFINE);
428         }
429     }
430 }
431
432 sub doMakefile {
433     my @makefileopts = ();
434
435     if ($] >= 5.005) {
436         push (@makefileopts,
437             'AUTHOR'    => 'Jarkko Hietaniemi <jhi@iki.fi>',
438             'ABSTRACT_FROM' => 'HiRes.pm',
439         );
440         $DEFINE .= " -DATLEASTFIVEOHOHFIVE";
441     }
442
443     push (@makefileopts,
444         'NAME'  => 'Time::HiRes',
445         'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
446         'LIBS'  => $LIBS,   # e.g., '-lm'
447         'DEFINE'        => $DEFINE,     # e.g., '-DHAS_SOMETHING'
448         'XSOPT' => $XSOPT,
449     # do not even think about 'INC' => '-I/usr/ucbinclude', Solaris will avenge.
450         'INC'   => '',     # e.g., '-I/usr/include/other'
451         'INSTALLDIRS' => ($] >= 5.008 ? 'perl' : 'site'),
452         'dist'      => {
453             'CI'       => 'ci -l',
454             'COMPRESS' => 'gzip -9f',
455             'SUFFIX'   => 'gz',
456         },
457         clean => { FILES => "xdefine" },
458         realclean => { FILES=> 'const-c.inc const-xs.inc' },
459     );
460
461     if ($ENV{PERL_CORE}) {
462         push @makefileopts, MAN3PODS => {};
463     }
464
465     WriteMakefile(@makefileopts);
466 }
467
468 sub doConstants {
469     if (eval {require ExtUtils::Constant; 1}) {
470         my @names = (qw(ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF
471                         ITIMER_REALPROF));
472         foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
473                      d_nanosleep)) {
474             my $macro = $_;
475             if ($macro eq 'd_nanosleep') {
476                 $macro =~ s/d_(.*)/TIME_HIRES_\U$1/;
477             } else {
478                 $macro =~ s/d_(.*)/HAS_\U$1/;
479             }
480             push @names, {name => $_, macro => $macro, value => 1,
481                           default => ["IV", "0"]};
482         }
483         ExtUtils::Constant::WriteConstants(
484                                            NAME => 'Time::HiRes',
485                                            NAMES => \@names,
486                                           );
487     } else {
488         my $file;
489         foreach $file ('const-c.inc', 'const-xs.inc') {
490             my $fallback = File::Spec->catfile('fallback', $file);
491             local $/;
492             open IN, "<$fallback" or die "Can't open $fallback: $!";
493             open OUT, ">$file" or die "Can't open $file: $!";
494             print OUT <IN> or die $!;
495             close OUT or die "Can't close $file: $!";
496             close IN or die "Can't close $fallback: $!";
497         }
498     }
499 }
500
501 sub main {
502     print "Configuring Time::HiRes...\n";
503     if ($] == 5.007002) {
504         die "Cannot Configure Time::HiRes for Perl $], aborting.\n";
505     }
506
507     if ($^O =~ /Win32/i) {
508       $DEFINE = '-DSELECT_IS_BROKEN';
509       $LIBS = [];
510     } else {
511       init();
512     }
513     doMakefile;
514     doConstants;
515     my $make = $Config{'make'} || "make";
516     unless (exists $ENV{PERL_CORE} && $ENV{PERL_CORE}) {
517         print  <<EOM;
518 Now you may issue '$make'.  Do not forget also '$make test'.
519 EOM
520        if ((exists $ENV{LC_ALL}   && $ENV{LC_ALL}   =~ /utf-?8/i) ||
521            (exists $ENV{LC_CTYPE} && $ENV{LC_CTYPE} =~ /utf-?8/i) ||
522            (exists $ENV{LANG}     && $ENV{LANG}     =~ /utf-?8/i)) {
523             print  <<EOM;
524 NOTE: if you get an error like this (the line number may vary):
525 Makefile:91: *** missing separator
526 then set the environment variable LC_ALL to "C" and retry
527 from scratch (re-run perl "Makefile.PL").
528 EOM
529         }
530     }
531 }
532
533 &main;
534
535 # EOF