This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #52000] Warn/abort on attempted perl exit
[perl5.git] / lib / ExtUtils / t / Embed.t
CommitLineData
39234879 1#!/usr/bin/perl
f1feeb72
NIS
2
3BEGIN {
39234879
MS
4 if( $ENV{PERL_CORE} ) {
5 chdir 't' if -d 't';
6 @INC = '../lib';
7 }
f1feeb72 8}
39234879
MS
9chdir 't';
10
f1feeb72
NIS
11use Config;
12use ExtUtils::Embed;
1091dea4 13use File::Spec;
f1feeb72
NIS
14
15open(my $fh,">embed_test.c") || die "Cannot open embed_test.c:$!";
16print $fh <DATA>;
17close($fh);
18
19$| = 1;
6136213b 20print "1..10\n";
1091dea4
NIS
21my $cc = $Config{'cc'};
22my $cl = ($^O eq 'MSWin32' && $cc eq 'cl');
dfcfdb64
IZ
23my $skip_exe = $^O eq 'os2' && $Config{ldflags} =~ /(?<!\S)-Zexe\b/;
24my $exe = 'embed_test';
25$exe .= $Config{'exe_ext'} unless $skip_exe; # Linker will auto-append it
9fb80172 26my $obj = 'embed_test' . $Config{'obj_ext'};
21f7eeb2
JH
27my $inc = File::Spec->updir;
28my $lib = File::Spec->updir;
88eeb692
SB
29my $libperl_copied;
30my $testlib;
1091dea4 31my @cmd;
20ebe338 32my (@cmd2) if $^O eq 'VMS';
92a665d6
YO
33# Don't use ccopts() here as we may want to overwrite an existing
34# perl with a new one with inconsistent header files, meaning
35# the usual value for perl_inc(), which is used by ccopts(),
36# will be wrong.
20ebe338
CL
37if ($^O eq 'VMS') {
38 push(@cmd,$cc,"/Obj=$obj");
39 my (@incs) = ($inc);
92a665d6 40 my $crazy = ccflags();
20ebe338
CL
41 if ($crazy =~ s#/inc[^=/]*=([\w\$\_\-\.\[\]\:]+)##i) {
42 push(@incs,$1);
43 }
44 if ($crazy =~ s/-I([a-zA-Z0-9\$\_\-\.\[\]\:]*)//) {
45 push(@incs,$1);
46 }
47 $crazy =~ s#/Obj[^=/]*=[\w\$\_\-\.\[\]\:]+##i;
48 push(@cmd,"/Include=(".join(',',@incs).")");
49 push(@cmd,$crazy);
50 push(@cmd,"embed_test.c");
51
52 push(@cmd2,$Config{'ld'}, $Config{'ldflags'}, "/exe=$exe");
53 push(@cmd2,"$obj,[-]perlshr.opt/opt,[-]perlshr_attr.opt/opt");
54
55} else {
56 if ($cl) {
1091dea4 57 push(@cmd,$cc,"-Fe$exe");
20ebe338
CL
58 }
59 else {
1091dea4 60 push(@cmd,$cc,'-o' => $exe);
20ebe338 61 }
14270b7a
JH
62 if ($^O eq 'dec_osf' && !defined $Config{usedl}) {
63 # The -non_shared is needed in case of -Uusedl or otherwise
64 # the test application will try to use libperl.so
65 # instead of libperl.a.
66 push @cmd, "-non_shared";
67 }
92a665d6
YO
68
69 push(@cmd,"-I$inc",ccflags(),'embed_test.c');
20ebe338 70 if ($^O eq 'MSWin32') {
b1b2427f 71 $inc = File::Spec->catdir($inc,'win32');
1091dea4 72 push(@cmd,"-I$inc");
b1b2427f 73 $inc = File::Spec->catdir($inc,'include');
1091dea4
NIS
74 push(@cmd,"-I$inc");
75 if ($cc eq 'cl') {
c623ac67 76 push(@cmd,'-link',"-libpath:$lib",$Config{'libperl'},$Config{'libs'});
1091dea4
NIS
77 }
78 else {
67bfc918 79 push(@cmd,"-L$lib",File::Spec->catfile($lib,$Config{'libperl'}),$Config{'libc'});
1091dea4 80 }
20ebe338 81 }
2f3efc97
JH
82 elsif ($^O eq 'os390' && $Config{usedl}) {
83 # Nothing for OS/390 (z/OS) dynamic.
84 } else { # Not MSWin32 or OS/390 (z/OS) dynamic.
1091dea4 85 push(@cmd,"-L$lib",'-lperl');
fee3faea 86 local $SIG{__WARN__} = sub {
c623ac67 87 warn $_[0] unless $_[0] =~ /No library found for .*perl/
fee3faea 88 };
dfcfdb64
IZ
89 push(@cmd, '-Zlinker', '/PM:VIO') # Otherwise puts a warning to STDOUT!
90 if $^O eq 'os2' and $Config{ldflags} =~ /(?<!\S)-Zomf\b/;
fee3faea 91 push(@cmd,ldopts());
20ebe338 92 }
1091dea4 93
20ebe338 94 if ($^O eq 'aix') { # AIX needs an explicit symbol export list.
f490490f
JH
95 my ($perl_exp) = grep { -f } qw(perl.exp ../perl.exp);
96 die "where is perl.exp?\n" unless defined $perl_exp;
97 for (@cmd) {
98 s!-bE:(\S+)!-bE:$perl_exp!;
99 }
88eeb692 100 }
b5920ff0
RU
101 elsif ($^O eq 'cygwin') { # Cygwin needs no special treatment like below
102 ;
103 }
88eeb692
SB
104 elsif ($Config{'libperl'} !~ /\Alibperl\./) {
105 # Everyone needs libperl copied if it's not found by '-lperl'.
106 $testlib = $Config{'libperl'};
107 my $srclib = $testlib;
c72f2af0 108 $testlib =~ s/.+(?=\.[^.]*)/libperl/;
88eeb692
SB
109 $testlib = File::Spec::->catfile($lib, $testlib);
110 $srclib = File::Spec::->catfile($lib, $srclib);
111 if (-f $srclib) {
112 unlink $testlib if -f $testlib;
c7e0473b
MB
113 my $ln_or_cp = $Config{'ln'} || $Config{'cp'};
114 my $lncmd = "$ln_or_cp $srclib $testlib";
88eeb692
SB
115 #print "# $lncmd\n";
116 $libperl_copied = 1 unless system($lncmd);
117 }
118 }
f490490f 119}
20ebe338 120my $status;
b2213df5 121# On OS/2 the linker will always emit an empty line to STDOUT; filter these
2bf35fab
JH
122my $cmd = join ' ', @cmd;
123chomp($cmd); # where is the newline coming from? ldopts()?
124print "# $cmd\n";
125my @out = `$cmd`;
126$status = $?;
127print "# $_\n" foreach @out;
b2213df5 128
20ebe338
CL
129if ($^O eq 'VMS' && !$status) {
130 print "# @cmd2\n";
5f9145a3 131 $status = system(join(' ',@cmd2));
20ebe338
CL
132}
133print (($status? 'not ': '')."ok 1\n");
3be77bcf 134
144d6c83 135my $embed_test = File::Spec->catfile(File::Spec->curdir, $exe);
20ebe338 136$embed_test = "run/nodebug $exe" if $^O eq 'VMS';
144d6c83 137print "# embed_test = $embed_test\n";
20ebe338 138$status = system($embed_test);
6136213b 139print (($status? 'not ':'')."ok 10 # system returned $status\n");
9fb80172 140unlink($exe,"embed_test.c",$obj);
d59896ca 141unlink("$exe.manifest") if $cl and $Config{'ccversion'} =~ /^(\d+)/ and $1 >= 14;
dfcfdb64 142unlink("$exe$Config{exe_ext}") if $skip_exe;
9fb80172 143unlink("embed_test.map","embed_test.lis") if $^O eq 'VMS';
b53432e4 144unlink(glob("./*.dll")) if $^O eq 'cygwin';
88eeb692 145unlink($testlib) if $libperl_copied;
f1feeb72 146
92a665d6 147# gcc -g -I.. -L../ -o perl_test perl_test.c -lperl `../perl -I../lib -MExtUtils::Embed -I../ -e ccflags -e ldopts`
f1feeb72
NIS
148__END__
149
150/* perl_test.c */
151
152#include <EXTERN.h>
153#include <perl.h>
154
155#define my_puts(a) if(puts(a) < 0) exit(666)
156
6136213b 157static const char * cmds [] = { "perl", "-e", "$|=1; print qq[ok 5\\n]; $SIG{__WARN__} = sub { print qq[ok 6\\n] if $_[0] =~ /Unexpected exit/; }; exit 5;", NULL };
f1feeb72 158
27da23d5
JH
159#ifdef PERL_GLOBAL_STRUCT_PRIVATE
160static struct perl_vars *my_plvarsp;
161struct perl_vars* Perl_GetVarsPrivate(void) { return my_plvarsp; }
162#endif
163
2f3efc97 164#ifdef NO_ENV_ARRAY_IN_MAIN
473d7a83
RB
165int main(int argc, char **argv) {
166 char **env;
2f3efc97 167#else
473d7a83 168int main(int argc, char **argv, char **env) {
2f3efc97 169#endif
1ccffcf5 170 PerlInterpreter *my_perl;
27da23d5 171#ifdef PERL_GLOBAL_STRUCT
79403e77 172 struct perl_vars *my_vars = init_global_struct();
27da23d5 173# ifdef PERL_GLOBAL_STRUCT_PRIVATE
79403e77 174 my_plvarsp = my_vars;
27da23d5
JH
175# endif
176#endif /* PERL_GLOBAL_STRUCT */
b7787f18
AL
177
178 (void)argc; /* PERL_SYS_INIT3 may #define away their use */
179 (void)argv;
473d7a83 180 PERL_SYS_INIT3(&argc, &argv, &env);
1ccffcf5
IZ
181
182 my_perl = perl_alloc();
f1feeb72
NIS
183
184 my_puts("ok 2");
185
186 perl_construct(my_perl);
6136213b 187 my_perl->Iexit_flags |= PERL_EXIT_WARN;
f1feeb72
NIS
188
189 my_puts("ok 3");
190
473d7a83 191 perl_parse(my_perl, NULL, (sizeof(cmds)/sizeof(char *))-1, (char **)cmds, env);
f1feeb72
NIS
192
193 my_puts("ok 4");
194
195 fflush(stdout);
196
197 perl_run(my_perl);
198
6136213b 199 my_puts("ok 7");
f1feeb72
NIS
200
201 perl_destruct(my_perl);
202
6136213b 203 my_puts("ok 8");
f1feeb72
NIS
204
205 perl_free(my_perl);
206
6136213b 207 my_puts("ok 9");
f1feeb72 208
1ccffcf5
IZ
209 PERL_SYS_TERM();
210
f0af002c 211#ifdef PERL_GLOBAL_STRUCT
79403e77
NC
212 free_global_struct(my_vars);
213# ifdef PERL_GLOBAL_STRUCT_PRIVATE
5c64bffd
NC
214 my_plvarsp = NULL;
215 /* Remember, functions registered with atexit() can run after this point,
216 and may access "global" variables, and hence end up calling
217 Perl_GetVarsPrivate() */
79403e77 218#endif
f0af002c
TC
219#endif /* PERL_GLOBAL_STRUCT */
220
f1feeb72
NIS
221 return 0;
222}