5 h2xs - convert .h C header files to Perl extensions
9 B<h2xs> [B<-Acfh>] [B<-n> module_name] [headerfile [extra_libraries]]
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.
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.
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.
34 =item B<-n> I<module_name>
36 Specifies a name to be used for the extension, e.g., S<-n RPC::DCE>
40 Allows an extension to be created for a header even if that header is
41 not found in /usr/include.
45 Omit C<constant()> from the .xs file and corresponding specialised
46 C<AUTOLOAD> from the .pm file.
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.
58 # Default behavior, extension is Rusers
61 # Same, but extension is RUSERS
62 h2xs -n RUSERS rpcsvc/rusers
64 # Extension is rpcsvc::rusers. Still finds <rpcsvc/rusers.h>
67 # Extension is ONC::RPC. Still finds <rpcsvc/rusers.h>
68 h2xs -n ONC::RPC rpcsvc/rusers
70 # Without constant() or AUTOLOAD
73 # Creates templates for an extension named RPC
76 # Extension is ONC::RPC.
79 # Makefile.PL will look for library -lrpc in
80 # additional directory /opt/net/lib
81 h2xs rpcsvc/rusers -L/opt/net/lib -lrpc
86 No environment variables are used.
94 L<perl>, L<ExtUtils::MakeMaker>, L<AutoLoader>
98 The usual warnings if it can't read or write the files involved.
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
114 are any libraries that might be needed for loading the
115 extension, e.g. -lm would try to link in the math library.
120 getopts("Acfhn:") || usage;
123 $opt_c = 1 if $opt_A;
126 $extralibs = "@ARGV";
128 usage "Must supply header file or module name\n"
129 unless ($path_h or $opt_n);
134 if( $path_h =~ s#::#/#g && $opt_n ){
135 warn "Nesting of headerfile ignored with -n\n";
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 );
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";
146 if (/^#[ \t]*define\s+(\w+)\b\s*[^("]/) {
148 next if /^_.*_h_*$/i; # special case, but for what?
153 @const_names = sort keys %const_names;
157 $module = $opt_n || do {
166 chdir 'ext' if -d 'ext';
168 if( $module =~ /::/ ){
170 @modparts = split(/::/,$module);
171 $modfname = $modparts[-1];
172 $modpname = join('/',@modparts);
177 $modfname = $modpname = $module;
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";
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";
191 warn "Writing ext/$modpname/$modfname.pm\n";
206 if( $opt_c && ! $opt_A ){
207 # we won't have our own AUTOLOAD(), so we'll inherit it.
210 \@ISA = qw(Exporter AutoLoader DynaLoader);
214 # 1) we have our own AUTOLOAD(), so don't need to inherit it.
216 # 2) we don't want autoloading mentioned.
219 \@ISA = qw(Exporter DynaLoader);
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.
230 # Other items we are prepared to export if requested
236 print PM <<"END" unless $opt_c;
238 # This AUTOLOAD is used to 'autoload' constants from the constant()
239 # XS function. If a constant is not found then control is passed
240 # to the AUTOLOAD in AutoLoader.
242 # NOTE: THIS AUTOLOAD FUNCTION IS FLAWED (but is the best we can do for now).
243 # Avoid old-style ``&CONST'' usage. Either remove the ``&'' or add ``()''.
245 \$AutoLoader::AUTOLOAD = \$AUTOLOAD;
246 goto &AutoLoader::AUTOLOAD;
249 (\$constname = \$AUTOLOAD) =~ s/.*:://;
250 \$val = constant(\$constname, \@_ ? \$_[0] : 0);
252 if (\$! =~ /Invalid/) {
253 \$AutoLoader::AUTOLOAD = \$AUTOLOAD;
254 goto &AutoLoader::AUTOLOAD;
257 (\$pack,\$file,\$line) = caller;
258 die "Your vendor has not defined $module macro \$constname, used at \$file line \$line.\n";
261 eval "sub \$AUTOLOAD { \$val }";
270 # Preloaded methods go here.
272 # Autoload methods go after __END__, and are processed by the autosplit program.
281 warn "Writing ext/$modpname/$modfname.xs\n";
291 $h =~ s#^/usr/include/##;
304 croak("$module::%s not implemented on this architecture", s);
317 my(@AZ, @az, @under);
319 foreach(@const_names){
320 @AZ = 'A' .. 'Z' if !@AZ && /^[A-Z]/;
321 @az = 'a' .. 'z' if !@az && /^[a-z]/;
322 @under = '_' if !@under && /^_/;
325 foreach $letter (@AZ, @az, @under) {
327 last if $letter eq 'a' && !@const_names;
329 print XS " case '$letter':\n";
331 while (substr($const_names[0],0,1) eq $letter) {
332 $name = shift(@const_names);
334 if (strEQ(name, "$name"))
359 # Now switch from C to XS by issuing the first MODULE declaration:
362 MODULE = $module PACKAGE = $module
366 # If a constant() function was written then output a corresponding
368 print XS <<"END" unless $opt_c;
380 warn "Writing ext/$modpname/Makefile.PL\n";
381 open(PL, ">Makefile.PL") || die "Can't create ext/$modpname/Makefile.PL: $!\n";
384 use ExtUtils::MakeMaker;
385 # See lib/ExtUtils/MakeMaker.pm for details of how to influence
386 # the contents of the Makefile that is written.
388 print PL "WriteMakefile(\n";
389 print PL " 'NAME' => '$module',\n";
390 print PL " 'VERSION' => '0.1',\n";
391 print PL " 'LIBS' => ['$extralibs'], # e.g., '-lm' \n";
392 print PL " 'DEFINE' => '', # e.g., '-DHAVE_SOMETHING' \n";
393 print PL " 'INC' => '', # e.g., '-I/usr/include/other' \n";
397 system '/bin/ls > MANIFEST';