This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
edc42de67b506e28e244b4f8a436c36ab7eb22f9
[perl5.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
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 "testing... ";
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 ($ENV{FORCE_NANOSLEEP_SCAN}) {
387         print "forced scan... ";
388         if (has_nanosleep()) {
389             $has_nanosleep++;
390             $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
391         }
392     }
393     elsif (exists $Config{d_nanosleep}) {
394         print "believing \$Config{d_nanosleep}... ";
395         if ($Config{d_nanosleep}) {
396             $has_nanosleep++;
397             $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
398         }
399     } elsif ($^O =~ /^(mpeix)$/) {
400         # MPE/iX falsely finds nanosleep from its libc equivalent.
401         print "skipping because in $^O... ";
402     } else {
403         if (has_nanosleep()) {
404             $has_nanosleep++;
405             $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
406         }
407     }
408
409     if ($has_nanosleep) {
410         print "found.\n";
411         print "You can mix subsecond sleeps with signals, if you want to.\n";
412         print "(It's still not portable, though.)\n";
413     } else {
414         print "NOT found.\n";
415         my $nt = ($^O eq 'os2' ? '' : 'not');
416         print "You can$nt mix subsecond sleeps with signals.\n";
417         print "(It would not be portable anyway.)\n";
418     }
419
420     my $has_w32api_windows_h;
421     if ($^O eq 'cygwin') {
422         print "Looking for <w32api/windows.h>... ";
423         if (has_include('w32api/windows.h')) {
424             $has_w32api_windows_h++;
425             $DEFINE .= ' -DHAS_W32API_WINDOWS_H';
426         }
427         if ($has_w32api_windows_h) {
428             print "found.\n";
429         } else {
430             print "NOT found.\n";
431         }
432     }
433
434     if ($DEFINE) {
435         $DEFINE =~ s/^\s+//;
436         if (open(XDEFINE, ">xdefine")) {
437             print XDEFINE $DEFINE, "\n";
438             close(XDEFINE);
439         }
440     }
441 }
442
443 sub doMakefile {
444     my @makefileopts = ();
445
446     if ($] >= 5.005) {
447         push (@makefileopts,
448             'AUTHOR'    => 'Jarkko Hietaniemi <jhi@iki.fi>',
449             'ABSTRACT_FROM' => 'HiRes.pm',
450         );
451         $DEFINE .= " -DATLEASTFIVEOHOHFIVE";
452     }
453
454     push (@makefileopts,
455         'NAME'  => 'Time::HiRes',
456         'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
457         'LIBS'  => $LIBS,   # e.g., '-lm'
458         'DEFINE'        => $DEFINE,     # e.g., '-DHAS_SOMETHING'
459         'XSOPT' => $XSOPT,
460     # do not even think about 'INC' => '-I/usr/ucbinclude', Solaris will avenge.
461         'INC'   => '',     # e.g., '-I/usr/include/other'
462         'INSTALLDIRS' => ($] >= 5.008 ? 'perl' : 'site'),
463         'dist'      => {
464             'CI'       => 'ci -l',
465             'COMPRESS' => 'gzip -9f',
466             'SUFFIX'   => 'gz',
467         },
468         clean => { FILES => "xdefine" },
469         realclean => { FILES=> 'const-c.inc const-xs.inc' },
470     );
471
472     if ($ENV{PERL_CORE}) {
473         push @makefileopts, MAN3PODS => {};
474     }
475
476     WriteMakefile(@makefileopts);
477 }
478
479 sub doConstants {
480     if (eval {require ExtUtils::Constant; 1}) {
481         my @names = (qw(ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF
482                         ITIMER_REALPROF));
483         foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
484                      d_nanosleep)) {
485             my $macro = $_;
486             if ($macro eq 'd_nanosleep') {
487                 $macro =~ s/d_(.*)/TIME_HIRES_\U$1/;
488             } else {
489                 $macro =~ s/d_(.*)/HAS_\U$1/;
490             }
491             push @names, {name => $_, macro => $macro, value => 1,
492                           default => ["IV", "0"]};
493         }
494         ExtUtils::Constant::WriteConstants(
495                                            NAME => 'Time::HiRes',
496                                            NAMES => \@names,
497                                           );
498     } else {
499         my $file;
500         foreach $file ('const-c.inc', 'const-xs.inc') {
501             my $fallback = File::Spec->catfile('fallback', $file);
502             local $/;
503             open IN, "<$fallback" or die "Can't open $fallback: $!";
504             open OUT, ">$file" or die "Can't open $file: $!";
505             print OUT <IN> or die $!;
506             close OUT or die "Can't close $file: $!";
507             close IN or die "Can't close $fallback: $!";
508         }
509     }
510 }
511
512 sub main {
513     print "Configuring Time::HiRes...\n";
514     if ($] == 5.007002) {
515         die "Cannot Configure Time::HiRes for Perl $], aborting.\n";
516     }
517
518     if ($^O =~ /Win32/i) {
519       $DEFINE = '-DSELECT_IS_BROKEN';
520       $LIBS = [];
521     } else {
522       init();
523     }
524     doMakefile;
525     doConstants;
526     my $make = $Config{'make'} || "make";
527     unless (exists $ENV{PERL_CORE} && $ENV{PERL_CORE}) {
528         print  <<EOM;
529 Now you may issue '$make'.  Do not forget also '$make test'.
530 EOM
531        if ((exists $ENV{LC_ALL}   && $ENV{LC_ALL}   =~ /utf-?8/i) ||
532            (exists $ENV{LC_CTYPE} && $ENV{LC_CTYPE} =~ /utf-?8/i) ||
533            (exists $ENV{LANG}     && $ENV{LANG}     =~ /utf-?8/i)) {
534             print  <<EOM;
535 NOTE: if you get an error like this (the line number may vary):
536 Makefile:91: *** missing separator
537 then set the environment variable LC_ALL to "C" and retry
538 from scratch (re-run perl "Makefile.PL").
539 EOM
540         }
541     }
542 }
543
544 &main;
545
546 # EOF