This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Patch 3945 was a bit overkeen - the effective parameter to cando() and
[perl5.git] / ext / Time / HiRes / Makefile.PL
CommitLineData
56c1b3bd
RGS
1#!/usr/bin/perl
2#
3# In general we trust %Config, but for nanosleep() this trust
3d0346a5 4# may be misplaced (it may be linkable but not really functional).
56c1b3bd
RGS
5# Use $ENV{FORCE_NANOSLEEP_SCAN} to force rescanning whether there
6# really is hope.
7
3f2ee006
HS
8require 5.002;
9
10use Config;
dcf686c9 11use ExtUtils::MakeMaker;
98b50af3 12use strict;
3f2ee006
HS
13
14my $VERBOSE = $ENV{VERBOSE};
98b50af3 15my $DEFINE;
046e3f33 16my $LIBS = [];
56c1b3bd 17my $XSOPT = '';
98b50af3 18
046e3f33
JH
19use vars qw($self); # Used in 'sourcing' the hints.
20
4ed0e2d4
RGS
21my $ld_exeext = ($^O eq 'cygwin' ||
22 $^O eq 'os2' && $Config{ldflags} =~ /-Zexe\b/) ? '.exe' : '';
622913ab 23
12724655
IZ
24unless($ENV{PERL_CORE}) {
25 $ENV{PERL_CORE} = 1 if grep { $_ eq 'PERL_CORE=1' } @ARGV;
98b50af3
JH
26}
27
28# Perls 5.002 and 5.003 did not have File::Spec, fake what we need.
3f2ee006
HS
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
JH
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
HS
48}
49
50sub my_updir {
51 shift;
52 $^O eq 'VMS' ? "-" : "..";
53}
54
55BEGIN {
56 eval { require File::Spec };
57 if ($@) {
25ca0970
JH
58 *File::Spec::catdir = \&my_catdir;
59 *File::Spec::updir = \&my_updir;
98b50af3 60 *File::Spec::catfile = \&my_catfile;
3f2ee006
HS
61 }
62}
63
98b50af3
JH
64# Avoid 'used only once' warnings.
65my $nop1 = *File::Spec::catdir;
66my $nop2 = *File::Spec::updir;
67my $nop3 = *File::Spec::catfile;
68
3f2ee006
HS
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
HS
75# release. Sorry, Edward :)
76
3f2ee006
HS
77sub try_compile_and_link {
78 my ($c, %args) = @_;
79
80 my ($ok) = 0;
2359510d 81 my ($tmp) = "tmp$$";
3f2ee006
HS
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
HS
92
93 my $errornull;
94
95 my $COREincdir;
98b50af3 96
3f2ee006
HS
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
HS
106 if ($^O eq 'VMS') {
107 if ($ENV{PERL_CORE}) {
3d036c2b 108 # Fragile if the extensions change hierarchy within
6a20eacc
HS
109 # the Perl core but this should do for now.
110 $cccmd = "$Config{'cc'} /include=([---]) $tmp.c";
3f2ee006
HS
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
HS
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
HS
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
HS
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
HS
137 }
138 }
139 else
140 {
622913ab 141 my $tmp_exe = "$tmp$ld_exeext";
3f2ee006 142 printf "cccmd = $cccmd\n" if $VERBOSE;
622913ab
IZ
143 my $res = system($cccmd);
144 $ok = defined($res) && $res==0 && -s $tmp_exe && -x _;
26e22fd9
RGS
145
146 if ( $ok && exists $args{run} && $args{run}) {
06252d99
RGS
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
RGS
155 }
156 }
622913ab 157 unlink("$tmp.c", $tmp_exe);
3f2ee006
HS
158 }
159 }
5d899d7e 160
26e22fd9 161 return $ok;
3f2ee006
HS
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
JH
168#include "EXTERN.h"
169#include "perl.h"
170#include "XSUB.h"
171#ifdef I_SYS_TYPES
3f2ee006
HS
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
HS
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 {
3d0346a5 225 print "testing... ";
26e22fd9
RGS
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
RGS
239 ts1.tv_sec = 0;
240 ts1.tv_nsec = 750000000;
241 ts2.tv_sec = 0;
242 ts2.tv_nsec = 0;
56c1b3bd
RGS
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
RGS
246}
247EOM
248}
249
4ed0e2d4
RGS
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
JH
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
HS
276 }
277 }
046e3f33
JH
278
279 $DEFINE = '';
3f2ee006 280
98b50af3 281 print "Looking for gettimeofday()... ";
3f2ee006 282 my $has_gettimeofday;
5d899d7e
JH
283 if (exists $Config{d_gettimeod}) {
284 $has_gettimeofday++ if $Config{d_gettimeod};
3f2ee006
HS
285 } elsif (has_gettimeofday()) {
286 $DEFINE .= ' -DHAS_GETTIMEOFDAY';
287 $has_gettimeofday++;
288 }
289
290 if ($has_gettimeofday) {
98b50af3 291 print "found.\n";
3f2ee006
HS
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
JH
308 if (exists $Config{d_setitimer}) {
309 $has_setitimer++ if $Config{d_setitimer};
3f2ee006
HS
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
HS
319 }
320
98b50af3 321 print "Looking for getitimer()... ";
3f2ee006 322 my $has_getitimer;
5d899d7e
JH
323 if (exists $Config{'d_getitimer'}) {
324 $has_getitimer++ if $Config{'d_getitimer'};
3f2ee006
HS
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
HS
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
HS
340 }
341
98b50af3
JH
342 print "Looking for ualarm()... ";
343 my $has_ualarm;
5d899d7e
JH
344 if (exists $Config{d_ualarm}) {
345 $has_ualarm++ if $Config{d_ualarm};
3f2ee006
HS
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
JH
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
HS
358 }
359 }
360
98b50af3 361 print "Looking for usleep()... ";
3f2ee006 362 my $has_usleep;
5d899d7e
JH
363 if (exists $Config{d_usleep}) {
364 $has_usleep++ if $Config{d_usleep};
3f2ee006
HS
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
JH
373 print "NOT found.\n";
374 print "Let's see if you have select()... ";
56c1b3bd 375 if ($Config{'d_select'}) {
98b50af3
JH
376 print "found.\n";
377 print "We can make a Time::HiRes::usleep().\n";
3f2ee006 378 } else {
98b50af3
JH
379 print "NOT found.\n";
380 print "You won't have a Time::HiRes::usleep().\n";
3f2ee006
HS
381 }
382 }
383
98b50af3 384 print "Looking for nanosleep()... ";
3f2ee006 385 my $has_nanosleep;
3d0346a5
SP
386 if ($ENV{FORCE_NANOSLEEP_SCAN}) {
387 print "forced scan... ";
388 if (has_nanosleep()) {
389 $has_nanosleep++;
390 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
391 }
392 }
393 elsif (exists $Config{d_nanosleep}) {
394 print "believing \$Config{d_nanosleep}... ";
c1363767
JH
395 if ($Config{d_nanosleep}) {
396 $has_nanosleep++;
397 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
398 }
3d0346a5
SP
399 } elsif ($^O =~ /^(mpeix)$/) {
400 # MPE/iX falsely finds nanosleep from its libc equivalent.
401 print "skipping because in $^O... ";
402 } else {
403 if (has_nanosleep()) {
404 $has_nanosleep++;
405 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
406 }
3f2ee006
HS
407 }
408
409 if ($has_nanosleep) {
98b50af3 410 print "found.\n";
4ed0e2d4
RGS
411 print "You can mix subsecond sleeps with signals, if you want to.\n";
412 print "(It's still not portable, though.)\n";
3f2ee006 413 } else {
98b50af3 414 print "NOT found.\n";
622913ab
IZ
415 my $nt = ($^O eq 'os2' ? '' : 'not');
416 print "You can$nt mix subsecond sleeps with signals.\n";
4ed0e2d4
RGS
417 print "(It would not be portable anyway.)\n";
418 }
419
420 my $has_w32api_windows_h;
421 if ($^O eq 'cygwin') {
422 print "Looking for <w32api/windows.h>... ";
423 if (has_include('w32api/windows.h')) {
424 $has_w32api_windows_h++;
425 $DEFINE .= ' -DHAS_W32API_WINDOWS_H';
426 }
427 if ($has_w32api_windows_h) {
428 print "found.\n";
429 } else {
430 print "NOT found.\n";
431 }
3f2ee006
HS
432 }
433
434 if ($DEFINE) {
435 $DEFINE =~ s/^\s+//;
436 if (open(XDEFINE, ">xdefine")) {
437 print XDEFINE $DEFINE, "\n";
438 close(XDEFINE);
439 }
440 }
441}
442
443sub doMakefile {
98b50af3 444 my @makefileopts = ();
3f2ee006
HS
445
446 if ($] >= 5.005) {
447 push (@makefileopts,
448 'AUTHOR' => 'Jarkko Hietaniemi <jhi@iki.fi>',
449 'ABSTRACT_FROM' => 'HiRes.pm',
450 );
451 $DEFINE .= " -DATLEASTFIVEOHOHFIVE";
452 }
453
454 push (@makefileopts,
455 'NAME' => 'Time::HiRes',
456 'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
5d899d7e
JH
457 'LIBS' => $LIBS, # e.g., '-lm'
458 'DEFINE' => $DEFINE, # e.g., '-DHAS_SOMETHING'
3f2ee006
HS
459 'XSOPT' => $XSOPT,
460 # do not even think about 'INC' => '-I/usr/ucbinclude', Solaris will avenge.
5d899d7e 461 'INC' => '', # e.g., '-I/usr/include/other'
9649b81b 462 'INSTALLDIRS' => ($] >= 5.008 ? 'perl' : 'site'),
3f2ee006
HS
463 'dist' => {
464 'CI' => 'ci -l',
5d899d7e 465 'COMPRESS' => 'gzip -9f',
3f2ee006
HS
466 'SUFFIX' => 'gz',
467 },
468 clean => { FILES => "xdefine" },
1caec985 469 realclean => { FILES=> 'const-c.inc const-xs.inc' },
3f2ee006
HS
470 );
471
9ac5eb64
JH
472 if ($ENV{PERL_CORE}) {
473 push @makefileopts, MAN3PODS => {};
474 }
475
3f2ee006
HS
476 WriteMakefile(@makefileopts);
477}
478
98b50af3
JH
479sub doConstants {
480 if (eval {require ExtUtils::Constant; 1}) {
481 my @names = (qw(ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF
482 ITIMER_REALPROF));
483 foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
484 d_nanosleep)) {
485 my $macro = $_;
672f6857
JH
486 if ($macro eq 'd_nanosleep') {
487 $macro =~ s/d_(.*)/TIME_HIRES_\U$1/;
488 } else {
489 $macro =~ s/d_(.*)/HAS_\U$1/;
490 }
98b50af3
JH
491 push @names, {name => $_, macro => $macro, value => 1,
492 default => ["IV", "0"]};
493 }
494 ExtUtils::Constant::WriteConstants(
495 NAME => 'Time::HiRes',
496 NAMES => \@names,
497 );
498 } else {
1fbb4de4
RGS
499 my $file;
500 foreach $file ('const-c.inc', 'const-xs.inc') {
98b50af3
JH
501 my $fallback = File::Spec->catfile('fallback', $file);
502 local $/;
503 open IN, "<$fallback" or die "Can't open $fallback: $!";
504 open OUT, ">$file" or die "Can't open $file: $!";
505 print OUT <IN> or die $!;
506 close OUT or die "Can't close $file: $!";
507 close IN or die "Can't close $fallback: $!";
508 }
509 }
510}
3f2ee006 511
98b50af3
JH
512sub main {
513 print "Configuring Time::HiRes...\n";
1fbb4de4
RGS
514 if ($] == 5.007002) {
515 die "Cannot Configure Time::HiRes for Perl $], aborting.\n";
516 }
3f2ee006
HS
517
518 if ($^O =~ /Win32/i) {
519 $DEFINE = '-DSELECT_IS_BROKEN';
046e3f33 520 $LIBS = [];
3f2ee006 521 } else {
046e3f33 522 init();
3f2ee006 523 }
3f2ee006 524 doMakefile;
98b50af3 525 doConstants;
3f2ee006 526 my $make = $Config{'make'} || "make";
1fbb4de4 527 unless (exists $ENV{PERL_CORE} && $ENV{PERL_CORE}) {
3f2ee006 528 print <<EOM;
3f2ee006 529Now you may issue '$make'. Do not forget also '$make test'.
dfffa540 530EOM
1fbb4de4
RGS
531 if ((exists $ENV{LC_ALL} && $ENV{LC_ALL} =~ /utf-?8/i) ||
532 (exists $ENV{LC_CTYPE} && $ENV{LC_CTYPE} =~ /utf-?8/i) ||
533 (exists $ENV{LANG} && $ENV{LANG} =~ /utf-?8/i)) {
dfffa540
JH
534 print <<EOM;
535NOTE: if you get an error like this (the line number may vary):
d7358e6a 536Makefile:91: *** missing separator
1caec985
DM
537then set the environment variable LC_ALL to "C" and retry
538from scratch (re-run perl "Makefile.PL").
3f2ee006 539EOM
dfffa540 540 }
3f2ee006
HS
541 }
542}
543
544&main;
dcf686c9 545
3f2ee006 546# EOF