Fix long standing memory leak with pop and shift!
[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
11 # Perls 5.002 and 5.003 did not have File::Spec, fake what we need.
12
13 my $VERBOSE = $ENV{VERBOSE};
14
15 sub my_dirsep {
16     $^O eq 'VMS' ? '.' :
17         $^O =~ /mswin32|netware|djgpp/i ? '\\' :
18             $^O eq 'MacOS' ? ':'
19                 : '/';
20 }
21
22 sub my_catdir {
23     shift;
24     my $catdir = join(my_dirsep, @_);
25     $^O eq 'VMS' ? "[$dirsep]" : $dirsep;
26 }
27
28 sub my_updir {
29     shift;
30     $^O eq 'VMS' ? "-" : "..";
31 }
32
33 BEGIN {
34     eval { require File::Spec };
35     if ($@) {
36         *File::Spec::catdir = \&my_catdir;
37         *File::Spec::updir  = \&my_updir;
38     }
39 }
40
41 # if you have 5.004_03 (and some slightly older versions?), xsubpp
42 # tries to generate line numbers in the C code generated from the .xs.
43 # unfortunately, it is a little buggy around #ifdef'd code.
44 # my choice is leave it in and have people with old perls complain 
45 # about the "Usage" bug, or leave it out and be unable to compile myself
46 # without changing it, and then I'd always forget to change it before a 
47 # release. Sorry, Edward :)
48
49 sub TMPDIR {
50     my $TMPDIR =
51         (grep(defined $_ && -d $_ && -w _,
52               ((defined $ENV{'TMPDIR'} ? $ENV{'TMPDIR'} : undef),
53                qw(/var/tmp /usr/tmp /tmp))))[0]
54                    unless defined $TMPDIR;
55     $TMPDIR || die "Cannot find writable temporary directory.\n";
56 }
57
58 sub try_compile_and_link {
59     my ($c, %args) = @_;
60
61     my ($ok) = 0;
62     my ($tmp) = (($^O eq 'VMS') ? "sys\$scratch:tmp$$" : TMPDIR . '/' . "tmp$$");
63     local(*TMPC);
64
65     my $obj_ext = $Config{obj_ext} || ".o";
66     unlink("$tmp.c", "$tmp$obj_ext");
67
68     if (open(TMPC, ">$tmp.c")) {
69         print TMPC $c;
70         close(TMPC);
71
72         $cccmd = $args{cccmd};
73
74         my $errornull;
75
76         my $COREincdir;
77         if ($ENV{PERL_CORE}) {
78             my $updir = File::Spec->updir;
79             $COREincdir = File::Spec->catdir(($updir) x 3);
80         } else {
81             $COREincdir = File::Spec->catdir($Config{'archlibexp'}, 'CORE');
82         }
83         my $ccflags = $Config{'ccflags'} . ' ' . "-I$COREincdir";
84         if ($^O eq 'VMS') {
85             if ($ENV{PERL_CORE}) {
86                 # Fragile if the extensions change hierachy within
87                 # the Perl core but this should do for now.
88                 $cccmd = "$Config{'cc'} /include=([---]) $tmp.c";
89             } else {
90                 my $perl_core = $Config{'installarchlib'};
91                 $perl_core =~ s/\]$/.CORE]/;
92                 $cccmd = "$Config{'cc'} /include=(perl_root:[000000],$perl_core) $tmp.c"; 
93             }
94         }
95
96         if ($args{silent} || !$VERBOSE) {
97             $errornull = "2>/dev/null" unless defined $errornull;
98         } else {
99             $errornull = '';
100         }
101
102         $cccmd = "$Config{'cc'} -o $tmp $ccflags $tmp.c @$LIBS $errornull"
103             unless defined $cccmd;
104         if ($^O eq 'VMS') {
105             open( CMDFILE, ">$tmp.com" );
106             print CMDFILE "\$ SET MESSAGE/NOFACILITY/NOSEVERITY/NOIDENT/NOTEXT\n";
107             print CMDFILE "\$ $cccmd\n";
108             print CMDFILE "\$ IF \$SEVERITY .NE. 1 THEN EXIT 44\n";  # escalate
109             close CMDFILE;
110             system("\@ $tmp.com");
111             $ok = $?==0;
112             for ("$tmp.c", "$tmp$obj_ext", "$tmp.com", "$tmp$Config{exe_ext}") { 
113                 1 while unlink $_; 
114             }
115         }
116         else
117         {
118             printf "cccmd = $cccmd\n" if $VERBOSE;
119             system($cccmd);
120             $ok = -s $tmp && -x _;
121             unlink("$tmp.c", $tmp);
122         }
123     }
124     
125     $ok;
126 }
127
128 sub has_gettimeofday {
129     # confusing but true (if condition true ==> -DHAS_GETTIMEOFDAY already)
130     return 0 if $Config{'d_gettimeod'} eq 'define';
131     return 1 if try_compile_and_link(<<EOM); 
132 #include "EXTERN.h" 
133 #include "perl.h" 
134 #include "XSUB.h" 
135 #ifdef I_SYS_TYPES 
136 #   include <sys/types.h>
137 #endif
138
139 #ifdef I_SYS_TIME
140 #   include <sys/time.h>
141 #endif
142
143 #ifdef I_SYS_SELECT
144 #   include <sys/select.h>      /* struct timeval might be hidden in here */
145 #endif
146 static int foo()
147 {
148     struct timeval tv;
149     gettimeofday(&tv, 0);
150 }
151 int main _((int argc, char** argv, char** env))
152 {
153     foo();
154 }
155 EOM
156     return 0;
157 }
158
159 sub has_x {
160     my ($x, %args) = @_; 
161
162     return 1 if
163     try_compile_and_link(<<EOM, %args);
164 #include "EXTERN.h"
165 #include "perl.h"
166 #include "XSUB.h"
167
168 #ifdef I_UNISTD
169 #   include <unistd.h>
170 #endif
171
172 #ifdef I_SYS_TYPES
173 #   include <sys/types.h>
174 #endif
175
176 #ifdef I_SYS_TIME
177 #   include <sys/time.h>
178 #endif
179
180 int main _((int argc, char** argv, char** env))
181 {
182         $x;
183 }
184 EOM
185     return 0;
186 }
187
188 sub unixinit {
189     $DEFINE = '';
190
191     $LIBS = [];
192
193     # this might break the link, try it if it can't find some things you 
194     # honestly think should be in there...
195     # $LIBS = ['-lucb -lbsd'];
196
197     # ... but ucb is poison for Solaris, and probably Linux. honest.
198     $LIBS = [] if $Config{'osname'} eq 'solaris';
199     $LIBS = [] if $Config{'osname'} eq 'linux';
200     $LIBS = ['-lm'] if $Config{'osname'} =~ /sco/i;
201     $LIBS = ['-lc'] if $Config{'osname'} =~ /dynixptx/i;
202
203     # For nanosleep
204     push @$LIBS, '-lrt'         unless $Config{'osname'} =~ /irix/;
205     push @$LIBS, '-lposix4'     ;
206
207     my @goodlibs;
208
209     select(STDOUT); $| = 1;
210
211     print "Checking for libraries...\n";
212     my $lib;
213     for $lib (@$LIBS) {
214         print "Checking for $lib...\n";
215         $LIBS = [ $lib ];
216         if ($Config{libs} =~ /\b$lib\b/ || has_x("time(0)")) {
217             push @goodlibs, $lib;
218         }
219     }
220     @$LIBS = @goodlibs;
221     print @$LIBS ?
222           "You have extra libraries: @$LIBS.\n" :
223           "You have no applicable extra libraries.\n";
224     print "\n";
225
226     print "Looking for gettimeofday()...\n";
227     my $has_gettimeofday;
228     if ($Config{'d_gettimeod'}) {
229         $has_gettimeofday++;
230     } elsif (has_gettimeofday()) {
231         $DEFINE .= ' -DHAS_GETTIMEOFDAY';
232         $has_gettimeofday++;
233     }
234
235     if ($has_gettimeofday) {
236         print "You have gettimeofday().\n\n";
237     } else {
238         die <<EOD
239 Your operating system does not seem to have the gettimeofday() function.
240 (or, at least, I cannot find it)
241
242 There is no way Time::HiRes is going to work.
243
244 I am awfully sorry but I cannot go further.
245
246 Aborting configuration.
247
248 EOD
249     }
250
251     print "Looking for setitimer()...\n";
252     my $has_setitimer;
253     if ($Config{d_setitimer}) {
254         $has_setitimer++;
255     } elsif (has_x("setitimer(ITIMER_REAL, 0, 0)")) {
256         $has_setitimer++;
257         $DEFINE .= ' -DHAS_SETITIMER';
258     }
259
260     if ($has_setitimer) {
261         print "You have setitimer().\n\n";
262     } else {
263         print "No setitimer().\n\n";
264     }
265
266     print "Looking for getitimer()...\n";
267     my $has_getitimer;
268     if ($Config{d_getitimer}) {
269         $has_getitimer++;
270     } elsif (has_x("getitimer(ITIMER_REAL, 0)")) {
271         $has_getitimer++;
272         $DEFINE .= ' -DHAS_GETITIMER';
273     }
274
275     if ($has_getitimer) {
276         print "You have getitimer().\n\n";
277     } else {
278         print "No getitimer().\n\n";
279     }
280
281     if ($has_setitimer && $has_getitimer) {
282         print "You have interval timers (both setitimer and setitimer).\n\n";
283     } else {
284         print "You do not have interval timers.\n\n";
285     }
286
287     print "Looking for ualarm()...\n";
288     my $has_ualarm; 
289     if ($Config{d_ualarm}) {
290         $has_ualarm++;
291     } elsif (has_x ("ualarm (0, 0)")) {
292         $has_ualarm++;
293         $DEFINE .= ' -DHAS_UALARM';
294     }
295
296     if ($has_ualarm) {
297         print "You have ualarm().\n\n";
298     } else {
299         print "Whoops! No ualarm()!\n";
300         if ($setitimer) {
301             print "You have setitimer(); we can make a Time::HiRes::ualarm()\n\n";
302         } else {
303              print "We'll manage.\n\n";
304         }
305     }
306
307     print "Looking for usleep()...\n";
308     my $has_usleep;
309     if ($Config{d_usleep}) {
310         $has_usleep++;
311     } elsif (has_x ("usleep (0)")) {
312         $has_usleep++;
313         $DEFINE .= ' -DHAS_USLEEP';
314     }
315
316     if ($has_usleep) {
317         print "You have usleep().\n\n";
318     } else {
319         print "Whoops! No usleep()! Let's see if you have select().\n";
320         if ($Config{'d_select'} eq 'define') {
321             print "You have select(); we can make a Time::HiRes::usleep()\n\n";
322         } else {
323             print "No select(); you won't have a Time::HiRes::usleep()\n\n";
324         }
325     }
326
327     print "Looking for nanosleep()...\n";
328     my $has_nanosleep;
329     if ($Config{d_nanosleep}) {
330         $has_nanosleep++;
331     } elsif (has_x ("nanosleep (NULL, NULL)")) {
332         $has_nanosleep++;
333         $DEFINE .= ' -DHAS_NANOSLEEP';
334     }
335
336     if ($has_nanosleep) {
337         print "You have nanosleep().  You can mix subsecond sleeps with signals.\n\n";
338     } else {
339         print "Whoops! No nanosleep()!  You cannot mix subsecond sleeps with signals.\n";
340     }
341
342     if ($DEFINE) {
343         $DEFINE =~ s/^\s+//;
344         if (open(XDEFINE, ">xdefine")) {
345             print XDEFINE $DEFINE, "\n";
346             close(XDEFINE);
347         }
348     }
349 }
350
351 sub doMakefile {
352     @makefileopts = ();
353
354     if ($] >= 5.005) {
355         push (@makefileopts,
356             'AUTHOR'    => 'Jarkko Hietaniemi <jhi@iki.fi>',
357             'ABSTRACT_FROM' => 'HiRes.pm',
358         );
359         $DEFINE .= " -DATLEASTFIVEOHOHFIVE";
360     }
361
362     push (@makefileopts,
363         'NAME'  => 'Time::HiRes',
364         'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
365         'LIBS'  => $LIBS,   # e.g., '-lm' 
366         'DEFINE'        => $DEFINE,     # e.g., '-DHAS_SOMETHING' 
367         'XSOPT' => $XSOPT,
368     # do not even think about 'INC' => '-I/usr/ucbinclude', Solaris will avenge.
369         'INC'   => '',     # e.g., '-I/usr/include/other' 
370         'INSTALLDIRS' => 'perl',
371         'dist'      => {
372             'CI'       => 'ci -l',
373             'COMPRESS' => 'gzip -9f', 
374             'SUFFIX'   => 'gz',
375         },
376         clean => { FILES => "xdefine" },
377     );
378
379     WriteMakefile(@makefileopts);
380 }
381
382 sub main {
383     print <<EOM;
384
385 Configuring Time::HiRes...
386
387 EOM
388
389     if ($^O =~ /Win32/i) {
390       $DEFINE = '-DSELECT_IS_BROKEN';
391       $LIBS = [''];
392     } else {
393       unixinit();
394     }
395     configure;
396     doMakefile;
397     my $make = $Config{'make'} || "make";
398     unless ($ENV{PERL_CORE}) {
399         print  <<EOM;
400
401 Done configuring.
402
403 Now you may issue '$make'.  Do not forget also '$make test'.
404
405 EOM
406     }
407 }
408
409 &main;
410
411 # EOF