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 ($^O eq 'VMS' or $^O eq 'os2'); # "case-forgiving"
20 open OUT,">$file" or die "Can't create $file: $!";
22 print "Extracting $file (with variable substitutions)\n";
24 # In this section, perl variables will be expanded during extraction.
25 # You can use $Config{...} to use Configure variables.
27 print OUT <<"!GROK!THIS!";
29 eval 'exec perl -S \$0 "\$@"'
33 # In the following, perl variables are not expanded during extraction.
35 print OUT <<'!NO!SUBS!';
39 h2xs - convert .h C header files to Perl extensions
43 B<h2xs> [B<-AOPXcf>] [B<-v> version] [B<-n> module_name] [B<-p> prefix] [B<-s> sub] [headerfile [extra_libraries]]
49 I<h2xs> builds a Perl extension from any C header file. The extension will
50 include functions which can be used to retrieve the value of any #define
51 statement which was in the C header.
53 The I<module_name> will be used for the name of the extension. If
54 module_name is not supplied then the name of the header file will be used,
55 with the first character capitalized.
57 If the extension might need extra libraries, they should be included
58 here. The extension Makefile.PL will take care of checking whether
59 the libraries actually exist and how they should be loaded.
60 The extra libraries should be specified in the form -lm -lposix, etc,
61 just as on the cc command line. By default, the Makefile.PL will
62 search through the library path determined by Configure. That path
63 can be augmented by including arguments of the form B<-L/another/library/path>
64 in the extra-libraries argument.
72 Omit all autoload facilities. This is the same as B<-c> but also removes the
73 S<C<require AutoLoader>> statement from the .pm file.
77 Allows a pre-existing extension directory to be overwritten.
81 Omit the autogenerated stub POD section.
85 Omit C<constant()> from the .xs file and corresponding specialised
86 C<AUTOLOAD> from the .pm file.
90 Allows an extension to be created for a header even if that header is
91 not found in /usr/include.
95 Print the usage, help and version for this h2xs and exit.
97 =item B<-n> I<module_name>
99 Specifies a name to be used for the extension, e.g., S<-n RPC::DCE>
101 =item B<-p> I<prefix>
103 Specify a prefix which should be removed from the Perl function names, e.g., S<-p sec_rgy_>
104 This sets up the XS B<PREFIX> keyword and removes the prefix from functions that are
105 autoloaded via the C<constant()> mechansim.
107 =item B<-s> I<sub1,sub2>
109 Create a perl subroutine for the specified macros rather than autoload with the constant() subroutine.
110 These macros are assumed to have a return type of B<char *>, e.g., S<-s sec_rgy_wildcard_name,sec_rgy_wildcard_sid>.
112 =item B<-v> I<version>
114 Specify a version number for this extension. This version number is added
115 to the templates. The default is 0.01.
119 Omit the XS portion. Used to generate templates for a module which is not
124 Automatically generate XSUBs basing on function declarations in the
125 header file. The package C<C::Scan> should be installed. If this
126 option is specified, the name of the header file may look like
127 C<NAME1,NAME2>. In this case NAME1 is used instead of the specified string,
128 but XSUBS are emited only for the declarations included from file NAME2.
132 Additional flags to specify to C preprocessor when scanning header for
133 function declarations. Should not be used without B<-x>.
140 # Default behavior, extension is Rusers
143 # Same, but extension is RUSERS
144 h2xs -n RUSERS rpcsvc/rusers
146 # Extension is rpcsvc::rusers. Still finds <rpcsvc/rusers.h>
149 # Extension is ONC::RPC. Still finds <rpcsvc/rusers.h>
150 h2xs -n ONC::RPC rpcsvc/rusers
152 # Without constant() or AUTOLOAD
153 h2xs -c rpcsvc/rusers
155 # Creates templates for an extension named RPC
158 # Extension is ONC::RPC.
161 # Makefile.PL will look for library -lrpc in
162 # additional directory /opt/net/lib
163 h2xs rpcsvc/rusers -L/opt/net/lib -lrpc
165 # Extension is DCE::rgynbase
166 # prefix "sec_rgy_" is dropped from perl function names
167 h2xs -n DCE::rgynbase -p sec_rgy_ dce/rgynbase
169 # Extension is DCE::rgynbase
170 # prefix "sec_rgy_" is dropped from perl function names
171 # subroutines are created for sec_rgy_wildcard_name and sec_rgy_wildcard_sid
172 h2xs -n DCE::rgynbase -p sec_rgy_ \
173 -s sec_rgy_wildcard_name,sec_rgy_wildcard_sid dce/rgynbase
175 # Make XS with defines in perl.h, and function declarations
176 # visible from perl.h. Name of the extension is perl1.
177 # When scanning perl.h, define -DEXT=extern -DdEXT= -DINIT(x)=
178 # Extra backslashes below because the string is passed to shell.
179 h2xs -xn perl1 -F "-DEXT=extern -DdEXT= -DINIT\(x\)=" \
180 ../perl5_003_01/perl.h
182 # Same with function declaration in proto.h as visible from perl.h.
183 perl H:\get\perl\perl5_003_01.try\utils\h2xs -xn perl1 \
184 ../perl5_003_01/perl.h,proto.h
188 No environment variables are used.
192 Larry Wall and others
196 L<perl>, L<perlxstut>, L<ExtUtils::MakeMaker>, and L<AutoLoader>.
200 The usual warnings if it cannot read or write the files involved.
204 my( $H2XS_VERSION ) = ' $Revision: 1.16 $ ' =~ /\$Revision:\s+([^\s]+)/;
205 my $TEMPLATE_VERSION = '0.01';
211 die "h2xs [-AOPXcfh] [-v version] [-n module_name] [-p prefix] [-s subs] [headerfile [extra_libraries]]
212 version: $H2XS_VERSION
213 -f Force creation of the extension even if the C header does not exist.
214 -n Specify a name to use for the extension (recommended).
215 -c Omit the constant() function and specialised AUTOLOAD from the XS file.
216 -p Specify a prefix which should be removed from the Perl function names.
217 -s Create subroutines for specified macros.
218 -A Omit all autoloading facilities (implies -c).
219 -O Allow overwriting of a pre-existing extension directory.
220 -P Omit the stub POD section.
221 -X Omit the XS portion.
222 -v Specify a version number for this extension.
223 -x Autogenerate XSUBs using C::Scan.
224 -F Additional flags for C preprocessor (used with -x).
225 -h Display this help message
227 are any libraries that might be needed for loading the
228 extension, e.g. -lm would try to link in the math library.
233 getopts("AOPXcfhxv:n:p:s:F:") || usage;
238 $TEMPLATE_VERSION = $opt_v;
240 $opt_c = 1 if $opt_A;
241 %const_xsub = map { $_,1 } split(/,+/, $opt_s) if $opt_s;
244 $extralibs = "@ARGV";
246 usage "Must supply header file or module name\n"
247 unless ($path_h or $opt_n);
252 if( $path_h =~ s#::#/#g && $opt_n ){
253 warn "Nesting of headerfile ignored with -n\n";
255 $path_h .= ".h" unless $path_h =~ /\.h$/;
257 $path_h =~ s/,.*$// if $opt_x;
258 if ($^O eq 'VMS') { # Consider overrides of default location
259 if ($path_h !~ m![:>\[]!) {
260 my($hadsys) = ($path_h =~ s!^sys/!!i);
261 if ($ENV{'DECC$System_Include'}) { $path_h = "DECC\$System_Include:$path_h"; }
262 elsif ($ENV{'DECC$Library_Include'}) { $path_h = "DECC\$Library_Include:$path_h"; }
263 elsif ($ENV{'GNU_CC_Include'}) { $path_h = 'GNU_CC_Include:' .
264 ($hadsys ? '[vms]' : '[000000]') . $path_h; }
265 elsif ($ENV{'VAXC$Include'}) { $path_h = "VAXC\$_Include:$path_h"; }
266 else { $path_h = "Sys\$Library:$path_h"; }
269 elsif ($^O eq 'os2') {
270 $path_h = "/usr/include/$path_h" unless $path_h =~ m#^([a-z]:)?[./]#i;
272 else { $path_h = "/usr/include/$path_h" unless $path_h =~ m#^[./]#; }
273 die "Can't find $path_h\n" if ( ! $opt_f && ! -f $path_h );
275 # Scan the header file (we should deal with nested header files)
276 # Record the names of simple #define constants into const_names
277 # Function prototypes are not (currently) processed.
278 open(CH, "<$path_h") || die "Can't open $path_h: $!\n";
280 if (/^#[ \t]*define\s+([\$\w]+)\b\s*[^("]/) {
281 print "Matched $_ ($1)\n";
283 next if /^_.*_h_*$/i; # special case, but for what?
284 if (defined $opt_p) {
285 if (!/^$opt_p(\d)/) {
286 ++$prefix{$_} if s/^$opt_p//;
289 warn "can't remove $opt_p prefix from '$_'!\n";
296 @const_names = sort keys %const_names;
300 $module = $opt_n || do {
309 (chdir 'ext', $ext = 'ext/') if -d 'ext';
311 if( $module =~ /::/ ){
313 @modparts = split(/::/,$module);
314 $modfname = $modparts[-1];
315 $modpname = join('/',@modparts);
320 $modfname = $modpname = $module;
325 warn "Overwriting existing $ext$modpname!!!\n" if -e $modpname;
327 die "Won't overwrite existing $ext$modpname\n" if -e $modpname;
332 mkdir("$modpath$_", 0777);
336 mkdir($modpname, 0777);
337 chdir($modpname) || die "Can't chdir $ext$modpname: $!\n";
339 if( ! $opt_X ){ # use XS, unless it was disabled
340 open(XS, ">$modfname.xs") || die "Can't create $ext$modpname/$modfname.xs: $!\n";
342 open(PM, ">$modfname.pm") || die "Can't create $ext$modpname/$modfname.pm: $!\n";
345 warn "Writing $ext$modpname/$modfname.pm\n";
353 if( $opt_X || $opt_c || $opt_A ){
354 # we won't have our own AUTOLOAD(), so won't have $AUTOLOAD
356 use vars qw($VERSION @ISA @EXPORT);
360 # we'll have an AUTOLOAD(), and it will have $AUTOLOAD and
364 use vars qw($VERSION @ISA @EXPORT $AUTOLOAD);
373 print PM <<"END" if ! $opt_X; # use DynaLoader, unless XS was disabled
377 # require autoloader if XS is disabled.
378 # if XS is enabled, require autoloader unless autoloading is disabled.
379 if( $opt_X || (! $opt_A) ){
385 if( $opt_X || ($opt_c && ! $opt_A) ){
386 # we won't have our own AUTOLOAD(), so we'll inherit it.
387 if( ! $opt_X ) { # use DynaLoader, unless XS was disabled
390 \@ISA = qw(Exporter AutoLoader DynaLoader);
396 \@ISA = qw(Exporter AutoLoader);
401 # 1) we have our own AUTOLOAD(), so don't need to inherit it.
403 # 2) we don't want autoloading mentioned.
404 if( ! $opt_X ){ # use DynaLoader, unless XS was disabled
407 \@ISA = qw(Exporter DynaLoader);
413 \@ISA = qw(Exporter);
419 # Items to export into callers namespace by default. Note: do not export
420 # names by default without a very good reason. Use EXPORT_OK instead.
421 # Do not simply export all your public functions/methods/constants.
425 \$VERSION = '$TEMPLATE_VERSION';
429 print PM <<"END" unless $opt_c or $opt_X;
431 # This AUTOLOAD is used to 'autoload' constants from the constant()
432 # XS function. If a constant is not found then control is passed
433 # to the AUTOLOAD in AutoLoader.
436 (\$constname = \$AUTOLOAD) =~ s/.*:://;
437 my \$val = constant(\$constname, \@_ ? \$_[0] : 0);
439 if (\$! =~ /Invalid/) {
440 \$AutoLoader::AUTOLOAD = \$AUTOLOAD;
441 goto &AutoLoader::AUTOLOAD;
444 croak "Your vendor has not defined $module macro \$constname";
447 eval "sub \$AUTOLOAD { \$val }";
453 if( ! $opt_X ){ # print bootstrap, unless XS is disabled
455 bootstrap $module \$VERSION;
459 if( $opt_P ){ # if POD is disabled
468 # Preloaded methods go here.
470 # Autoload methods go after $after, and are processed by the autosplit program.
476 $author = "A. U. Thor";
477 $email = 'a.u.thor@a.galaxy.far.far.away';
479 $pod = <<"END" unless $opt_P;
480 ## Below is the stub of documentation for your module. You better edit it!
484 #$module - Perl extension for blah blah blah
493 #Stub documentation for $module was created by h2xs. It looks like the
494 #author of the extension was negligent enough to leave the stub
510 $pod =~ s/^\#//gm unless $opt_P;
511 print PM $pod unless $opt_P;
516 if( ! $opt_X ){ # print XS, unless it is disabled
517 warn "Writing $ext$modpname/$modfname.xs\n";
533 $h =~ s#^/usr/include/##;
534 if ($^O eq 'VMS') { $h =~ s#.*vms\]#sys/# or $h =~ s#.*[:>\]]##; }
547 croak("$module::%s not implemented on this architecture", s);
560 my(@AZ, @az, @under);
562 foreach(@const_names){
563 @AZ = 'A' .. 'Z' if !@AZ && /^[A-Z]/;
564 @az = 'a' .. 'z' if !@az && /^[a-z]/;
565 @under = '_' if !@under && /^_/;
568 foreach $letter (@AZ, @az, @under) {
570 last if $letter eq 'a' && !@const_names;
572 print XS " case '$letter':\n";
574 while (substr($const_names[0],0,1) eq $letter) {
575 $name = shift(@const_names);
576 $macro = $prefix{$name} ? "$opt_p$name" : $name;
577 next if $const_xsub{$macro};
579 if (strEQ(name, "$name"))
604 $prefix = "PREFIX = $opt_p" if defined $opt_p;
605 # Now switch from C to XS by issuing the first MODULE declaration:
608 MODULE = $module PACKAGE = $module $prefix
612 foreach (sort keys %const_xsub) {
621 croak("Your vendor has not defined the $module macro $_");
630 # If a constant() function was written then output a corresponding
632 print XS <<"END" unless $opt_c;
644 my ($type, $name, $args) = @$decl;
645 my @argnames = map {$_->[1]} @$args;
646 my @argtypes = map { normalize_type( $_->[0] ) } @$args;
647 my $numargs = @$args;
648 if ($numargs and $argtypes[-1] eq '...') {
650 $argnames[-1] = '...';
653 $type = normalize_type($type);
661 for $arg (0 .. $numargs - 1) {
663 $argtypes[$arg] $argnames[$arg]
668 my $ignore_mods = '(?:\b(?:__const__|static|inline|__inline__)\b\s*)*';
672 $type =~ s/$ignore_mods//go;
676 $type =~ s/\b\*/ */g;
677 $type =~ s/\*\b/* /g;
678 $type =~ s/\*\s+(?=\*)/*/g;
683 require C::Scan; # Run-time directive
684 require Config; # Run-time directive
687 my $filename = $path_h;
688 my $addflags = $opt_F || '';
689 if ($fullpath =~ /,/) {
693 $c = new C::Scan 'filename' => $filename, 'filename_filter' => $filter,
694 'add_cppflags' => $addflags;
695 $c->set('includeDirs' => [$Config::Config{shrpdir}]);
697 my $fdec = $c->get('parsed_fdecls');
699 for $decl (@$fdec) { print_decl(\*XS, $decl) }
705 warn "Writing $ext$modpname/Makefile.PL\n";
706 open(PL, ">Makefile.PL") || die "Can't create $ext$modpname/Makefile.PL: $!\n";
709 use ExtUtils::MakeMaker;
710 # See lib/ExtUtils/MakeMaker.pm for details of how to influence
711 # the contents of the Makefile that is written.
713 print PL "WriteMakefile(\n";
714 print PL " 'NAME' => '$module',\n";
715 print PL " 'VERSION_FROM' => '$modfname.pm', # finds \$VERSION\n";
716 if( ! $opt_X ){ # print C stuff, unless XS is disabled
717 print PL " 'LIBS' => ['$extralibs'], # e.g., '-lm' \n";
718 print PL " 'DEFINE' => '', # e.g., '-DHAVE_SOMETHING' \n";
719 print PL " 'INC' => '', # e.g., '-I/usr/include/other' \n";
722 close(PL) || die "Can't close $ext$modpname/Makefile.PL: $!\n";
724 warn "Writing $ext$modpname/test.pl\n";
725 open(EX, ">test.pl") || die "Can't create $ext$modpname/test.pl: $!\n";
727 # Before `make install' is performed this script should be runnable with
728 # `make test'. After `make install' it should work as `perl test.pl'
730 ######################### We start with some black magic to print on failure.
732 # Change 1..1 below to 1..last_test_to_print .
733 # (It may become useful if the test is moved to ./t subdirectory.)
735 BEGIN { $| = 1; print "1..1\n"; }
736 END {print "not ok 1\n" unless $loaded;}
745 ######################### End of black magic.
747 # Insert your test code below (better if it prints "ok 13"
748 # (correspondingly "not ok 13") depending on the success of chunk 13
752 close(EX) || die "Can't close $ext$modpname/test.pl: $!\n";
754 warn "Writing $ext$modpname/Changes\n";
755 open(EX, ">Changes") || die "Can't create $ext$modpname/Changes: $!\n";
756 print EX "Revision history for Perl extension $module.\n\n";
757 print EX "$TEMPLATE_VERSION ",scalar localtime,"\n";
758 print EX "\t- original version; created by h2xs $H2XS_VERSION\n\n";
759 close(EX) || die "Can't close $ext$modpname/Changes: $!\n";
761 warn "Writing $ext$modpname/MANIFEST\n";
762 open(MANI,'>MANIFEST') or die "Can't create MANIFEST: $!";
765 eval {opendir(D,'.');};
766 unless ($@) { @files = readdir(D); closedir(D); }
768 if (!@files) { @files = map {chomp && $_} `ls`; }
769 print MANI join("\n",@files);
773 close OUT or die "Can't close $file: $!";
774 chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
775 exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';