home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Editores / Perl5 / perl / bin / h2xs.bat < prev    next >
Encoding:
DOS Batch File  |  1997-08-10  |  22.0 KB  |  865 lines

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