blib/lib/Inline/C.pm untested

filter: Statementstmt 0.0% 0 / 558 Branchbran 0.0% 0 / 264 Conditioncond 0.0% 0 / 112 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 55 Podpod 0.0% 0 / 41 totaltotal 0.0% 0 / 1030 CC 133 SCAR 60.1
CC
133
Cov
0%
validate
1640.0
xs_bindings
306.0
get_types
210.0
CRAP 408.1
1 package Inline::C;
2 $Inline::C::VERSION = '0.54_02';
3 $Inline::C::VERSION = eval $Inline::C::VERSION;
4  
5 use strict;
6 require Inline;
7 use Config;
8 use Data::Dumper;
9 use Carp;
10 use Cwd qw(cwd abs_path);
11 use File::Spec;
12  
13 @Inline::C::ISA = qw(Inline);
14  
15 #==============================================================================
16 # Register this module as an Inline language support module
17 #==============================================================================
18 sub register {
19 return {
20 language => 'C',
21 # XXX Breaking this on purpose; let's see who screams
22 # aliases => ['c'],
23 type => 'compiled',
24 suffix => $Config{dlext},
25 };
26 }
27  
28 #==============================================================================
29 # Validate the C config options
30 #==============================================================================
31 sub usage_validate {
32 my $key = shift;
33 return <<END;
34 The value of config option '$key' must be a string or an array ref
35  
36 END
37 }
38  
39 sub validate {
40 my $o = shift;
41  
42 print STDERR "validate Stage\n" if $o->{CONFIG}{BUILD_NOISY};
43 $o->{ILSM} ||= {};
44 $o->{ILSM}{XS} ||= {};
45 $o->{ILSM}{MAKEFILE} ||= {};
46 if (not $o->UNTAINT) {
47 require FindBin;
48 $o->{ILSM}{MAKEFILE}{INC} = "-I\"$FindBin::Bin\"" if not defined $o->{ILSM}{MAKEFILE}{INC};
49 }
50 $o->{ILSM}{AUTOWRAP} = 0 if not defined $o->{ILSM}{AUTOWRAP};
51 $o->{ILSM}{XSMODE} = 0 if not defined $o->{ILSM}{XSMODE};
52 $o->{ILSM}{AUTO_INCLUDE} ||= <<END;
53 #include "EXTERN.h"
54 #include "perl.h"
55 #include "XSUB.h"
56 #include "INLINE.h"
57 END
58 $o->{ILSM}{FILTERS} ||= [];
59 $o->{STRUCT} ||= {
60 '.macros' => '',
61 '.xs' => '',
62 '.any' => 0,
63 '.all' => 0,
64 };
65  
66 while (@_) {
67 my ($key, $value) = (shift, shift);
68 if ($key eq 'PRE_HEAD') {
69 unless( -f $value) {
70 $o->{ILSM}{AUTO_INCLUDE} = $value . "\n" . $o->{ILSM}{AUTO_INCLUDE};
71 }
72 else {
73 my $insert;
74 open RD, '<', $value or die "Couldn't open $value for reading: $!";
75 while(<RD>) {$insert .= $_}
76 close RD or die "Couldn't close $value after reading: $!";
77 $o->{ILSM}{AUTO_INCLUDE} = $insert . "\n" . $o->{ILSM}{AUTO_INCLUDE};
78 }
79 next;
80 }
81 if ($key eq 'MAKE' or
82 $key eq 'AUTOWRAP' or
83 $key eq 'XSMODE'
84 ) {
85 $o->{ILSM}{$key} = $value;
86 next;
87 }
88 if ($key eq 'CC' or
89 $key eq 'LD') {
90 $o->{ILSM}{MAKEFILE}{$key} = $value;
91 next;
92 }
93 if ($key eq 'LIBS') {
94 $o->add_list($o->{ILSM}{MAKEFILE}, $key, $value, []);
95 next;
96 }
97 if ($key eq 'INC') {
98 $o->add_string($o->{ILSM}{MAKEFILE}, $key, quote_space($value), '');
99 next;
100 }
101 if ($key eq 'MYEXTLIB' or
102 $key eq 'OPTIMIZE' or
103 $key eq 'CCFLAGS' or
104 $key eq 'LDDLFLAGS') {
105 $o->add_string($o->{ILSM}{MAKEFILE}, $key, $value, '');
106 next;
107 }
108 if ($key eq 'CCFLAGSEX') {
109 $o->add_string($o->{ILSM}{MAKEFILE}, 'CCFLAGS', $Config{ccflags} . ' ' . $value, '');
110 next;
111 }
112 if ($key eq 'TYPEMAPS') {
113 unless(ref($value) eq 'ARRAY') {
114 croak "TYPEMAPS file '$value' not found"
115 unless -f $value;
116 $value = File::Spec->rel2abs($value);
117 }
118 else {
119 for (my $i = 0; $i < scalar(@$value); $i++) {
120 croak "TYPEMAPS file '${$value}[$i]' not found"
121 unless -f ${$value}[$i];
122 ${$value}[$i] = File::Spec->rel2abs(${$value}[$i]);
123 }
124 }
125 $o->add_list($o->{ILSM}{MAKEFILE}, $key, $value, []);
126 next;
127 }
128 if ($key eq 'AUTO_INCLUDE') {
129 $o->add_text($o->{ILSM}, $key, $value, '');
130 next;
131 }
132 if ($key eq 'BOOT') {
133 $o->add_text($o->{ILSM}{XS}, $key, $value, '');
134 next;
135 }
136 if ($key eq 'PREFIX') {
137 croak "Invalid value for 'PREFIX' option"
138 unless ($value =~ /^\w*$/ and
139 $value !~ /\n/);
140 $o->{ILSM}{XS}{PREFIX} = $value;
141 next;
142 }
143 if ($key eq 'FILTERS') {
144 next if $value eq '1' or $value eq '0'; # ignore ENABLE, DISABLE
145 $value = [$value] unless ref($value) eq 'ARRAY';
146 my %filters;
147 for my $val (@$value) {
148 if (ref($val) eq 'CODE') {
149 $o->add_list($o->{ILSM}, $key, $val, []);
150 }
151 else {
152 eval { require Inline::Filters };
153 croak "'FILTERS' option requires Inline::Filters to be installed."
154 if $@;
155 %filters = Inline::Filters::get_filters($o->{API}{language})
156 unless keys %filters;
157 if (defined $filters{$val}) {
158 my $filter = Inline::Filters->new($val,
159 $filters{$val});
160 $o->add_list($o->{ILSM}, $key, $filter, []);
161 }
162 else {
163 croak "Invalid filter $val specified.";
164 }
165 }
166 }
167 next;
168 }
169 if ($key eq 'STRUCTS') {
170 # A list of struct names
171 if (ref($value) eq 'ARRAY') {
172 for my $val (@$value) {
173 croak "Invalid value for 'STRUCTS' option"
174 unless ($val =~ /^[_a-z][_0-9a-z]*$/i);
175 $o->{STRUCT}{$val}++;
176 }
177 }
178 # Enable or disable
179 elsif ($value =~ /^\d+$/) {
180 $o->{STRUCT}{'.any'} = $value;
181 }
182 # A single struct name
183 else {
184 croak "Invalid value for 'STRUCTS' option"
185 unless ($value =~ /^[_a-z][_0-9a-z]*$/i);
186 $o->{STRUCT}{$value}++;
187 }
188 eval { require Inline::Struct };
189 croak "'STRUCTS' option requires Inline::Struct to be installed."
190 if $@;
191 $o->{STRUCT}{'.any'} = 1;
192 next;
193 }
194 if($key eq 'PROTOTYPES') {
195 $o->{CONFIG}{PROTOTYPES} = $value;
196 next if $value eq 'ENABLE';
197 next if $value eq 'DISABLE';
198 die "PROTOTYPES can be only either 'ENABLE' or 'DISABLE' - not $value";
199 }
200 if($key eq 'PROTOTYPE') {
201 die "PROTOTYPE configure arg must specify a hash reference"
202 unless ref($value) eq 'HASH';
203 $o->{CONFIG}{PROTOTYPE} = $value;
204 next;
205 }
206 my $class = ref $o; # handles subclasses correctly.
207 croak "'$key' is not a valid config option for $class\n";
208 }
209 }
210  
211 sub add_list {
212 my $o = shift;
213 my ($ref, $key, $value, $default) = @_;
214 $value = [$value] unless ref $value eq 'ARRAY';
215 for (@$value) {
216 if (defined $_) {
217 push @{$ref->{$key}}, $_;
218 }
219 else {
220 $ref->{$key} = $default;
221 }
222 }
223 }
224  
225 sub add_string {
226 my $o = shift;
227 my ($ref, $key, $value, $default) = @_;
228 $value = [$value] unless ref $value;
229 croak usage_validate($key) unless ref($value) eq 'ARRAY';
230 for (@$value) {
231 if (defined $_) {
232 $ref->{$key} .= ' ' . $_;
233 }
234 else {
235 $ref->{$key} = $default;
236 }
237 }
238 }
239  
240 sub add_text {
241 my $o = shift;
242 my ($ref, $key, $value, $default) = @_;
243 $value = [$value] unless ref $value;
244 croak usage_validate($key) unless ref($value) eq 'ARRAY';
245 for (@$value) {
246 if (defined $_) {
247 chomp;
248 $ref->{$key} .= $_ . "\n";
249 }
250 else {
251 $ref->{$key} = $default;
252 }
253 }
254 }
255  
256 #==============================================================================
257 # Return a small report about the C code..
258 #==============================================================================
259 sub info {
260 my $o = shift;
261 return <<END if $o->{ILSM}{XSMODE};
262 No information is currently generated when using XSMODE.
263  
264 END
265 my $text = '';
266 $o->preprocess;
267 $o->parse;
268 if (defined $o->{ILSM}{parser}{data}{functions}) {
269 $text .= "The following Inline $o->{API}{language} function(s) have been successfully bound to Perl:\n";
270 my $parser = $o->{ILSM}{parser};
271 my $data = $parser->{data};
272 for my $function (sort @{$data->{functions}}) {
273 my $return_type = $data->{function}{$function}{return_type};
274 my @arg_names = @{$data->{function}{$function}{arg_names}};
275 my @arg_types = @{$data->{function}{$function}{arg_types}};
276 my @args = map {$_ . ' ' . shift @arg_names} @arg_types;
277 $text .= "\t$return_type $function(" . join(', ', @args) . ")\n";
278 }
279 }
280 else {
281 $text .= "No $o->{API}{language} functions have been successfully bound to Perl.\n\n";
282 }
283 $text .= Inline::Struct::info($o) if $o->{STRUCT}{'.any'};
284 return $text;
285 }
286  
287 sub config {
288 my $o = shift;
289 }
290  
291 #==============================================================================
292 # Parse and compile C code
293 #==============================================================================
294 my $total_build_time;
295 sub build {
296 my $o = shift;
297  
298 if ($o->{CONFIG}{BUILD_TIMERS}) {
299 eval {require Time::HiRes};
300 croak "You need Time::HiRes for BUILD_TIMERS option:\n$@" if $@;
301 $total_build_time = Time::HiRes::time();
302 }
303 $o->call('preprocess', 'Build Preprocess');
304 $o->call('parse', 'Build Parse');
305 $o->call('write_XS', 'Build Glue 1');
306 $o->call('write_Inline_headers', 'Build Glue 2');
307 $o->call('write_Makefile_PL', 'Build Glue 3');
308 $o->call('compile', 'Build Compile');
309 if ($o->{CONFIG}{BUILD_TIMERS}) {
310 $total_build_time = Time::HiRes::time() - $total_build_time;
311 printf STDERR "Total Build Time: %5.4f secs\n", $total_build_time;
312 }
313 }
314  
315 sub call {
316 my ($o, $method, $header, $indent) = (@_, 0);
317 my $time;
318 my $i = ' ' x $indent;
319 print STDERR "${i}Starting $header Stage\n" if $o->{CONFIG}{BUILD_NOISY};
320 $time = Time::HiRes::time()
321 if $o->{CONFIG}{BUILD_TIMERS};
322  
323 $o->$method();
324  
325 $time = Time::HiRes::time() - $time
326 if $o->{CONFIG}{BUILD_TIMERS};
327 print STDERR "${i}Finished $header Stage\n" if $o->{CONFIG}{BUILD_NOISY};
328 printf STDERR "${i}Time for $header Stage: %5.4f secs\n", $time
329 if $o->{CONFIG}{BUILD_TIMERS};
330 print STDERR "\n" if $o->{CONFIG}{BUILD_NOISY};
331 }
332  
333 #==============================================================================
334 # Apply any
335 #==============================================================================
336 sub preprocess {
337 my $o = shift;
338 return if $o->{ILSM}{parser};
339 $o->get_maps;
340 $o->get_types;
341 $o->{ILSM}{code} = $o->filter(@{$o->{ILSM}{FILTERS}});
342 }
343  
344 #==============================================================================
345 # Parse the function definition information out of the C code
346 #==============================================================================
347 sub parse {
348 my $o = shift;
349 return if $o->{ILSM}{parser};
350 return if $o->{ILSM}{XSMODE};
351 my $parser = $o->{ILSM}{parser} = $o->get_parser;
352 $parser->{data}{typeconv} = $o->{ILSM}{typeconv};
353 $parser->{data}{AUTOWRAP} = $o->{ILSM}{AUTOWRAP};
354 Inline::Struct::parse($o) if $o->{STRUCT}{'.any'};
355 $parser->code($o->{ILSM}{code})
356 or croak <<END;
357 Bad $o->{API}{language} code passed to Inline at @{[caller(2)]}
358 END
359 }
360  
361 # Create and initialize a parser
362 sub get_parser {
363 my $o = shift;
364 Inline::C::_parser_test("Inline::C::get_parser called\n") if $o->{CONFIG}{_TESTING};
365 require Inline::C::ParseRecDescent;
366 Inline::C::ParseRecDescent::get_parser($o);
367 }
368  
369 #==============================================================================
370 # Gather the path names of all applicable typemap files.
371 #==============================================================================
372 sub get_maps {
373 my $o = shift;
374  
375 print STDERR "get_maps Stage\n" if $o->{CONFIG}{BUILD_NOISY};
376 my $typemap = '';
377 my $file;
378 $file = File::Spec->catfile($Config::Config{installprivlib},"ExtUtils","typemap");
379 $typemap = $file if -f $file;
380 $file = File::Spec->catfile($Config::Config{privlibexp} ,"ExtUtils","typemap");
381 $typemap = $file
382 if (not $typemap and -f $file);
383 warn "Can't find the default system typemap file"
384 if (not $typemap and $^W);
385  
386 unshift(@{$o->{ILSM}{MAKEFILE}{TYPEMAPS}}, $typemap) if $typemap;
387  
388 if (not $o->UNTAINT) {
389 require FindBin;
390 $file = File::Spec->catfile($FindBin::Bin,"typemap");
391 if ( -f $file ) {
392 push(@{$o->{ILSM}{MAKEFILE}{TYPEMAPS}}, $file);
393 }
394 }
395 }
396  
397 #==============================================================================
398 # This routine parses XS typemap files to get a list of valid types to create
399 # bindings to. This code is mostly hacked out of Larry Wall's xsubpp program.
400 #==============================================================================
401 sub get_types {
402 my (%type_kind, %proto_letter, %input_expr, %output_expr);
403 my $o = shift;
404 local $_;
405 croak "No typemaps specified for Inline C code"
406 unless @{$o->{ILSM}{MAKEFILE}{TYPEMAPS}};
407  
408 my $proto_re = "[" . quotemeta('\$%&*@;') . "]";
409 foreach my $typemap (@{$o->{ILSM}{MAKEFILE}{TYPEMAPS}}) {
410 next unless -e $typemap;
411 # skip directories, binary files etc.
412 warn("Warning: ignoring non-text typemap file '$typemap'\n"), next
413 unless -T $typemap;
414 open(TYPEMAP, $typemap)
415 or warn ("Warning: could not open typemap file '$typemap': $!\n"), next;
416 my $mode = 'Typemap';
417 my $junk = "";
418 my $current = \$junk;
419 while (<TYPEMAP>) {
420 next if /^\s*\#/;
421 my $line_no = $. + 1;
422 if (/^INPUT\s*$/) {$mode = 'Input'; $current = \$junk; next}
423 if (/^OUTPUT\s*$/) {$mode = 'Output'; $current = \$junk; next}
424 if (/^TYPEMAP\s*$/) {$mode = 'Typemap'; $current = \$junk; next}
425 if ($mode eq 'Typemap') {
426 chomp;
427 my $line = $_;
428 TrimWhitespace($_);
429 # skip blank lines and comment lines
430 next if /^$/ or /^\#/;
431 my ($type,$kind, $proto) =
432 /^\s*(.*?\S)\s+(\S+)\s*($proto_re*)\s*$/ or
433 warn("Warning: File '$typemap' Line $. '$line' TYPEMAP entry needs 2 or 3 columns\n"), next;
434 $type = TidyType($type);
435 $type_kind{$type} = $kind;
436 # prototype defaults to '$'
437 $proto = "\$" unless $proto;
438 warn("Warning: File '$typemap' Line $. '$line' Invalid prototype '$proto'\n")
439 unless ValidProtoString($proto);
440 $proto_letter{$type} = C_string($proto);
441 }
442 elsif (/^\s/) {
443 $$current .= $_;
444 }
445 elsif ($mode eq 'Input') {
446 s/\s+$//;
447 $input_expr{$_} = '';
448 $current = \$input_expr{$_};
449 }
450 else {
451 s/\s+$//;
452 $output_expr{$_} = '';
453 $current = \$output_expr{$_};
454 }
455 }
456 close(TYPEMAP);
457 }
458  
459 my %valid_types =
460 map {($_, 1)}
461 grep {defined $input_expr{$type_kind{$_}}}
462 keys %type_kind;
463  
464 my %valid_rtypes =
465 map {($_, 1)}
466 (grep {defined $output_expr{$type_kind{$_}}}
467 keys %type_kind), 'void';
468  
469 $o->{ILSM}{typeconv}{type_kind} = \%type_kind;
470 $o->{ILSM}{typeconv}{input_expr} = \%input_expr;
471 $o->{ILSM}{typeconv}{output_expr} = \%output_expr;
472 $o->{ILSM}{typeconv}{valid_types} = \%valid_types;
473 $o->{ILSM}{typeconv}{valid_rtypes} = \%valid_rtypes;
474 }
475  
476 sub ValidProtoString ($) {
477 my $string = shift;
478 my $proto_re = "[" . quotemeta('\$%&*@;') . "]";
479 return ($string =~ /^$proto_re+$/) ? $string : 0;
480 }
481  
482 sub TrimWhitespace {
483 $_[0] =~ s/^\s+|\s+$//go;
484 }
485  
486 sub TidyType {
487 local $_ = shift;
488 s|\s*(\*+)\s*|$1|g;
489 s|(\*+)| $1 |g;
490 s|\s+| |g;
491 TrimWhitespace($_);
492 $_;
493 }
494  
495 sub C_string ($) {
496 (my $string = shift) =~ s|\\|\\\\|g;
497 $string;
498 }
499  
500 #==============================================================================
501 # Write the XS code
502 #==============================================================================
503 sub write_XS {
504 my $o = shift;
505 my $modfname = $o->{API}{modfname};
506 my $module = $o->{API}{module};
507 $o->mkpath($o->{API}{build_dir});
508 open XS, "> ".File::Spec->catfile($o->{API}{build_dir},"$modfname.xs")
509 or croak $!;
510 if ($o->{ILSM}{XSMODE}) {
511 warn <<END if $^W and $o->{ILSM}{code} !~ /MODULE\s*=\s*$module\b/;
512 While using Inline XSMODE, your XS code does not have a line with
513  
514 MODULE = $module
515  
516 You should use the Inline NAME config option, and it should match the
517 XS MODULE name.
518  
519 END
520 print XS $o->xs_code;
521 }
522 else {
523 print XS $o->xs_generate;
524 }
525 close XS;
526 }
527  
528 #==============================================================================
529 # Generate the XS glue code (piece together lots of snippets)
530 #==============================================================================
531 sub xs_generate {
532 my $o = shift;
533 return join '', ($o->xs_includes,
534 $o->xs_struct_macros,
535 $o->xs_code,
536 $o->xs_struct_code,
537 $o->xs_bindings,
538 $o->xs_boot,
539 );
540 }
541  
542 sub xs_includes {
543 my $o = shift;
544 return $o->{ILSM}{AUTO_INCLUDE};
545 }
546  
547 sub xs_struct_macros {
548 my $o = shift;
549 return $o->{STRUCT}{'.macros'};
550 }
551  
552 sub xs_code {
553 my $o = shift;
554 return $o->{ILSM}{code};
555 }
556  
557 sub xs_struct_code {
558 my $o = shift;
559 return $o->{STRUCT}{'.xs'};
560 }
561  
562 sub xs_boot {
563 my $o = shift;
564 if (defined $o->{ILSM}{XS}{BOOT} and
565 $o->{ILSM}{XS}{BOOT}) {
566 return <<END;
567 BOOT:
568 $o->{ILSM}{XS}{BOOT}
569 END
570 }
571 return '';
572 }
573  
574 sub xs_bindings {
575 my $o = shift;
576 my $dir = '_Inline_test';
577  
578 if($o->{CONFIG}{_TESTING}) {
579 if(! -d $dir) {
580 my $ok = mkdir $dir;
581 warn $! if !$ok;
582 }
583  
584 if(! -f "$dir/void_test") {
585 warn $! if !open(TEST_FH, '>', "$dir/void_test");
586 warn $! if !close(TEST_FH);
587 }
588 }
589  
590 my ($pkg, $module) = @{$o->{API}}{qw(pkg module)};
591 my $prefix = (($o->{ILSM}{XS}{PREFIX}) ?
592 "PREFIX = $o->{ILSM}{XS}{PREFIX}" :
593 '');
594  
595 my $prototypes = defined($o->{CONFIG}{PROTOTYPES}) ? $o->{CONFIG}{PROTOTYPES}
596 : 'DISABLE';
597  
598 my $XS = <<END;
599  
600 MODULE = $module PACKAGE = $pkg $prefix
601  
602 PROTOTYPES: $prototypes
603  
604 END
605  
606 my $parser = $o->{ILSM}{parser};
607 my $data = $parser->{data};
608  
609 warn("Warning. No Inline C functions bound to Perl in ", $o->{API}{script}, "\n" .
610 "Check your C function definition(s) for Inline compatibility\n\n")
611 if ((not defined$data->{functions}) and ($^W));
612  
613 for my $function (@{$data->{functions}}) {
614 my $return_type = $data->{function}->{$function}->{return_type};
615 my @arg_names = @{$data->{function}->{$function}->{arg_names}};
616 my @arg_types = @{$data->{function}->{$function}->{arg_types}};
617  
618 $XS .= join '', ("\n$return_type\n$function (",
619 join(', ', @arg_names), ")\n");
620  
621 for my $arg_name (@arg_names) {
622 my $arg_type = shift @arg_types;
623 last if $arg_type eq '...';
624 $XS .= "\t$arg_type\t$arg_name\n";
625 }
626  
627 my %h;
628 if (defined($o->{CONFIG}{PROTOTYPE})) {
629 %h = %{$o->{CONFIG}{PROTOTYPE}};
630 }
631  
632 if(defined($h{$function})) {
633 $XS .= " PROTOTYPE: $h{$function}\n";
634 }
635  
636 my $listargs = '';
637 $listargs = pop @arg_names if (@arg_names and
638 $arg_names[-1] eq '...');
639 my $arg_name_list = join(', ', @arg_names);
640  
641 if ($return_type eq 'void') {
642 if($o->{CONFIG}{_TESTING}) {
643 $XS .= <<END;
644 PREINIT:
645 PerlIO* stream;
646 I32* temp;
647 PPCODE:
648 temp = PL_markstack_ptr++;
649 $function($arg_name_list);
650 stream = PerlIO_open(\"$dir/void_test\", \"a\");
651 if(stream == NULL) warn(\"%s\\n\", \"Unable to open $dir/void_test for appending\");
652 if (PL_markstack_ptr != temp) {
653 PerlIO_printf(stream, \"%s\\n\", \"TRULY_VOID\");
654 PerlIO_close(stream);
655 PL_markstack_ptr = temp;
656 XSRETURN_EMPTY; /* return empty stack */
657 }
658 PerlIO_printf(stream, \"%s\\n\", \"LIST_CONTEXT\");
659 PerlIO_close(stream);
660 return; /* assume stack size is correct */
661 END
662 }
663 else {
664 $XS .= <<END;
665 PREINIT:
666 I32* temp;
667 PPCODE:
668 temp = PL_markstack_ptr++;
669 $function($arg_name_list);
670 if (PL_markstack_ptr != temp) {
671 /* truly void, because dXSARGS not invoked */
672 PL_markstack_ptr = temp;
673 XSRETURN_EMPTY; /* return empty stack */
674 }
675 /* must have used dXSARGS; list context implied */
676 return; /* assume stack size is correct */
677 END
678 }
679 }
680 elsif ($listargs) {
681 $XS .= <<END;
682 PREINIT:
683 I32* temp;
684 CODE:
685 temp = PL_markstack_ptr++;
686 RETVAL = $function($arg_name_list);
687 PL_markstack_ptr = temp;
688 OUTPUT:
689 RETVAL
690 END
691 }
692 }
693 $XS .= "\n";
694 return $XS;
695 }
696  
697 #==============================================================================
698 # Generate the INLINE.h file.
699 #==============================================================================
700 sub write_Inline_headers {
701 my $o = shift;
702  
703 open HEADER, "> ".File::Spec->catfile($o->{API}{build_dir},"INLINE.h")
704 or croak;
705  
706 print HEADER <<'END';
707 #define Inline_Stack_Vars dXSARGS
708 #define Inline_Stack_Items items
709 #define Inline_Stack_Item(x) ST(x)
710 #define Inline_Stack_Reset sp = mark
711 #define Inline_Stack_Push(x) XPUSHs(x)
712 #define Inline_Stack_Done PUTBACK
713 #define Inline_Stack_Return(x) XSRETURN(x)
714 #define Inline_Stack_Void XSRETURN(0)
715  
716 #define INLINE_STACK_VARS Inline_Stack_Vars
717 #define INLINE_STACK_ITEMS Inline_Stack_Items
718 #define INLINE_STACK_ITEM(x) Inline_Stack_Item(x)
719 #define INLINE_STACK_RESET Inline_Stack_Reset
720 #define INLINE_STACK_PUSH(x) Inline_Stack_Push(x)
721 #define INLINE_STACK_DONE Inline_Stack_Done
722 #define INLINE_STACK_RETURN(x) Inline_Stack_Return(x)
723 #define INLINE_STACK_VOID Inline_Stack_Void
724  
725 #define inline_stack_vars Inline_Stack_Vars
726 #define inline_stack_items Inline_Stack_Items
727 #define inline_stack_item(x) Inline_Stack_Item(x)
728 #define inline_stack_reset Inline_Stack_Reset
729 #define inline_stack_push(x) Inline_Stack_Push(x)
730 #define inline_stack_done Inline_Stack_Done
731 #define inline_stack_return(x) Inline_Stack_Return(x)
732 #define inline_stack_void Inline_Stack_Void
733 END
734  
735 close HEADER;
736 }
737  
738 #==============================================================================
739 # Generate the Makefile.PL
740 #==============================================================================
741 sub write_Makefile_PL {
742 my $o = shift;
743 $o->{ILSM}{xsubppargs} = '';
744 my $i = 0;
745 for (@{$o->{ILSM}{MAKEFILE}{TYPEMAPS}}) {
746 $o->{ILSM}{xsubppargs} .= "-typemap \"$_\" ";
747 $o->{ILSM}{MAKEFILE}{TYPEMAPS}->[$i++] = fix_space($_);
748 }
749  
750 my %options = (
751 VERSION => $o->{API}{version} || '0.00',
752 %{$o->{ILSM}{MAKEFILE}},
753 NAME => $o->{API}{module},
754 );
755  
756 open MF, "> ".File::Spec->catfile($o->{API}{build_dir},"Makefile.PL")
757 or croak;
758  
759 print MF <<END;
760 use ExtUtils::MakeMaker;
761 my %options = %\{
762 END
763  
764 local $Data::Dumper::Terse = 1;
765 local $Data::Dumper::Indent = 1;
766 print MF Data::Dumper::Dumper(\ %options);
767  
768 print MF <<END;
769 \};
770 WriteMakefile(\%options);
771  
772 # Remove the Makefile dependency. Causes problems on a few systems.
773 sub MY::makefile { '' }
774 END
775 close MF;
776 }
777  
778 #==============================================================================
779 # Run the build process.
780 #==============================================================================
781 sub compile {
782 my $o = shift;
783  
784 my $build_dir = $o->{API}{build_dir};
785 my $cwd = &cwd;
786 ($cwd) = $cwd =~ /(.*)/ if $o->UNTAINT;
787  
788 chdir $build_dir;
789 # Run these in an eval block, so that we get to chdir back to
790 # $cwd if there's a failure. (Ticket #81375.)
791 eval {
792 $o->call('makefile_pl', '"perl Makefile.PL"', 2);
793 $o->call('make', '"make"', 2);
794 $o->call('make_install', '"make install"', 2);
795 };
796 chdir $cwd;
797 die if $@; #Die now that we've done the chdir back to $cwd. (#81375)
798 $o->call('cleanup', 'Cleaning Up', 2);
799 }
800  
801 sub makefile_pl {
802 my ($o) = @_;
803 my $perl;
804 -f ($perl = $Config::Config{perlpath})
805 or ($perl = $^X)
806 or croak "Can't locate your perl binary";
807 $perl = qq{"$perl"} if $perl =~ m/\s/;
808 $o->system_call("$perl Makefile.PL", 'out.Makefile_PL');
809 $o->fix_make;
810 }
811 sub make {
812 my ($o) = @_;
813 my $make = $o->{ILSM}{MAKE} || $Config::Config{make}
814 or croak "Can't locate your make binary";
815 local $ENV{MAKEFLAGS} = $ENV{MAKEFLAGS} =~ s/(--jobserver-fds=[\d,]+)//;
816 $o->system_call("$make", 'out.make');
817 }
818 sub make_install {
819 my ($o) = @_;
820 my $make = $o->{ILSM}{MAKE} || $Config::Config{make}
821 or croak "Can't locate your make binary";
822 local $ENV{MAKEFLAGS} = $ENV{MAKEFLAGS} =~ s/(--jobserver-fds=[\d,]+)//;
823 $o->system_call("$make pure_install", 'out.make_install');
824 }
825 sub cleanup {
826 my ($o) = @_;
827 my ($modpname, $modfname, $install_lib) =
828 @{$o->{API}}{qw(modpname modfname install_lib)};
829 if ($o->{API}{cleanup}) {
830 $o->rmpath(File::Spec->catdir($o->{API}{directory},'build'),
831 $modpname);
832 my $autodir = File::Spec->catdir($install_lib,'auto',$modpname);
833 unlink (File::Spec->catfile($autodir,'.packlist'),
834 File::Spec->catfile($autodir,'$modfname.bs'),
835 File::Spec->catfile($autodir,'$modfname.exp'), #MSWin32
836 File::Spec->catfile($autodir,'$modfname.lib'), #MSWin32
837 );
838 }
839 }
840  
841 sub system_call {
842 my ($o, $cmd, $output_file) = @_;
843 my $build_noisy =
844 defined $ENV{PERL_INLINE_BUILD_NOISY}
845 ? $ENV{PERL_INLINE_BUILD_NOISY}
846 : $o->{CONFIG}{BUILD_NOISY};
847 $build_noisy = undef if $build_noisy and $^O eq 'MSWin32' and $Config::Config{sh} =~ /^cmd/;
848 if (not $build_noisy) {
849 $cmd = "$cmd > $output_file 2>&1";
850 }
851 ($cmd) = $cmd =~ /(.*)/ if $o->UNTAINT;
852 system($cmd) == 0
853 or croak($o->build_error_message($cmd, $output_file, $build_noisy));
854 }
855  
856 sub build_error_message {
857 my ($o, $cmd, $output_file, $build_noisy) = @_;
858 my $build_dir = $o->{API}{build_dir};
859 my $output = '';
860 if (not $build_noisy and
861 open(OUTPUT, $output_file)
862 ) {
863 local $/;
864 $output = <OUTPUT>;
865 close OUTPUT;
866 }
867  
868 my $errcode = $? >> 8;
869 $output .= <<END;
870  
871 A problem was encountered while attempting to compile and install your Inline
872 $o->{API}{language} code. The command that failed was:
873 \"$cmd\" with error code $errcode
874  
875 The build directory was:
876 $build_dir
877  
878 To debug the problem, cd to the build directory, and inspect the output files.
879  
880 END
881 if ($cmd =~ /^make >/) {
882 for (sort keys %ENV) {
883 $output .= "$_ = $ENV{$_}\n" if /^MAKE/;
884 }
885 }
886 return $output;
887 }
888  
889 #==============================================================================
890 # This routine fixes problems with the MakeMaker Makefile.
891 #==============================================================================
892 my %fixes = (
893 INSTALLSITEARCH => 'install_lib',
894 INSTALLDIRS => 'installdirs',
895 XSUBPPARGS => 'xsubppargs',
896 INSTALLSITELIB => 'install_lib',
897 );
898  
899 sub fix_make {
900 use strict;
901 my (@lines, $fix);
902 my $o = shift;
903  
904 $o->{ILSM}{install_lib} = $o->{API}{install_lib};
905 $o->{ILSM}{installdirs} = 'site';
906  
907 open(MAKEFILE, '< Makefile')
908 or croak "Can't open Makefile for input: $!\n";
909 @lines = <MAKEFILE>;
910 close MAKEFILE;
911  
912 open(MAKEFILE, '> Makefile')
913 or croak "Can't open Makefile for output: $!\n";
914 for (@lines) {
915 if (/^(\w+)\s*=\s*\S+.*$/ and
916 $fix = $fixes{$1}
917 ) {
918 my $fixed = $o->{ILSM}{$fix};
919 $fixed = fix_space($fixed) if $fix eq 'install_lib';
920 print MAKEFILE "$1 = $fixed\n";
921 }
922 else {
923 print MAKEFILE;
924 }
925 }
926 close MAKEFILE;
927 }
928  
929 sub quote_space {
930  
931 # Do nothing if $ENV{NO_INSANE_DIRNAMES} is set
932 return $_[0] if $ENV{NO_INSANE_DIRNAMES};
933  
934 # If $_[0] contains one or more doublequote characters, assume
935 # that whitespace has already been quoted as required. Hence,
936 # do nothing other than immediately return $_[0] as is.
937 # We currently don't properly handle tabs either, so we'll
938 # do the same if $_[0] =~ /\t/.
939 return $_[0] if ($_[0] =~ /"/ || $_[0] =~ /\t/);
940  
941 # We want to split on /\s\-I/ not /\-I/
942 my @in = split /\s\-I/, $_[0];
943 my $s = @in - 1;
944 my %s;
945 my %q;
946  
947 # First up, let's reinstate the ' ' characters that split
948 # removed
949 for(my $i = 0; $i < $s; $i++) {
950 $in[$i] .= ' ';
951 }
952  
953 # This for{} block dies if it finds that any of the ' -I'
954 # occurrences in $_[0] are part of a directory name.
955 for(my $i = 1; $i < $s; $i++) {
956 my $t = $in[$i + 1];
957 while($t =~ /\s$/) {chop $t}
958 die "Found a '", $in[$i], "-I", $t, "' directory.",
959 " INC Config argument is ambiguous.",
960 " Please use doublequotes to signify your intentions"
961 if -d ($in[$i] . "-I" . $t);
962 }
963  
964 $s++; # Now the same as scalar(@in)
965  
966 # Remove (but also Keep track of the amount of) whitespace
967 # at the end of each element of @in.
968 for(my $i = 0; $i < $s; $i++) {
969 my $count = 0;
970 while($in[$i] =~ /\s$/) {
971 chop $in[$i];
972 $count++;
973 }
974 $s{$i} = $count;
975 }
976  
977 # Note which elements of @in still contain whitespace. These
978 # (and only these) elements will be quoted
979 for(my $i = 0; $i < $s; $i++) {
980 $q{$i} = 1 if $in[$i] =~ /\s/;
981 }
982  
983 # Reinstate the occurrences of '-I' that were removed by split(),
984 # insert any quotes that are needed, reinstate the whitespace
985 # that was removed earlier, then join() the array back together
986 # again.
987 for(my $i = 0; $i < $s; $i++) {
988 $in[$i] = '-I' . $in[$i] if $i;
989 $in[$i] = '"' . $in[$i] . '"' if $q{$i};
990 $in[$i] .= ' ' x $s{$i};
991 }
992  
993 # Note: If there was no whitespace that needed quoting, the
994 # original argument should not have changed in any way.
995  
996 my $out = join '', @in;
997 $out =~ s/"\-I\s+\//"\-I\//g;
998 $_[0] = $out;
999 }
1000  
1001 sub fix_space {
1002 $_[0] =~ s/ /\\ /g if $_[0] =~ / /;
1003 $_[0];
1004 }
1005  
1006 #==============================================================================
1007 # This routine used by C/t/09parser to test that the expected parser is in use
1008 #==============================================================================
1009  
1010 sub _parser_test {
1011 my $dir = '_Inline_test';
1012 if(! -d $dir) {
1013 my $ok = mkdir $dir;
1014 warn $! if !$ok;
1015 }
1016  
1017 warn $! if !open(TEST_FH, '>>', "$dir/parser_id");
1018 print TEST_FH $_[0];
1019 warn $! if !close(TEST_FH);
1020 }
1021  
1022 #=======================================================================
1023 # This routine used to cleanup files created by _TESTING (config option)
1024 #=======================================================================
1025  
1026 sub _testing_cleanup {
1027 my $dir = '_Inline_test';
1028  
1029 if(-f "$dir/parser_id") {
1030 warn "Failed to unlink C/$dir/parser_id\n" if !unlink("$dir/parser_id");
1031 }
1032  
1033 if(-f "$dir/void_test") {
1034 warn "Failed to unlink C/$dir/void_test\n" if !unlink("$dir/void_test");
1035 }
1036 }
1037  
1038 1;
1039  
1040 __END__