This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
add Module::Build 0.27_08
[perl5.git] / lib / Module / Build / t / xs.t
CommitLineData
bb4e9162
YST
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;
25my $tmp = File::Spec->catdir( $cwd, 't', '_tmp' );
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
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.
77eval {$mb->dispatch('test')};
78is $@, '';
79
80eval {$mb->dispatch('clean')};
81is $@, '';
82
83
84SKIP: {
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
93eval {$mb->dispatch('realclean')};
94is $@, '';
95
96# Make sure blib/ is gone after 'realclean'
97ok ! -e 'blib';
98
99
100# cleanup
101chdir( $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;
112chdir( $dist->dirname ) or die "Can't chdir to '@{[$dist->dirname]}': $!";
113
114$mb = Module::Build->new_from_context;
115is $@, '';
116
117$mb->dispatch('build');
118is $@, '';
119
120$mb->dispatch('test');
121is $@, '';
122
123$mb->dispatch('realclean');
124is $@, '';
125
126# cleanup
127chdir( $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', <<"---");
142use strict;
143use Module::Build;
144
145my \$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
159MODULE = Simple PACKAGE = Simple
160
161SV *
162okay()
163 CODE:
164 RETVAL = newSVpv( "ok", 0 );
165 OUTPUT:
166 RETVAL
167---
168
169$dist->add_file( 'Simple.pm', <<"---" );
170package Simple;
171
172\$VERSION = '0.01';
173
174require Exporter;
175require DynaLoader;
176
177\@ISA = qw( Exporter DynaLoader );
178\@EXPORT_OK = qw( okay );
179
180bootstrap Simple \$VERSION;
181
1821;
183
184__END__
185
186=head1 NAME
187
188Simple - Perl extension for blah blah blah
189
190=head1 DESCRIPTION
191
192Stub documentation for Simple.
193
194=head1 AUTHOR
195
196A. U. Thor, a.u.thor\@a.galaxy.far.far.away
197
198=cut
199---
200$dist->change_file('t/basic.t', <<"---");
201use Test::More tests => 2;
202use strict;
203
204use Simple;
205ok( 1 );
206
207ok( Simple::okay() eq 'ok' );
208---
209
210$dist->regen;
211chdir( $dist->dirname ) or die "Can't chdir to '@{[$dist->dirname]}': $!";
212
213
214$mb = Module::Build->new_from_context;
215is $@, '';
216
217$mb->dispatch('build');
218is $@, '';
219
220$mb->dispatch('test');
221is $@, '';
222
223$mb->dispatch('realclean');
224is $@, '';
225
226# cleanup
227chdir( $cwd ) or die "Can''t chdir to '$cwd': $!";
228$dist->remove;
229
230use File::Path;
231rmtree( $tmp );