This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Someone actually reads the Makefile.PL messages, it seems.
[perl5.git] / ext / Time / HiRes / Makefile.PL
CommitLineData
3f2ee006 1
dcf686c9
JH
2# See lib/ExtUtils/MakeMaker.pm for details of how to influence
3# the contents of the Makefile that is written.
4#
5
3f2ee006
HS
6require 5.002;
7
8use Config;
dcf686c9 9use ExtUtils::MakeMaker;
98b50af3 10use strict;
3f2ee006
HS
11
12my $VERBOSE = $ENV{VERBOSE};
98b50af3 13my $DEFINE;
046e3f33 14my $LIBS = [];
98b50af3
JH
15my $XSOPT;
16
046e3f33
JH
17use vars qw($self); # Used in 'sourcing' the hints.
18
622913ab
IZ
19my $ld_exeext = ($^O eq 'os2' and $Config{ldflags} =~ /-Zexe\b/) ? '.exe' : '';
20
12724655
IZ
21unless($ENV{PERL_CORE}) {
22 $ENV{PERL_CORE} = 1 if grep { $_ eq 'PERL_CORE=1' } @ARGV;
98b50af3
JH
23}
24
25# Perls 5.002 and 5.003 did not have File::Spec, fake what we need.
3f2ee006
HS
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
JH
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
HS
45}
46
47sub my_updir {
48 shift;
49 $^O eq 'VMS' ? "-" : "..";
50}
51
52BEGIN {
53 eval { require File::Spec };
54 if ($@) {
25ca0970
JH
55 *File::Spec::catdir = \&my_catdir;
56 *File::Spec::updir = \&my_updir;
98b50af3 57 *File::Spec::catfile = \&my_catfile;
3f2ee006
HS
58 }
59}
60
98b50af3
JH
61# Avoid 'used only once' warnings.
62my $nop1 = *File::Spec::catdir;
63my $nop2 = *File::Spec::updir;
64my $nop3 = *File::Spec::catfile;
65
3f2ee006
HS
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
HS
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
HS
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
HS
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
HS
97
98 my $errornull;
99
100 my $COREincdir;
98b50af3 101
3f2ee006
HS
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
HS
111 if ($^O eq 'VMS') {
112 if ($ENV{PERL_CORE}) {
6a20eacc
HS
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
HS
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
HS
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
c1363767 132 if ($^O eq 'VMS') {
3f2ee006
HS
133 open( CMDFILE, ">$tmp.com" );
134 print CMDFILE "\$ SET MESSAGE/NOFACILITY/NOSEVERITY/NOIDENT/NOTEXT\n";
135 print CMDFILE "\$ $cccmd\n";
98b50af3 136 print CMDFILE "\$ IF \$SEVERITY .NE. 1 THEN EXIT 44\n"; # escalate
3f2ee006
HS
137 close CMDFILE;
138 system("\@ $tmp.com");
139 $ok = $?==0;
5d899d7e 140 for ("$tmp.c", "$tmp$obj_ext", "$tmp.com", "$tmp$Config{exe_ext}") {
98b50af3 141 1 while unlink $_;
3f2ee006
HS
142 }
143 }
144 else
145 {
622913ab 146 my $tmp_exe = "$tmp$ld_exeext";
3f2ee006 147 printf "cccmd = $cccmd\n" if $VERBOSE;
622913ab
IZ
148 my $res = system($cccmd);
149 $ok = defined($res) && $res==0 && -s $tmp_exe && -x _;
150 unlink("$tmp.c", $tmp_exe);
3f2ee006
HS
151 }
152 }
5d899d7e 153
3f2ee006
HS
154 $ok;
155}
156
157sub has_gettimeofday {
158 # confusing but true (if condition true ==> -DHAS_GETTIMEOFDAY already)
5d899d7e 159 return 0 if $Config{d_gettimeod} eq 'define';
98b50af3 160 return 1 if try_compile_and_link(<<EOM);
5d899d7e
JH
161#include "EXTERN.h"
162#include "perl.h"
163#include "XSUB.h"
164#ifdef I_SYS_TYPES
3f2ee006
HS
165# include <sys/types.h>
166#endif
167
168#ifdef I_SYS_TIME
169# include <sys/time.h>
170#endif
171
172#ifdef I_SYS_SELECT
173# include <sys/select.h> /* struct timeval might be hidden in here */
174#endif
175static int foo()
176{
177 struct timeval tv;
178 gettimeofday(&tv, 0);
179}
180int main _((int argc, char** argv, char** env))
181{
182 foo();
183}
184EOM
185 return 0;
186}
187
188sub has_x {
98b50af3 189 my ($x, %args) = @_;
3f2ee006
HS
190
191 return 1 if
192 try_compile_and_link(<<EOM, %args);
193#include "EXTERN.h"
194#include "perl.h"
195#include "XSUB.h"
196
197#ifdef I_UNISTD
198# include <unistd.h>
199#endif
200
201#ifdef I_SYS_TYPES
202# include <sys/types.h>
203#endif
204
205#ifdef I_SYS_TIME
206# include <sys/time.h>
207#endif
208
209int main _((int argc, char** argv, char** env))
210{
211 $x;
212}
213EOM
214 return 0;
215}
216
046e3f33
JH
217sub init {
218 my $hints = File::Spec->catfile("hints", "$^O.pl");
219 if (-f $hints) {
220 print "Using hints $hints...\n";
221 local $self;
222 do $hints;
223 if (exists $self->{LIBS}) {
224 $LIBS = $self->{LIBS};
225 print "Extra libraries: @$LIBS...\n";
3f2ee006
HS
226 }
227 }
046e3f33
JH
228
229 $DEFINE = '';
3f2ee006 230
98b50af3 231 print "Looking for gettimeofday()... ";
3f2ee006 232 my $has_gettimeofday;
5d899d7e
JH
233 if (exists $Config{d_gettimeod}) {
234 $has_gettimeofday++ if $Config{d_gettimeod};
3f2ee006
HS
235 } elsif (has_gettimeofday()) {
236 $DEFINE .= ' -DHAS_GETTIMEOFDAY';
237 $has_gettimeofday++;
238 }
239
240 if ($has_gettimeofday) {
98b50af3 241 print "found.\n";
3f2ee006
HS
242 } else {
243 die <<EOD
244Your operating system does not seem to have the gettimeofday() function.
245(or, at least, I cannot find it)
246
247There is no way Time::HiRes is going to work.
248
249I am awfully sorry but I cannot go further.
250
251Aborting configuration.
252
253EOD
254 }
255
98b50af3 256 print "Looking for setitimer()... ";
3f2ee006 257 my $has_setitimer;
5d899d7e
JH
258 if (exists $Config{d_setitimer}) {
259 $has_setitimer++ if $Config{d_setitimer};
3f2ee006
HS
260 } elsif (has_x("setitimer(ITIMER_REAL, 0, 0)")) {
261 $has_setitimer++;
262 $DEFINE .= ' -DHAS_SETITIMER';
263 }
264
265 if ($has_setitimer) {
98b50af3 266 print "found.\n";
3f2ee006 267 } else {
98b50af3 268 print "NOT found.\n";
3f2ee006
HS
269 }
270
98b50af3 271 print "Looking for getitimer()... ";
3f2ee006 272 my $has_getitimer;
5d899d7e
JH
273 if (exists $Config{'d_getitimer'}) {
274 $has_getitimer++ if $Config{'d_getitimer'};
3f2ee006
HS
275 } elsif (has_x("getitimer(ITIMER_REAL, 0)")) {
276 $has_getitimer++;
277 $DEFINE .= ' -DHAS_GETITIMER';
278 }
279
280 if ($has_getitimer) {
98b50af3 281 print "found.\n";
3f2ee006 282 } else {
98b50af3 283 print "NOT found.\n";
3f2ee006
HS
284 }
285
286 if ($has_setitimer && $has_getitimer) {
477413c8 287 print "You have interval timers (both setitimer and getitimer).\n";
3f2ee006 288 } else {
98b50af3 289 print "You do not have interval timers.\n";
3f2ee006
HS
290 }
291
98b50af3
JH
292 print "Looking for ualarm()... ";
293 my $has_ualarm;
5d899d7e
JH
294 if (exists $Config{d_ualarm}) {
295 $has_ualarm++ if $Config{d_ualarm};
3f2ee006
HS
296 } elsif (has_x ("ualarm (0, 0)")) {
297 $has_ualarm++;
298 $DEFINE .= ' -DHAS_UALARM';
299 }
300
301 if ($has_ualarm) {
98b50af3 302 print "found.\n";
3f2ee006 303 } else {
98b50af3
JH
304 print "NOT found.\n";
305 if ($has_setitimer) {
306 print "But you have setitimer().\n";
307 print "We can make a Time::HiRes::ualarm().\n";
3f2ee006
HS
308 }
309 }
310
98b50af3 311 print "Looking for usleep()... ";
3f2ee006 312 my $has_usleep;
5d899d7e
JH
313 if (exists $Config{d_usleep}) {
314 $has_usleep++ if $Config{d_usleep};
3f2ee006
HS
315 } elsif (has_x ("usleep (0)")) {
316 $has_usleep++;
317 $DEFINE .= ' -DHAS_USLEEP';
318 }
319
320 if ($has_usleep) {
98b50af3 321 print "found.\n";
3f2ee006 322 } else {
98b50af3
JH
323 print "NOT found.\n";
324 print "Let's see if you have select()... ";
3f2ee006 325 if ($Config{'d_select'} eq 'define') {
98b50af3
JH
326 print "found.\n";
327 print "We can make a Time::HiRes::usleep().\n";
3f2ee006 328 } else {
98b50af3
JH
329 print "NOT found.\n";
330 print "You won't have a Time::HiRes::usleep().\n";
3f2ee006
HS
331 }
332 }
333
98b50af3 334 print "Looking for nanosleep()... ";
3f2ee006 335 my $has_nanosleep;
5d899d7e 336 if (exists $Config{d_nanosleep}) {
c1363767
JH
337 if ($Config{d_nanosleep}) {
338 $has_nanosleep++;
339 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
340 }
c077e422
JH
341 } elsif ($^O ne 'mpeix' && # MPE/iX falsely finds nanosleep.
342 has_x ("nanosleep (NULL, NULL)")) {
3f2ee006 343 $has_nanosleep++;
046e3f33 344 $DEFINE .= ' -DTIME_HIRES_NANOSLEEP';
3f2ee006
HS
345 }
346
347 if ($has_nanosleep) {
98b50af3
JH
348 print "found.\n";
349 print "You can mix subsecond sleeps with signals.\n";
3f2ee006 350 } else {
98b50af3 351 print "NOT found.\n";
622913ab
IZ
352 my $nt = ($^O eq 'os2' ? '' : 'not');
353 print "You can$nt mix subsecond sleeps with signals.\n";
3f2ee006
HS
354 }
355
356 if ($DEFINE) {
357 $DEFINE =~ s/^\s+//;
358 if (open(XDEFINE, ">xdefine")) {
359 print XDEFINE $DEFINE, "\n";
360 close(XDEFINE);
361 }
362 }
363}
364
365sub doMakefile {
98b50af3 366 my @makefileopts = ();
3f2ee006
HS
367
368 if ($] >= 5.005) {
369 push (@makefileopts,
370 'AUTHOR' => 'Jarkko Hietaniemi <jhi@iki.fi>',
371 'ABSTRACT_FROM' => 'HiRes.pm',
372 );
373 $DEFINE .= " -DATLEASTFIVEOHOHFIVE";
374 }
375
376 push (@makefileopts,
377 'NAME' => 'Time::HiRes',
378 'VERSION_FROM' => 'HiRes.pm', # finds $VERSION
5d899d7e
JH
379 'LIBS' => $LIBS, # e.g., '-lm'
380 'DEFINE' => $DEFINE, # e.g., '-DHAS_SOMETHING'
3f2ee006
HS
381 'XSOPT' => $XSOPT,
382 # do not even think about 'INC' => '-I/usr/ucbinclude', Solaris will avenge.
5d899d7e 383 'INC' => '', # e.g., '-I/usr/include/other'
3f2ee006
HS
384 'INSTALLDIRS' => 'perl',
385 'dist' => {
386 'CI' => 'ci -l',
5d899d7e 387 'COMPRESS' => 'gzip -9f',
3f2ee006
HS
388 'SUFFIX' => 'gz',
389 },
390 clean => { FILES => "xdefine" },
98b50af3 391 realclean => {FILES=> 'const-c.inc const-xs.inc'},
3f2ee006
HS
392 );
393
9ac5eb64
JH
394 if ($ENV{PERL_CORE}) {
395 push @makefileopts, MAN3PODS => {};
396 }
397
3f2ee006
HS
398 WriteMakefile(@makefileopts);
399}
400
98b50af3
JH
401sub doConstants {
402 if (eval {require ExtUtils::Constant; 1}) {
403 my @names = (qw(ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF
404 ITIMER_REALPROF));
405 foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
406 d_nanosleep)) {
407 my $macro = $_;
672f6857
JH
408 if ($macro eq 'd_nanosleep') {
409 $macro =~ s/d_(.*)/TIME_HIRES_\U$1/;
410 } else {
411 $macro =~ s/d_(.*)/HAS_\U$1/;
412 }
98b50af3
JH
413 push @names, {name => $_, macro => $macro, value => 1,
414 default => ["IV", "0"]};
415 }
416 ExtUtils::Constant::WriteConstants(
417 NAME => 'Time::HiRes',
418 NAMES => \@names,
419 );
420 } else {
421 foreach my $file ('const-c.inc', 'const-xs.inc') {
422 my $fallback = File::Spec->catfile('fallback', $file);
423 local $/;
424 open IN, "<$fallback" or die "Can't open $fallback: $!";
425 open OUT, ">$file" or die "Can't open $file: $!";
426 print OUT <IN> or die $!;
427 close OUT or die "Can't close $file: $!";
428 close IN or die "Can't close $fallback: $!";
429 }
430 }
431}
3f2ee006 432
98b50af3
JH
433sub main {
434 print "Configuring Time::HiRes...\n";
3f2ee006
HS
435
436 if ($^O =~ /Win32/i) {
437 $DEFINE = '-DSELECT_IS_BROKEN';
046e3f33 438 $LIBS = [];
3f2ee006 439 } else {
046e3f33 440 init();
3f2ee006 441 }
3f2ee006 442 doMakefile;
98b50af3 443 doConstants;
3f2ee006
HS
444 my $make = $Config{'make'} || "make";
445 unless ($ENV{PERL_CORE}) {
446 print <<EOM;
3f2ee006 447Now you may issue '$make'. Do not forget also '$make test'.
d7358e6a 448
dfffa540
JH
449EOM
450 if ($ENV{LC_ALL} =~ /utf-?8/i ||
451 $ENV{LC_CTYPE} =~ /utf-?8/i ||
452 $ENV{LANG} =~ /utf-?8/i) {
453 print <<EOM;
454NOTE: if you get an error like this (the line number may vary):
d7358e6a
JH
455Makefile:91: *** missing separator
456then set the environment variable LC_ALL to "C" and retry.
dfffa540 457
3f2ee006 458EOM
dfffa540 459 }
3f2ee006
HS
460 }
461}
462
463&main;
dcf686c9 464
3f2ee006 465# EOF