4 use File::Basename qw(&basename &dirname);
6 # List explicitly here the variables you want Configure to
7 # generate. Metaconfig only looks for shell variables, so you
8 # have to mention them as if they were shell variables, not
9 # %Config entries. Thus you write
11 # to ensure Configure will look for $Config{startperl}.
13 # This forces PL files to create target in same directory as PL file.
14 # This is so that make depend always knows where to find PL derivatives.
16 ($file = basename($0)) =~ s/\.PL$//;
18 if ($Config{'osname'} eq 'VMS' or
19 $Config{'osname'} eq 'OS2'); # "case-forgiving"
21 open OUT,">$file" or die "Can't create $file: $!";
23 print "Extracting $file (with variable substitutions)\n";
25 # In this section, perl variables will be expanded during extraction.
26 # You can use $Config{...} to use Configure variables.
28 print OUT <<"!GROK!THIS!";
30 eval 'exec perl -S \$0 "\$@"'
34 # In the following, perl variables are not expanded during extraction.
36 print OUT <<'!NO!SUBS!';
40 h2xs - convert .h C header files to Perl extensions
44 B<h2xs> [B<-APcf>] [B<-v> version] [B<-n> module_name] [headerfile [extra_libraries]]
50 I<h2xs> builds a Perl extension from any C header file. The extension will
51 include functions which can be used to retrieve the value of any #define
52 statement which was in the C header.
54 The I<module_name> will be used for the name of the extension. If
55 module_name is not supplied then the name of the header file will be used,
56 with the first character capitalized.
58 If the extension might need extra libraries, they should be included
59 here. The extension Makefile.PL will take care of checking whether
60 the libraries actually exist and how they should be loaded.
61 The extra libraries should be specified in the form -lm -lposix, etc,
62 just as on the cc command line. By default, the Makefile.PL will
63 search through the library path determined by Configure. That path
64 can be augmented by including arguments of the form B<-L/another/library/path>
65 in the extra-libraries argument.
73 Omit all autoload facilities. This is the same as B<-c> but also removes the
74 S<C<require AutoLoader>> statement from the .pm file.
78 Omit the autogenerated stub POD section.
82 Omit C<constant()> from the .xs file and corresponding specialised
83 C<AUTOLOAD> from the .pm file.
87 Allows an extension to be created for a header even if that header is
88 not found in /usr/include.
92 Print the usage, help and version for this h2xs and exit.
94 =item B<-n> I<module_name>
96 Specifies a name to be used for the extension, e.g., S<-n RPC::DCE>
98 =item B<-v> I<version>
100 Specify a version number for this extension. This version number is added
101 to the templates. The default is 0.01.
108 # Default behavior, extension is Rusers
111 # Same, but extension is RUSERS
112 h2xs -n RUSERS rpcsvc/rusers
114 # Extension is rpcsvc::rusers. Still finds <rpcsvc/rusers.h>
117 # Extension is ONC::RPC. Still finds <rpcsvc/rusers.h>
118 h2xs -n ONC::RPC rpcsvc/rusers
120 # Without constant() or AUTOLOAD
121 h2xs -c rpcsvc/rusers
123 # Creates templates for an extension named RPC
126 # Extension is ONC::RPC.
129 # Makefile.PL will look for library -lrpc in
130 # additional directory /opt/net/lib
131 h2xs rpcsvc/rusers -L/opt/net/lib -lrpc
136 No environment variables are used.
140 Larry Wall and others
144 L<perl>, L<perlxstut>, L<ExtUtils::MakeMaker>, and L<AutoLoader>.
148 The usual warnings if it can't read or write the files involved.
152 my( $H2XS_VERSION ) = '$Revision: 1.14 $' =~ /\$Revision:\s+([^\s]+)/;
153 my $TEMPLATE_VERSION = '0.01';
159 die "h2xs [-APcfh] [-v version] [-n module_name] [headerfile [extra_libraries]]
160 version: $H2XS_VERSION
161 -f Force creation of the extension even if the C header does not exist.
162 -n Specify a name to use for the extension (recommended).
163 -c Omit the constant() function and specialised AUTOLOAD from the XS file.
164 -A Omit all autoloading facilities (implies -c).
165 -P Omit the stub POD section.
166 -v Specify a version number for this extension.
167 -h Display this help message
169 are any libraries that might be needed for loading the
170 extension, e.g. -lm would try to link in the math library.
175 getopts("APcfhv:n:") || usage;
180 $TEMPLATE_VERSION = $opt_v;
182 $opt_c = 1 if $opt_A;
185 $extralibs = "@ARGV";
187 usage "Must supply header file or module name\n"
188 unless ($path_h or $opt_n);
193 if( $path_h =~ s#::#/#g && $opt_n ){
194 warn "Nesting of headerfile ignored with -n\n";
196 $path_h .= ".h" unless $path_h =~ /\.h$/;
197 $path_h = "/usr/include/$path_h" unless $path_h =~ m#^[./]#;
198 die "Can't find $path_h\n" if ( ! $opt_f && ! -f $path_h );
200 # Scan the header file (we should deal with nested header files)
201 # Record the names of simple #define constants into const_names
202 # Function prototypes are not (currently) processed.
203 open(CH, "<$path_h") || die "Can't open $path_h: $!\n";
205 if (/^#[ \t]*define\s+(\w+)\b\s*[^("]/) {
207 next if /^_.*_h_*$/i; # special case, but for what?
212 @const_names = sort keys %const_names;
216 $module = $opt_n || do {
225 (chdir 'ext', $ext = 'ext/') if -d 'ext';
227 if( $module =~ /::/ ){
229 @modparts = split(/::/,$module);
230 $modfname = $modparts[-1];
231 $modpname = join('/',@modparts);
236 $modfname = $modpname = $module;
240 die "Won't overwrite existing $ext$modpname\n" if -e $modpname;
244 mkdir("$modpath$_", 0777);
248 mkdir($modpname, 0777);
249 chdir($modpname) || die "Can't chdir $ext$modpname: $!\n";
251 open(XS, ">$modfname.xs") || die "Can't create $ext$modpname/$modfname.xs: $!\n";
252 open(PM, ">$modfname.pm") || die "Can't create $ext$modpname/$modfname.pm: $!\n";
255 warn "Writing $ext$modpname/$modfname.pm\n";
270 if( $opt_c && ! $opt_A ){
271 # we won't have our own AUTOLOAD(), so we'll inherit it.
274 \@ISA = qw(Exporter AutoLoader DynaLoader);
278 # 1) we have our own AUTOLOAD(), so don't need to inherit it.
280 # 2) we don't want autoloading mentioned.
283 \@ISA = qw(Exporter DynaLoader);
288 # Items to export into callers namespace by default. Note: do not export
289 # names by default without a very good reason. Use EXPORT_OK instead.
290 # Do not simply export all your public functions/methods/constants.
294 \$VERSION = '$TEMPLATE_VERSION';
298 print PM <<"END" unless $opt_c;
300 # This AUTOLOAD is used to 'autoload' constants from the constant()
301 # XS function. If a constant is not found then control is passed
302 # to the AUTOLOAD in AutoLoader.
305 (\$constname = \$AUTOLOAD) =~ s/.*:://;
306 \$val = constant(\$constname, \@_ ? \$_[0] : 0);
308 if (\$! =~ /Invalid/) {
309 \$AutoLoader::AUTOLOAD = \$AUTOLOAD;
310 goto &AutoLoader::AUTOLOAD;
313 (\$pack,\$file,\$line) = caller;
314 die "Your vendor has not defined $module macro \$constname, used at \$file line \$line.\n";
317 eval "sub \$AUTOLOAD { \$val }";
324 bootstrap $module \$VERSION;
326 # Preloaded methods go here.
328 # Autoload methods go after __END__, and are processed by the autosplit program.
334 $author = "A. U. Thor";
335 $email = 'a.u.thor@a.galaxy.far.far.away';
337 $pod = <<"END" unless $opt_P;
338 ## Below is the stub of documentation for your module. You better edit it!
342 #$module - Perl extension for blah blah blah
351 #Stub documentation for $module was created by h2xs. It looks like the
352 #author of the extension was negligent enough to leave the stub
368 $pod =~ s/^\#//gm unless $opt_P;
369 print PM $pod unless $opt_P;
374 warn "Writing $ext$modpname/$modfname.xs\n";
390 $h =~ s#^/usr/include/##;
403 croak("$module::%s not implemented on this architecture", s);
416 my(@AZ, @az, @under);
418 foreach(@const_names){
419 @AZ = 'A' .. 'Z' if !@AZ && /^[A-Z]/;
420 @az = 'a' .. 'z' if !@az && /^[a-z]/;
421 @under = '_' if !@under && /^_/;
424 foreach $letter (@AZ, @az, @under) {
426 last if $letter eq 'a' && !@const_names;
428 print XS " case '$letter':\n";
430 while (substr($const_names[0],0,1) eq $letter) {
431 $name = shift(@const_names);
433 if (strEQ(name, "$name"))
458 # Now switch from C to XS by issuing the first MODULE declaration:
461 MODULE = $module PACKAGE = $module
465 # If a constant() function was written then output a corresponding
467 print XS <<"END" unless $opt_c;
479 warn "Writing $ext$modpname/Makefile.PL\n";
480 open(PL, ">Makefile.PL") || die "Can't create $ext$modpname/Makefile.PL: $!\n";
483 use ExtUtils::MakeMaker;
484 # See lib/ExtUtils/MakeMaker.pm for details of how to influence
485 # the contents of the Makefile that is written.
487 print PL "WriteMakefile(\n";
488 print PL " 'NAME' => '$module',\n";
489 print PL " 'VERSION_FROM' => '$modfname.pm', # finds \$VERSION\n";
490 print PL " 'LIBS' => ['$extralibs'], # e.g., '-lm' \n";
491 print PL " 'DEFINE' => '', # e.g., '-DHAVE_SOMETHING' \n";
492 print PL " 'INC' => '', # e.g., '-I/usr/include/other' \n";
494 close(PL) || die "Can't close $ext$modpname/Makefile.PL: $!\n";
496 warn "Writing $ext$modpname/test.pl\n";
497 open(EX, ">test.pl") || die "Can't create $ext$modpname/test.pl: $!\n";
499 # Before `make install' is performed this script should be runnable with
500 # `make test'. After `make install' it should work as `perl test.pl'
502 ######################### We start with some black magic to print on failure.
504 # Change 1..1 below to 1..last_test_to_print .
505 # (It may become useful if the test is moved to ./t subdirectory.)
507 BEGIN {print "1..1\n";}
508 END {print "not ok 1\n" unless $loaded;}
517 ######################### End of black magic.
519 # Insert your test code below (better if it prints "ok 13"
520 # (correspondingly "not ok 13") depending on the success of chunk 13
524 close(EX) || die "Can't close $ext$modpname/test.pl: $!\n";
526 warn "Writing $ext$modpname/Changes\n";
527 open(EX, ">Changes") || die "Can't create $ext$modpname/Changes: $!\n";
528 print EX "Revision history for Perl extension $module.\n\n";
529 print EX "$TEMPLATE_VERSION ",scalar localtime,"\n";
530 print EX "\t- original version; created by h2xs $H2XS_VERSION\n\n";
531 close(EX) || die "Can't close $ext$modpname/Changes: $!\n";
533 warn "Writing $ext$modpname/MANIFEST\n";
534 system '/bin/ls > MANIFEST' or system 'ls > MANIFEST';
537 close OUT or die "Can't close $file: $!";
538 chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
539 exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';