home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2 / Openstep-4.2-Intel-User.iso / usr / bin / h2xs < prev    next >
Text File  |  1997-03-29  |  9KB  |  388 lines

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