1898184e3Ssthen#! perl -w
2898184e3Ssthen
3898184e3Ssthenuse strict;
4898184e3Ssthenuse Test::More;
5898184e3SsthenBEGIN {
6898184e3Ssthen  if ($^O eq 'VMS') {
7898184e3Ssthen    # So we can get the return value of system()
8898184e3Ssthen    require vmsish;
9898184e3Ssthen    import vmsish;
10898184e3Ssthen  }
11898184e3Ssthen}
12898184e3Ssthenuse ExtUtils::CBuilder;
13898184e3Ssthenuse File::Spec;
14898184e3Ssthen
15898184e3Ssthen# TEST does not like extraneous output
16898184e3Ssthenmy $quiet = $ENV{PERL_CORE} && !$ENV{HARNESS_ACTIVE};
17898184e3Ssthenmy ($source_file, $object_file, $exe_file);
18898184e3Ssthen
19898184e3Ssthenmy $b = ExtUtils::CBuilder->new(quiet => $quiet);
20898184e3Ssthen
21898184e3Ssthen# test plan
22b8851fccSafresh1if ($^O =~ / ^ ( MSWin32 | os390 ) $ /xi) {
23b8851fccSafresh1  plan skip_all => "link_executable() is not implemented yet on $^O";
24898184e3Ssthen}
25898184e3Ssthenelsif ( ! $b->have_compiler ) {
26898184e3Ssthen  plan skip_all => "no compiler available for testing";
27898184e3Ssthen}
28898184e3Ssthenelse {
29898184e3Ssthen  plan tests => 8;
30898184e3Ssthen}
31898184e3Ssthen
32898184e3Ssthenok $b, "created EU::CB object";
33898184e3Ssthen
34898184e3Ssthen$source_file = File::Spec->catfile('t', 'linkt.c');
35898184e3Ssthen{
36*9f11ffb7Safresh1  open my $FH, '>', $source_file or die "Can't create $source_file: $!";
37898184e3Ssthen  print $FH "int main(void) { return 11; }\n";
38898184e3Ssthen  close $FH;
39898184e3Ssthen}
40898184e3Ssthenok -e $source_file, "generated '$source_file'";
41898184e3Ssthen
42898184e3Ssthen# Compile
43898184e3Sstheneval { $object_file = $b->compile(source => $source_file) };
44898184e3Ssthenis $@, q{}, "no exception from compilation";
45898184e3Ssthenok -e $object_file, "found object file";
46898184e3Ssthen
47898184e3Ssthen# Link
48898184e3SsthenSKIP: {
49898184e3Ssthen  skip "error compiling source", 4
50898184e3Ssthen    unless -e $object_file;
51898184e3Ssthen
52898184e3Ssthen  my @temps;
53898184e3Ssthen  eval { ($exe_file, @temps) = $b->link_executable(objects => $object_file) };
54898184e3Ssthen  is $@, q{}, "no exception from linking";
55898184e3Ssthen  ok -e $exe_file, "found executable file";
56898184e3Ssthen  ok -x $exe_file, "executable file appears to be executable";
57898184e3Ssthen
58898184e3Ssthen  if ($^O eq 'os2') {		# Analogue of LDLOADPATH...
59898184e3Ssthen          # Actually, not needed now, since we do not link with the generated DLL
60898184e3Ssthen    my $old = OS2::extLibpath();	# [builtin function]
61898184e3Ssthen    $old = ";$old" if defined $old and length $old;
62898184e3Ssthen    # To pass the sanity check, components must have backslashes...
63898184e3Ssthen    OS2::extLibpath_set(".\\$old");
64898184e3Ssthen  }
65898184e3Ssthen
66898184e3Ssthen  # Try the executable
67898184e3Ssthen  my $ec = my_system($exe_file);
68898184e3Ssthen  is( $ec, 11, "got expected exit code from executable" )
69898184e3Ssthen    or diag( $ec == -1 ? "Could not run '$exe_file': $!\n"
70898184e3Ssthen                       : "Unexpected exit code '$ec'\n");
71898184e3Ssthen}
72898184e3Ssthen
73898184e3Ssthen# Clean up
74898184e3Ssthenfor ($source_file, $object_file, $exe_file) {
75898184e3Ssthen  tr/"'//d;
76898184e3Ssthen  1 while unlink;
77898184e3Ssthen}
78898184e3Ssthen
79898184e3Ssthenif ($^O eq 'VMS') {
80898184e3Ssthen   1 while unlink 'LINKT.LIS';
81898184e3Ssthen   1 while unlink 'LINKT.OPT';
82898184e3Ssthen}
83898184e3Ssthen
84898184e3Ssthensub my_system {
85898184e3Ssthen  my $cmd = shift;
86898184e3Ssthen  my $ec;
87898184e3Ssthen  if ($^O eq 'VMS') {
88898184e3Ssthen    # Preserve non-posixified status and don't bit shift the result
89898184e3Ssthen    # because we're running under "use vmsish";
90898184e3Ssthen    $ec = system("mcr $cmd");
91898184e3Ssthen    return $ec;
92898184e3Ssthen  }
93898184e3Ssthen  else {
94898184e3Ssthen    $ec = system($cmd);
95898184e3Ssthen    return $ec == -1 ? -1 : $ec >> 8;
96898184e3Ssthen  }
97898184e3Ssthen}
98