Commit | Line | Data |
3f2ee006 |
1 | |
dcf686c9 |
2 | # See lib/ExtUtils/MakeMaker.pm for details of how to influence |
3 | # the contents of the Makefile that is written. |
4 | # |
5 | |
3f2ee006 |
6 | require 5.002; |
7 | |
8 | use Config; |
dcf686c9 |
9 | use ExtUtils::MakeMaker; |
98b50af3 |
10 | use strict; |
3f2ee006 |
11 | |
12 | my $VERBOSE = $ENV{VERBOSE}; |
98b50af3 |
13 | my $DEFINE; |
14 | my $LIBS; |
15 | my $XSOPT; |
16 | |
17 | unless($ENV{PERL_CORE}) { # This trick from Encode/Makefile.PL. |
18 | $ENV{PERL_CORE} = 1 if ($^X =~ m{\bminiperl[^/\\\]>:]*$}o); |
19 | } |
20 | |
21 | # Perls 5.002 and 5.003 did not have File::Spec, fake what we need. |
3f2ee006 |
22 | |
23 | sub my_dirsep { |
24 | $^O eq 'VMS' ? '.' : |
25 | $^O =~ /mswin32|netware|djgpp/i ? '\\' : |
26 | $^O eq 'MacOS' ? ':' |
27 | : '/'; |
28 | } |
29 | |
30 | sub my_catdir { |
31 | shift; |
32 | my $catdir = join(my_dirsep, @_); |
98b50af3 |
33 | $^O eq 'VMS' ? "[$catdir]" : $catdir; |
34 | } |
35 | |
36 | sub my_catfile { |
37 | shift; |
38 | return join(my_dirsep, @_) unless $^O eq 'VMS'; |
39 | my $file = pop; |
40 | return my_catdir (undef, @_) . $file; |
3f2ee006 |
41 | } |
42 | |
43 | sub my_updir { |
44 | shift; |
45 | $^O eq 'VMS' ? "-" : ".."; |
46 | } |
47 | |
48 | BEGIN { |
49 | eval { require File::Spec }; |
50 | if ($@) { |
51 | *File::Spec::catdir = \&my_catdir; |
52 | *File::Spec::updir = \&my_updir; |
98b50af3 |
53 | *File::Spec::catfile = \&my_catfile; |
3f2ee006 |
54 | } |
55 | } |
56 | |
98b50af3 |
57 | # Avoid 'used only once' warnings. |
58 | my $nop1 = *File::Spec::catdir; |
59 | my $nop2 = *File::Spec::updir; |
60 | my $nop3 = *File::Spec::catfile; |
61 | |
3f2ee006 |
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. |
65 | # my choice is leave it in and have people with old perls complain |
66 | # about the "Usage" bug, or leave it out and be unable to compile myself |
67 | # without changing it, and then I'd always forget to change it before a |
68 | # release. Sorry, Edward :) |
69 | |
70 | sub TMPDIR { |
71 | my $TMPDIR = |
72 | (grep(defined $_ && -d $_ && -w _, |
73 | ((defined $ENV{'TMPDIR'} ? $ENV{'TMPDIR'} : undef), |
a0c8e3cf |
74 | qw(/var/tmp /usr/tmp /tmp c:/temp))))[0]; |
3f2ee006 |
75 | $TMPDIR || die "Cannot find writable temporary directory.\n"; |
76 | } |
77 | |
78 | sub try_compile_and_link { |
79 | my ($c, %args) = @_; |
80 | |
81 | my ($ok) = 0; |
98b50af3 |
82 | my ($tmp) = (($^O eq 'VMS') ? "sys\$scratch:tmp$$" : TMPDIR() . '/' . "tmp$$"); |
3f2ee006 |
83 | local(*TMPC); |
84 | |
85 | my $obj_ext = $Config{obj_ext} || ".o"; |
86 | unlink("$tmp.c", "$tmp$obj_ext"); |
87 | |
88 | if (open(TMPC, ">$tmp.c")) { |
89 | print TMPC $c; |
90 | close(TMPC); |
91 | |
98b50af3 |
92 | my $cccmd = $args{cccmd}; |
3f2ee006 |
93 | |
94 | my $errornull; |
95 | |
96 | my $COREincdir; |
98b50af3 |
97 | |
3f2ee006 |
98 | if ($ENV{PERL_CORE}) { |
99 | my $updir = File::Spec->updir; |
100 | $COREincdir = File::Spec->catdir(($updir) x 3); |
101 | } else { |
102 | $COREincdir = File::Spec->catdir($Config{'archlibexp'}, 'CORE'); |
103 | } |
98b50af3 |
104 | |
3f2ee006 |
105 | my $ccflags = $Config{'ccflags'} . ' ' . "-I$COREincdir"; |
98b50af3 |
106 | |
3f2ee006 |
107 | if ($^O eq 'VMS') { |
108 | if ($ENV{PERL_CORE}) { |
6a20eacc |
109 | # Fragile if the extensions change hierachy within |
110 | # the Perl core but this should do for now. |
111 | $cccmd = "$Config{'cc'} /include=([---]) $tmp.c"; |
3f2ee006 |
112 | } else { |
113 | my $perl_core = $Config{'installarchlib'}; |
114 | $perl_core =~ s/\]$/.CORE]/; |
98b50af3 |
115 | $cccmd = "$Config{'cc'} /include=(perl_root:[000000],$perl_core) $tmp.c"; |
3f2ee006 |
116 | } |
117 | } |
118 | |
119 | if ($args{silent} || !$VERBOSE) { |
120 | $errornull = "2>/dev/null" unless defined $errornull; |
121 | } else { |
122 | $errornull = ''; |
123 | } |
124 | |
98b50af3 |
125 | $cccmd = "$Config{'cc'} -o $tmp $ccflags $tmp.c @$LIBS $errornull" |
3f2ee006 |
126 | unless defined $cccmd; |
98b50af3 |
127 | |
3f2ee006 |
128 | if ($^O eq 'VMS') { |
129 | open( CMDFILE, ">$tmp.com" ); |
130 | print CMDFILE "\$ SET MESSAGE/NOFACILITY/NOSEVERITY/NOIDENT/NOTEXT\n"; |
131 | print CMDFILE "\$ $cccmd\n"; |
98b50af3 |
132 | print CMDFILE "\$ IF \$SEVERITY .NE. 1 THEN EXIT 44\n"; # escalate |
3f2ee006 |
133 | close CMDFILE; |
134 | system("\@ $tmp.com"); |
135 | $ok = $?==0; |
136 | for ("$tmp.c", "$tmp$obj_ext", "$tmp.com", "$tmp$Config{exe_ext}") { |
98b50af3 |
137 | 1 while unlink $_; |
3f2ee006 |
138 | } |
139 | } |
140 | else |
141 | { |
142 | printf "cccmd = $cccmd\n" if $VERBOSE; |
143 | system($cccmd); |
144 | $ok = -s $tmp && -x _; |
145 | unlink("$tmp.c", $tmp); |
146 | } |
147 | } |
148 | |
149 | $ok; |
150 | } |
151 | |
152 | sub has_gettimeofday { |
153 | # confusing but true (if condition true ==> -DHAS_GETTIMEOFDAY already) |
154 | return 0 if $Config{'d_gettimeod'} eq 'define'; |
98b50af3 |
155 | return 1 if try_compile_and_link(<<EOM); |
3f2ee006 |
156 | #include "EXTERN.h" |
157 | #include "perl.h" |
158 | #include "XSUB.h" |
159 | #ifdef I_SYS_TYPES |
160 | # include <sys/types.h> |
161 | #endif |
162 | |
163 | #ifdef I_SYS_TIME |
164 | # include <sys/time.h> |
165 | #endif |
166 | |
167 | #ifdef I_SYS_SELECT |
168 | # include <sys/select.h> /* struct timeval might be hidden in here */ |
169 | #endif |
170 | static int foo() |
171 | { |
172 | struct timeval tv; |
173 | gettimeofday(&tv, 0); |
174 | } |
175 | int main _((int argc, char** argv, char** env)) |
176 | { |
177 | foo(); |
178 | } |
179 | EOM |
180 | return 0; |
181 | } |
182 | |
183 | sub has_x { |
98b50af3 |
184 | my ($x, %args) = @_; |
3f2ee006 |
185 | |
186 | return 1 if |
187 | try_compile_and_link(<<EOM, %args); |
188 | #include "EXTERN.h" |
189 | #include "perl.h" |
190 | #include "XSUB.h" |
191 | |
192 | #ifdef I_UNISTD |
193 | # include <unistd.h> |
194 | #endif |
195 | |
196 | #ifdef I_SYS_TYPES |
197 | # include <sys/types.h> |
198 | #endif |
199 | |
200 | #ifdef I_SYS_TIME |
201 | # include <sys/time.h> |
202 | #endif |
203 | |
204 | int main _((int argc, char** argv, char** env)) |
205 | { |
206 | $x; |
207 | } |
208 | EOM |
209 | return 0; |
210 | } |
211 | |
212 | sub unixinit { |
213 | $DEFINE = ''; |
214 | |
215 | $LIBS = []; |
216 | |
217 | # this might break the link, try it if it can't find some things you |
218 | # honestly think should be in there... |
219 | # $LIBS = ['-lucb -lbsd']; |
220 | |
221 | # ... but ucb is poison for Solaris, and probably Linux. honest. |
222 | $LIBS = [] if $Config{'osname'} eq 'solaris'; |
223 | $LIBS = [] if $Config{'osname'} eq 'linux'; |
224 | $LIBS = ['-lm'] if $Config{'osname'} =~ /sco/i; |
225 | $LIBS = ['-lc'] if $Config{'osname'} =~ /dynixptx/i; |
226 | |
227 | # For nanosleep |
228 | push @$LIBS, '-lrt' unless $Config{'osname'} =~ /irix/; |
229 | push @$LIBS, '-lposix4' ; |
230 | |
231 | my @goodlibs; |
232 | |
98b50af3 |
233 | select(STDOUT); |
234 | $| = 1; |
3f2ee006 |
235 | |
236 | print "Checking for libraries...\n"; |
237 | my $lib; |
238 | for $lib (@$LIBS) { |
98b50af3 |
239 | print "Checking for $lib... "; |
3f2ee006 |
240 | $LIBS = [ $lib ]; |
241 | if ($Config{libs} =~ /\b$lib\b/ || has_x("time(0)")) { |
242 | push @goodlibs, $lib; |
98b50af3 |
243 | print "found.\n"; |
244 | } else { |
245 | print "NOT found.\n"; |
3f2ee006 |
246 | } |
247 | } |
98b50af3 |
248 | $LIBS = [ @goodlibs ]; |
3f2ee006 |
249 | print @$LIBS ? |
250 | "You have extra libraries: @$LIBS.\n" : |
251 | "You have no applicable extra libraries.\n"; |
3f2ee006 |
252 | |
98b50af3 |
253 | print "Looking for gettimeofday()... "; |
3f2ee006 |
254 | my $has_gettimeofday; |
255 | if ($Config{'d_gettimeod'}) { |
256 | $has_gettimeofday++; |
257 | } elsif (has_gettimeofday()) { |
258 | $DEFINE .= ' -DHAS_GETTIMEOFDAY'; |
259 | $has_gettimeofday++; |
260 | } |
261 | |
262 | if ($has_gettimeofday) { |
98b50af3 |
263 | print "found.\n"; |
3f2ee006 |
264 | } else { |
265 | die <<EOD |
266 | Your operating system does not seem to have the gettimeofday() function. |
267 | (or, at least, I cannot find it) |
268 | |
269 | There is no way Time::HiRes is going to work. |
270 | |
271 | I am awfully sorry but I cannot go further. |
272 | |
273 | Aborting configuration. |
274 | |
275 | EOD |
276 | } |
277 | |
98b50af3 |
278 | print "Looking for setitimer()... "; |
3f2ee006 |
279 | my $has_setitimer; |
280 | if ($Config{d_setitimer}) { |
281 | $has_setitimer++; |
282 | } elsif (has_x("setitimer(ITIMER_REAL, 0, 0)")) { |
283 | $has_setitimer++; |
284 | $DEFINE .= ' -DHAS_SETITIMER'; |
285 | } |
286 | |
287 | if ($has_setitimer) { |
98b50af3 |
288 | print "found.\n"; |
3f2ee006 |
289 | } else { |
98b50af3 |
290 | print "NOT found.\n"; |
3f2ee006 |
291 | } |
292 | |
98b50af3 |
293 | print "Looking for getitimer()... "; |
3f2ee006 |
294 | my $has_getitimer; |
295 | if ($Config{d_getitimer}) { |
296 | $has_getitimer++; |
297 | } elsif (has_x("getitimer(ITIMER_REAL, 0)")) { |
298 | $has_getitimer++; |
299 | $DEFINE .= ' -DHAS_GETITIMER'; |
300 | } |
301 | |
302 | if ($has_getitimer) { |
98b50af3 |
303 | print "found.\n"; |
3f2ee006 |
304 | } else { |
98b50af3 |
305 | print "NOT found.\n"; |
3f2ee006 |
306 | } |
307 | |
308 | if ($has_setitimer && $has_getitimer) { |
98b50af3 |
309 | print "You have interval timers (both setitimer and setitimer).\n"; |
3f2ee006 |
310 | } else { |
98b50af3 |
311 | print "You do not have interval timers.\n"; |
3f2ee006 |
312 | } |
313 | |
98b50af3 |
314 | print "Looking for ualarm()... "; |
315 | my $has_ualarm; |
3f2ee006 |
316 | if ($Config{d_ualarm}) { |
317 | $has_ualarm++; |
318 | } elsif (has_x ("ualarm (0, 0)")) { |
319 | $has_ualarm++; |
320 | $DEFINE .= ' -DHAS_UALARM'; |
321 | } |
322 | |
323 | if ($has_ualarm) { |
98b50af3 |
324 | print "found.\n"; |
3f2ee006 |
325 | } else { |
98b50af3 |
326 | print "NOT found.\n"; |
327 | if ($has_setitimer) { |
328 | print "But you have setitimer().\n"; |
329 | print "We can make a Time::HiRes::ualarm().\n"; |
3f2ee006 |
330 | } |
331 | } |
332 | |
98b50af3 |
333 | print "Looking for usleep()... "; |
3f2ee006 |
334 | my $has_usleep; |
335 | if ($Config{d_usleep}) { |
336 | $has_usleep++; |
337 | } elsif (has_x ("usleep (0)")) { |
338 | $has_usleep++; |
339 | $DEFINE .= ' -DHAS_USLEEP'; |
340 | } |
341 | |
342 | if ($has_usleep) { |
98b50af3 |
343 | print "found.\n"; |
3f2ee006 |
344 | } else { |
98b50af3 |
345 | print "NOT found.\n"; |
346 | print "Let's see if you have select()... "; |
3f2ee006 |
347 | if ($Config{'d_select'} eq 'define') { |
98b50af3 |
348 | print "found.\n"; |
349 | print "We can make a Time::HiRes::usleep().\n"; |
3f2ee006 |
350 | } else { |
98b50af3 |
351 | print "NOT found.\n"; |
352 | print "You won't have a Time::HiRes::usleep().\n"; |
3f2ee006 |
353 | } |
354 | } |
355 | |
98b50af3 |
356 | print "Looking for nanosleep()... "; |
3f2ee006 |
357 | my $has_nanosleep; |
358 | if ($Config{d_nanosleep}) { |
359 | $has_nanosleep++; |
360 | } elsif (has_x ("nanosleep (NULL, NULL)")) { |
361 | $has_nanosleep++; |
362 | $DEFINE .= ' -DHAS_NANOSLEEP'; |
363 | } |
364 | |
365 | if ($has_nanosleep) { |
98b50af3 |
366 | print "found.\n"; |
367 | print "You can mix subsecond sleeps with signals.\n"; |
3f2ee006 |
368 | } else { |
98b50af3 |
369 | print "NOT found.\n"; |
370 | print "You cannot mix subsecond sleeps with signals.\n"; |
3f2ee006 |
371 | } |
372 | |
373 | if ($DEFINE) { |
374 | $DEFINE =~ s/^\s+//; |
375 | if (open(XDEFINE, ">xdefine")) { |
376 | print XDEFINE $DEFINE, "\n"; |
377 | close(XDEFINE); |
378 | } |
379 | } |
380 | } |
381 | |
382 | sub doMakefile { |
98b50af3 |
383 | my @makefileopts = (); |
3f2ee006 |
384 | |
385 | if ($] >= 5.005) { |
386 | push (@makefileopts, |
387 | 'AUTHOR' => 'Jarkko Hietaniemi <jhi@iki.fi>', |
388 | 'ABSTRACT_FROM' => 'HiRes.pm', |
389 | ); |
390 | $DEFINE .= " -DATLEASTFIVEOHOHFIVE"; |
391 | } |
392 | |
393 | push (@makefileopts, |
394 | 'NAME' => 'Time::HiRes', |
395 | 'VERSION_FROM' => 'HiRes.pm', # finds $VERSION |
396 | 'LIBS' => $LIBS, # e.g., '-lm' |
397 | 'DEFINE' => $DEFINE, # e.g., '-DHAS_SOMETHING' |
398 | 'XSOPT' => $XSOPT, |
399 | # do not even think about 'INC' => '-I/usr/ucbinclude', Solaris will avenge. |
400 | 'INC' => '', # e.g., '-I/usr/include/other' |
401 | 'INSTALLDIRS' => 'perl', |
402 | 'dist' => { |
403 | 'CI' => 'ci -l', |
404 | 'COMPRESS' => 'gzip -9f', |
405 | 'SUFFIX' => 'gz', |
406 | }, |
407 | clean => { FILES => "xdefine" }, |
98b50af3 |
408 | realclean => {FILES=> 'const-c.inc const-xs.inc'}, |
3f2ee006 |
409 | ); |
410 | |
411 | WriteMakefile(@makefileopts); |
412 | } |
413 | |
98b50af3 |
414 | sub doConstants { |
415 | if (eval {require ExtUtils::Constant; 1}) { |
416 | my @names = (qw(ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF |
417 | ITIMER_REALPROF)); |
418 | foreach (qw (d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer |
419 | d_nanosleep)) { |
420 | my $macro = $_; |
421 | $macro =~ s/d_(.*)/HAS_\U$1/; |
422 | push @names, {name => $_, macro => $macro, value => 1, |
423 | default => ["IV", "0"]}; |
424 | } |
425 | ExtUtils::Constant::WriteConstants( |
426 | NAME => 'Time::HiRes', |
427 | NAMES => \@names, |
428 | ); |
429 | } else { |
430 | foreach my $file ('const-c.inc', 'const-xs.inc') { |
431 | my $fallback = File::Spec->catfile('fallback', $file); |
432 | local $/; |
433 | open IN, "<$fallback" or die "Can't open $fallback: $!"; |
434 | open OUT, ">$file" or die "Can't open $file: $!"; |
435 | print OUT <IN> or die $!; |
436 | close OUT or die "Can't close $file: $!"; |
437 | close IN or die "Can't close $fallback: $!"; |
438 | } |
439 | } |
440 | } |
3f2ee006 |
441 | |
98b50af3 |
442 | sub main { |
443 | print "Configuring Time::HiRes...\n"; |
3f2ee006 |
444 | |
445 | if ($^O =~ /Win32/i) { |
446 | $DEFINE = '-DSELECT_IS_BROKEN'; |
447 | $LIBS = ['']; |
448 | } else { |
449 | unixinit(); |
450 | } |
3f2ee006 |
451 | doMakefile; |
98b50af3 |
452 | doConstants; |
3f2ee006 |
453 | my $make = $Config{'make'} || "make"; |
454 | unless ($ENV{PERL_CORE}) { |
455 | print <<EOM; |
3f2ee006 |
456 | Now you may issue '$make'. Do not forget also '$make test'. |
3f2ee006 |
457 | EOM |
458 | } |
459 | } |
460 | |
461 | &main; |
dcf686c9 |
462 | |
3f2ee006 |
463 | # EOF |