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<-Acfh>] [B<-n> module_name] [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.
69 =item B<-n> I<module_name>
71 Specifies a name to be used for the extension, e.g., S<-n RPC::DCE>
75 Allows an extension to be created for a header even if that header is
76 not found in /usr/include.
80 Omit C<constant()> from the .xs file and corresponding specialised
81 C<AUTOLOAD> from the .pm file.
85 Omit all autoload facilities. This is the same as B<-c> but also removes the
86 S<C<require AutoLoader>> statement from the .pm file.
93 # Default behavior, extension is Rusers
96 # Same, but extension is RUSERS
97 h2xs -n RUSERS rpcsvc/rusers
99 # Extension is rpcsvc::rusers. Still finds <rpcsvc/rusers.h>
102 # Extension is ONC::RPC. Still finds <rpcsvc/rusers.h>
103 h2xs -n ONC::RPC rpcsvc/rusers
105 # Without constant() or AUTOLOAD
106 h2xs -c rpcsvc/rusers
108 # Creates templates for an extension named RPC
111 # Extension is ONC::RPC.
114 # Makefile.PL will look for library -lrpc in
115 # additional directory /opt/net/lib
116 h2xs rpcsvc/rusers -L/opt/net/lib -lrpc
121 No environment variables are used.
125 Larry Wall and others
129 L<perl>, L<ExtUtils::MakeMaker>, L<AutoLoader>
133 The usual warnings if it can't read or write the files involved.
142 die 'h2xs [-Acfh] [-n module_name] [headerfile [extra_libraries]]
143 -f Force creation of the extension even if the C header does not exist.
144 -n Specify a name to use for the extension (recommended).
145 -c Omit the constant() function and specialised AUTOLOAD from the XS file.
146 -A Omit all autoloading facilities (implies -c).
147 -h Display this help message
149 are any libraries that might be needed for loading the
150 extension, e.g. -lm would try to link in the math library.
155 getopts("Acfhn:") || usage;
158 $opt_c = 1 if $opt_A;
161 $extralibs = "@ARGV";
163 usage "Must supply header file or module name\n"
164 unless ($path_h or $opt_n);
169 if( $path_h =~ s#::#/#g && $opt_n ){
170 warn "Nesting of headerfile ignored with -n\n";
172 $path_h .= ".h" unless $path_h =~ /\.h$/;
173 $path_h = "/usr/include/$path_h" unless $path_h =~ m#^[./]#;
174 die "Can't find $path_h\n" if ( ! $opt_f && ! -f $path_h );
176 # Scan the header file (we should deal with nested header files)
177 # Record the names of simple #define constants into const_names
178 # Function prototypes are not (currently) processed.
179 open(CH, "<$path_h") || die "Can't open $path_h: $!\n";
181 if (/^#[ \t]*define\s+(\w+)\b\s*[^("]/) {
183 next if /^_.*_h_*$/i; # special case, but for what?
188 @const_names = sort keys %const_names;
192 $module = $opt_n || do {
201 (chdir 'ext', $ext = 'ext/') if -d 'ext';
203 if( $module =~ /::/ ){
205 @modparts = split(/::/,$module);
206 $modfname = $modparts[-1];
207 $modpname = join('/',@modparts);
212 $modfname = $modpname = $module;
216 die "Won't overwrite existing $ext$modpname\n" if -e $modpname;
217 # quick hack, should really loop over @modparts
218 mkdir($modparts[0], 0777) if $nested;
219 mkdir($modpname, 0777);
220 chdir($modpname) || die "Can't chdir $ext$modpname: $!\n";
222 open(XS, ">$modfname.xs") || die "Can't create $ext$modpname/$modfname.xs: $!\n";
223 open(PM, ">$modfname.pm") || die "Can't create $ext$modpname/$modfname.pm: $!\n";
226 warn "Writing $ext$modpname/$modfname.pm\n";
241 if( $opt_c && ! $opt_A ){
242 # we won't have our own AUTOLOAD(), so we'll inherit it.
245 \@ISA = qw(Exporter AutoLoader DynaLoader);
249 # 1) we have our own AUTOLOAD(), so don't need to inherit it.
251 # 2) we don't want autoloading mentioned.
254 \@ISA = qw(Exporter DynaLoader);
259 # Items to export into callers namespace by default. Note: do not export
260 # names by default without a very good reason. Use EXPORT_OK instead.
261 # Do not simply export all your public functions/methods/constants.
267 print PM <<"END" unless $opt_c;
269 # This AUTOLOAD is used to 'autoload' constants from the constant()
270 # XS function. If a constant is not found then control is passed
271 # to the AUTOLOAD in AutoLoader.
274 (\$constname = \$AUTOLOAD) =~ s/.*:://;
275 \$val = constant(\$constname, \@_ ? \$_[0] : 0);
277 if (\$! =~ /Invalid/) {
278 \$AutoLoader::AUTOLOAD = \$AUTOLOAD;
279 goto &AutoLoader::AUTOLOAD;
282 (\$pack,\$file,\$line) = caller;
283 die "Your vendor has not defined $module macro \$constname, used at \$file line \$line.\n";
286 eval "sub \$AUTOLOAD { \$val }";
295 # Preloaded methods go here.
297 # Autoload methods go after __END__, and are processed by the autosplit program.
306 warn "Writing $ext$modpname/$modfname.xs\n";
322 $h =~ s#^/usr/include/##;
335 croak("$module::%s not implemented on this architecture", s);
348 my(@AZ, @az, @under);
350 foreach(@const_names){
351 @AZ = 'A' .. 'Z' if !@AZ && /^[A-Z]/;
352 @az = 'a' .. 'z' if !@az && /^[a-z]/;
353 @under = '_' if !@under && /^_/;
356 foreach $letter (@AZ, @az, @under) {
358 last if $letter eq 'a' && !@const_names;
360 print XS " case '$letter':\n";
362 while (substr($const_names[0],0,1) eq $letter) {
363 $name = shift(@const_names);
365 if (strEQ(name, "$name"))
390 # Now switch from C to XS by issuing the first MODULE declaration:
393 MODULE = $module PACKAGE = $module
397 # If a constant() function was written then output a corresponding
399 print XS <<"END" unless $opt_c;
411 warn "Writing $ext$modpname/Makefile.PL\n";
412 open(PL, ">Makefile.PL") || die "Can't create $ext$modpname/Makefile.PL: $!\n";
415 use ExtUtils::MakeMaker;
416 # See lib/ExtUtils/MakeMaker.pm for details of how to influence
417 # the contents of the Makefile that is written.
419 print PL "WriteMakefile(\n";
420 print PL " 'NAME' => '$module',\n";
421 print PL " 'VERSION' => '0.1',\n";
422 print PL " 'LIBS' => ['$extralibs'], # e.g., '-lm' \n";
423 print PL " 'DEFINE' => '', # e.g., '-DHAVE_SOMETHING' \n";
424 print PL " 'INC' => '', # e.g., '-I/usr/include/other' \n";
428 system '/bin/ls > MANIFEST' or system 'ls > MANIFEST';
431 close OUT or die "Can't close $file: $!";
432 chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
433 exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';