home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / perl / Perl / h2xs.SH < prev    next >
Text File  |  1995-12-03  |  9KB  |  415 lines

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