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