Cygwin build harmonization, remove cygwin/Makefile.SHs
[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 # Don't use ccopts() here as we may want to overwrite an existing
35 # perl with a new one with inconsistent header files, meaning
36 # the usual value for perl_inc(), which is used by ccopts(),
37 # will be wrong.
38 if ($^O eq 'VMS') {
39     push(@cmd,$cc,"/Obj=$obj");
40     my (@incs) = ($inc);
41     my $crazy = ccflags();
42     if ($crazy =~ s#/inc[^=/]*=([\w\$\_\-\.\[\]\:]+)##i) {
43         push(@incs,$1);
44     }
45     if ($crazy =~ s/-I([a-zA-Z0-9\$\_\-\.\[\]\:]*)//) {
46         push(@incs,$1);
47     }
48     $crazy =~ s#/Obj[^=/]*=[\w\$\_\-\.\[\]\:]+##i;
49     push(@cmd,"/Include=(".join(',',@incs).")");
50     push(@cmd,$crazy);
51     push(@cmd,"embed_test.c");
52
53     push(@cmd2,$Config{'ld'}, $Config{'ldflags'}, "/exe=$exe"); 
54     push(@cmd2,"$obj,[-]perlshr.opt/opt,[-]perlshr_attr.opt/opt");
55
56 } else {
57    if ($cl) {
58     push(@cmd,$cc,"-Fe$exe");
59    }
60    elsif ($borl) {
61     push(@cmd,$cc,"-o$exe");
62    }
63    else {
64     push(@cmd,$cc,'-o' => $exe);
65    }
66    if ($^O eq 'dec_osf' && !defined $Config{usedl}) {
67        # The -non_shared is needed in case of -Uusedl or otherwise
68        # the test application will try to use libperl.so
69        # instead of libperl.a.
70        push @cmd, "-non_shared";
71    }
72
73    push(@cmd,"-I$inc",ccflags(),'embed_test.c');
74    if ($^O eq 'MSWin32') {
75     $inc = File::Spec->catdir($inc,'win32');
76     push(@cmd,"-I$inc");
77     $inc = File::Spec->catdir($inc,'include');
78     push(@cmd,"-I$inc");
79     if ($cc eq 'cl') {
80         push(@cmd,'-link',"-libpath:$lib",$Config{'libperl'},$Config{'libs'});
81     }
82     else {
83         push(@cmd,"-L$lib",File::Spec->catfile($lib,$Config{'libperl'}),$Config{'libc'});
84     }
85    }
86    elsif ($^O eq 'os390' && $Config{usedl}) {
87     # Nothing for OS/390 (z/OS) dynamic.
88    } else { # Not MSWin32 or OS/390 (z/OS) dynamic.
89     push(@cmd,"-L$lib",'-lperl');
90     local $SIG{__WARN__} = sub {
91         warn $_[0] unless $_[0] =~ /No library found for .*perl/
92     };
93     push(@cmd, '-Zlinker', '/PM:VIO')   # Otherwise puts a warning to STDOUT!
94         if $^O eq 'os2' and $Config{ldflags} =~ /(?<!\S)-Zomf\b/;
95     push(@cmd,ldopts());
96    }
97    if ($borl) {
98      @cmd = ($cmd[0],(grep{/^-[LI]/}@cmd[1..$#cmd]),(grep{!/^-[LI]/}@cmd[1..$#cmd]));
99    }
100
101    if ($^O eq 'aix') { # AIX needs an explicit symbol export list.
102     my ($perl_exp) = grep { -f } qw(perl.exp ../perl.exp);
103     die "where is perl.exp?\n" unless defined $perl_exp;
104     for (@cmd) {
105         s!-bE:(\S+)!-bE:$perl_exp!;
106     }
107    }
108    elsif ($Config{'libperl'} !~ /\Alibperl\./) {
109      # Everyone needs libperl copied if it's not found by '-lperl'.
110      $testlib = $Config{'libperl'};
111      my $srclib = $testlib;
112      $testlib =~ s/.+(?=\.[^.]*)/libperl/;
113      $testlib = File::Spec::->catfile($lib, $testlib);
114      $srclib = File::Spec::->catfile($lib, $srclib);
115      if (-f $srclib) {
116        unlink $testlib if -f $testlib;
117        my $ln_or_cp = $Config{'ln'} || $Config{'cp'};
118        my $lncmd = "$ln_or_cp $srclib $testlib";
119        #print "# $lncmd\n";
120        $libperl_copied = 1      unless system($lncmd);
121      }
122    }
123 }
124 my $status;
125 # On OS/2 the linker will always emit an empty line to STDOUT; filter these
126 my $cmd = join ' ', @cmd;
127 chomp($cmd); # where is the newline coming from? ldopts()?
128 print "# $cmd\n";
129 my @out = `$cmd`;
130 $status = $?;
131 print "# $_\n" foreach @out;
132
133 if ($^O eq 'VMS' && !$status) {
134   print "# @cmd2\n";
135   $status = system(join(' ',@cmd2));
136 }
137 print (($status? 'not ': '')."ok 1\n");
138
139 my $embed_test = File::Spec->catfile(File::Spec->curdir, $exe);
140 $embed_test = "run/nodebug $exe" if $^O eq 'VMS';
141 print "# embed_test = $embed_test\n";
142 $status = system($embed_test);
143 print (($status? 'not ':'')."ok 9 # system returned $status\n");
144 unlink($exe,"embed_test.c",$obj);
145 unlink("$exe.manifest") if $cl and $Config{'ccversion'} =~ /^(\d+)/ and $1 >= 14;
146 unlink("$exe$Config{exe_ext}") if $skip_exe;
147 unlink("embed_test.map","embed_test.lis") if $^O eq 'VMS';
148 unlink(glob("./*.dll")) if $^O eq 'cygwin';
149 unlink($testlib)               if $libperl_copied;
150
151 # gcc -g -I.. -L../ -o perl_test perl_test.c -lperl `../perl -I../lib -MExtUtils::Embed -I../ -e ccflags -e ldopts`
152 __END__
153
154 /* perl_test.c */
155
156 #include <EXTERN.h>
157 #include <perl.h>
158
159 #define my_puts(a) if(puts(a) < 0) exit(666)
160
161 static const char * cmds [] = { "perl", "-e", "$|=1; print qq[ok 5\\n]", NULL };
162
163 #ifdef PERL_GLOBAL_STRUCT_PRIVATE
164 static struct perl_vars *my_plvarsp;
165 struct perl_vars* Perl_GetVarsPrivate(void) { return my_plvarsp; }
166 #endif
167
168 #ifdef NO_ENV_ARRAY_IN_MAIN
169 int main(int argc, char **argv) {
170     char **env;
171 #else
172 int main(int argc, char **argv, char **env) {
173 #endif
174     PerlInterpreter *my_perl;
175 #ifdef PERL_GLOBAL_STRUCT
176     dVAR;
177     struct perl_vars *plvarsp = init_global_struct();
178 #  ifdef PERL_GLOBAL_STRUCT_PRIVATE
179     my_vars = my_plvarsp = plvarsp;
180 #  endif
181 #endif /* PERL_GLOBAL_STRUCT */
182
183     (void)argc; /* PERL_SYS_INIT3 may #define away their use */
184     (void)argv;
185     PERL_SYS_INIT3(&argc, &argv, &env);
186
187     my_perl = perl_alloc();
188
189     my_puts("ok 2");
190
191     perl_construct(my_perl);
192
193     my_puts("ok 3");
194
195     perl_parse(my_perl, NULL, (sizeof(cmds)/sizeof(char *))-1, (char **)cmds, env);
196
197     my_puts("ok 4");
198
199     fflush(stdout);
200
201     perl_run(my_perl);
202
203     my_puts("ok 6");
204
205     perl_destruct(my_perl);
206
207     my_puts("ok 7");
208
209     perl_free(my_perl);
210
211 #ifdef PERL_GLOBAL_STRUCT
212     free_global_struct(plvarsp);
213 #endif /* PERL_GLOBAL_STRUCT */
214
215     my_puts("ok 8");
216
217     PERL_SYS_TERM();
218
219     return 0;
220 }