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