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