home *** CD-ROM | disk | FTP | other *** search
/ Chip: Windows 2000 Professional Resource Kit / W2KPRK.iso / apps / perl / ActivePerl.exe / data.z / h2xs.bat < prev    next >
Encoding:
DOS Batch File  |  1999-10-16  |  22.6 KB  |  873 lines

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