This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
ExtUtils-CBuilder test tweak for VMS.
[perl5.git] / cpan / ExtUtils-CBuilder / t / 02-link.t
1 #! perl -w
2
3 use strict;
4 use Test::More;
5 BEGIN {
6   if ($^O eq 'VMS') {
7     # So we can get the return value of system()
8     require vmsish;
9     import vmsish;
10   }
11 }
12 use ExtUtils::CBuilder;
13 use File::Spec;
14
15 # TEST doesn't like extraneous output
16 my $quiet = $ENV{PERL_CORE} && !$ENV{HARNESS_ACTIVE};
17 my ($source_file, $object_file, $exe_file);
18
19 my $b = ExtUtils::CBuilder->new(quiet => $quiet);
20
21 # test plan
22 if ($^O eq 'MSWin32') {
23   plan skip_all => "link_executable() is not implemented yet on Win32";
24 }
25 elsif ( ! $b->have_compiler ) {
26   plan skip_all => "no compiler available for testing";
27 }
28 else {
29   plan tests => 8;
30 }
31
32 ok $b, "created EU::CB object";
33
34 $source_file = File::Spec->catfile('t', 'compilet.c');
35 {
36   local *FH;
37   open FH, "> $source_file" or die "Can't create $source_file: $!";
38   print FH "int main(void) { return 11; }\n";
39   close FH;
40 }
41 ok -e $source_file, "generated '$source_file'";
42
43 # Compile
44 eval { $object_file = $b->compile(source => $source_file) };
45 is $@, q{}, "no exception from compilation";
46 ok -e $object_file, "found object file";
47
48 # Link
49 SKIP: {
50   skip "error compiling source", 4
51     unless -e $object_file;
52
53   my @temps;
54   eval { ($exe_file, @temps) = $b->link_executable(objects => $object_file) };
55   is $@, q{}, "no exception from linking";
56   ok -e $exe_file, "found executable file";
57   ok -x $exe_file, "executable file appears to be executable";
58
59   if ($^O eq 'os2') {           # Analogue of LDLOADPATH...
60           # Actually, not needed now, since we do not link with the generated DLL
61     my $old = OS2::extLibpath();        # [builtin function]
62     $old = ";$old" if defined $old and length $old;
63     # To pass the sanity check, components must have backslashes...
64     OS2::extLibpath_set(".\\$old");
65   }
66
67   # Try the executable
68   my $ec = my_system($exe_file);
69   is( $ec, 11, "got expected exit code from executable" )
70     or diag( $ec == -1 ? "Could not run '$exe_file': $!\n"
71                        : "Unexpected exit code '$ec'\n");
72 }
73
74 # Clean up
75 for ($source_file, $object_file, $exe_file) {
76   tr/"'//d;
77   1 while unlink;
78 }
79
80 if ($^O eq 'VMS') {
81    1 while unlink 'COMPILET.LIS';
82    1 while unlink 'COMPILET.OPT';
83 }
84
85 sub my_system {
86   my $cmd = shift;
87   my $ec;
88   if ($^O eq 'VMS') {
89     # Preserve non-posixified status and don't bit shift the result.
90     use vmsish 'status';
91     $ec = system("mcr $cmd");
92     return $ec;
93   }
94   $ec = system($cmd);
95   return $ec == -1 ? -1 : $ec >> 8;
96 }