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, $lib_file);
18
19my $b = ExtUtils::CBuilder->new(quiet => $quiet);
20
21# test plan
22if ( ! $b->have_compiler ) {
23  plan skip_all => "no compiler available for testing";
24}
25else {
26  plan tests => 12;
27}
28
29ok $b, "created EU::CB object";
30
31ok $b->have_compiler, "have_compiler";
32
33$source_file = File::Spec->catfile('t', 'basict.c');
34{
35  local *FH;
36  open FH, '>', $source_file or die "Can't create $source_file: $!";
37  print FH "int boot_basict(void) { return 1; }\n";
38  close FH;
39}
40ok -e $source_file, "source file '$source_file' created";
41
42$object_file = $b->object_file($source_file);
43ok 1;
44
45is $object_file, $b->compile(source => $source_file);
46
47$lib_file = $b->lib_file($object_file, module_name => 'basict');
48ok 1;
49
50my ($lib, @temps) = $b->link(objects => $object_file,
51                             module_name => 'basict');
52$lib =~ tr/"'//d;
53$_ = File::Spec->rel2abs($_) for $lib_file, $lib;
54is $lib_file, $lib;
55
56for ($source_file, $object_file, $lib_file) {
57  tr/"'//d;
58  1 while unlink;
59}
60
61if ($^O eq 'VMS') {
62   1 while unlink 'BASICT.LIS';
63   1 while unlink 'BASICT.OPT';
64}
65
66my @words = $b->split_like_shell(' foo bar');
67
68SKIP: {
69  skip "MSWindows", 3 if $^O =~ m/MSWin/;
70  is( @words, 2 );
71  is( $words[0], 'foo' );
72  is( $words[1], 'bar' );
73}
74
75# include_dirs should be settable as string or list
76{
77  package Sub;
78  our @ISA = ('ExtUtils::CBuilder');
79  my $saw = 0;
80  sub do_system {
81    if ($^O eq "MSWin32") {
82	# ExtUtils::CBuilder::MSVC::write_compiler_script() puts the
83	# include_dirs into a response file and not the commandline
84	for (@_) {
85	    next unless /^\@"(.*)"$/;
86	    open(my $fh, "<", $1) or next;
87	    local $/;
88	    $saw = 1 if <$fh> =~ /another dir/;
89	    last;
90	}
91    }
92    $saw = 1 if grep {$_ =~ /another dir/} @_;
93    return 1;
94  }
95
96  package main;
97  my $s = Sub->new();
98  $s->compile(source => 'foo',
99	      include_dirs => 'another dir');
100  ok $saw;
101
102  $saw = 0;
103  $s->compile(source => 'foo',
104	      include_dirs => ['a dir', 'another dir']);
105  ok $saw;
106}
107