home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl501m.zip / OS2 / h2xs.cmd < prev    next >
OS/2 REXX Batch file  |  1995-05-06  |  9KB  |  389 lines

  1. extproc perl5 -Sx 
  2. #!/usr/bin/perl
  3.  
  4. =head1 NAME
  5.  
  6. h2xs - convert .h C header files to Perl extensions
  7.  
  8. =head1 SYNOPSIS
  9.  
  10. B<h2xs> [B<-Acfh>] [B<-n> module_name] [headerfile [extra_libraries]]
  11.  
  12. =head1 DESCRIPTION
  13.  
  14. I<h2xs> builds a Perl extension from any C header file.  The extension will
  15. include functions which can be used to retrieve the value of any #define
  16. statement which was in the C header.
  17.  
  18. The I<module_name> will be used for the name of the extension.  If
  19. module_name is not supplied then the name of the header file will be used,
  20. with the first character capitalized.
  21.  
  22. If the extension might need extra libraries, they should be included
  23. here.  The extension Makefile.PL will take care of checking whether
  24. the libraries actually exist and how they should be loaded.
  25. The extra libraries should be specified in the form -lm -lposix, etc,
  26. just as on the cc command line.  By default, the Makefile.PL will
  27. search through the library path determined by Configure.  That path
  28. can be augmented by including arguments of the form B<-L/another/library/path>
  29. in the extra-libraries argument.
  30.  
  31. =head1 OPTIONS
  32.  
  33. =over 5
  34.  
  35. =item B<-n> I<module_name>
  36.  
  37. Specifies a name to be used for the extension, e.g., S<-n RPC::DCE>
  38.  
  39. =item B<-f>
  40.  
  41. Allows an extension to be created for a header even if that header is
  42. not found in /usr/include.
  43.  
  44. =item B<-c>
  45.  
  46. Omit C<constant()> from the .xs file and corresponding specialised
  47. C<AUTOLOAD> from the .pm file.
  48.  
  49. =item B<-A>
  50.  
  51. Omit all autoload facilities.  This is the same as B<-c> but also removes the
  52. S<C<require AutoLoader>> statement from the .pm file.
  53.  
  54. =back
  55.  
  56. =head1 EXAMPLES
  57.  
  58.  
  59.     # Default behavior, extension is Rusers
  60.     h2xs rpcsvc/rusers
  61.  
  62.     # Same, but extension is RUSERS
  63.     h2xs -n RUSERS rpcsvc/rusers
  64.  
  65.     # Extension is rpcsvc::rusers. Still finds <rpcsvc/rusers.h>
  66.     h2xs rpcsvc::rusers
  67.  
  68.     # Extension is ONC::RPC.  Still finds <rpcsvc/rusers.h>
  69.     h2xs -n ONC::RPC rpcsvc/rusers
  70.  
  71.     # Without constant() or AUTOLOAD
  72.     h2xs -c rpcsvc/rusers
  73.  
  74.     # Creates templates for an extension named RPC
  75.     h2xs -cfn RPC
  76.  
  77.     # Extension is ONC::RPC.
  78.     h2xs -cfn ONC::RPC
  79.  
  80.     # Makefile.PL will look for library -lrpc in 
  81.     # additional directory /opt/net/lib
  82.     h2xs rpcsvc/rusers -L/opt/net/lib -lrpc
  83.  
  84.  
  85. =head1 ENVIRONMENT
  86.  
  87. No environment variables are used.
  88.  
  89. =head1 AUTHOR
  90.  
  91. Larry Wall and others
  92.  
  93. =head1 SEE ALSO
  94.  
  95. L<perl>, L<ExtUtils::MakeMaker>, L<AutoLoader>
  96.  
  97. =head1 DIAGNOSTICS
  98.  
  99. The usual warnings if it can't read or write the files involved.
  100.  
  101. =cut
  102.  
  103.  
  104. use Getopt::Std;
  105.  
  106. sub usage{
  107.     warn "@_\n" if @_;
  108.     die 'h2xs [-Acfh] [-n module_name] [headerfile [extra_libraries]]
  109.     -f   Force creation of the extension even if the C header does not exist.
  110.     -n   Specify a name to use for the extension (recommended).
  111.     -c   Omit the constant() function and specialised AUTOLOAD from the XS file.
  112.     -A   Omit all autoloading facilities (implies -c).
  113.     -h   Display this help message
  114. extra_libraries
  115.          are any libraries that might be needed for loading the
  116.          extension, e.g. -lm would try to link in the math library.
  117. ';
  118. }
  119.  
  120.  
  121. getopts("Acfhn:") || usage;
  122.  
  123. usage if $opt_h;
  124. $opt_c = 1 if $opt_A;
  125.  
  126. $path_h    = shift;
  127. $extralibs = "@ARGV";
  128.  
  129. usage "Must supply header file or module name\n"
  130.     unless ($path_h or $opt_n);
  131.  
  132.  
  133. if( $path_h ){
  134.     $name = $path_h;
  135.     if( $path_h =~ s#::#/#g && $opt_n ){
  136.     warn "Nesting of headerfile ignored with -n\n";
  137.     }
  138.     $path_h .= ".h" unless $path_h =~ /\.h$/;
  139.     $path_h = "/usr/include/$path_h" unless $path_h =~ m#^[./]#;
  140.     die "Can't find $path_h\n" if ( ! $opt_f && ! -f $path_h );
  141.  
  142.     # Scan the header file (we should deal with nested header files)
  143.     # Record the names of simple #define constants into const_names
  144.     # Function prototypes are not (currently) processed.
  145.     open(CH, "<$path_h") || die "Can't open $path_h: $!\n";
  146.     while (<CH>) {
  147.     if (/^#[ \t]*define\s+(\w+)\b\s*[^("]/) {
  148.         $_ = $1;
  149.         next if /^_.*_h_*$/i; # special case, but for what?
  150.         $const_names{$_}++;
  151.     }
  152.     }
  153.     close(CH);
  154.     @const_names = sort keys %const_names;
  155. }
  156.  
  157.  
  158. $module = $opt_n || do {
  159.     $name =~ s/\.h$//;
  160.     if( $name !~ /::/ ){
  161.         $name =~ s#^.*/##;
  162.         $name = "\u$name";
  163.     }
  164.     $name;
  165. };
  166.  
  167. chdir 'ext' if -d 'ext';
  168.  
  169. if( $module =~ /::/ ){
  170.     $nested = 1;
  171.     @modparts = split(/::/,$module);
  172.     $modfname = $modparts[-1];
  173.     $modpname = join('/',@modparts);
  174. }
  175. else {
  176.     $nested = 0;
  177.     @modparts = ();
  178.     $modfname = $modpname = $module;
  179. }
  180.  
  181.  
  182. die "Won't overwrite existing ext/$modpname\n" if -e $modpname;
  183. # quick hack, should really loop over @modparts
  184. mkdir($modparts[0], 0777) if $nested;
  185. mkdir($modpname, 0777);
  186. chdir($modpname) || die "Can't chdir ext/$modpname: $!\n";
  187.  
  188. open(XS, ">$modfname.xs") || die "Can't create ext/$modpname/$modfname.xs: $!\n";
  189. open(PM, ">$modfname.pm") || die "Can't create ext/$modpname/$modfname.pm: $!\n";
  190.  
  191. $" = "\n\t";
  192. warn "Writing ext/$modpname/$modfname.pm\n";
  193.  
  194. print PM <<"END";
  195. package $module;
  196.  
  197. require Exporter;
  198. require DynaLoader;
  199. END
  200.  
  201. if( ! $opt_A ){
  202.     print PM <<"END";
  203. require AutoLoader;
  204. END
  205. }
  206.  
  207. if( $opt_c && ! $opt_A ){
  208.     # we won't have our own AUTOLOAD(), so we'll inherit it.
  209.     print PM <<"END";
  210.  
  211. \@ISA = qw(Exporter AutoLoader DynaLoader);
  212. END
  213. }
  214. else{
  215.     # 1) we have our own AUTOLOAD(), so don't need to inherit it.
  216.     # or
  217.     # 2) we don't want autoloading mentioned.
  218.     print PM <<"END";
  219.  
  220. \@ISA = qw(Exporter DynaLoader);
  221. END
  222. }
  223.  
  224. print PM<<"END";
  225. # Items to export into callers namespace by default. Note: do not export
  226. # names by default without a very good reason. Use EXPORT_OK instead.
  227. # Do not simply export all your public functions/methods/constants.
  228. \@EXPORT = qw(
  229.     @const_names
  230. );
  231. END
  232.  
  233. print PM <<"END" unless $opt_c;
  234. sub AUTOLOAD {
  235.     # This AUTOLOAD is used to 'autoload' constants from the constant()
  236.     # XS function.  If a constant is not found then control is passed
  237.     # to the AUTOLOAD in AutoLoader.
  238.  
  239.     local(\$constname);
  240.     (\$constname = \$AUTOLOAD) =~ s/.*:://;
  241.     \$val = constant(\$constname, \@_ ? \$_[0] : 0);
  242.     if (\$! != 0) {
  243.     if (\$! =~ /Invalid/) {
  244.         \$AutoLoader::AUTOLOAD = \$AUTOLOAD;
  245.         goto &AutoLoader::AUTOLOAD;
  246.     }
  247.     else {
  248.         (\$pack,\$file,\$line) = caller;
  249.         die "Your vendor has not defined $module macro \$constname, used at \$file line \$line.\n";
  250.     }
  251.     }
  252.     eval "sub \$AUTOLOAD { \$val }";
  253.     goto &\$AUTOLOAD;
  254. }
  255.  
  256. END
  257.  
  258. print PM <<"END";
  259. bootstrap $module;
  260.  
  261. # Preloaded methods go here.
  262.  
  263. # Autoload methods go after __END__, and are processed by the autosplit program.
  264.  
  265. 1;
  266. __END__
  267. END
  268.  
  269. close PM;
  270.  
  271.  
  272. warn "Writing ext/$modpname/$modfname.xs\n";
  273.  
  274. print XS <<"END";
  275. #include "EXTERN.h"
  276. #include "perl.h"
  277. #include "XSUB.h"
  278.  
  279. END
  280. if( $path_h ){
  281.     my($h) = $path_h;
  282.     $h =~ s#^/usr/include/##;
  283. print XS <<"END";
  284. #include <$h>
  285.  
  286. END
  287. }
  288.  
  289. if( ! $opt_c ){
  290. print XS <<"END";
  291. static int
  292. not_here(s)
  293. char *s;
  294. {
  295.     croak("$module::%s not implemented on this architecture", s);
  296.     return -1;
  297. }
  298.  
  299. static double
  300. constant(name, arg)
  301. char *name;
  302. int arg;
  303. {
  304.     errno = 0;
  305.     switch (*name) {
  306. END
  307.  
  308. my(@AZ, @az, @under);
  309.  
  310. foreach(@const_names){
  311.     @AZ = 'A' .. 'Z' if !@AZ && /^[A-Z]/;
  312.     @az = 'a' .. 'z' if !@az && /^[a-z]/;
  313.     @under = '_'  if !@under && /^_/;
  314. }
  315.  
  316. foreach $letter (@AZ, @az, @under) {
  317.  
  318.     last if $letter eq 'a' && !@const_names;
  319.  
  320.     print XS "    case '$letter':\n";
  321.     my($name);
  322.     while (substr($const_names[0],0,1) eq $letter) {
  323.     $name = shift(@const_names);
  324.     print XS <<"END";
  325.     if (strEQ(name, "$name"))
  326. #ifdef $name
  327.         return $name;
  328. #else
  329.         goto not_there;
  330. #endif
  331. END
  332.     }
  333.     print XS <<"END";
  334.     break;
  335. END
  336. }
  337. print XS <<"END";
  338.     }
  339.     errno = EINVAL;
  340.     return 0;
  341.  
  342. not_there:
  343.     errno = ENOENT;
  344.     return 0;
  345. }
  346.  
  347. END
  348. }
  349.  
  350. # Now switch from C to XS by issuing the first MODULE declaration:
  351. print XS <<"END";
  352.  
  353. MODULE = $module        PACKAGE = $module
  354.  
  355. END
  356.  
  357. # If a constant() function was written then output a corresponding
  358. # XS declaration:
  359. print XS <<"END" unless $opt_c;
  360.  
  361. double
  362. constant(name,arg)
  363.     char *        name
  364.     int        arg
  365.  
  366. END
  367.  
  368. close XS;
  369.  
  370.  
  371. warn "Writing ext/$modpname/Makefile.PL\n";
  372. open(PL, ">Makefile.PL") || die "Can't create ext/$modpname/Makefile.PL: $!\n";
  373.  
  374. print PL <<'END';
  375. use ExtUtils::MakeMaker;
  376. # See lib/ExtUtils/MakeMaker.pm for details of how to influence
  377. # the contents of the Makefile that is written.
  378. END
  379. print PL "WriteMakefile(\n";
  380. print PL "    'NAME'    => '$module',\n";
  381. print PL "    'VERSION'    => '0.1',\n";
  382. print PL "    'LIBS'    => ['$extralibs'],   # e.g., '-lm' \n";
  383. print PL "    'DEFINE'    => '',     # e.g., '-DHAVE_SOMETHING' \n";
  384. print PL "    'INC'    => '',     # e.g., '-I/usr/include/other' \n";
  385. print PL ");\n";
  386.  
  387.  
  388. system 'ls -1 > MANIFEST';
  389.