Windows 64-bit support:
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / t / Embed.t
1 #!/usr/bin/perl
2
3 BEGIN {
4     if( $ENV{PERL_CORE} ) {
5         chdir 't' if -d 't';
6         @INC = '../lib';
7     }
8 }
9 chdir 't';
10
11 use Config;
12 use ExtUtils::Embed;
13 use File::Spec;
14
15 open(my $fh,">embed_test.c") || die "Cannot open embed_test.c:$!";
16 print $fh <DATA>;
17 close($fh);
18
19 $| = 1;
20 print "1..9\n";
21 my $cc = $Config{'cc'};
22 my $cl  = ($^O eq 'MSWin32' && $cc eq 'cl');
23 my $borl  = ($^O eq 'MSWin32' && $cc eq 'bcc32');
24 my $skip_exe = $^O eq 'os2' && $Config{ldflags} =~ /(?<!\S)-Zexe\b/;
25 my $exe = 'embed_test';
26 $exe .= $Config{'exe_ext'} unless $skip_exe;    # Linker will auto-append it
27 my $obj = 'embed_test' . $Config{'obj_ext'};
28 my $inc = File::Spec->updir;
29 my $lib = File::Spec->updir;
30 my $libperl_copied;
31 my $testlib;
32 my @cmd;
33 my (@cmd2) if $^O eq 'VMS';
34
35 if ($^O eq 'VMS') {
36     push(@cmd,$cc,"/Obj=$obj");
37     my (@incs) = ($inc);
38     my $crazy = ccopts();
39     if ($crazy =~ s#/inc[^=/]*=([\w\$\_\-\.\[\]\:]+)##i) {
40         push(@incs,$1);
41     }
42     if ($crazy =~ s/-I([a-zA-Z0-9\$\_\-\.\[\]\:]*)//) {
43         push(@incs,$1);
44     }
45     $crazy =~ s#/Obj[^=/]*=[\w\$\_\-\.\[\]\:]+##i;
46     push(@cmd,"/Include=(".join(',',@incs).")");
47     push(@cmd,$crazy);
48     push(@cmd,"embed_test.c");
49
50     push(@cmd2,$Config{'ld'}, $Config{'ldflags'}, "/exe=$exe"); 
51     push(@cmd2,"$obj,[-]perlshr.opt/opt,[-]perlshr_attr.opt/opt");
52
53 } else {
54    if ($cl) {
55     push(@cmd,$cc,"-Fe$exe");
56    }
57    elsif ($borl) {
58     push(@cmd,$cc,"-o$exe");
59    }
60    else {
61     push(@cmd,$cc,'-o' => $exe);
62    }
63    push(@cmd,"-I$inc",ccopts(),'embed_test.c');
64    if ($^O eq 'MSWin32') {
65     $inc = File::Spec->catdir($inc,'win32');
66     push(@cmd,"-I$inc");
67     $inc = File::Spec->catdir($inc,'include');
68     push(@cmd,"-I$inc");
69     if ($cc eq 'cl') {
70         push(@cmd,'-link',"-libpath:$lib",$Config{'libperl'},$Config{'libs'});
71     }
72     else {
73         push(@cmd,"-L$lib",File::Spec->catfile($lib,$Config{'libperl'}),$Config{'libc'});
74     }
75    }
76    else {
77     push(@cmd,"-L$lib",'-lperl');
78    }
79    {
80     local $SIG{__WARN__} = sub {
81         warn $_[0] unless $_[0] =~ /No library found for .*perl/
82     };
83     push(@cmd, '-Zlinker', '/PM:VIO')   # Otherwise puts a warning to STDOUT!
84         if $^O eq 'os2' and $Config{ldflags} =~ /(?<!\S)-Zomf\b/;
85     push(@cmd,ldopts());
86    }
87    if ($borl) {
88      @cmd = ($cmd[0],(grep{/^-[LI]/}@cmd[1..$#cmd]),(grep{!/^-[LI]/}@cmd[1..$#cmd]));
89    }
90
91    if ($^O eq 'aix') { # AIX needs an explicit symbol export list.
92     my ($perl_exp) = grep { -f } qw(perl.exp ../perl.exp);
93     die "where is perl.exp?\n" unless defined $perl_exp;
94     for (@cmd) {
95         s!-bE:(\S+)!-bE:$perl_exp!;
96     }
97    }
98    elsif ($^O eq 'cygwin') { # Cygwin needs the shared libperl copied
99      my $v_e_r_s = $Config{version};
100      $v_e_r_s =~ tr/./_/;
101      system("cp ../cygperl$v_e_r_s.dll ./");    # for test 1
102    }
103    elsif ($Config{'libperl'} !~ /\Alibperl\./) {
104      # Everyone needs libperl copied if it's not found by '-lperl'.
105      $testlib = $Config{'libperl'};
106      my $srclib = $testlib;
107      $testlib =~ s/^[^.]+/libperl/;
108      $testlib = File::Spec::->catfile($lib, $testlib);
109      $srclib = File::Spec::->catfile($lib, $srclib);
110      if (-f $srclib) {
111        unlink $testlib if -f $testlib;
112        my $ln_or_cp = $Config{'ln'} || $Config{'cp'};
113        my $lncmd = "$ln_or_cp $srclib $testlib";
114        #print "# $lncmd\n";
115        $libperl_copied = 1      unless system($lncmd);
116      }
117    }
118 }
119 my $status;
120 # On OS/2 the linker will always emit an empty line to STDOUT; filter these
121 my $cmd = join ' ', @cmd;
122 chomp($cmd); # where is the newline coming from? ldopts()?
123 print "# $cmd\n";
124 my @out = `$cmd`;
125 $status = $?;
126 print "# $_\n" foreach @out;
127
128 if ($^O eq 'VMS' && !$status) {
129   print "# @cmd2\n";
130   $status = system(join(' ',@cmd2)); 
131 }
132 print (($status? 'not ': '')."ok 1\n");
133
134 my $embed_test = File::Spec->catfile(File::Spec->curdir, $exe);
135 $embed_test = "run/nodebug $exe" if $^O eq 'VMS';
136 print "# embed_test = $embed_test\n";
137 $status = system($embed_test);
138 print (($status? 'not ':'')."ok 9 # $status\n");
139 unlink($exe,"embed_test.c",$obj);
140 unlink("$exe$Config{exe_ext}") if $skip_exe;
141 unlink("embed_test.map","embed_test.lis") if $^O eq 'VMS';
142 unlink(glob("./*.dll")) if $^O eq 'cygwin';
143 unlink($testlib)               if $libperl_copied;
144
145 # gcc -g -I.. -L../ -o perl_test perl_test.c -lperl `../perl -I../lib -MExtUtils::Embed -I../ -e ccopts -e ldopts`
146
147 __END__
148
149 /* perl_test.c */
150
151 #include <EXTERN.h>
152 #include <perl.h>
153
154 #define my_puts(a) if(puts(a) < 0) exit(666)
155
156 static char *cmds[] = { "perl","-e", "print qq[ok 5\\n]", NULL };
157
158 int main(int argc, char **argv, char **env)
159 {
160     PerlInterpreter *my_perl = perl_alloc();
161
162     my_puts("ok 2");
163
164     perl_construct(my_perl);
165
166     my_puts("ok 3");
167
168     perl_parse(my_perl, NULL, (sizeof(cmds)/sizeof(char *))-1, cmds, env);
169
170     my_puts("ok 4");
171
172     fflush(stdout);
173
174     perl_run(my_perl);
175
176     my_puts("ok 6");
177
178     perl_destruct(my_perl);
179
180     my_puts("ok 7");
181
182     perl_free(my_perl);
183
184     my_puts("ok 8");
185
186     return 0;
187 }
188
189
190
191