Upgrade to Module::Build 0.2808_01
[p5sagit/p5-mst-13.2.git] / lib / Module / Build / t / xs.t
CommitLineData
bb4e9162 1#!/usr/bin/perl -w
2
3use strict;
4use lib $ENV{PERL_CORE} ? '../lib/Module/Build/t/lib' : 't/lib';
5use MBTest;
6use 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
23use Cwd ();
24my $cwd = Cwd::cwd;
7a827510 25my $tmp = MBTest->tmpdir;
bb4e9162 26
27use DistGen;
28my $dist = DistGen->new( dir => $tmp, xs => 1 );
29$dist->regen;
30
31chdir( $dist->dirname ) or die "Can't chdir to '@{[$dist->dirname]}': $!";
32my $mb = Module::Build->new_from_context;
33
34
35eval {$mb->dispatch('clean')};
36is $@, '';
37
38eval {$mb->dispatch('build')};
39is $@, '';
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
77e96e88 68
bb4e9162 69 $mb->create_build_script;
77e96e88 70 my $script = $mb->build_script;
71 ok -e $script;
bb4e9162 72
77e96e88 73 eval {$mb->run_perl_script($script)};
bb4e9162 74 is $@, '';
75}
76
77# We can't be verbose in the sub-test, because Test::Harness will
78# think that the output is for the top-level test.
79eval {$mb->dispatch('test')};
80is $@, '';
81
82eval {$mb->dispatch('clean')};
83is $@, '';
84
85
86SKIP: {
87 skip( "skipping a Unixish-only tests", 1 )
c1d8f74e 88 unless $mb->is_unixish;
bb4e9162 89
77e96e88 90 $mb->{config}->push(ld => "FOO=BAR ".$mb->config('ld'));
bb4e9162 91 eval {$mb->dispatch('build')};
92 is $@, '';
77e96e88 93 $mb->{config}->pop('ld');
bb4e9162 94}
95
96eval {$mb->dispatch('realclean')};
97is $@, '';
98
99# Make sure blib/ is gone after 'realclean'
100ok ! -e 'blib';
101
102
103# cleanup
104chdir( $cwd ) or die "Can''t chdir to '$cwd': $!";
105$dist->remove;
106
107
108########################################
109
110# Try a XS distro with a deep namespace
111
7a827510 112$dist = DistGen->new( name => 'Simple::With::Deep::Namespace',
bb4e9162 113 dir => $tmp, xs => 1 );
114$dist->regen;
115chdir( $dist->dirname ) or die "Can't chdir to '@{[$dist->dirname]}': $!";
116
117$mb = Module::Build->new_from_context;
118is $@, '';
119
120$mb->dispatch('build');
121is $@, '';
122
123$mb->dispatch('test');
124is $@, '';
125
126$mb->dispatch('realclean');
127is $@, '';
128
129# cleanup
130chdir( $cwd ) or die "Can''t chdir to '$cwd': $!";
131$dist->remove;
132
133
134########################################
135
136# Try a XS distro using a flat directory structure
137# and a 'dist_name' instead of a 'module_name'
138
139$dist = DistGen->new( name => 'Dist-Name', dir => $tmp, xs => 1 );
140
141$dist->remove_file('lib/Dist-Name.pm');
142$dist->remove_file('lib/Dist-Name.xs');
143
7a827510 144$dist->change_build_pl
145 ({
bb4e9162 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' },
7a827510 150 });
bb4e9162 151
bb4e9162 152$dist->add_file('Simple.xs', <<"---");
153#include "EXTERN.h"
154#include "perl.h"
155#include "XSUB.h"
156
157MODULE = Simple PACKAGE = Simple
158
159SV *
160okay()
161 CODE:
162 RETVAL = newSVpv( "ok", 0 );
163 OUTPUT:
164 RETVAL
165---
166
167$dist->add_file( 'Simple.pm', <<"---" );
168package Simple;
169
170\$VERSION = '0.01';
171
172require Exporter;
173require DynaLoader;
174
175\@ISA = qw( Exporter DynaLoader );
176\@EXPORT_OK = qw( okay );
177
178bootstrap Simple \$VERSION;
179
1801;
181
182__END__
183
184=head1 NAME
185
186Simple - Perl extension for blah blah blah
187
188=head1 DESCRIPTION
189
190Stub documentation for Simple.
191
192=head1 AUTHOR
193
194A. U. Thor, a.u.thor\@a.galaxy.far.far.away
195
196=cut
197---
198$dist->change_file('t/basic.t', <<"---");
199use Test::More tests => 2;
200use strict;
201
202use Simple;
203ok( 1 );
204
205ok( Simple::okay() eq 'ok' );
206---
207
208$dist->regen;
209chdir( $dist->dirname ) or die "Can't chdir to '@{[$dist->dirname]}': $!";
210
211
212$mb = Module::Build->new_from_context;
213is $@, '';
214
215$mb->dispatch('build');
216is $@, '';
217
218$mb->dispatch('test');
219is $@, '';
220
221$mb->dispatch('realclean');
222is $@, '';
223
224# cleanup
225chdir( $cwd ) or die "Can''t chdir to '$cwd': $!";
226$dist->remove;
227
228use File::Path;
229rmtree( $tmp );