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