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