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!';
38 h2xs - convert .h C header files to Perl extensions
42 B<h2xs> [B<-AOPXcf>] [B<-v> version] [B<-n> module_name] [B<-p> prefix] [B<-s> sub] [headerfile [extra_libraries]]
48 I<h2xs> builds a Perl extension from any C header file. The extension will
49 include functions which can be used to retrieve the value of any #define
50 statement which was in the C header.
52 The I<module_name> will be used for the name of the extension. If
53 module_name is not supplied then the name of the header file will be used,
54 with the first character capitalized.
56 If the extension might need extra libraries, they should be included
57 here. The extension Makefile.PL will take care of checking whether
58 the libraries actually exist and how they should be loaded.
59 The extra libraries should be specified in the form -lm -lposix, etc,
60 just as on the cc command line. By default, the Makefile.PL will
61 search through the library path determined by Configure. That path
62 can be augmented by including arguments of the form B<-L/another/library/path>
63 in the extra-libraries argument.
71 Omit all autoload facilities. This is the same as B<-c> but also removes the
72 S<C<require AutoLoader>> statement from the .pm file.
76 Allows a pre-existing extension directory to be overwritten.
80 Omit the autogenerated stub POD section.
84 Omit C<constant()> from the .xs file and corresponding specialised
85 C<AUTOLOAD> from the .pm file.
89 Allows an extension to be created for a header even if that header is
90 not found in /usr/include.
94 Print the usage, help and version for this h2xs and exit.
96 =item B<-n> I<module_name>
98 Specifies a name to be used for the extension, e.g., S<-n RPC::DCE>
100 =item B<-p> I<prefix>
102 Specify a prefix which should be removed from the Perl function names, e.g., S<-p sec_rgy_>
103 This sets up the XS B<PREFIX> keyword and removes the prefix from functions that are
104 autoloaded via the C<constant()> mechansim.
106 =item B<-s> I<sub1,sub2>
108 Create a perl subroutine for the specified macros rather than autoload with the constant() subroutine.
109 These macros are assumed to have a return type of B<char *>, e.g., S<-s sec_rgy_wildcard_name,sec_rgy_wildcard_sid>.
111 =item B<-v> I<version>
113 Specify a version number for this extension. This version number is added
114 to the templates. The default is 0.01.
118 Omit the XS portion. Used to generate templates for a module which is not
123 Automatically generate XSUBs basing on function declarations in the
124 header file. The package C<C::Scan> should be installed. If this
125 option is specified, the name of the header file may look like
126 C<NAME1,NAME2>. In this case NAME1 is used instead of the specified string,
127 but XSUBS are emited only for the declarations included from file NAME2.
131 Additional flags to specify to C preprocessor when scanning header for
132 function declarations. Should not be used without B<-x>.
139 # Default behavior, extension is Rusers
142 # Same, but extension is RUSERS
143 h2xs -n RUSERS rpcsvc/rusers
145 # Extension is rpcsvc::rusers. Still finds <rpcsvc/rusers.h>
148 # Extension is ONC::RPC. Still finds <rpcsvc/rusers.h>
149 h2xs -n ONC::RPC rpcsvc/rusers
151 # Without constant() or AUTOLOAD
152 h2xs -c rpcsvc/rusers
154 # Creates templates for an extension named RPC
157 # Extension is ONC::RPC.
160 # Makefile.PL will look for library -lrpc in
161 # additional directory /opt/net/lib
162 h2xs rpcsvc/rusers -L/opt/net/lib -lrpc
164 # Extension is DCE::rgynbase
165 # prefix "sec_rgy_" is dropped from perl function names
166 h2xs -n DCE::rgynbase -p sec_rgy_ dce/rgynbase
168 # Extension is DCE::rgynbase
169 # prefix "sec_rgy_" is dropped from perl function names
170 # subroutines are created for sec_rgy_wildcard_name and sec_rgy_wildcard_sid
171 h2xs -n DCE::rgynbase -p sec_rgy_ \
172 -s sec_rgy_wildcard_name,sec_rgy_wildcard_sid dce/rgynbase
174 # Make XS with defines in perl.h, and function declarations
175 # visible from perl.h. Name of the extension is perl1.
176 # When scanning perl.h, define -DEXT=extern -DdEXT= -DINIT(x)=
177 # Extra backslashes below because the string is passed to shell.
178 h2xs -xn perl1 -F "-DEXT=extern -DdEXT= -DINIT\(x\)=" \
179 ../perl5_003_01/perl.h
181 # Same with function declaration in proto.h as visible from perl.h.
182 perl H:\get\perl\perl5_003_01.try\utils\h2xs -xn perl1 \
183 ../perl5_003_01/perl.h,proto.h
187 No environment variables are used.
191 Larry Wall and others
195 L<perl>, L<perlxstut>, L<ExtUtils::MakeMaker>, and L<AutoLoader>.
199 The usual warnings if it cannot read or write the files involved.
203 my( $H2XS_VERSION ) = ' $Revision: 1.16 $ ' =~ /\$Revision:\s+([^\s]+)/;
204 my $TEMPLATE_VERSION = '0.01';
210 die "h2xs [-AOPXcfh] [-v version] [-n module_name] [-p prefix] [-s subs] [headerfile [extra_libraries]]
211 version: $H2XS_VERSION
212 -f Force creation of the extension even if the C header does not exist.
213 -n Specify a name to use for the extension (recommended).
214 -c Omit the constant() function and specialised AUTOLOAD from the XS file.
215 -p Specify a prefix which should be removed from the Perl function names.
216 -s Create subroutines for specified macros.
217 -A Omit all autoloading facilities (implies -c).
218 -O Allow overwriting of a pre-existing extension directory.
219 -P Omit the stub POD section.
220 -X Omit the XS portion.
221 -v Specify a version number for this extension.
222 -x Autogenerate XSUBs using C::Scan.
223 -F Additional flags for C preprocessor (used with -x).
224 -h Display this help message
226 are any libraries that might be needed for loading the
227 extension, e.g. -lm would try to link in the math library.
232 getopts("AOPXcfhxv:n:p:s:F:") || usage;
237 $TEMPLATE_VERSION = $opt_v;
239 $opt_c = 1 if $opt_A;
240 %const_xsub = map { $_,1 } split(/,+/, $opt_s) if $opt_s;
243 $extralibs = "@ARGV";
245 usage "Must supply header file or module name\n"
246 unless ($path_h or $opt_n);
251 if( $path_h =~ s#::#/#g && $opt_n ){
252 warn "Nesting of headerfile ignored with -n\n";
254 $path_h .= ".h" unless $path_h =~ /\.h$/;
256 $path_h =~ s/,.*$// if $opt_x;
257 if ($^O eq 'VMS') { # Consider overrides of default location
258 if ($path_h !~ m![:>\[]!) {
259 my($hadsys) = ($path_h =~ s!^sys/!!i);
260 if ($ENV{'DECC$System_Include'}) { $path_h = "DECC\$System_Include:$path_h"; }
261 elsif ($ENV{'DECC$Library_Include'}) { $path_h = "DECC\$Library_Include:$path_h"; }
262 elsif ($ENV{'GNU_CC_Include'}) { $path_h = 'GNU_CC_Include:' .
263 ($hadsys ? '[vms]' : '[000000]') . $path_h; }
264 elsif ($ENV{'VAXC$Include'}) { $path_h = "VAXC\$_Include:$path_h"; }
265 else { $path_h = "Sys\$Library:$path_h"; }
268 elsif ($^O eq 'os2') {
269 $path_h = "/usr/include/$path_h" unless $path_h =~ m#^([a-z]:)?[./]#i;
271 else { $path_h = "/usr/include/$path_h" unless $path_h =~ m#^[./]#; }
272 die "Can't find $path_h\n" if ( ! $opt_f && ! -f $path_h );
274 # Scan the header file (we should deal with nested header files)
275 # Record the names of simple #define constants into const_names
276 # Function prototypes are not (currently) processed.
277 open(CH, "<$path_h") || die "Can't open $path_h: $!\n";
279 if (/^#[ \t]*define\s+([\$\w]+)\b\s*[^("]/) {
280 print "Matched $_ ($1)\n";
282 next if /^_.*_h_*$/i; # special case, but for what?
283 if (defined $opt_p) {
284 if (!/^$opt_p(\d)/) {
285 ++$prefix{$_} if s/^$opt_p//;
288 warn "can't remove $opt_p prefix from '$_'!\n";
295 @const_names = sort keys %const_names;
299 $module = $opt_n || do {
308 (chdir 'ext', $ext = 'ext/') if -d 'ext';
310 if( $module =~ /::/ ){
312 @modparts = split(/::/,$module);
313 $modfname = $modparts[-1];
314 $modpname = join('/',@modparts);
319 $modfname = $modpname = $module;
324 warn "Overwriting existing $ext$modpname!!!\n" if -e $modpname;
326 die "Won't overwrite existing $ext$modpname\n" if -e $modpname;
331 mkdir("$modpath$_", 0777);
335 mkdir($modpname, 0777);
336 chdir($modpname) || die "Can't chdir $ext$modpname: $!\n";
338 if( ! $opt_X ){ # use XS, unless it was disabled
339 open(XS, ">$modfname.xs") || die "Can't create $ext$modpname/$modfname.xs: $!\n";
341 open(PM, ">$modfname.pm") || die "Can't create $ext$modpname/$modfname.pm: $!\n";
344 warn "Writing $ext$modpname/$modfname.pm\n";
352 if( $opt_X || $opt_c || $opt_A ){
353 # we won't have our own AUTOLOAD(), so won't have $AUTOLOAD
355 use vars qw($VERSION @ISA @EXPORT);
359 # we'll have an AUTOLOAD(), and it will have $AUTOLOAD and
363 use vars qw($VERSION @ISA @EXPORT $AUTOLOAD);
372 print PM <<"END" if ! $opt_X; # use DynaLoader, unless XS was disabled
376 # require autoloader if XS is disabled.
377 # if XS is enabled, require autoloader unless autoloading is disabled.
378 if( $opt_X || (! $opt_A) ){
384 if( $opt_X || ($opt_c && ! $opt_A) ){
385 # we won't have our own AUTOLOAD(), so we'll inherit it.
386 if( ! $opt_X ) { # use DynaLoader, unless XS was disabled
389 \@ISA = qw(Exporter AutoLoader DynaLoader);
395 \@ISA = qw(Exporter AutoLoader);
400 # 1) we have our own AUTOLOAD(), so don't need to inherit it.
402 # 2) we don't want autoloading mentioned.
403 if( ! $opt_X ){ # use DynaLoader, unless XS was disabled
406 \@ISA = qw(Exporter DynaLoader);
412 \@ISA = qw(Exporter);
418 # Items to export into callers namespace by default. Note: do not export
419 # names by default without a very good reason. Use EXPORT_OK instead.
420 # Do not simply export all your public functions/methods/constants.
424 \$VERSION = '$TEMPLATE_VERSION';
428 print PM <<"END" unless $opt_c or $opt_X;
430 # This AUTOLOAD is used to 'autoload' constants from the constant()
431 # XS function. If a constant is not found then control is passed
432 # to the AUTOLOAD in AutoLoader.
435 (\$constname = \$AUTOLOAD) =~ s/.*:://;
436 my \$val = constant(\$constname, \@_ ? \$_[0] : 0);
438 if (\$! =~ /Invalid/) {
439 \$AutoLoader::AUTOLOAD = \$AUTOLOAD;
440 goto &AutoLoader::AUTOLOAD;
443 croak "Your vendor has not defined $module macro \$constname";
446 eval "sub \$AUTOLOAD { \$val }";
452 if( ! $opt_X ){ # print bootstrap, unless XS is disabled
454 bootstrap $module \$VERSION;
458 if( $opt_P ){ # if POD is disabled
467 # Preloaded methods go here.
469 # Autoload methods go after $after, and are processed by the autosplit program.
475 $author = "A. U. Thor";
476 $email = 'a.u.thor@a.galaxy.far.far.away';
478 $pod = <<"END" unless $opt_P;
479 ## Below is the stub of documentation for your module. You better edit it!
483 #$module - Perl extension for blah blah blah
492 #Stub documentation for $module was created by h2xs. It looks like the
493 #author of the extension was negligent enough to leave the stub
509 $pod =~ s/^\#//gm unless $opt_P;
510 print PM $pod unless $opt_P;
515 if( ! $opt_X ){ # print XS, unless it is disabled
516 warn "Writing $ext$modpname/$modfname.xs\n";
532 $h =~ s#^/usr/include/##;
533 if ($^O eq 'VMS') { $h =~ s#.*vms\]#sys/# or $h =~ s#.*[:>\]]##; }
546 croak("$module::%s not implemented on this architecture", s);
559 my(@AZ, @az, @under);
561 foreach(@const_names){
562 @AZ = 'A' .. 'Z' if !@AZ && /^[A-Z]/;
563 @az = 'a' .. 'z' if !@az && /^[a-z]/;
564 @under = '_' if !@under && /^_/;
567 foreach $letter (@AZ, @az, @under) {
569 last if $letter eq 'a' && !@const_names;
571 print XS " case '$letter':\n";
573 while (substr($const_names[0],0,1) eq $letter) {
574 $name = shift(@const_names);
575 $macro = $prefix{$name} ? "$opt_p$name" : $name;
576 next if $const_xsub{$macro};
578 if (strEQ(name, "$name"))
603 $prefix = "PREFIX = $opt_p" if defined $opt_p;
604 # Now switch from C to XS by issuing the first MODULE declaration:
607 MODULE = $module PACKAGE = $module $prefix
611 foreach (sort keys %const_xsub) {
620 croak("Your vendor has not defined the $module macro $_");
629 # If a constant() function was written then output a corresponding
631 print XS <<"END" unless $opt_c;
643 my ($type, $name, $args) = @$decl;
644 my @argnames = map {$_->[1]} @$args;
645 my @argtypes = map { normalize_type( $_->[0] ) } @$args;
646 my $numargs = @$args;
647 if ($numargs and $argtypes[-1] eq '...') {
649 $argnames[-1] = '...';
652 $type = normalize_type($type);
660 for $arg (0 .. $numargs - 1) {
662 $argtypes[$arg] $argnames[$arg]
667 my $ignore_mods = '(?:\b(?:__const__|static|inline|__inline__)\b\s*)*';
671 $type =~ s/$ignore_mods//go;
675 $type =~ s/\b\*/ */g;
676 $type =~ s/\*\b/* /g;
677 $type =~ s/\*\s+(?=\*)/*/g;
682 require C::Scan; # Run-time directive
683 require Config; # Run-time directive
686 my $filename = $path_h;
687 my $addflags = $opt_F || '';
688 if ($fullpath =~ /,/) {
692 $c = new C::Scan 'filename' => $filename, 'filename_filter' => $filter,
693 'add_cppflags' => $addflags;
694 $c->set('includeDirs' => [$Config::Config{shrpdir}]);
696 my $fdec = $c->get('parsed_fdecls');
698 for $decl (@$fdec) { print_decl(\*XS, $decl) }
704 warn "Writing $ext$modpname/Makefile.PL\n";
705 open(PL, ">Makefile.PL") || die "Can't create $ext$modpname/Makefile.PL: $!\n";
708 use ExtUtils::MakeMaker;
709 # See lib/ExtUtils/MakeMaker.pm for details of how to influence
710 # the contents of the Makefile that is written.
712 print PL "WriteMakefile(\n";
713 print PL " 'NAME' => '$module',\n";
714 print PL " 'VERSION_FROM' => '$modfname.pm', # finds \$VERSION\n";
715 if( ! $opt_X ){ # print C stuff, unless XS is disabled
716 print PL " 'LIBS' => ['$extralibs'], # e.g., '-lm' \n";
717 print PL " 'DEFINE' => '', # e.g., '-DHAVE_SOMETHING' \n";
718 print PL " 'INC' => '', # e.g., '-I/usr/include/other' \n";
721 close(PL) || die "Can't close $ext$modpname/Makefile.PL: $!\n";
723 warn "Writing $ext$modpname/test.pl\n";
724 open(EX, ">test.pl") || die "Can't create $ext$modpname/test.pl: $!\n";
726 # Before `make install' is performed this script should be runnable with
727 # `make test'. After `make install' it should work as `perl test.pl'
729 ######################### We start with some black magic to print on failure.
731 # Change 1..1 below to 1..last_test_to_print .
732 # (It may become useful if the test is moved to ./t subdirectory.)
734 BEGIN { $| = 1; print "1..1\n"; }
735 END {print "not ok 1\n" unless $loaded;}
744 ######################### End of black magic.
746 # Insert your test code below (better if it prints "ok 13"
747 # (correspondingly "not ok 13") depending on the success of chunk 13
751 close(EX) || die "Can't close $ext$modpname/test.pl: $!\n";
753 warn "Writing $ext$modpname/Changes\n";
754 open(EX, ">Changes") || die "Can't create $ext$modpname/Changes: $!\n";
755 print EX "Revision history for Perl extension $module.\n\n";
756 print EX "$TEMPLATE_VERSION ",scalar localtime,"\n";
757 print EX "\t- original version; created by h2xs $H2XS_VERSION\n\n";
758 close(EX) || die "Can't close $ext$modpname/Changes: $!\n";
760 warn "Writing $ext$modpname/MANIFEST\n";
761 open(MANI,'>MANIFEST') or die "Can't create MANIFEST: $!";
764 eval {opendir(D,'.');};
765 unless ($@) { @files = readdir(D); closedir(D); }
767 if (!@files) { @files = map {chomp && $_} `ls`; }
768 print MANI join("\n",@files);
772 close OUT or die "Can't close $file: $!";
773 chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
774 exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';