0105609b225ac037f19ec34186a233b60a86c883
[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 sub has_gettimeofday {
173     # confusing but true (if condition true ==> -DHAS_GETTIMEOFDAY already)
174     return 0 if $Config{d_gettimeod};
175     return 1 if try_compile_and_link(<<EOM);
176 #include "EXTERN.h"
177 #include "perl.h"
178 #include "XSUB.h"
179 #ifdef I_SYS_TYPES
180 #   include <sys/types.h>
181 #endif
182
183 #ifdef I_SYS_TIME
184 #   include <sys/time.h>
185 #endif
186
187 #ifdef I_SYS_SELECT
188 #   include <sys/select.h>      /* struct timeval might be hidden in here */
189 #endif
190 static int foo()
191 {
192     struct timeval tv;
193     gettimeofday(&tv, 0);
194 }
195 int main _((int argc, char** argv, char** env))
196 {
197     foo();
198 }
199 EOM
200     return 0;
201 }
202
203 sub has_x {
204     my ($x, %args) = @_;
205
206     return 1 if
207     try_compile_and_link(<<EOM, %args);
208 #include "EXTERN.h"
209 #include "perl.h"
210 #include "XSUB.h"
211
212 #ifdef I_UNISTD
213 #   include <unistd.h>
214 #endif
215
216 #ifdef I_SYS_TYPES
217 #   include <sys/types.h>
218 #endif
219
220 #ifdef I_SYS_TIME
221 #   include <sys/time.h>
222 #endif
223
224 int main _((int argc, char** argv, char** env))
225 {
226         $x;
227 }
228 EOM
229     return 0;
230 }
231
232 sub has_nanosleep {
233     print "testing... ";
234     return 1 if
235     try_compile_and_link(<<EOM, run => 1);
236 #include <time.h>
237 #include <sys/time.h>
238 #include <stdio.h>
239 #include <stdlib.h>
240 #include <errno.h>
241
242 /* int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); */
243
244 int main() {
245     struct timespec ts1, ts2;
246     int ret;
247     ts1.tv_sec  = 0;
248     ts1.tv_nsec = 750000000;
249     ts2.tv_sec  = 0;
250     ts2.tv_nsec = 0;
251     errno = 0;
252     ret = nanosleep(&ts1, &ts2); /* E.g. in AIX nanosleep() fails and sets errno to ENOSYS. */
253     ret == 0 ? exit(0) : exit(errno ? errno : -1);
254 }
255 EOM
256 }
257
258 sub has_include {
259     my ($inc) = @_;
260     return 1 if
261     try_compile_and_link(<<EOM);
262 #include "EXTERN.h"
263 #include "perl.h"
264 #include "XSUB.h"
265
266 #include <$inc>
267 int main _((int argc, char** argv, char** env))
268 {
269         return 0;
270 }
271 EOM
272     return 0;
273 }
274
275 sub has_clock_x_syscall {
276     my $x = shift;
277     return 0 unless defined $SYSCALL_H;
278     return 1 if
279     try_compile_and_link(<<EOM, run => 1);
280 #include "EXTERN.h"
281 #include "perl.h"
282 #include "XSUB.h"
283 #include <$SYSCALL_H>
284 int main _((int argc, char** argv, char** env))
285 {
286     struct timespec ts;
287     /* Many Linuxes get ENOSYS even though the syscall exists. */
288     /* All implementations are supposed to support CLOCK_REALTIME. */
289     int ret = syscall(SYS_clock_$x, CLOCK_REALTIME, &ts);
290     ret == 0 ? exit(0) : exit(errno ? errno : -1);
291 }
292 EOM
293 }
294
295 sub has_clock_x {
296     my $x = shift;
297     return 1 if
298     try_compile_and_link(<<EOM, run => 1);
299 #include "EXTERN.h"
300 #include "perl.h"
301 #include "XSUB.h"
302 int main _((int argc, char** argv, char** env))
303 {
304     struct timespec ts;
305     int ret = clock_$x(CLOCK_REALTIME, &ts); /* Many Linuxes get ENOSYS. */
306     /* All implementations are supposed to support CLOCK_REALTIME. */
307     ret == 0 ? exit(0) : exit(errno ? errno : -1);
308 }
309 EOM
310 }
311
312 sub init {
313     my $hints = File::Spec->catfile("hints", "$^O.pl");
314     if (-f $hints) {
315         print "Using hints $hints...\n";
316         local $self;
317         do $hints;
318         if (exists $self->{LIBS}) {
319             $LIBS = $self->{LIBS};
320             print "Extra libraries: @$LIBS...\n";
321         }
322     }
323
324     $DEFINE = '';
325
326     if ($Config{d_syscall}) {
327         print "Have syscall(), looking for syscall.h... ";
328         if (has_include('syscall.h')) {
329             $SYSCALL_H = 'syscall.h';
330         } elsif (has_include('sys/syscall.h')) {
331             $SYSCALL_H = 'sys/syscall.h';
332         }
333     }
334
335     if (defined $SYSCALL_H) {
336         print "found <$SYSCALL_H>.\n";
337     } else {
338         print "NOT found.\n";
339     }
340
341     print "Looking for gettimeofday()... ";
342     my $has_gettimeofday;
343     if (exists $Config{d_gettimeod}) {
344         $has_gettimeofday++ if $Config{d_gettimeod};
345     } elsif (has_gettimeofday()) {
346         $DEFINE .= ' -DHAS_GETTIMEOFDAY';
347         $has_gettimeofday++;
348     }
349
350     if ($has_gettimeofday) {
351         print "found.\n";
352     } else {
353         die <<EOD
354 Your operating system does not seem to have the gettimeofday() function.
355 (or, at least, I cannot find it)
356
357 There is no way Time::HiRes is going to work.
358
359 I am awfully sorry but I cannot go further.
360
361 Aborting configuration.
362
363 EOD
364     }
365
366     print "Looking for setitimer()... ";
367     my $has_setitimer;
368     if (exists $Config{d_setitimer}) {
369         $has_setitimer++ if $Config{d_setitimer};
370     } elsif (has_x("setitimer(ITIMER_REAL, 0, 0)")) {
371         $has_setitimer++;
372         $DEFINE .= ' -DHAS_SETITIMER';
373     }
374
375     if ($has_setitimer) {
376         print "found.\n";
377     } else {
378         print "NOT found.\n";
379     }
380
381     print "Looking for getitimer()... ";
382     my $has_getitimer;
383     if (exists $Config{'d_getitimer'}) {
384         $has_getitimer++ if $Config{'d_getitimer'};
385     } elsif (has_x("getitimer(ITIMER_REAL, 0)")) {
386         $has_getitimer++;
387         $DEFINE .= ' -DHAS_GETITIMER';
388     }
389
390     if ($has_getitimer) {
391         print "found.\n";
392     } else {
393         print "NOT found.\n";
394     }
395
396     if ($has_setitimer && $has_getitimer) {
397         print "You have interval timers (both setitimer and getitimer).\n";
398     } else {
399         print "You do not have interval timers.\n";
400     }
401
402     print "Looking for ualarm()... ";
403     my $has_ualarm;
404     if (exists $Config{d_ualarm}) {
405         $has_ualarm++ if $Config{d_ualarm};
406     } elsif (has_x ("ualarm (0, 0)")) {
407         $has_ualarm++;
408         $DEFINE .= ' -DHAS_UALARM';
409     }
410
411     if ($has_ualarm) {
412         print "found.\n";
413     } else {
414         print "NOT found.\n";
415         if ($has_setitimer) {
416             print "But you have setitimer().\n";
417             print "We can make a Time::HiRes::ualarm().\n";
418         }
419     }
420
421     print "Looking for usleep()... ";
422     my $has_usleep;
423     if (exists $Config{d_usleep}) {
424         $has_usleep++ if $Config{d_usleep};
425     } elsif (has_x ("usleep (0)")) {
426         $has_usleep++;
427         $DEFINE .= ' -DHAS_USLEEP';
428     }
429
430     if ($has_usleep) {
431         print "found.\n";
432     } else {
433         print "NOT found.\n";
434         print "Let's see if you have select()... ";
435         if ($Config{'d_select'}) {
436             print "found.\n";
437             print "We can make a Time::HiRes::usleep().\n";
438         } else {
439             print "NOT found.\n";
440             print "You won't have a Time::HiRes::usleep().\n";
441         }
442     }
443
444     print "Looking for nanosleep()... ";
445     my $has_nanosleep;
446     if ($ENV{FORCE_NANOSLEEP_SCAN}) {
447         print "forced scan... ";
448         if (has_nanosleep()) {
449             $has_nanosleep++;
450             $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
451         }
452     }
453     elsif (exists $Config{d_nanosleep}) {
454         print "believing \$Config{d_nanosleep}... ";
455         if ($Config{d_nanosleep}) {
456             $has_nanosleep++;
457             $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
458         }
459     } elsif ($^O =~ /^(mpeix)$/) {
460         # MPE/iX falsely finds nanosleep from its libc equivalent.
461         print "skipping because in $^O... ";
462     } else {
463         if (has_nanosleep()) {
464             $has_nanosleep++;
465             $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
466         }
467     }
468
469     if ($has_nanosleep) {
470         print "found.\n";
471         print "You can mix subsecond sleeps with signals, if you want to.\n";
472         print "(It's still not portable, though.)\n";
473     } else {
474         print "NOT found.\n";
475         my $nt = ($^O eq 'os2' ? '' : 'not');
476         print "You can$nt mix subsecond sleeps with signals.\n";
477         print "(It would not be portable anyway.)\n";
478     }
479
480     print "Looking for clock_gettime()... ";
481     my $has_clock_gettime;
482     if (exists $Config{d_clock_gettime}) {
483         $has_clock_gettime++ if $Config{d_clock_gettime}; # Unlikely...
484     } elsif (has_clock_x('gettime')) {
485         $has_clock_gettime++;
486         $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME';
487     } elsif (defined $SYSCALL_H && has_clock_x_syscall('gettime')) {
488         $has_clock_gettime++;
489         $DEFINE .= ' -DTIME_HIRES_CLOCK_GETTIME -DTIME_HIRES_CLOCK_GETTIME_SYSCALL';
490     }
491
492     if ($has_clock_gettime) {
493         if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETTIME_SYSCALL/) {
494             print "found (via syscall).\n";
495         } else {
496             print "found.\n";
497         }
498     } else {
499         print "NOT found.\n";
500     }
501
502     print "Looking for clock_getres()... ";
503     my $has_clock_getres;
504     if (exists $Config{d_clock_getres}) {
505         $has_clock_getres++ if $Config{d_clock_getres}; # Unlikely...
506     } elsif (has_clock_x('getres')) {
507         $has_clock_getres++;
508         $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES';
509     } elsif (defined $SYSCALL_H && has_clock_x_syscall('getres')) {
510         $has_clock_getres++;
511         $DEFINE .= ' -DTIME_HIRES_CLOCK_GETRES -DTIME_HIRES_CLOCK_GETRES_SYSCALL';
512     }
513
514     if ($has_clock_getres) {
515         if ($DEFINE =~ /-DTIME_HIRES_CLOCK_GETRES_SYSCALL/) {
516             print "found (via syscall).\n";
517         } else {
518             print "found.\n";
519         }
520     } else {
521         print "NOT found.\n";
522     }
523
524     my $has_w32api_windows_h;
525     if ($^O eq 'cygwin') {
526         print "Looking for <w32api/windows.h>... ";
527         if (has_include('w32api/windows.h')) {
528             $has_w32api_windows_h++;
529             $DEFINE .= ' -DHAS_W32API_WINDOWS_H';
530         }
531         if ($has_w32api_windows_h) {
532             print "found.\n";
533         } else {
534             print "NOT found.\n";
535         }
536     }
537
538     if ($DEFINE) {
539         $DEFINE =~ s/^\s+//;
540         if (open(XDEFINE, ">xdefine")) {
541             print XDEFINE $DEFINE, "\n";
542             close(XDEFINE);
543         }
544     }
545 }
546
547 sub doMakefile {
548     my @makefileopts = ();
549
550     if ($] >= 5.005) {
551         push (@makefileopts,
552             'AUTHOR'    => 'Jarkko Hietaniemi <jhi@iki.fi>',
553             'ABSTRACT_FROM' => 'HiRes.pm',
554         );
555         $DEFINE .= " -DATLEASTFIVEOHOHFIVE";
556     }
557
558     push (@makefileopts,
559         'NAME'  => 'Time::HiRes',
560         'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
561         'LIBS'  => $LIBS,   # e.g., '-lm'
562         'DEFINE'        => $DEFINE,     # e.g., '-DHAS_SOMETHING'
563         'XSOPT' => $XSOPT,
564           # Do not even think about 'INC' => '-I/usr/ucbinclude',
565           # Solaris will avenge.
566         'INC'   => '',     # e.g., '-I/usr/include/other'
567         'INSTALLDIRS' => ($] >= 5.008 ? 'perl' : 'site'),
568         'dist'      => {
569             'CI'       => 'ci -l',
570             'COMPRESS' => 'gzip -9f',
571             'SUFFIX'   => 'gz',
572         },
573         clean => { FILES => "xdefine" },
574         realclean => { FILES=> 'const-c.inc const-xs.inc' },
575     );
576
577     if ($ENV{PERL_CORE}) {
578         push @makefileopts, MAN3PODS => {};
579     }
580
581     WriteMakefile(@makefileopts);
582 }
583
584 sub doConstants {
585     if (eval {require ExtUtils::Constant; 1}) {
586         my @names = (qw(CLOCK_HIGHRES CLOCK_MONOTONIC
587                         CLOCK_PROCESS_CPUTIME_ID
588                         CLOCK_REALTIME
589                         CLOCK_THREAD_CPUTIME_ID
590                         CLOCK_TIMEOFDAY
591                         ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF
592                         ITIMER_REALPROF));
593         foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
594                      d_nanosleep d_clock_gettime d_clock_getres)) {
595             my $macro = $_;
596             if ($macro =~ /^(d_nanosleep|d_clock_gettime|d_clock_getres)$/) {
597                 $macro =~ s/^d_(.+)/TIME_HIRES_\U$1/;
598             } else {
599                 $macro =~ s/^d_(.+)/HAS_\U$1/;
600             }
601             push @names, {name => $_, macro => $macro, value => 1,
602                           default => ["IV", "0"]};
603         }
604         ExtUtils::Constant::WriteConstants(
605                                            NAME => 'Time::HiRes',
606                                            NAMES => \@names,
607                                           );
608     } else {
609         my $file;
610         foreach $file ('const-c.inc', 'const-xs.inc') {
611             my $fallback = File::Spec->catfile('fallback', $file);
612             local $/;
613             open IN, "<$fallback" or die "Can't open $fallback: $!";
614             open OUT, ">$file" or die "Can't open $file: $!";
615             print OUT <IN> or die $!;
616             close OUT or die "Can't close $file: $!";
617             close IN or die "Can't close $fallback: $!";
618         }
619     }
620 }
621
622 sub main {
623     print "Configuring Time::HiRes...\n";
624     if ($] == 5.007002) {
625         die "Cannot Configure Time::HiRes for Perl $], aborting.\n";
626     }
627
628     if ($^O =~ /Win32/i) {
629       $DEFINE = '-DSELECT_IS_BROKEN';
630       $LIBS = [];
631     } else {
632       init();
633     }
634     doMakefile;
635     doConstants;
636     my $make = $Config{'make'} || "make";
637     unless (exists $ENV{PERL_CORE} && $ENV{PERL_CORE}) {
638         print  <<EOM;
639 Now you may issue '$make'.  Do not forget also '$make test'.
640 EOM
641        if ((exists $ENV{LC_ALL}   && $ENV{LC_ALL}   =~ /utf-?8/i) ||
642            (exists $ENV{LC_CTYPE} && $ENV{LC_CTYPE} =~ /utf-?8/i) ||
643            (exists $ENV{LANG}     && $ENV{LANG}     =~ /utf-?8/i)) {
644             print  <<EOM;
645 NOTE: if you get an error like this (the Makefile line number may vary):
646 Makefile:91: *** missing separator
647 then set the environment variable LC_ALL to "C" and retry
648 from scratch (re-run perl "Makefile.PL").
649 EOM
650         }
651     }
652 }
653
654 &main;
655
656 # EOF