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