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