Good catch from Gisle.
[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
74sub TMPDIR {
75 my $TMPDIR =
76 (grep(defined $_ && -d $_ && -w _,
77 ((defined $ENV{'TMPDIR'} ? $ENV{'TMPDIR'} : undef),
a0c8e3cf 78 qw(/var/tmp /usr/tmp /tmp c:/temp))))[0];
3f2ee006 79 $TMPDIR || die "Cannot find writable temporary directory.\n";
80}
81
82sub try_compile_and_link {
83 my ($c, %args) = @_;
84
85 my ($ok) = 0;
98b50af3 86 my ($tmp) = (($^O eq 'VMS') ? "sys\$scratch:tmp$$" : TMPDIR() . '/' . "tmp$$");
3f2ee006 87 local(*TMPC);
88
89 my $obj_ext = $Config{obj_ext} || ".o";
90 unlink("$tmp.c", "$tmp$obj_ext");
91
92 if (open(TMPC, ">$tmp.c")) {
93 print TMPC $c;
94 close(TMPC);
95
98b50af3 96 my $cccmd = $args{cccmd};
3f2ee006 97
98 my $errornull;
99
100 my $COREincdir;
98b50af3 101
3f2ee006 102 if ($ENV{PERL_CORE}) {
103 my $updir = File::Spec->updir;
104 $COREincdir = File::Spec->catdir(($updir) x 3);
105 } else {
106 $COREincdir = File::Spec->catdir($Config{'archlibexp'}, 'CORE');
107 }
98b50af3 108
3f2ee006 109 my $ccflags = $Config{'ccflags'} . ' ' . "-I$COREincdir";
98b50af3 110
3f2ee006 111 if ($^O eq 'VMS') {
112 if ($ENV{PERL_CORE}) {
6a20eacc 113 # Fragile if the extensions change hierachy within
114 # the Perl core but this should do for now.
115 $cccmd = "$Config{'cc'} /include=([---]) $tmp.c";
3f2ee006 116 } else {
117 my $perl_core = $Config{'installarchlib'};
118 $perl_core =~ s/\]$/.CORE]/;
98b50af3 119 $cccmd = "$Config{'cc'} /include=(perl_root:[000000],$perl_core) $tmp.c";
3f2ee006 120 }
121 }
122
123 if ($args{silent} || !$VERBOSE) {
124 $errornull = "2>/dev/null" unless defined $errornull;
125 } else {
126 $errornull = '';
127 }
128
98b50af3 129 $cccmd = "$Config{'cc'} -o $tmp $ccflags $tmp.c @$LIBS $errornull"
3f2ee006 130 unless defined $cccmd;
98b50af3 131
5e8fe3da 132 if ($^O eq 'mpeix') {
133 $cccmd = 'MPEAUTOCONF=1 '.$cccmd;
5d899d7e 134 } elsif ($^O eq 'VMS') {
3f2ee006 135 open( CMDFILE, ">$tmp.com" );
136 print CMDFILE "\$ SET MESSAGE/NOFACILITY/NOSEVERITY/NOIDENT/NOTEXT\n";
137 print CMDFILE "\$ $cccmd\n";
98b50af3 138 print CMDFILE "\$ IF \$SEVERITY .NE. 1 THEN EXIT 44\n"; # escalate
3f2ee006 139 close CMDFILE;
140 system("\@ $tmp.com");
141 $ok = $?==0;
5d899d7e 142 for ("$tmp.c", "$tmp$obj_ext", "$tmp.com", "$tmp$Config{exe_ext}") {
98b50af3 143 1 while unlink $_;
3f2ee006 144 }
145 }
146 else
147 {
622913ab 148 my $tmp_exe = "$tmp$ld_exeext";
3f2ee006 149 printf "cccmd = $cccmd\n" if $VERBOSE;
622913ab 150 my $res = system($cccmd);
151 $ok = defined($res) && $res==0 && -s $tmp_exe && -x _;
152 unlink("$tmp.c", $tmp_exe);
3f2ee006 153 }
154 }
5d899d7e 155
3f2ee006 156 $ok;
157}
158
159sub has_gettimeofday {
160 # confusing but true (if condition true ==> -DHAS_GETTIMEOFDAY already)
5d899d7e 161 return 0 if $Config{d_gettimeod} eq 'define';
98b50af3 162 return 1 if try_compile_and_link(<<EOM);
5d899d7e 163#include "EXTERN.h"
164#include "perl.h"
165#include "XSUB.h"
166#ifdef I_SYS_TYPES
3f2ee006 167# include <sys/types.h>
168#endif
169
170#ifdef I_SYS_TIME
171# include <sys/time.h>
172#endif
173
174#ifdef I_SYS_SELECT
175# include <sys/select.h> /* struct timeval might be hidden in here */
176#endif
177static int foo()
178{
179 struct timeval tv;
180 gettimeofday(&tv, 0);
181}
182int main _((int argc, char** argv, char** env))
183{
184 foo();
185}
186EOM
187 return 0;
188}
189
190sub has_x {
98b50af3 191 my ($x, %args) = @_;
3f2ee006 192
193 return 1 if
194 try_compile_and_link(<<EOM, %args);
195#include "EXTERN.h"
196#include "perl.h"
197#include "XSUB.h"
198
199#ifdef I_UNISTD
200# include <unistd.h>
201#endif
202
203#ifdef I_SYS_TYPES
204# include <sys/types.h>
205#endif
206
207#ifdef I_SYS_TIME
208# include <sys/time.h>
209#endif
210
211int main _((int argc, char** argv, char** env))
212{
213 $x;
214}
215EOM
216 return 0;
217}
218
046e3f33 219sub init {
220 my $hints = File::Spec->catfile("hints", "$^O.pl");
221 if (-f $hints) {
222 print "Using hints $hints...\n";
223 local $self;
224 do $hints;
225 if (exists $self->{LIBS}) {
226 $LIBS = $self->{LIBS};
227 print "Extra libraries: @$LIBS...\n";
3f2ee006 228 }
229 }
046e3f33 230
231 $DEFINE = '';
3f2ee006 232
98b50af3 233 print "Looking for gettimeofday()... ";
3f2ee006 234 my $has_gettimeofday;
5d899d7e 235 if (exists $Config{d_gettimeod}) {
236 $has_gettimeofday++ if $Config{d_gettimeod};
3f2ee006 237 } elsif (has_gettimeofday()) {
238 $DEFINE .= ' -DHAS_GETTIMEOFDAY';
239 $has_gettimeofday++;
240 }
241
242 if ($has_gettimeofday) {
98b50af3 243 print "found.\n";
3f2ee006 244 } else {
245 die <<EOD
246Your operating system does not seem to have the gettimeofday() function.
247(or, at least, I cannot find it)
248
249There is no way Time::HiRes is going to work.
250
251I am awfully sorry but I cannot go further.
252
253Aborting configuration.
254
255EOD
256 }
257
98b50af3 258 print "Looking for setitimer()... ";
3f2ee006 259 my $has_setitimer;
5d899d7e 260 if (exists $Config{d_setitimer}) {
261 $has_setitimer++ if $Config{d_setitimer};
3f2ee006 262 } elsif (has_x("setitimer(ITIMER_REAL, 0, 0)")) {
263 $has_setitimer++;
264 $DEFINE .= ' -DHAS_SETITIMER';
265 }
266
267 if ($has_setitimer) {
98b50af3 268 print "found.\n";
3f2ee006 269 } else {
98b50af3 270 print "NOT found.\n";
3f2ee006 271 }
272
98b50af3 273 print "Looking for getitimer()... ";
3f2ee006 274 my $has_getitimer;
5d899d7e 275 if (exists $Config{'d_getitimer'}) {
276 $has_getitimer++ if $Config{'d_getitimer'};
3f2ee006 277 } elsif (has_x("getitimer(ITIMER_REAL, 0)")) {
278 $has_getitimer++;
279 $DEFINE .= ' -DHAS_GETITIMER';
280 }
281
282 if ($has_getitimer) {
98b50af3 283 print "found.\n";
3f2ee006 284 } else {
98b50af3 285 print "NOT found.\n";
3f2ee006 286 }
287
288 if ($has_setitimer && $has_getitimer) {
98b50af3 289 print "You have interval timers (both setitimer and setitimer).\n";
3f2ee006 290 } else {
98b50af3 291 print "You do not have interval timers.\n";
3f2ee006 292 }
293
98b50af3 294 print "Looking for ualarm()... ";
295 my $has_ualarm;
5d899d7e 296 if (exists $Config{d_ualarm}) {
297 $has_ualarm++ if $Config{d_ualarm};
3f2ee006 298 } elsif (has_x ("ualarm (0, 0)")) {
299 $has_ualarm++;
300 $DEFINE .= ' -DHAS_UALARM';
301 }
302
303 if ($has_ualarm) {
98b50af3 304 print "found.\n";
3f2ee006 305 } else {
98b50af3 306 print "NOT found.\n";
307 if ($has_setitimer) {
308 print "But you have setitimer().\n";
309 print "We can make a Time::HiRes::ualarm().\n";
3f2ee006 310 }
311 }
312
98b50af3 313 print "Looking for usleep()... ";
3f2ee006 314 my $has_usleep;
5d899d7e 315 if (exists $Config{d_usleep}) {
316 $has_usleep++ if $Config{d_usleep};
3f2ee006 317 } elsif (has_x ("usleep (0)")) {
318 $has_usleep++;
319 $DEFINE .= ' -DHAS_USLEEP';
320 }
321
322 if ($has_usleep) {
98b50af3 323 print "found.\n";
3f2ee006 324 } else {
98b50af3 325 print "NOT found.\n";
326 print "Let's see if you have select()... ";
3f2ee006 327 if ($Config{'d_select'} eq 'define') {
98b50af3 328 print "found.\n";
329 print "We can make a Time::HiRes::usleep().\n";
3f2ee006 330 } else {
98b50af3 331 print "NOT found.\n";
332 print "You won't have a Time::HiRes::usleep().\n";
3f2ee006 333 }
334 }
335
98b50af3 336 print "Looking for nanosleep()... ";
3f2ee006 337 my $has_nanosleep;
5d899d7e 338 if (exists $Config{d_nanosleep}) {
339 $has_nanosleep++ if $Config{d_nanosleep};
046e3f33 340 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
3f2ee006 341 } elsif (has_x ("nanosleep (NULL, NULL)")) {
342 $has_nanosleep++;
046e3f33 343 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
3f2ee006 344 }
345
346 if ($has_nanosleep) {
98b50af3 347 print "found.\n";
348 print "You can mix subsecond sleeps with signals.\n";
3f2ee006 349 } else {
98b50af3 350 print "NOT found.\n";
622913ab 351 my $nt = ($^O eq 'os2' ? '' : 'not');
352 print "You can$nt mix subsecond sleeps with signals.\n";
3f2ee006 353 }
354
355 if ($DEFINE) {
356 $DEFINE =~ s/^\s+//;
357 if (open(XDEFINE, ">xdefine")) {
358 print XDEFINE $DEFINE, "\n";
359 close(XDEFINE);
360 }
361 }
362}
363
364sub doMakefile {
98b50af3 365 my @makefileopts = ();
3f2ee006 366
367 if ($] >= 5.005) {
368 push (@makefileopts,
369 'AUTHOR' => 'Jarkko Hietaniemi <jhi@iki.fi>',
370 'ABSTRACT_FROM' => 'HiRes.pm',
371 );
372 $DEFINE .= " -DATLEASTFIVEOHOHFIVE";
373 }
374
375 push (@makefileopts,
376 'NAME' => 'Time::HiRes',
377 'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
5d899d7e 378 'LIBS' => $LIBS, # e.g., '-lm'
379 'DEFINE' => $DEFINE, # e.g., '-DHAS_SOMETHING'
3f2ee006 380 'XSOPT' => $XSOPT,
381 # do not even think about 'INC' => '-I/usr/ucbinclude', Solaris will avenge.
5d899d7e 382 'INC' => '', # e.g., '-I/usr/include/other'
3f2ee006 383 'INSTALLDIRS' => 'perl',
384 'dist' => {
385 'CI' => 'ci -l',
5d899d7e 386 'COMPRESS' => 'gzip -9f',
3f2ee006 387 'SUFFIX' => 'gz',
388 },
389 clean => { FILES => "xdefine" },
98b50af3 390 realclean => {FILES=> 'const-c.inc const-xs.inc'},
3f2ee006 391 );
392
9ac5eb64 393 if ($ENV{PERL_CORE}) {
394 push @makefileopts, MAN3PODS => {};
395 }
396
3f2ee006 397 WriteMakefile(@makefileopts);
398}
399
98b50af3 400sub doConstants {
401 if (eval {require ExtUtils::Constant; 1}) {
402 my @names = (qw(ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF
403 ITIMER_REALPROF));
404 foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
405 d_nanosleep)) {
406 my $macro = $_;
407 $macro =~ s/d_(.*)/HAS_\U$1/;
408 push @names, {name => $_, macro => $macro, value => 1,
409 default => ["IV", "0"]};
410 }
411 ExtUtils::Constant::WriteConstants(
412 NAME => 'Time::HiRes',
413 NAMES => \@names,
414 );
415 } else {
416 foreach my $file ('const-c.inc', 'const-xs.inc') {
417 my $fallback = File::Spec->catfile('fallback', $file);
418 local $/;
419 open IN, "<$fallback" or die "Can't open $fallback: $!";
420 open OUT, ">$file" or die "Can't open $file: $!";
421 print OUT <IN> or die $!;
422 close OUT or die "Can't close $file: $!";
423 close IN or die "Can't close $fallback: $!";
424 }
425 }
426}
3f2ee006 427
98b50af3 428sub main {
429 print "Configuring Time::HiRes...\n";
3f2ee006 430
431 if ($^O =~ /Win32/i) {
432 $DEFINE = '-DSELECT_IS_BROKEN';
046e3f33 433 $LIBS = [];
3f2ee006 434 } else {
046e3f33 435 init();
3f2ee006 436 }
3f2ee006 437 doMakefile;
98b50af3 438 doConstants;
3f2ee006 439 my $make = $Config{'make'} || "make";
440 unless ($ENV{PERL_CORE}) {
441 print <<EOM;
3f2ee006 442Now you may issue '$make'. Do not forget also '$make test'.
3f2ee006 443EOM
444 }
445}
446
447&main;
dcf686c9 448
3f2ee006 449# EOF