[perl #22141] patch for Time::HiRes to get rid of -lrt on linux
[p5sagit/p5-mst-13.2.git] / ext / Time / HiRes / Makefile.PL
1
2 # See lib/ExtUtils/MakeMaker.pm for details of how to influence
3 # the contents of the Makefile that is written.
4 #
5
6 require 5.002;
7
8 use Config;
9 use ExtUtils::MakeMaker;
10 use strict;
11
12 my $VERBOSE = $ENV{VERBOSE};
13 my $DEFINE;
14 my $LIBS;
15 my $XSOPT;
16
17 unless($ENV{PERL_CORE}) {
18     $ENV{PERL_CORE} = 1 if grep { $_ eq 'PERL_CORE=1' } @ARGV;
19 }
20
21 # Perls 5.002 and 5.003 did not have File::Spec, fake what we need.
22
23 sub my_dirsep {
24     $^O eq 'VMS' ? '.' :
25         $^O =~ /mswin32|netware|djgpp/i ? '\\' :
26             $^O eq 'MacOS' ? ':'
27                 : '/';
28 }
29
30 sub my_catdir {
31     shift;
32     my $catdir = join(my_dirsep, @_);
33     $^O eq 'VMS' ? "[$catdir]" : $catdir;
34 }
35
36 sub my_catfile {
37     shift;
38     return join(my_dirsep, @_) unless $^O eq 'VMS';
39     my $file = pop;
40     return my_catdir (undef, @_) . $file;
41 }
42
43 sub my_updir {
44     shift;
45     $^O eq 'VMS' ? "-" : "..";
46 }
47
48 BEGIN {
49     eval { require File::Spec };
50     if ($@) {
51         *File::Spec::catdir  = \&my_catdir;
52         *File::Spec::updir   = \&my_updir;
53         *File::Spec::catfile = \&my_catfile;
54     }
55 }
56
57 # Avoid 'used only once' warnings.
58 my $nop1 = *File::Spec::catdir;
59 my $nop2 = *File::Spec::updir;
60 my $nop3 = *File::Spec::catfile;
61
62 # if you have 5.004_03 (and some slightly older versions?), xsubpp
63 # tries to generate line numbers in the C code generated from the .xs.
64 # unfortunately, it is a little buggy around #ifdef'd code.
65 # my choice is leave it in and have people with old perls complain 
66 # about the "Usage" bug, or leave it out and be unable to compile myself
67 # without changing it, and then I'd always forget to change it before a 
68 # release. Sorry, Edward :)
69
70 sub TMPDIR {
71     my $TMPDIR =
72         (grep(defined $_ && -d $_ && -w _,
73               ((defined $ENV{'TMPDIR'} ? $ENV{'TMPDIR'} : undef),
74                qw(/var/tmp /usr/tmp /tmp c:/temp))))[0];
75     $TMPDIR || die "Cannot find writable temporary directory.\n";
76 }
77
78 sub try_compile_and_link {
79     my ($c, %args) = @_;
80
81     my ($ok) = 0;
82     my ($tmp) = (($^O eq 'VMS') ? "sys\$scratch:tmp$$" : TMPDIR() . '/' . "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 hierachy 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             printf "cccmd = $cccmd\n" if $VERBOSE;
143             system($cccmd);
144             $ok = -s $tmp && -x _;
145             unlink("$tmp.c", $tmp);
146         }
147     }
148     
149     $ok;
150 }
151
152 sub has_gettimeofday {
153     # confusing but true (if condition true ==> -DHAS_GETTIMEOFDAY already)
154     return 0 if $Config{'d_gettimeod'} eq 'define';
155     return 1 if try_compile_and_link(<<EOM);
156 #include "EXTERN.h" 
157 #include "perl.h" 
158 #include "XSUB.h" 
159 #ifdef I_SYS_TYPES 
160 #   include <sys/types.h>
161 #endif
162
163 #ifdef I_SYS_TIME
164 #   include <sys/time.h>
165 #endif
166
167 #ifdef I_SYS_SELECT
168 #   include <sys/select.h>      /* struct timeval might be hidden in here */
169 #endif
170 static int foo()
171 {
172     struct timeval tv;
173     gettimeofday(&tv, 0);
174 }
175 int main _((int argc, char** argv, char** env))
176 {
177     foo();
178 }
179 EOM
180     return 0;
181 }
182
183 sub has_x {
184     my ($x, %args) = @_;
185
186     return 1 if
187     try_compile_and_link(<<EOM, %args);
188 #include "EXTERN.h"
189 #include "perl.h"
190 #include "XSUB.h"
191
192 #ifdef I_UNISTD
193 #   include <unistd.h>
194 #endif
195
196 #ifdef I_SYS_TYPES
197 #   include <sys/types.h>
198 #endif
199
200 #ifdef I_SYS_TIME
201 #   include <sys/time.h>
202 #endif
203
204 int main _((int argc, char** argv, char** env))
205 {
206         $x;
207 }
208 EOM
209     return 0;
210 }
211
212 sub unixinit {
213     $DEFINE = '';
214
215     $LIBS = [];
216
217     # this might break the link, try it if it can't find some things you 
218     # honestly think should be in there...
219     # $LIBS = ['-lucb -lbsd'];
220
221     # ... but ucb is poison for Solaris, and probably Linux. honest.
222     $LIBS = [] if $Config{'osname'} eq 'solaris';
223     $LIBS = [] if $Config{'osname'} eq 'linux';
224     $LIBS = ['-lm'] if $Config{'osname'} =~ /sco/i;
225     $LIBS = ['-lc'] if $Config{'osname'} =~ /dynixptx/i;
226
227     # For nanosleep
228     push @$LIBS, '-lrt' unless $Config{'osname'} =~ /^(?:irix|linux)$/;
229     push @$LIBS, '-lposix4';
230
231     my @goodlibs;
232
233     select(STDOUT);
234     $| = 1;
235
236     print "Checking for libraries...\n";
237     my $lib;
238     for $lib (@$LIBS) {
239         print "Checking for $lib... ";
240         $LIBS = [ $lib ];
241         if ($Config{libs} =~ /\b$lib\b/ || has_x("time(0)")) {
242             push @goodlibs, $lib;
243             print "found.\n";
244         } else {
245             print "NOT found.\n";
246         }
247     }
248     $LIBS = [ @goodlibs ];
249     print @$LIBS ?
250           "You have extra libraries: @$LIBS.\n" :
251           "You have no applicable extra libraries.\n";
252
253     print "Looking for gettimeofday()... ";
254     my $has_gettimeofday;
255     if ($Config{'d_gettimeod'}) {
256         $has_gettimeofday++;
257     } elsif (has_gettimeofday()) {
258         $DEFINE .= ' -DHAS_GETTIMEOFDAY';
259         $has_gettimeofday++;
260     }
261
262     if ($has_gettimeofday) {
263         print "found.\n";
264     } else {
265         die <<EOD
266 Your operating system does not seem to have the gettimeofday() function.
267 (or, at least, I cannot find it)
268
269 There is no way Time::HiRes is going to work.
270
271 I am awfully sorry but I cannot go further.
272
273 Aborting configuration.
274
275 EOD
276     }
277
278     print "Looking for setitimer()... ";
279     my $has_setitimer;
280     if ($Config{d_setitimer}) {
281         $has_setitimer++;
282     } elsif (has_x("setitimer(ITIMER_REAL, 0, 0)")) {
283         $has_setitimer++;
284         $DEFINE .= ' -DHAS_SETITIMER';
285     }
286
287     if ($has_setitimer) {
288         print "found.\n";
289     } else {
290         print "NOT found.\n";
291     }
292
293     print "Looking for getitimer()... ";
294     my $has_getitimer;
295     if ($Config{d_getitimer}) {
296         $has_getitimer++;
297     } elsif (has_x("getitimer(ITIMER_REAL, 0)")) {
298         $has_getitimer++;
299         $DEFINE .= ' -DHAS_GETITIMER';
300     }
301
302     if ($has_getitimer) {
303         print "found.\n";
304     } else {
305         print "NOT found.\n";
306     }
307
308     if ($has_setitimer && $has_getitimer) {
309         print "You have interval timers (both setitimer and setitimer).\n";
310     } else {
311         print "You do not have interval timers.\n";
312     }
313
314     print "Looking for ualarm()... ";
315     my $has_ualarm;
316     if ($Config{d_ualarm}) {
317         $has_ualarm++;
318     } elsif (has_x ("ualarm (0, 0)")) {
319         $has_ualarm++;
320         $DEFINE .= ' -DHAS_UALARM';
321     }
322
323     if ($has_ualarm) {
324         print "found.\n";
325     } else {
326         print "NOT found.\n";
327         if ($has_setitimer) {
328             print "But you have setitimer().\n";
329             print "We can make a Time::HiRes::ualarm().\n";
330         }
331     }
332
333     print "Looking for usleep()... ";
334     my $has_usleep;
335     if ($Config{d_usleep}) {
336         $has_usleep++;
337     } elsif (has_x ("usleep (0)")) {
338         $has_usleep++;
339         $DEFINE .= ' -DHAS_USLEEP';
340     }
341
342     if ($has_usleep) {
343         print "found.\n";
344     } else {
345         print "NOT found.\n";
346         print "Let's see if you have select()... ";
347         if ($Config{'d_select'} eq 'define') {
348             print "found.\n";
349             print "We can make a Time::HiRes::usleep().\n";
350         } else {
351             print "NOT found.\n";
352             print "You won't have a Time::HiRes::usleep().\n";
353         }
354     }
355
356     print "Looking for nanosleep()... ";
357     my $has_nanosleep;
358     if ($Config{d_nanosleep}) {
359         $has_nanosleep++;
360     } elsif (has_x ("nanosleep (NULL, NULL)")) {
361         $has_nanosleep++;
362         $DEFINE .= ' -DHAS_NANOSLEEP';
363     }
364
365     if ($has_nanosleep) {
366         print "found.\n";
367         print "You can mix subsecond sleeps with signals.\n";
368     } else {
369         print "NOT found.\n";
370         print "You cannot mix subsecond sleeps with signals.\n";
371     }
372
373     if ($DEFINE) {
374         $DEFINE =~ s/^\s+//;
375         if (open(XDEFINE, ">xdefine")) {
376             print XDEFINE $DEFINE, "\n";
377             close(XDEFINE);
378         }
379     }
380 }
381
382 sub doMakefile {
383     my @makefileopts = ();
384
385     if ($] >= 5.005) {
386         push (@makefileopts,
387             'AUTHOR'    => 'Jarkko Hietaniemi <jhi@iki.fi>',
388             'ABSTRACT_FROM' => 'HiRes.pm',
389         );
390         $DEFINE .= " -DATLEASTFIVEOHOHFIVE";
391     }
392
393     push (@makefileopts,
394         'NAME'  => 'Time::HiRes',
395         'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
396         'LIBS'  => $LIBS,   # e.g., '-lm' 
397         'DEFINE'        => $DEFINE,     # e.g., '-DHAS_SOMETHING' 
398         'XSOPT' => $XSOPT,
399     # do not even think about 'INC' => '-I/usr/ucbinclude', Solaris will avenge.
400         'INC'   => '',     # e.g., '-I/usr/include/other' 
401         'INSTALLDIRS' => 'perl',
402         'dist'      => {
403             'CI'       => 'ci -l',
404             'COMPRESS' => 'gzip -9f', 
405             'SUFFIX'   => 'gz',
406         },
407         clean => { FILES => "xdefine" },
408         realclean => {FILES=> 'const-c.inc const-xs.inc'},
409     );
410
411     if ($ENV{PERL_CORE}) {
412         push @makefileopts, MAN3PODS => {};
413     }
414
415     WriteMakefile(@makefileopts);
416 }
417
418 sub doConstants {
419     if (eval {require ExtUtils::Constant; 1}) {
420         my @names = (qw(ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF
421                         ITIMER_REALPROF));
422         foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
423                      d_nanosleep)) {
424             my $macro = $_;
425             $macro =~ s/d_(.*)/HAS_\U$1/;
426             push @names, {name => $_, macro => $macro, value => 1,
427                           default => ["IV", "0"]};
428         }
429         ExtUtils::Constant::WriteConstants(
430                                            NAME => 'Time::HiRes',
431                                            NAMES => \@names,
432                                           );
433     } else {
434         foreach my $file ('const-c.inc', 'const-xs.inc') {
435             my $fallback = File::Spec->catfile('fallback', $file);
436             local $/;
437             open IN, "<$fallback" or die "Can't open $fallback: $!";
438             open OUT, ">$file" or die "Can't open $file: $!";
439             print OUT <IN> or die $!;
440             close OUT or die "Can't close $file: $!";
441             close IN or die "Can't close $fallback: $!";
442         }
443     }
444 }
445
446 sub main {
447     print "Configuring Time::HiRes...\n";
448
449     if ($^O =~ /Win32/i) {
450       $DEFINE = '-DSELECT_IS_BROKEN';
451       $LIBS = [''];
452     } else {
453       unixinit();
454     }
455     doMakefile;
456     doConstants;
457     my $make = $Config{'make'} || "make";
458     unless ($ENV{PERL_CORE}) {
459         print  <<EOM;
460 Now you may issue '$make'.  Do not forget also '$make test'.
461 EOM
462     }
463 }
464
465 &main;
466
467 # EOF