Correct CODE block declaration
[p5sagit/p5-mst-13.2.git] / utils / h2xs.PL
index b7bf496..f9868dc 100644 (file)
@@ -15,8 +15,7 @@ use File::Basename qw(&basename &dirname);
 chdir(dirname($0));
 ($file = basename($0)) =~ s/\.PL$//;
 $file =~ s/\.pl$//
-       if ($Config{'osname'} eq 'VMS' or
-           $Config{'osname'} eq 'OS2');  # "case-forgiving"
+       if ($^O eq 'VMS' or $^O eq 'os2');  # "case-forgiving"
 
 open OUT,">$file" or die "Can't create $file: $!";
 
@@ -41,7 +40,9 @@ h2xs - convert .h C header files to Perl extensions
 
 =head1 SYNOPSIS
 
-B<h2xs> [B<-Acfh>] [B<-n> module_name] [headerfile [extra_libraries]]
+B<h2xs> [B<-AOPXcf>] [B<-v> version] [B<-n> module_name] [headerfile [extra_libraries]]
+
+B<h2xs> B<-h>
 
 =head1 DESCRIPTION
 
@@ -66,24 +67,46 @@ in the extra-libraries argument.
 
 =over 5
 
-=item B<-n> I<module_name>
+=item B<-A>
 
-Specifies a name to be used for the extension, e.g., S<-n RPC::DCE>
+Omit all autoload facilities.  This is the same as B<-c> but also removes the
+S<C<require AutoLoader>> statement from the .pm file.
 
-=item B<-f>
+=item B<-O>
 
-Allows an extension to be created for a header even if that header is
-not found in /usr/include.
+Allows a pre-existing extension directory to be overwritten.
+
+=item B<-P>
+
+Omit the autogenerated stub POD section. 
 
 =item B<-c>
 
 Omit C<constant()> from the .xs file and corresponding specialised
 C<AUTOLOAD> from the .pm file.
 
-=item B<-A>
+=item B<-f>
 
-Omit all autoload facilities.  This is the same as B<-c> but also removes the
-S<C<require AutoLoader>> statement from the .pm file.
+Allows an extension to be created for a header even if that header is
+not found in /usr/include.
+
+=item B<-h>
+
+Print the usage, help and version for this h2xs and exit.
+
+=item B<-n> I<module_name>
+
+Specifies a name to be used for the extension, e.g., S<-n RPC::DCE>
+
+=item B<-v> I<version>
+
+Specify a version number for this extension.  This version number is added
+to the templates.  The default is 0.01.
+
+=item B<-X>
+
+Omit the XS portion.  Used to generate templates for a module which is not
+XS-based.
 
 =back
 
@@ -126,7 +149,7 @@ Larry Wall and others
 
 =head1 SEE ALSO
 
-L<perl>, L<ExtUtils::MakeMaker>, L<AutoLoader>
+L<perl>, L<perlxstut>, L<ExtUtils::MakeMaker>, and L<AutoLoader>.
 
 =head1 DIAGNOSTICS
 
@@ -134,27 +157,38 @@ The usual warnings if it can't read or write the files involved.
 
 =cut
 
+my( $H2XS_VERSION ) = '$Revision: 1.16 $' =~ /\$Revision:\s+([^\s]+)/;
+my $TEMPLATE_VERSION = '0.01';
 
 use Getopt::Std;
 
 sub usage{
        warn "@_\n" if @_;
-    die 'h2xs [-Acfh] [-n module_name] [headerfile [extra_libraries]]
+    die "h2xs [-AOPXcfh] [-v version] [-n module_name] [headerfile [extra_libraries]]
+version: $H2XS_VERSION
     -f   Force creation of the extension even if the C header does not exist.
     -n   Specify a name to use for the extension (recommended).
     -c   Omit the constant() function and specialised AUTOLOAD from the XS file.
     -A   Omit all autoloading facilities (implies -c).
+    -O   Allow overwriting of a pre-existing extension directory.
+    -P   Omit the stub POD section.
+    -X   Omit the XS portion.
+    -v   Specify a version number for this extension.
     -h   Display this help message
 extra_libraries
          are any libraries that might be needed for loading the
          extension, e.g. -lm would try to link in the math library.
-';
+";
 }
 
 
-getopts("Acfhn:") || usage;
+getopts("AOPXcfhv:n:") || usage;
 
 usage if $opt_h;
+
+if( $opt_v ){
+       $TEMPLATE_VERSION = $opt_v;
+}
 $opt_c = 1 if $opt_A;
 
 $path_h    = shift;
@@ -213,13 +247,24 @@ else {
 }
 
 
-die "Won't overwrite existing $ext$modpname\n" if -e $modpname;
-# quick hack, should really loop over @modparts
-mkdir($modparts[0], 0777) if $nested;
+if ($opt_O) {
+       warn "Overwriting existing $ext$modpname!!!\n" if -e $modpname;
+} else {
+       die "Won't overwrite existing $ext$modpname\n" if -e $modpname;
+}
+if( $nested ){
+       $modpath = "";
+       foreach (@modparts){
+               mkdir("$modpath$_", 0777);
+               $modpath .= "$_/";
+       }
+}
 mkdir($modpname, 0777);
 chdir($modpname) || die "Can't chdir $ext$modpname: $!\n";
 
-open(XS, ">$modfname.xs") || die "Can't create $ext$modpname/$modfname.xs: $!\n";
+if( ! $opt_X ){  # use XS, unless it was disabled
+  open(XS, ">$modfname.xs") || die "Can't create $ext$modpname/$modfname.xs: $!\n";
+}
 open(PM, ">$modfname.pm") || die "Can't create $ext$modpname/$modfname.pm: $!\n";
 
 $" = "\n\t";
@@ -228,31 +273,72 @@ warn "Writing $ext$modpname/$modfname.pm\n";
 print PM <<"END";
 package $module;
 
+use strict;
+END
+
+if( $opt_X || $opt_c || $opt_A ){
+       # we won't have our own AUTOLOAD(), so won't have $AUTOLOAD
+       print PM <<'END';
+use vars qw($VERSION @ISA @EXPORT);
+END
+}
+else{
+       # we'll have an AUTOLOAD(), and it will have $AUTOLOAD and
+       # will want Carp.
+       print PM <<'END';
+use Carp;
+use vars qw($VERSION @ISA @EXPORT $AUTOLOAD);
+END
+}
+
+print PM <<'END';
+
 require Exporter;
+END
+
+print PM <<"END" if ! $opt_X;  # use DynaLoader, unless XS was disabled
 require DynaLoader;
 END
 
-if( ! $opt_A ){
+# require autoloader if XS is disabled.
+# if XS is enabled, require autoloader unless autoloading is disabled.
+if( $opt_X || (! $opt_A) ){
        print PM <<"END";
 require AutoLoader;
 END
 }
 
-if( $opt_c && ! $opt_A ){
+if( $opt_X || ($opt_c && ! $opt_A) ){
        # we won't have our own AUTOLOAD(), so we'll inherit it.
-       print PM <<"END";
+       if( ! $opt_X ) { # use DynaLoader, unless XS was disabled
+               print PM <<"END";
 
 \@ISA = qw(Exporter AutoLoader DynaLoader);
 END
+       }
+       else{
+               print PM <<"END";
+
+\@ISA = qw(Exporter AutoLoader);
+END
+       }
 }
 else{
        # 1) we have our own AUTOLOAD(), so don't need to inherit it.
        # or
        # 2) we don't want autoloading mentioned.
-       print PM <<"END";
+       if( ! $opt_X ){ # use DynaLoader, unless XS was disabled
+               print PM <<"END";
 
 \@ISA = qw(Exporter DynaLoader);
 END
+       }
+       else{
+               print PM <<"END";
+
+\@ISA = qw(Exporter);
+END
+       }
 }
 
 print PM<<"END";
@@ -262,25 +348,26 @@ print PM<<"END";
 \@EXPORT = qw(
        @const_names
 );
+\$VERSION = '$TEMPLATE_VERSION';
+
 END
 
-print PM <<"END" unless $opt_c;
+print PM <<"END" unless $opt_c or $opt_X;
 sub AUTOLOAD {
     # This AUTOLOAD is used to 'autoload' constants from the constant()
     # XS function.  If a constant is not found then control is passed
     # to the AUTOLOAD in AutoLoader.
 
-    local(\$constname);
+    my \$constname;
     (\$constname = \$AUTOLOAD) =~ s/.*:://;
-    \$val = constant(\$constname, \@_ ? \$_[0] : 0);
+    my \$val = constant(\$constname, \@_ ? \$_[0] : 0);
     if (\$! != 0) {
        if (\$! =~ /Invalid/) {
            \$AutoLoader::AUTOLOAD = \$AUTOLOAD;
            goto &AutoLoader::AUTOLOAD;
        }
        else {
-           (\$pack,\$file,\$line) = caller;
-           die "Your vendor has not defined $module macro \$constname, used at \$file line \$line.\n";
+               croak "Your vendor has not defined $module macro \$constname";
        }
     }
     eval "sub \$AUTOLOAD { \$val }";
@@ -289,20 +376,70 @@ sub AUTOLOAD {
 
 END
 
+if( ! $opt_X ){ # print bootstrap, unless XS is disabled
+       print PM <<"END";
+bootstrap $module \$VERSION;
+END
+}
+
+if( $opt_P ){ # if POD is disabled
+       $after = '__END__';
+}
+else {
+       $after = '=cut';
+}
+
 print PM <<"END";
-bootstrap $module;
 
 # Preloaded methods go here.
 
-# Autoload methods go after __END__, and are processed by the autosplit program.
+# Autoload methods go after $after, and are processed by the autosplit program.
 
 1;
 __END__
 END
 
+$author = "A. U. Thor";
+$email = 'a.u.thor@a.galaxy.far.far.away';
+
+$pod = <<"END" unless $opt_P;
+## Below is the stub of documentation for your module. You better edit it!
+#
+#=head1 NAME
+#
+#$module - Perl extension for blah blah blah
+#
+#=head1 SYNOPSIS
+#
+#  use $module;
+#  blah blah blah
+#
+#=head1 DESCRIPTION
+#
+#Stub documentation for $module was created by h2xs. It looks like the
+#author of the extension was negligent enough to leave the stub
+#unedited.
+#
+#Blah blah blah.
+#
+#=head1 AUTHOR
+#
+#$author, $email
+#
+#=head1 SEE ALSO
+#
+#perl(1).
+#
+#=cut
+END
+
+$pod =~ s/^\#//gm unless $opt_P;
+print PM $pod unless $opt_P;
+
 close PM;
 
 
+if( ! $opt_X ){ # print XS, unless it is disabled
 warn "Writing $ext$modpname/$modfname.xs\n";
 
 print XS <<"END";
@@ -406,7 +543,7 @@ constant(name,arg)
 END
 
 close XS;
-
+} # if( ! $opt_X )
 
 warn "Writing $ext$modpname/Makefile.PL\n";
 open(PL, ">Makefile.PL") || die "Can't create $ext$modpname/Makefile.PL: $!\n";
@@ -418,14 +555,62 @@ use ExtUtils::MakeMaker;
 END
 print PL "WriteMakefile(\n";
 print PL "    'NAME'   => '$module',\n";
-print PL "    'VERSION'        => '0.1',\n";
-print PL "    'LIBS'   => ['$extralibs'],   # e.g., '-lm' \n";
-print PL "    'DEFINE' => '',     # e.g., '-DHAVE_SOMETHING' \n";
-print PL "    'INC'    => '',     # e.g., '-I/usr/include/other' \n";
+print PL "    'VERSION_FROM' => '$modfname.pm', # finds \$VERSION\n"; 
+if( ! $opt_X ){ # print C stuff, unless XS is disabled
+  print PL "    'LIBS' => ['$extralibs'],   # e.g., '-lm' \n";
+  print PL "    'DEFINE'       => '',     # e.g., '-DHAVE_SOMETHING' \n";
+  print PL "    'INC'  => '',     # e.g., '-I/usr/include/other' \n";
+}
 print PL ");\n";
-
-
-system '/bin/ls > MANIFEST' or system 'ls > MANIFEST';
+close(PL) || die "Can't close $ext$modpname/Makefile.PL: $!\n";
+
+warn "Writing $ext$modpname/test.pl\n";
+open(EX, ">test.pl") || die "Can't create $ext$modpname/test.pl: $!\n";
+print EX <<'_END_';
+# Before `make install' is performed this script should be runnable with
+# `make test'. After `make install' it should work as `perl test.pl'
+
+######################### We start with some black magic to print on failure.
+
+# Change 1..1 below to 1..last_test_to_print .
+# (It may become useful if the test is moved to ./t subdirectory.)
+
+BEGIN { $| = 1; print "1..1\n"; }
+END {print "not ok 1\n" unless $loaded;}
+_END_
+print EX <<_END_;
+use $module;
+_END_
+print EX <<'_END_';
+$loaded = 1;
+print "ok 1\n";
+
+######################### End of black magic.
+
+# Insert your test code below (better if it prints "ok 13"
+# (correspondingly "not ok 13") depending on the success of chunk 13
+# of the test code):
+
+_END_
+close(EX) || die "Can't close $ext$modpname/test.pl: $!\n";
+
+warn "Writing $ext$modpname/Changes\n";
+open(EX, ">Changes") || die "Can't create $ext$modpname/Changes: $!\n";
+print EX "Revision history for Perl extension $module.\n\n";
+print EX "$TEMPLATE_VERSION  ",scalar localtime,"\n";
+print EX "\t- original version; created by h2xs $H2XS_VERSION\n\n";
+close(EX) || die "Can't close $ext$modpname/Changes: $!\n";
+
+warn "Writing $ext$modpname/MANIFEST\n";
+open(MANI,'>MANIFEST') or die "Can't create MANIFEST: $!";
+@files = <*>;
+if (!@files) {
+  eval {opendir(D,'.');};
+  unless ($@) { @files = readdir(D); closedir(D); }
+}
+if (!@files) { @files = map {chomp && $_} `ls`; }
+print MANI join("\n",@files);
+close MANI;
 !NO!SUBS!
 
 close OUT or die "Can't close $file: $!";