Symbian: New vs New
[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}) {
06252d99 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) {
56c1b3bd 151 $ok = 1;
26e22fd9 152 } else {
56c1b3bd 153 $ok = 0;
06252d99 154 print "[ system('$tmp_exe') failed: status $? ] ";
26e22fd9 155 }
156 }
622913ab 157 unlink("$tmp.c", $tmp_exe);
3f2ee006 158 }
159 }
5d899d7e 160
26e22fd9 161 return $ok;
3f2ee006 162}
163
164sub has_gettimeofday {
165 # confusing but true (if condition true ==> -DHAS_GETTIMEOFDAY already)
56c1b3bd 166 return 0 if $Config{d_gettimeod};
98b50af3 167 return 1 if try_compile_and_link(<<EOM);
5d899d7e 168#include "EXTERN.h"
169#include "perl.h"
170#include "XSUB.h"
171#ifdef I_SYS_TYPES
3f2ee006 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
182static int foo()
183{
184 struct timeval tv;
185 gettimeofday(&tv, 0);
186}
187int main _((int argc, char** argv, char** env))
188{
189 foo();
190}
191EOM
192 return 0;
193}
194
195sub has_x {
98b50af3 196 my ($x, %args) = @_;
3f2ee006 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
216int main _((int argc, char** argv, char** env))
217{
218 $x;
219}
220EOM
221 return 0;
222}
223
26e22fd9 224sub has_nanosleep {
56c1b3bd 225 print "Trying out nanosleep... ";
26e22fd9 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
236int main() {
237 struct timespec ts1, ts2;
56c1b3bd 238 int ret;
26e22fd9 239 ts1.tv_sec = 0;
240 ts1.tv_nsec = 750000000;
241 ts2.tv_sec = 0;
242 ts2.tv_nsec = 0;
56c1b3bd 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);
26e22fd9 246}
247EOM
248}
249
4ed0e2d4 250sub 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>
259int main _((int argc, char** argv, char** env))
260{
261 return 0;
262}
263EOM
264 return 0;
265}
266
046e3f33 267sub 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";
3f2ee006 276 }
277 }
046e3f33 278
279 $DEFINE = '';
3f2ee006 280
98b50af3 281 print "Looking for gettimeofday()... ";
3f2ee006 282 my $has_gettimeofday;
5d899d7e 283 if (exists $Config{d_gettimeod}) {
284 $has_gettimeofday++ if $Config{d_gettimeod};
3f2ee006 285 } elsif (has_gettimeofday()) {
286 $DEFINE .= ' -DHAS_GETTIMEOFDAY';
287 $has_gettimeofday++;
288 }
289
290 if ($has_gettimeofday) {
98b50af3 291 print "found.\n";
3f2ee006 292 } else {
293 die <<EOD
294Your operating system does not seem to have the gettimeofday() function.
295(or, at least, I cannot find it)
296
297There is no way Time::HiRes is going to work.
298
299I am awfully sorry but I cannot go further.
300
301Aborting configuration.
302
303EOD
304 }
305
98b50af3 306 print "Looking for setitimer()... ";
3f2ee006 307 my $has_setitimer;
5d899d7e 308 if (exists $Config{d_setitimer}) {
309 $has_setitimer++ if $Config{d_setitimer};
3f2ee006 310 } elsif (has_x("setitimer(ITIMER_REAL, 0, 0)")) {
311 $has_setitimer++;
312 $DEFINE .= ' -DHAS_SETITIMER';
313 }
314
315 if ($has_setitimer) {
98b50af3 316 print "found.\n";
3f2ee006 317 } else {
98b50af3 318 print "NOT found.\n";
3f2ee006 319 }
320
98b50af3 321 print "Looking for getitimer()... ";
3f2ee006 322 my $has_getitimer;
5d899d7e 323 if (exists $Config{'d_getitimer'}) {
324 $has_getitimer++ if $Config{'d_getitimer'};
3f2ee006 325 } elsif (has_x("getitimer(ITIMER_REAL, 0)")) {
326 $has_getitimer++;
327 $DEFINE .= ' -DHAS_GETITIMER';
328 }
329
330 if ($has_getitimer) {
98b50af3 331 print "found.\n";
3f2ee006 332 } else {
98b50af3 333 print "NOT found.\n";
3f2ee006 334 }
335
336 if ($has_setitimer && $has_getitimer) {
4ed0e2d4 337 print "You have interval timers (both setitimer and getitimer).\n";
3f2ee006 338 } else {
98b50af3 339 print "You do not have interval timers.\n";
3f2ee006 340 }
341
98b50af3 342 print "Looking for ualarm()... ";
343 my $has_ualarm;
5d899d7e 344 if (exists $Config{d_ualarm}) {
345 $has_ualarm++ if $Config{d_ualarm};
3f2ee006 346 } elsif (has_x ("ualarm (0, 0)")) {
347 $has_ualarm++;
348 $DEFINE .= ' -DHAS_UALARM';
349 }
350
351 if ($has_ualarm) {
98b50af3 352 print "found.\n";
3f2ee006 353 } else {
98b50af3 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";
3f2ee006 358 }
359 }
360
98b50af3 361 print "Looking for usleep()... ";
3f2ee006 362 my $has_usleep;
5d899d7e 363 if (exists $Config{d_usleep}) {
364 $has_usleep++ if $Config{d_usleep};
3f2ee006 365 } elsif (has_x ("usleep (0)")) {
366 $has_usleep++;
367 $DEFINE .= ' -DHAS_USLEEP';
368 }
369
370 if ($has_usleep) {
98b50af3 371 print "found.\n";
3f2ee006 372 } else {
98b50af3 373 print "NOT found.\n";
374 print "Let's see if you have select()... ";
56c1b3bd 375 if ($Config{'d_select'}) {
98b50af3 376 print "found.\n";
377 print "We can make a Time::HiRes::usleep().\n";
3f2ee006 378 } else {
98b50af3 379 print "NOT found.\n";
380 print "You won't have a Time::HiRes::usleep().\n";
3f2ee006 381 }
382 }
383
98b50af3 384 print "Looking for nanosleep()... ";
3f2ee006 385 my $has_nanosleep;
56c1b3bd 386 if (exists $Config{d_nanosleep} && !$ENV{FORCE_NANOSLEEP_SCAN}) {
387 # Believe $Config{d_nanosleep}.
c1363767 388 if ($Config{d_nanosleep}) {
389 $has_nanosleep++;
390 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
391 }
c077e422 392 } elsif ($^O ne 'mpeix' && # MPE/iX falsely finds nanosleep.
26e22fd9 393 has_nanosleep()) {
3f2ee006 394 $has_nanosleep++;
046e3f33 395 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
3f2ee006 396 }
397
398 if ($has_nanosleep) {
98b50af3 399 print "found.\n";
4ed0e2d4 400 print "You can mix subsecond sleeps with signals, if you want to.\n";
401 print "(It's still not portable, though.)\n";
3f2ee006 402 } else {
98b50af3 403 print "NOT found.\n";
622913ab 404 my $nt = ($^O eq 'os2' ? '' : 'not');
405 print "You can$nt mix subsecond sleeps with signals.\n";
4ed0e2d4 406 print "(It would not be portable anyway.)\n";
407 }
408
409 my $has_w32api_windows_h;
410 if ($^O eq 'cygwin') {
411 print "Looking for <w32api/windows.h>... ";
412 if (has_include('w32api/windows.h')) {
413 $has_w32api_windows_h++;
414 $DEFINE .= ' -DHAS_W32API_WINDOWS_H';
415 }
416 if ($has_w32api_windows_h) {
417 print "found.\n";
418 } else {
419 print "NOT found.\n";
420 }
3f2ee006 421 }
422
423 if ($DEFINE) {
424 $DEFINE =~ s/^\s+//;
425 if (open(XDEFINE, ">xdefine")) {
426 print XDEFINE $DEFINE, "\n";
427 close(XDEFINE);
428 }
429 }
430}
431
432sub doMakefile {
98b50af3 433 my @makefileopts = ();
3f2ee006 434
435 if ($] >= 5.005) {
436 push (@makefileopts,
437 'AUTHOR' => 'Jarkko Hietaniemi <jhi@iki.fi>',
438 'ABSTRACT_FROM' => 'HiRes.pm',
439 );
440 $DEFINE .= " -DATLEASTFIVEOHOHFIVE";
441 }
442
443 push (@makefileopts,
444 'NAME' => 'Time::HiRes',
445 'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
5d899d7e 446 'LIBS' => $LIBS, # e.g., '-lm'
447 'DEFINE' => $DEFINE, # e.g., '-DHAS_SOMETHING'
3f2ee006 448 'XSOPT' => $XSOPT,
449 # do not even think about 'INC' => '-I/usr/ucbinclude', Solaris will avenge.
5d899d7e 450 'INC' => '', # e.g., '-I/usr/include/other'
3f2ee006 451 'INSTALLDIRS' => 'perl',
452 'dist' => {
453 'CI' => 'ci -l',
5d899d7e 454 'COMPRESS' => 'gzip -9f',
3f2ee006 455 'SUFFIX' => 'gz',
456 },
457 clean => { FILES => "xdefine" },
1caec985 458 realclean => { FILES=> 'const-c.inc const-xs.inc' },
3f2ee006 459 );
460
9ac5eb64 461 if ($ENV{PERL_CORE}) {
462 push @makefileopts, MAN3PODS => {};
463 }
464
3f2ee006 465 WriteMakefile(@makefileopts);
466}
467
98b50af3 468sub doConstants {
469 if (eval {require ExtUtils::Constant; 1}) {
470 my @names = (qw(ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF
471 ITIMER_REALPROF));
472 foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
473 d_nanosleep)) {
474 my $macro = $_;
672f6857 475 if ($macro eq 'd_nanosleep') {
476 $macro =~ s/d_(.*)/TIME_HIRES_\U$1/;
477 } else {
478 $macro =~ s/d_(.*)/HAS_\U$1/;
479 }
98b50af3 480 push @names, {name => $_, macro => $macro, value => 1,
481 default => ["IV", "0"]};
482 }
483 ExtUtils::Constant::WriteConstants(
484 NAME => 'Time::HiRes',
485 NAMES => \@names,
486 );
487 } else {
1fbb4de4 488 my $file;
489 foreach $file ('const-c.inc', 'const-xs.inc') {
98b50af3 490 my $fallback = File::Spec->catfile('fallback', $file);
491 local $/;
492 open IN, "<$fallback" or die "Can't open $fallback: $!";
493 open OUT, ">$file" or die "Can't open $file: $!";
494 print OUT <IN> or die $!;
495 close OUT or die "Can't close $file: $!";
496 close IN or die "Can't close $fallback: $!";
497 }
498 }
499}
3f2ee006 500
98b50af3 501sub main {
502 print "Configuring Time::HiRes...\n";
1fbb4de4 503 if ($] == 5.007002) {
504 die "Cannot Configure Time::HiRes for Perl $], aborting.\n";
505 }
3f2ee006 506
507 if ($^O =~ /Win32/i) {
508 $DEFINE = '-DSELECT_IS_BROKEN';
046e3f33 509 $LIBS = [];
3f2ee006 510 } else {
046e3f33 511 init();
3f2ee006 512 }
3f2ee006 513 doMakefile;
98b50af3 514 doConstants;
3f2ee006 515 my $make = $Config{'make'} || "make";
1fbb4de4 516 unless (exists $ENV{PERL_CORE} && $ENV{PERL_CORE}) {
3f2ee006 517 print <<EOM;
3f2ee006 518Now you may issue '$make'. Do not forget also '$make test'.
dfffa540 519EOM
1fbb4de4 520 if ((exists $ENV{LC_ALL} && $ENV{LC_ALL} =~ /utf-?8/i) ||
521 (exists $ENV{LC_CTYPE} && $ENV{LC_CTYPE} =~ /utf-?8/i) ||
522 (exists $ENV{LANG} && $ENV{LANG} =~ /utf-?8/i)) {
dfffa540 523 print <<EOM;
524NOTE: if you get an error like this (the line number may vary):
d7358e6a 525Makefile:91: *** missing separator
1caec985 526then set the environment variable LC_ALL to "C" and retry
527from scratch (re-run perl "Makefile.PL").
3f2ee006 528EOM
dfffa540 529 }
3f2ee006 530 }
531}
532
533&main;
dcf686c9 534
3f2ee006 535# EOF