add Module::Build 0.27_08
[p5sagit/p5-mst-13.2.git] / lib / Module / Build / t / xs.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use lib $ENV{PERL_CORE} ? '../lib/Module/Build/t/lib' : 't/lib';
5 use MBTest;
6 use Module::Build;
7
8 {
9   my ($have_c_compiler, $C_support_feature) = check_compiler();
10
11   if (! $C_support_feature) {
12     plan skip_all => 'C_support not enabled';
13   } elsif ( !$have_c_compiler ) {
14     plan skip_all => 'C_support enabled, but no compiler found';
15   } else {
16     plan tests => 22;
17   }
18 }
19
20 #########################
21
22
23 use Cwd ();
24 my $cwd = Cwd::cwd;
25 my $tmp = File::Spec->catdir( $cwd, 't', '_tmp' );
26
27 use DistGen;
28 my $dist = DistGen->new( dir => $tmp, xs => 1 );
29 $dist->regen;
30
31 chdir( $dist->dirname ) or die "Can't chdir to '@{[$dist->dirname]}': $!";
32 my $mb = Module::Build->new_from_context;
33
34
35 eval {$mb->dispatch('clean')};
36 is $@, '';
37
38 eval {$mb->dispatch('build')};
39 is $@, '';
40
41 {
42   # Make sure it actually works: that we can call methods in the XS module
43
44   # Unfortunately, We must do this is a subprocess because some OS will not
45   # release the handle on a dynamic lib until the attaching process terminates
46
47   ok $mb->run_perl_command(['-Mblib', '-M'.$dist->name, '-e1']);
48
49   like stdout_of( sub {$mb->run_perl_command([
50        '-Mblib', '-M'.$dist->name,
51        '-we', "print @{[$dist->name]}::okay()"])}), qr/ok$/;
52
53   like stdout_of( sub {$mb->run_perl_command([
54        '-Mblib', '-M'.$dist->name,
55        '-we', "print @{[$dist->name]}::version()"])}), qr/0.01$/;
56
57   like stdout_of( sub {$mb->run_perl_command([
58        '-Mblib', '-M'.$dist->name,
59        '-we', "print @{[$dist->name]}::xs_version()"])}), qr/0.01$/;
60
61 }
62
63 {
64   # Try again in a subprocess 
65   eval {$mb->dispatch('clean')};
66   is $@, '';
67
68   $mb->create_build_script;
69   ok -e 'Build';
70
71   eval {$mb->run_perl_script('Build')};
72   is $@, '';
73 }
74
75 # We can't be verbose in the sub-test, because Test::Harness will
76 # think that the output is for the top-level test.
77 eval {$mb->dispatch('test')};
78 is $@, '';
79
80 eval {$mb->dispatch('clean')};
81 is $@, '';
82
83
84 SKIP: {
85   skip( "skipping a Unixish-only tests", 1 )
86       unless $mb->os_type eq 'Unix';
87
88   local $mb->{config}{ld} = "FOO=BAR $mb->{config}{ld}";
89   eval {$mb->dispatch('build')};
90   is $@, '';
91 }
92
93 eval {$mb->dispatch('realclean')};
94 is $@, '';
95
96 # Make sure blib/ is gone after 'realclean'
97 ok ! -e 'blib';
98
99
100 # cleanup
101 chdir( $cwd ) or die "Can''t chdir to '$cwd': $!";
102 $dist->remove;
103
104
105 ########################################
106
107 # Try a XS distro with a deep namespace
108
109 $dist = DistGen->new( name => 'Simple::With::Deep::Namespace',
110                       dir => $tmp, xs => 1 );
111 $dist->regen;
112 chdir( $dist->dirname ) or die "Can't chdir to '@{[$dist->dirname]}': $!";
113
114 $mb = Module::Build->new_from_context;
115 is $@, '';
116
117 $mb->dispatch('build');
118 is $@, '';
119
120 $mb->dispatch('test');
121 is $@, '';
122
123 $mb->dispatch('realclean');
124 is $@, '';
125
126 # cleanup
127 chdir( $cwd ) or die "Can''t chdir to '$cwd': $!";
128 $dist->remove;
129
130
131 ########################################
132
133 # Try a XS distro using a flat directory structure
134 # and a 'dist_name' instead of a 'module_name'
135
136 $dist = DistGen->new( name => 'Dist-Name', dir => $tmp, xs => 1 );
137
138 $dist->remove_file('lib/Dist-Name.pm');
139 $dist->remove_file('lib/Dist-Name.xs');
140
141 $dist->change_file('Build.PL', <<"---");
142 use strict;
143 use Module::Build;
144
145 my \$builder = Module::Build->new(
146     dist_name         => 'Dist-Name',
147     dist_version_from => 'Simple.pm',
148     pm_files => { 'Simple.pm' => 'lib/Simple.pm' },
149     xs_files => { 'Simple.xs' => 'lib/Simple.xs' },
150 );
151
152 \$builder->create_build_script();
153 ---
154 $dist->add_file('Simple.xs', <<"---");
155 #include "EXTERN.h"
156 #include "perl.h"
157 #include "XSUB.h"
158
159 MODULE = Simple         PACKAGE = Simple
160
161 SV *
162 okay()
163     CODE:
164         RETVAL = newSVpv( "ok", 0 );
165     OUTPUT:
166         RETVAL
167 ---
168
169 $dist->add_file( 'Simple.pm', <<"---" );
170 package Simple;
171
172 \$VERSION = '0.01';
173
174 require Exporter;
175 require DynaLoader;
176
177 \@ISA = qw( Exporter DynaLoader );
178 \@EXPORT_OK = qw( okay );
179
180 bootstrap Simple \$VERSION;
181
182 1;
183
184 __END__
185
186 =head1 NAME
187
188 Simple - Perl extension for blah blah blah
189
190 =head1 DESCRIPTION
191
192 Stub documentation for Simple.
193
194 =head1 AUTHOR
195
196 A. U. Thor, a.u.thor\@a.galaxy.far.far.away
197
198 =cut
199 ---
200 $dist->change_file('t/basic.t', <<"---");
201 use Test::More tests => 2;
202 use strict;
203
204 use Simple;
205 ok( 1 );
206
207 ok( Simple::okay() eq 'ok' );
208 ---
209
210 $dist->regen;
211 chdir( $dist->dirname ) or die "Can't chdir to '@{[$dist->dirname]}': $!";
212
213
214 $mb = Module::Build->new_from_context;
215 is $@, '';
216
217 $mb->dispatch('build');
218 is $@, '';
219
220 $mb->dispatch('test');
221 is $@, '';
222
223 $mb->dispatch('realclean');
224 is $@, '';
225
226 # cleanup
227 chdir( $cwd ) or die "Can''t chdir to '$cwd': $!";
228 $dist->remove;
229
230 use File::Path;
231 rmtree( $tmp );