This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl 5.003_01: [changes beteween cumulative patches and tarball release]
[perl5.git] / utils / h2xs.PL
CommitLineData
4633a7c4
LW
1#!/usr/local/bin/perl
2
3use Config;
4use File::Basename qw(&basename &dirname);
5
6# List explicitly here the variables you want Configure to
7# generate. Metaconfig only looks for shell variables, so you
8# have to mention them as if they were shell variables, not
9# %Config entries. Thus you write
10# $startperl
11# to ensure Configure will look for $Config{startperl}.
12
13# This forces PL files to create target in same directory as PL file.
14# This is so that make depend always knows where to find PL derivatives.
15chdir(dirname($0));
16($file = basename($0)) =~ s/\.PL$//;
17$file =~ s/\.pl$//
5ae7f1db 18 if ($^O eq 'VMS' or $^O eq 'os2'); # "case-forgiving"
4633a7c4
LW
19
20open OUT,">$file" or die "Can't create $file: $!";
21
22print "Extracting $file (with variable substitutions)\n";
23
24# In this section, perl variables will be expanded during extraction.
25# You can use $Config{...} to use Configure variables.
26
27print OUT <<"!GROK!THIS!";
28$Config{'startperl'}
29 eval 'exec perl -S \$0 "\$@"'
30 if 0;
40000a8c
AD
31!GROK!THIS!
32
4633a7c4
LW
33# In the following, perl variables are not expanded during extraction.
34
35print OUT <<'!NO!SUBS!';
3edbfbe5
TB
36=head1 NAME
37
38h2xs - convert .h C header files to Perl extensions
39
40=head1 SYNOPSIS
41
ead2a595 42B<h2xs> [B<-AOPXcf>] [B<-v> version] [B<-n> module_name] [B<-p> prefix] [B<-s> sub] [headerfile [extra_libraries]]
f508c652
PP
43
44B<h2xs> B<-h>
3edbfbe5
TB
45
46=head1 DESCRIPTION
47
48I<h2xs> builds a Perl extension from any C header file. The extension will
49include functions which can be used to retrieve the value of any #define
50statement which was in the C header.
51
52The I<module_name> will be used for the name of the extension. If
53module_name is not supplied then the name of the header file will be used,
54with the first character capitalized.
55
56If the extension might need extra libraries, they should be included
57here. The extension Makefile.PL will take care of checking whether
58the libraries actually exist and how they should be loaded.
59The extra libraries should be specified in the form -lm -lposix, etc,
60just as on the cc command line. By default, the Makefile.PL will
61search through the library path determined by Configure. That path
62can be augmented by including arguments of the form B<-L/another/library/path>
63in the extra-libraries argument.
64
65=head1 OPTIONS
66
67=over 5
68
f508c652 69=item B<-A>
3edbfbe5 70
f508c652
PP
71Omit all autoload facilities. This is the same as B<-c> but also removes the
72S<C<require AutoLoader>> statement from the .pm file.
3edbfbe5 73
2920c5d2
PP
74=item B<-O>
75
76Allows a pre-existing extension directory to be overwritten.
77
f508c652 78=item B<-P>
3edbfbe5 79
f508c652 80Omit the autogenerated stub POD section.
3edbfbe5
TB
81
82=item B<-c>
83
84Omit C<constant()> from the .xs file and corresponding specialised
85C<AUTOLOAD> from the .pm file.
86
f508c652 87=item B<-f>
3edbfbe5 88
f508c652
PP
89Allows an extension to be created for a header even if that header is
90not found in /usr/include.
91
92=item B<-h>
93
94Print the usage, help and version for this h2xs and exit.
95
96=item B<-n> I<module_name>
97
98Specifies a name to be used for the extension, e.g., S<-n RPC::DCE>
99
ead2a595
PP
100=item B<-p> I<prefix>
101
102Specify a prefix which should be removed from the Perl function names, e.g., S<-p sec_rgy_>
103This sets up the XS B<PREFIX> keyword and removes the prefix from functions that are
104autoloaded via the C<constant()> mechansim.
105
106=item B<-s> I<sub1,sub2>
107
108Create a perl subroutine for the specified macros rather than autoload with the constant() subroutine.
109These macros are assumed to have a return type of B<char *>, e.g., S<-s sec_rgy_wildcard_name,sec_rgy_wildcard_sid>.
110
f508c652
PP
111=item B<-v> I<version>
112
113Specify a version number for this extension. This version number is added
114to the templates. The default is 0.01.
3edbfbe5 115
2920c5d2
PP
116=item B<-X>
117
118Omit the XS portion. Used to generate templates for a module which is not
119XS-based.
120
3edbfbe5
TB
121=back
122
123=head1 EXAMPLES
124
125
126 # Default behavior, extension is Rusers
127 h2xs rpcsvc/rusers
128
129 # Same, but extension is RUSERS
130 h2xs -n RUSERS rpcsvc/rusers
131
132 # Extension is rpcsvc::rusers. Still finds <rpcsvc/rusers.h>
133 h2xs rpcsvc::rusers
134
135 # Extension is ONC::RPC. Still finds <rpcsvc/rusers.h>
136 h2xs -n ONC::RPC rpcsvc/rusers
137
138 # Without constant() or AUTOLOAD
139 h2xs -c rpcsvc/rusers
140
141 # Creates templates for an extension named RPC
142 h2xs -cfn RPC
143
144 # Extension is ONC::RPC.
145 h2xs -cfn ONC::RPC
146
147 # Makefile.PL will look for library -lrpc in
148 # additional directory /opt/net/lib
149 h2xs rpcsvc/rusers -L/opt/net/lib -lrpc
150
ead2a595
PP
151 # Extension is DCE::rgynbase
152 # prefix "sec_rgy_" is dropped from perl function names
153 h2xs -n DCE::rgynbase -p sec_rgy_ dce/rgynbase
154
155 # Extension is DCE::rgynbase
156 # prefix "sec_rgy_" is dropped from perl function names
157 # subroutines are created for sec_rgy_wildcard_name and sec_rgy_wildcard_sid
158 h2xs -n DCE::rgynbase -p sec_rgy_ \
159 -s sec_rgy_wildcard_name,sec_rgy_wildcard_sid dce/rgynbase
3edbfbe5
TB
160
161=head1 ENVIRONMENT
162
163No environment variables are used.
164
165=head1 AUTHOR
166
167Larry Wall and others
168
169=head1 SEE ALSO
170
f508c652 171L<perl>, L<perlxstut>, L<ExtUtils::MakeMaker>, and L<AutoLoader>.
3edbfbe5
TB
172
173=head1 DIAGNOSTICS
174
175The usual warnings if it can't read or write the files involved.
176
177=cut
178
2920c5d2 179my( $H2XS_VERSION ) = '$Revision: 1.16 $' =~ /\$Revision:\s+([^\s]+)/;
f508c652 180my $TEMPLATE_VERSION = '0.01';
a0d0e21e
LW
181
182use Getopt::Std;
183
e1666bf5
TB
184sub usage{
185 warn "@_\n" if @_;
ead2a595 186 die "h2xs [-AOPXcfh] [-v version] [-n module_name] [-p prefix] [-s subs] [headerfile [extra_libraries]]
f508c652 187version: $H2XS_VERSION
e1666bf5
TB
188 -f Force creation of the extension even if the C header does not exist.
189 -n Specify a name to use for the extension (recommended).
190 -c Omit the constant() function and specialised AUTOLOAD from the XS file.
ead2a595
PP
191 -p Specify a prefix which should be removed from the Perl function names.
192 -s Create subroutines for specified macros.
3edbfbe5 193 -A Omit all autoloading facilities (implies -c).
2920c5d2 194 -O Allow overwriting of a pre-existing extension directory.
f508c652 195 -P Omit the stub POD section.
2920c5d2 196 -X Omit the XS portion.
f508c652 197 -v Specify a version number for this extension.
e1666bf5
TB
198 -h Display this help message
199extra_libraries
200 are any libraries that might be needed for loading the
201 extension, e.g. -lm would try to link in the math library.
f508c652 202";
e1666bf5 203}
a0d0e21e 204
a0d0e21e 205
ead2a595 206getopts("AOPXcfhxv:n:p:s:") || usage;
a0d0e21e 207
e1666bf5 208usage if $opt_h;
f508c652
PP
209
210if( $opt_v ){
211 $TEMPLATE_VERSION = $opt_v;
212}
e1666bf5 213$opt_c = 1 if $opt_A;
ead2a595 214%const_xsub = map { $_,1 } split(/,+/, $opt_s) if $opt_s;
a0d0e21e 215
e1666bf5 216$path_h = shift;
a0d0e21e 217$extralibs = "@ARGV";
e1666bf5
TB
218
219usage "Must supply header file or module name\n"
220 unless ($path_h or $opt_n);
221
a0d0e21e
LW
222
223if( $path_h ){
e1666bf5
TB
224 $name = $path_h;
225 if( $path_h =~ s#::#/#g && $opt_n ){
226 warn "Nesting of headerfile ignored with -n\n";
227 }
228 $path_h .= ".h" unless $path_h =~ /\.h$/;
ead2a595
PP
229 if ($^O eq 'VMS') { # Consider overrides of default location
230 if ($path_h !~ m![:>\[]!) {
231 my($hadsys) = ($path_h =~ s!^sys/!!i);
232 if ($ENV{'DECC$System_Include'}) { $path_h = "DECC\$System_Include:$path_h"; }
233 elsif ($ENV{'DECC$Library_Include'}) { $path_h = "DECC\$Library_Include:$path_h"; }
234 elsif ($ENV{'GNU_CC_Include'}) { $path_h = 'GNU_CC_Include:' .
235 ($hadsys ? '[vms]' : '[000000]') . $path_h; }
236 elsif ($ENV{'VAXC$Include'}) { $path_h = "VAXC\$_Include:$path_h"; }
237 else { $path_h = "Sys\$Library:$path_h"; }
238 }
239 }
240 elsif ($^O eq 'os2') {
241 $path_h = "/usr/include/$path_h" unless $path_h =~ m#^([a-z]:)?[./]#i;
242 }
243 else { $path_h = "/usr/include/$path_h" unless $path_h =~ m#^[./]#; }
e1666bf5
TB
244 die "Can't find $path_h\n" if ( ! $opt_f && ! -f $path_h );
245
246 # Scan the header file (we should deal with nested header files)
247 # Record the names of simple #define constants into const_names
248 # Function prototypes are not (currently) processed.
249 open(CH, "<$path_h") || die "Can't open $path_h: $!\n";
250 while (<CH>) {
ead2a595
PP
251 if (/^#[ \t]*define\s+([\$\w]+)\b\s*[^("]/) {
252 print "Matched $_ ($1)\n";
e1666bf5
TB
253 $_ = $1;
254 next if /^_.*_h_*$/i; # special case, but for what?
ead2a595
PP
255 if (defined $opt_p)
256 if (!/^$opt_p(\d)/) {
257 ++$prefix{$_} if s/^$opt_p//;
258 }
259 else {
260 warn "can't remove $opt_p prefix from '$_'!\n";
261 }
262 }
e1666bf5 263 $const_names{$_}++;
a0d0e21e 264 }
e1666bf5
TB
265 }
266 close(CH);
267 @const_names = sort keys %const_names;
a0d0e21e
LW
268}
269
e1666bf5 270
a0d0e21e
LW
271$module = $opt_n || do {
272 $name =~ s/\.h$//;
273 if( $name !~ /::/ ){
274 $name =~ s#^.*/##;
275 $name = "\u$name";
276 }
277 $name;
278};
279
8e07c86e 280(chdir 'ext', $ext = 'ext/') if -d 'ext';
a0d0e21e
LW
281
282if( $module =~ /::/ ){
283 $nested = 1;
284 @modparts = split(/::/,$module);
285 $modfname = $modparts[-1];
286 $modpname = join('/',@modparts);
287}
288else {
289 $nested = 0;
290 @modparts = ();
291 $modfname = $modpname = $module;
292}
293
294
2920c5d2
PP
295if ($opt_O) {
296 warn "Overwriting existing $ext$modpname!!!\n" if -e $modpname;
297} else {
298 die "Won't overwrite existing $ext$modpname\n" if -e $modpname;
299}
c07a80fd
PP
300if( $nested ){
301 $modpath = "";
302 foreach (@modparts){
303 mkdir("$modpath$_", 0777);
304 $modpath .= "$_/";
305 }
306}
a0d0e21e 307mkdir($modpname, 0777);
8e07c86e 308chdir($modpname) || die "Can't chdir $ext$modpname: $!\n";
a0d0e21e 309
2920c5d2
PP
310if( ! $opt_X ){ # use XS, unless it was disabled
311 open(XS, ">$modfname.xs") || die "Can't create $ext$modpname/$modfname.xs: $!\n";
312}
8e07c86e 313open(PM, ">$modfname.pm") || die "Can't create $ext$modpname/$modfname.pm: $!\n";
a0d0e21e 314
a0d0e21e 315$" = "\n\t";
8e07c86e 316warn "Writing $ext$modpname/$modfname.pm\n";
a0d0e21e 317
a0d0e21e
LW
318print PM <<"END";
319package $module;
320
2920c5d2
PP
321use strict;
322END
323
324if( $opt_X || $opt_c || $opt_A ){
325 # we won't have our own AUTOLOAD(), so won't have $AUTOLOAD
326 print PM <<'END';
327use vars qw($VERSION @ISA @EXPORT);
328END
329}
330else{
331 # we'll have an AUTOLOAD(), and it will have $AUTOLOAD and
332 # will want Carp.
333 print PM <<'END';
334use Carp;
335use vars qw($VERSION @ISA @EXPORT $AUTOLOAD);
336END
337}
338
339print PM <<'END';
340
a0d0e21e 341require Exporter;
2920c5d2
PP
342END
343
344print PM <<"END" if ! $opt_X; # use DynaLoader, unless XS was disabled
a0d0e21e 345require DynaLoader;
3edbfbe5
TB
346END
347
2920c5d2
PP
348# require autoloader if XS is disabled.
349# if XS is enabled, require autoloader unless autoloading is disabled.
350if( $opt_X || (! $opt_A) ){
3edbfbe5
TB
351 print PM <<"END";
352require AutoLoader;
353END
354}
355
2920c5d2 356if( $opt_X || ($opt_c && ! $opt_A) ){
3edbfbe5 357 # we won't have our own AUTOLOAD(), so we'll inherit it.
2920c5d2
PP
358 if( ! $opt_X ) { # use DynaLoader, unless XS was disabled
359 print PM <<"END";
e1666bf5 360
a0d0e21e 361\@ISA = qw(Exporter AutoLoader DynaLoader);
3edbfbe5 362END
2920c5d2
PP
363 }
364 else{
365 print PM <<"END";
366
367\@ISA = qw(Exporter AutoLoader);
368END
369 }
3edbfbe5
TB
370}
371else{
372 # 1) we have our own AUTOLOAD(), so don't need to inherit it.
373 # or
374 # 2) we don't want autoloading mentioned.
2920c5d2
PP
375 if( ! $opt_X ){ # use DynaLoader, unless XS was disabled
376 print PM <<"END";
3edbfbe5
TB
377
378\@ISA = qw(Exporter DynaLoader);
379END
2920c5d2
PP
380 }
381 else{
382 print PM <<"END";
383
384\@ISA = qw(Exporter);
385END
386 }
3edbfbe5 387}
e1666bf5 388
3edbfbe5 389print PM<<"END";
e1666bf5
TB
390# Items to export into callers namespace by default. Note: do not export
391# names by default without a very good reason. Use EXPORT_OK instead.
392# Do not simply export all your public functions/methods/constants.
a0d0e21e 393\@EXPORT = qw(
e1666bf5 394 @const_names
a0d0e21e 395);
f508c652
PP
396\$VERSION = '$TEMPLATE_VERSION';
397
e1666bf5
TB
398END
399
2920c5d2 400print PM <<"END" unless $opt_c or $opt_X;
a0d0e21e 401sub AUTOLOAD {
3edbfbe5
TB
402 # This AUTOLOAD is used to 'autoload' constants from the constant()
403 # XS function. If a constant is not found then control is passed
404 # to the AUTOLOAD in AutoLoader.
e1666bf5 405
2920c5d2 406 my \$constname;
a0d0e21e 407 (\$constname = \$AUTOLOAD) =~ s/.*:://;
2920c5d2 408 my \$val = constant(\$constname, \@_ ? \$_[0] : 0);
a0d0e21e
LW
409 if (\$! != 0) {
410 if (\$! =~ /Invalid/) {
411 \$AutoLoader::AUTOLOAD = \$AUTOLOAD;
412 goto &AutoLoader::AUTOLOAD;
413 }
414 else {
2920c5d2 415 croak "Your vendor has not defined $module macro \$constname";
a0d0e21e
LW
416 }
417 }
418 eval "sub \$AUTOLOAD { \$val }";
419 goto &\$AUTOLOAD;
420}
421
a0d0e21e 422END
a0d0e21e 423
2920c5d2
PP
424if( ! $opt_X ){ # print bootstrap, unless XS is disabled
425 print PM <<"END";
f508c652 426bootstrap $module \$VERSION;
2920c5d2
PP
427END
428}
429
430if( $opt_P ){ # if POD is disabled
431 $after = '__END__';
432}
433else {
434 $after = '=cut';
435}
436
437print PM <<"END";
a0d0e21e 438
e1666bf5 439# Preloaded methods go here.
a0d0e21e 440
2920c5d2 441# Autoload methods go after $after, and are processed by the autosplit program.
a0d0e21e
LW
442
4431;
e1666bf5 444__END__
a0d0e21e 445END
a0d0e21e 446
f508c652
PP
447$author = "A. U. Thor";
448$email = 'a.u.thor@a.galaxy.far.far.away';
449
450$pod = <<"END" unless $opt_P;
451## Below is the stub of documentation for your module. You better edit it!
452#
453#=head1 NAME
454#
455#$module - Perl extension for blah blah blah
456#
457#=head1 SYNOPSIS
458#
459# use $module;
460# blah blah blah
461#
462#=head1 DESCRIPTION
463#
464#Stub documentation for $module was created by h2xs. It looks like the
465#author of the extension was negligent enough to leave the stub
466#unedited.
467#
468#Blah blah blah.
469#
470#=head1 AUTHOR
471#
472#$author, $email
473#
474#=head1 SEE ALSO
475#
476#perl(1).
477#
478#=cut
479END
480
481$pod =~ s/^\#//gm unless $opt_P;
482print PM $pod unless $opt_P;
483
a0d0e21e
LW
484close PM;
485
e1666bf5 486
2920c5d2 487if( ! $opt_X ){ # print XS, unless it is disabled
8e07c86e 488warn "Writing $ext$modpname/$modfname.xs\n";
e1666bf5 489
a0d0e21e 490print XS <<"END";
4633a7c4
LW
491#ifdef __cplusplus
492extern "C" {
493#endif
a0d0e21e
LW
494#include "EXTERN.h"
495#include "perl.h"
496#include "XSUB.h"
4633a7c4
LW
497#ifdef __cplusplus
498}
499#endif
a0d0e21e
LW
500
501END
502if( $path_h ){
503 my($h) = $path_h;
504 $h =~ s#^/usr/include/##;
ead2a595 505 if ($^O eq 'VMS') { $h =~ s#.*vms\]#sys/# or $h =~ s#.*[:>\]]##; }
a0d0e21e
LW
506print XS <<"END";
507#include <$h>
508
509END
510}
511
512if( ! $opt_c ){
513print XS <<"END";
514static int
515not_here(s)
516char *s;
517{
518 croak("$module::%s not implemented on this architecture", s);
519 return -1;
520}
521
522static double
523constant(name, arg)
524char *name;
525int arg;
526{
527 errno = 0;
528 switch (*name) {
529END
530
e1666bf5
TB
531my(@AZ, @az, @under);
532
533foreach(@const_names){
534 @AZ = 'A' .. 'Z' if !@AZ && /^[A-Z]/;
535 @az = 'a' .. 'z' if !@az && /^[a-z]/;
536 @under = '_' if !@under && /^_/;
537}
538
a0d0e21e
LW
539foreach $letter (@AZ, @az, @under) {
540
e1666bf5 541 last if $letter eq 'a' && !@const_names;
a0d0e21e
LW
542
543 print XS " case '$letter':\n";
544 my($name);
e1666bf5
TB
545 while (substr($const_names[0],0,1) eq $letter) {
546 $name = shift(@const_names);
ead2a595
PP
547 $macro = $prefix{$name} ? "$opt_p$name" : $name;
548 next if $const_xsub{$macro};
a0d0e21e
LW
549 print XS <<"END";
550 if (strEQ(name, "$name"))
ead2a595
PP
551#ifdef $macro
552 return $macro;
a0d0e21e
LW
553#else
554 goto not_there;
555#endif
556END
557 }
558 print XS <<"END";
559 break;
560END
561}
562print XS <<"END";
563 }
564 errno = EINVAL;
565 return 0;
566
567not_there:
568 errno = ENOENT;
569 return 0;
570}
571
e1666bf5
TB
572END
573}
574
ead2a595 575$prefix = "PREFIX = $opt_p" if defined $opt_p;
e1666bf5
TB
576# Now switch from C to XS by issuing the first MODULE declaration:
577print XS <<"END";
a0d0e21e 578
ead2a595
PP
579MODULE = $module PACKAGE = $module $prefix
580
581END
582
583foreach (sort keys %const_xsub) {
584 print XS <<"END";
585char *
586$_()
587
588 CODE:
589#ifdef $_
590 RETVAL = $_;
591#else
592 croak("Your vendor has not defined the $module macro $_");
593#endif
594
595 OUTPUT:
596 RETVAL
a0d0e21e 597
e1666bf5 598END
ead2a595 599}
e1666bf5
TB
600
601# If a constant() function was written then output a corresponding
602# XS declaration:
603print XS <<"END" unless $opt_c;
604
a0d0e21e
LW
605double
606constant(name,arg)
607 char * name
608 int arg
609
610END
a0d0e21e 611
ead2a595
PP
612sub print_decl {
613 my $fh = shift;
614 my $decl = shift;
615 my ($type, $name, $args) = @$decl;
616 my @argnames = map {$_->[1]} @$args;
617 my @argtypes = map { normalize_type( $_->[0] ) } @$args;
618 my $numargs = @$args;
619 if ($numargs and $argtypes[-1] eq '...') {
620 $numargs--;
621 $argnames[-1] = '...';
622 }
623 local $" = ', ';
624 $type = normalize_type($type);
625
626 print $fh <<"EOP";
627
628$type
629$name(@argnames)
630EOP
631
632 for $arg (0 .. $numargs - 1) {
633 print $fh <<"EOP";
634 $argtypes[$arg] $argnames[$arg]
635EOP
636 }
637}
638
639my $ignore_mods = '(?:\b(?:__const__|static|inline|__inline__)\b\s*)*';
640
641sub normalize_type {
642 my $type = shift;
643 $type =~ s/$ignore_mods//go;
644 $type =~ s/\s+/ /g;
645 $type =~ s/\s+$//;
646 $type =~ s/^\s+//;
647 $type =~ s/\b\*/ */g;
648 $type =~ s/\*\b/* /g;
649 $type =~ s/\*\s+(?=\*)/*/g;
650 $type;
651}
652
653if ($opt_x) {
654 require C::Scan; # Run-time directive
655 require Config; # Run-time directive
656 my $c = new C::Scan 'filename' => $path_h;
657 $c->set('includeDirs' => [$Config::Config{shrpdir}]);
658
659 my $fdec = $c->get('parsed_fdecls');
660
661 for $decl (@$fdec) { print_decl(\*XS, $decl) }
662}
663
a0d0e21e 664close XS;
2920c5d2 665} # if( ! $opt_X )
e1666bf5 666
8e07c86e
AD
667warn "Writing $ext$modpname/Makefile.PL\n";
668open(PL, ">Makefile.PL") || die "Can't create $ext$modpname/Makefile.PL: $!\n";
a0d0e21e 669
a0d0e21e
LW
670print PL <<'END';
671use ExtUtils::MakeMaker;
672# See lib/ExtUtils/MakeMaker.pm for details of how to influence
42793c05 673# the contents of the Makefile that is written.
a0d0e21e 674END
42793c05
TB
675print PL "WriteMakefile(\n";
676print PL " 'NAME' => '$module',\n";
c07a80fd 677print PL " 'VERSION_FROM' => '$modfname.pm', # finds \$VERSION\n";
2920c5d2
PP
678if( ! $opt_X ){ # print C stuff, unless XS is disabled
679 print PL " 'LIBS' => ['$extralibs'], # e.g., '-lm' \n";
680 print PL " 'DEFINE' => '', # e.g., '-DHAVE_SOMETHING' \n";
681 print PL " 'INC' => '', # e.g., '-I/usr/include/other' \n";
682}
a0d0e21e 683print PL ");\n";
f508c652
PP
684close(PL) || die "Can't close $ext$modpname/Makefile.PL: $!\n";
685
686warn "Writing $ext$modpname/test.pl\n";
687open(EX, ">test.pl") || die "Can't create $ext$modpname/test.pl: $!\n";
688print EX <<'_END_';
689# Before `make install' is performed this script should be runnable with
690# `make test'. After `make install' it should work as `perl test.pl'
691
692######################### We start with some black magic to print on failure.
693
694# Change 1..1 below to 1..last_test_to_print .
695# (It may become useful if the test is moved to ./t subdirectory.)
696
5ae7f1db 697BEGIN { $| = 1; print "1..1\n"; }
f508c652
PP
698END {print "not ok 1\n" unless $loaded;}
699_END_
700print EX <<_END_;
701use $module;
702_END_
703print EX <<'_END_';
704$loaded = 1;
705print "ok 1\n";
706
707######################### End of black magic.
708
709# Insert your test code below (better if it prints "ok 13"
710# (correspondingly "not ok 13") depending on the success of chunk 13
711# of the test code):
e1666bf5 712
f508c652
PP
713_END_
714close(EX) || die "Can't close $ext$modpname/test.pl: $!\n";
a0d0e21e 715
c07a80fd
PP
716warn "Writing $ext$modpname/Changes\n";
717open(EX, ">Changes") || die "Can't create $ext$modpname/Changes: $!\n";
718print EX "Revision history for Perl extension $module.\n\n";
719print EX "$TEMPLATE_VERSION ",scalar localtime,"\n";
720print EX "\t- original version; created by h2xs $H2XS_VERSION\n\n";
721close(EX) || die "Can't close $ext$modpname/Changes: $!\n";
722
723warn "Writing $ext$modpname/MANIFEST\n";
5ae7f1db
PP
724open(MANI,'>MANIFEST') or die "Can't create MANIFEST: $!";
725@files = <*>;
726if (!@files) {
727 eval {opendir(D,'.');};
728 unless ($@) { @files = readdir(D); closedir(D); }
729}
730if (!@files) { @files = map {chomp && $_} `ls`; }
731print MANI join("\n",@files);
732close MANI;
40000a8c 733!NO!SUBS!
4633a7c4
LW
734
735close OUT or die "Can't close $file: $!";
736chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
737exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';