6 if( $ENV{PERL_CORE} ) {
14 use ExtUtils::MakeMaker;
15 use ExtUtils::Constant qw (constant_types C_constant XS_constant autoload);
17 use File::Spec::Functions qw(catfile rel2abs);
18 # Because were are going to be changing directory before running Makefile.PL
20 $perl = rel2abs( $^X ) unless $] < 5.006; # Hack. Until 5.00503 has rel2abs
21 # ExtUtils::Constant::C_constant uses $^X inside a comment, and we want to
22 # compare output to ensure that it is the same. We were probably run as ./perl
23 # whereas we will run the child with the full path in $perl. So make $^X for
24 # us the same as our child will see.
27 print "# perl=$perl\n";
28 my $runperl = "$perl \"-I../../lib\"";
35 print "# $dir being created...\n";
36 mkdir $dir, 0777 or die "mkdir: $!\n";
38 my $output = "output";
40 # For debugging set this to 1.
45 print "# $dir being removed...\n";
46 rmtree($dir) unless $keep_files;
49 my $package = "ExtTest";
51 # Test the code that generates 1 and 2 letter name comparisons.
53 N => 0, 'NE' => 45, E => 90, SE => 135, S => 180, SW => 225, W => 270, NW => 315
57 'A Standard for the Transmission of IP Datagrams on Avian Carriers';
58 # Check that 8 bit and unicode names don't cause problems.
60 if (ord('A') == 193) { # EBCDIC platform
61 $pound = chr 177; # A pound sign. (Currency)
62 } else { # ASCII platform
63 $pound = chr 163; # A pound sign. (Currency)
66 # Check that we can distiguish the pathological case of a string, and the
67 # utf8 representation of that string.
68 my $pound_bytes = my $pound_utf8 = $pound . '1';
69 utf8::encode ($pound_bytes);
71 my @names = ("FIVE", {name=>"OK6", type=>"PV",},
72 {name=>"OK7", type=>"PVN",
73 value=>['"not ok 7\\n\\0ok 7\\n"', 15]},
74 {name => "FARTHING", type=>"NV"},
75 {name => "NOT_ZERO", type=>"UV", value=>"~(UV)0"},
76 {name => "OPEN", type=>"PV", value=>'"/*"', macro=>1},
77 {name => "CLOSE", type=>"PV", value=>'"*/"',
78 macro=>["#if 1\n", "#endif\n"]},
79 {name => "ANSWER", default=>["UV", 42]}, "NOTDEF",
80 {name => "Yes", type=>"YES"},
81 {name => "No", type=>"NO"},
82 {name => "Undef", type=>"UNDEF"},
83 # OK. It wasn't really designed to allow the creation of dual valued constants.
84 # It was more for INADDR_ANY INADDR_BROADCAST INADDR_LOOPBACK INADDR_NONE
85 {name=>"RFC1149", type=>"SV", value=>"sv_2mortal(temp_sv)",
86 pre=>"SV *temp_sv = newSVpv(RFC1149, 0); "
87 . "(void) SvUPGRADE(temp_sv,SVt_PVIV); SvIOK_on(temp_sv); "
88 . "SvIVX(temp_sv) = 1149;"},
89 {name=>"perl", type=>"PV",},
92 push @names, $_ foreach keys %compass;
94 # Automatically compile the list of all the macro names, and make them
96 my @names_only = map {(ref $_) ? $_->{name} : $_} @names;
98 # Exporter::Heavy (currently) isn't able to export these names:
99 push @names, ({name=>"*/", type=>"PV", value=>'"CLOSE"', macro=>1},
100 {name=>"/*", type=>"PV", value=>'"OPEN"', macro=>1},
101 {name=>$pound, type=>"PV", value=>'"Sterling"', macro=>1},
102 {name=>$inf, type=>"PV", value=>'"Infinity"', macro=>1},
103 {name=>$pound_utf8, type=>"PV", value=>'"1 Pound"', macro=>1},
104 {name=>$pound_bytes, type=>"PV", value=>'"1 Pound (as bytes)"',
110 The above set of names seems to produce a suitably bad set of compile
111 problems on a Unicode naive version of ExtUtils::Constant (ie 0.11):
113 nick@thinking-cap 15439-32-utf$ PERL_CORE=1 ./perl lib/ExtUtils/t/Constant.t
115 # perl=/stuff/perl5/15439-32-utf/perl
116 # ext-30370 being created...
117 Wide character in print at lib/ExtUtils/t/Constant.t line 140.
121 ExtTest.xs: In function `constant_1':
122 ExtTest.xs:80: warning: multi-character character constant
123 ExtTest.xs:80: warning: case value out of range
129 my $constant_types = constant_types(); # macro defs
130 my $C_constant = join "\n",
131 C_constant ($package, undef, "IV", $types, undef, undef, @names);
132 my $XS_constant = XS_constant ($package, $types); # XS for ExtTest::constant
134 ################ Header
135 my $header = catfile($dir, "test.h");
136 push @files, "test.h";
137 open FH, ">$header" or die "open >$header: $!\n";
140 #define OK6 "ok 6\\n"
142 #define FARTHING 0.25
147 #define RFC1149 "$parent_rfc1149"
152 while (my ($point, $bearing) = each %compass) {
153 print FH "#define $point $bearing\n"
155 close FH or die "close $header: $!\n";
158 my $xs = catfile($dir, "$package.xs");
159 push @files, "$package.xs";
160 open FH, ">$xs" or die "open >$xs: $!\n";
168 print FH "#include \"test.h\"\n\n";
169 print FH $constant_types;
170 print FH $C_constant, "\n";
171 print FH "MODULE = $package PACKAGE = $package\n";
172 print FH "PROTOTYPES: ENABLE\n";
173 print FH $XS_constant;
174 close FH or die "close $xs: $!\n";
177 my $pm = catfile($dir, "$package.pm");
178 push @files, "$package.pm";
179 open FH, ">$pm" or die "open >$pm: $!\n";
180 print FH "package $package;\n";
181 print FH "use $];\n";
187 printf FH "use warnings;\n" unless $] < 5.006;
193 use vars qw ($VERSION @ISA @EXPORT_OK $AUTOLOAD);
196 @ISA = qw(Exporter DynaLoader);
200 # Print the names of all our autoloaded constants
201 print FH "\t$_\n" foreach (@names_only);
203 # Print the AUTOLOAD subroutine ExtUtils::Constant generated for us
204 print FH autoload ($package, $]);
205 print FH "bootstrap $package \$VERSION;\n1;\n__END__\n";
206 close FH or die "close $pm: $!\n";
208 ################ test.pl
209 my $testpl = catfile($dir, "test.pl");
210 push @files, "test.pl";
211 open FH, ">$testpl" or die "open >$testpl: $!\n";
213 print FH "use strict;\n";
214 print FH "use $package qw(@names_only);\n";
220 if (open OUTPUT, ">$output") {
224 print "not ok 1 # Failed to open '$output': $!\n";
231 # What follows goes to the temporary file.
237 print "not ok 5 # $five\n";
243 # PVN containing embedded \0s
249 my $farthing = FARTHING;
250 if ($farthing == 0.25) {
253 print "not ok 8 # $farthing\n";
257 my $not_zero = NOT_ZERO;
258 if ($not_zero > 0 && $not_zero == ~0) {
261 print "not ok 9 # \$not_zero=$not_zero ~0=" . (~0) . "\n";
264 # Value includes a "*/" in an attempt to bust out of a C comment.
265 # Also tests custom cpp #if clauses
267 if ($close eq '*/') {
270 print "not ok 10 # \$close='$close'\n";
273 # Default values if macro not defined.
278 print "not ok 11 # What do you get if you multiply six by nine? '$answer'\n";
282 my $notdef = eval { NOTDEF; };
283 if (defined $notdef) {
284 print "not ok 12 # \$notdef='$notdef'\n";
285 } elsif ($@ !~ /Your vendor has not defined ExtTest macro NOTDEF/) {
286 print "not ok 12 # \$@='$@'\n";
292 my $notthere = eval { &ExtTest::NOTTHERE; };
293 if (defined $notthere) {
294 print "not ok 13 # \$notthere='$notthere'\n";
295 } elsif ($@ !~ /NOTTHERE is not a valid ExtTest macro/) {
297 print "not ok 13 # \$@='$@'\n";
307 print "not ok 14 # $yes='\$yes'\n";
312 if (defined $no and !$no) {
315 print "not ok 15 # \$no=" . defined ($no) ? "'$no'\n" : "undef\n";
320 unless (defined $undef) {
323 print "not ok 16 # \$undef='$undef'\n";
327 # invalid macro (chosen to look like a mix up between No and SW)
328 $notdef = eval { &ExtTest::So };
329 if (defined $notdef) {
330 print "not ok 17 # \$notdef='$notdef'\n";
331 } elsif ($@ !~ /^So is not a valid ExtTest macro/) {
332 print "not ok 17 # \$@='$@'\n";
337 # invalid defined macro
338 $notdef = eval { &ExtTest::EW };
339 if (defined $notdef) {
340 print "not ok 18 # \$notdef='$notdef'\n";
341 } elsif ($@ !~ /^EW is not a valid ExtTest macro/) {
342 print "not ok 18 # \$@='$@'\n";
350 while (my ($point, $bearing) = each %compass) {
351 print FH "'$point' => $bearing, "
359 while (my ($point, $bearing) = each %compass) {
360 my $val = eval $point;
362 print "# $point: \$@='$@'\n";
364 } elsif (!defined $bearing) {
365 print "# $point: \$val=undef\n";
367 } elsif ($val != $bearing) {
368 print "# $point: \$val=$val, not $bearing\n";
381 my \$rfc1149 = RFC1149;
382 if (\$rfc1149 ne "$parent_rfc1149") {
383 print "not ok 20 # '\$rfc1149' ne '$parent_rfc1149'\n";
388 if (\$rfc1149 != 1149) {
389 printf "not ok 21 # %d != 1149\n", \$rfc1149;
402 print "not ok 22 # \$open='$open'\n";
406 # Do this in 7 bit in case someone is testing with some settings that cause
407 # 8 bit files incapable of storing this character.
409 = map {"'" . join (",", unpack "U*", $_) . "'"}
410 ($pound, $inf, $pound_bytes, $pound_utf8);
411 # Values is a list of strings, such as ('194,163,49', '163,49')
415 # I can see that this child test program might be about to use parts of
419 my ($pound, $inf, $pound_bytes, $pound_utf8) = map {eval "pack 'U*', $_"}
422 print FH join ",", @values;
427 foreach (["perl", "rules", "rules"],
428 ["/*", "OPEN", "OPEN"],
429 ["*/", "CLOSE", "CLOSE"],
430 [$pound, 'Sterling', []],
431 [$inf, 'Infinity', []],
432 [$pound_utf8, '1 Pound', '1 Pound (as bytes)'],
433 [$pound_bytes, '1 Pound (as bytes)', []],
435 # Flag an expected error with a reference for the expect string.
436 my ($string, $expect, $expect_bytes) = @$_;
437 (my $name = $string) =~ s/([^ -~])/sprintf '\x{%X}', ord $1/ges;
438 print "# \"$name\" => \'$expect\'\n";
439 # Try to force this to be bytes if possible.
440 utf8::downgrade ($string, 1);
443 print FH "my (\$error, \$got) = ${package}::constant (\$string);\n";
446 if ($error or $got ne $expect) {
447 print "not ok $test # error '$error', got '$got'\n";
452 print "# Now upgrade '$name' to utf8\n";
453 utf8::upgrade ($string);
456 print FH "my (\$error, \$got) = ${package}::constant (\$string);\n";
459 if ($error or $got ne $expect) {
460 print "not ok $test # error '$error', got '$got'\n";
465 if (defined $expect_bytes) {
466 print "# And now with the utf8 byte sequence for name\n";
467 # Try the encoded bytes.
468 utf8::encode ($string);
471 print FH "my (\$error, \$got) = ${package}::constant (\$string);\n";
474 if (ref $expect_bytes) {
477 print "ok $test # error='$error' (as expected)\n";
479 print "not ok $test # expected error, got no error and '$got'\n";
481 } elsif ($got ne $expect_bytes) {
482 print "not ok $test # error '$error', expect '$expect_bytes', got '$got'\n";
491 close FH or die "close $testpl: $!\n";
493 # This is where the test numbers carry on after the test number above are
497 ################ Makefile.PL
498 # We really need a Makefile.PL because make test for a no dynamic linking perl
499 # will run Makefile.PL again as part of the "make perl" target.
500 my $makefilePL = catfile($dir, "Makefile.PL");
501 push @files, "Makefile.PL";
502 open FH, ">$makefilePL" or die "open >$makefilePL: $!\n";
505 use ExtUtils::MakeMaker;
507 'NAME' => "$package",
508 'VERSION_FROM' => "$package.pm", # finds \$VERSION
510 (#ABSTRACT_FROM => "$package.pm", # XXX add this
511 AUTHOR => "$0") : ())
515 close FH or die "close $makefilePL: $!\n";
517 ################ MANIFEST
518 # We really need a MANIFEST because make distclean checks it.
519 my $manifest = catfile($dir, "MANIFEST");
520 push @files, "MANIFEST";
521 open FH, ">$manifest" or die "open >$manifest: $!\n";
522 print FH "$_\n" foreach @files;
523 close FH or die "close $manifest: $!\n";
525 chdir $dir or die $!; push @INC, '../../lib';
526 END {chdir ".." or warn $!};
528 my @perlout = `$runperl Makefile.PL PERL_CORE=1`;
530 print "not ok 1 # $runperl Makefile.PL failed: $?\n";
531 print "# $_" foreach @perlout;
538 my $makefile = ($^O eq 'VMS' ? 'descrip' : 'Makefile');
539 my $makefile_ext = ($^O eq 'VMS' ? '.mms' : '');
540 if (-f "$makefile$makefile_ext") {
546 # Renamed by make clean
547 my $makefile_rename = $makefile . ($^O eq 'VMS' ? '.mms' : '.old');
549 my $make = $Config{make};
551 $make = $ENV{MAKE} if exists $ENV{MAKE};
553 if ($^O eq 'MSWin32' && $make eq 'nmake') { $make .= " -nologo"; }
557 if ($^O eq 'VMS') { $make .= ' all'; }
558 print "# make = '$make'\n";
561 print "not ok 3 # $make failed: $?\n";
562 print "# $_" foreach @makeout;
568 if ($^O eq 'VMS') { $make =~ s{ all}{}; }
570 if ($Config{usedl}) {
573 my $makeperl = "$make perl";
574 print "# make = '$makeperl'\n";
575 @makeout = `$makeperl`;
577 print "not ok 4 # $makeperl failed: $?\n";
578 print "# $_" foreach @makeout;
585 my $maketest = "$make test";
586 print "# make = '$maketest'\n";
588 @makeout = `$maketest`;
590 if (open OUTPUT, "<$output") {
591 print while <OUTPUT>;
592 close OUTPUT or print "# Close $output failed: $!\n";
594 # Harness will report missing test results at this point.
595 print "# Open <$output failed: $!\n";
599 print "not ok $test # $maketest failed: $?\n";
600 print "# $_" foreach @makeout;
602 print "ok $test - maketest\n";
607 # -x is busted on Win32 < 5.6.1, so we emulate it.
609 if( $^O eq 'MSWin32' && $] <= 5.006001 ) {
610 open(REGENTMP, ">regentmp") or die $!;
611 open(XS, "$package.xs") or die $!;
614 $saw_shebang++ if /^#!.*/i ;
615 print REGENTMP $_ if $saw_shebang;
617 close XS; close REGENTMP;
618 $regen = `$runperl regentmp`;
622 $regen = `$runperl -x $package.xs`;
625 print "not ok $test # $runperl -x $package.xs failed: $?\n";
627 print "ok $test - regen\n";
631 my $expect = $constant_types . $C_constant .
632 "\n#### XS Section:\n" . $XS_constant;
634 if ($expect eq $regen) {
635 print "ok $test - regen worked\n";
637 print "not ok $test - regen worked\n";
638 # open FOO, ">expect"; print FOO $expect;
639 # open FOO, ">regen"; print FOO $regen; close FOO;
643 my $makeclean = "$make clean";
644 print "# make = '$makeclean'\n";
645 @makeout = `$makeclean`;
647 print "not ok $test # $make failed: $?\n";
648 print "# $_" foreach @makeout;
654 sub check_for_bonus_files {
656 my %expect = map {($^O eq 'VMS' ? lc($_) : $_), 1} @_;
659 opendir DIR, $dir or die "opendir '$dir': $!";
660 while (defined (my $entry = readdir DIR)) {
661 $entry =~ s/\.$// if $^O eq 'VMS'; # delete trailing dot that indicates no extension
662 next if $expect{$entry};
663 print "# Extra file '$entry'\n";
667 closedir DIR or warn "closedir '.': $!";
669 print "not ok $test\n";
676 check_for_bonus_files ('.', @files, $output, $makefile_rename, '.', '..');
678 rename $makefile_rename, $makefile
679 or die "Can't rename '$makefile_rename' to '$makefile': $!";
681 unlink $output or warn "Can't unlink '$output': $!";
683 # Need to make distclean to remove ../../lib/ExtTest.pm
684 my $makedistclean = "$make distclean";
685 print "# make = '$makedistclean'\n";
686 @makeout = `$makedistclean`;
688 print "not ok $test # $make failed: $?\n";
689 print "# $_" foreach @makeout;
695 check_for_bonus_files ('.', @files, '.', '..');
697 unless ($keep_files) {
699 unlink $_ or warn "unlink $_: $!";
703 check_for_bonus_files ('.', '.', '..');