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