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