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