3 if test -f config.sh; then TOP=.;
4 elif test -f ../config.sh; then TOP=..;
5 elif test -f ../../config.sh; then TOP=../..;
6 elif test -f ../../../config.sh; then TOP=../../..;
7 elif test -f ../../../../config.sh; then TOP=../../../..;
9 echo "Can't find config.sh."; exit 1
14 : This forces SH files to create target in same directory as SH file.
15 : This is so that make depend always knows where to find SH derivatives.
17 */*) cd `expr X$0 : 'X\(.*\)/'` ;;
19 echo "Extracting h2xs (with variable substitutions)"
20 $spitshell >h2xs <<!GROK!THIS!
24 $spitshell >>h2xs <<'!NO!SUBS!'
28 h2xs - convert .h C header files to Perl extensions
32 B<h2xs> [B<-Acfh>] [B<-n> module_name] [headerfile [extra_libraries]]
36 I<h2xs> builds a Perl extension from any C header file. The extension will
37 include functions which can be used to retrieve the value of any #define
38 statement which was in the C header.
40 The I<module_name> will be used for the name of the extension. If
41 module_name is not supplied then the name of the header file will be used,
42 with the first character capitalized.
44 If the extension might need extra libraries, they should be included
45 here. The extension Makefile.PL will take care of checking whether
46 the libraries actually exist and how they should be loaded.
47 The extra libraries should be specified in the form -lm -lposix, etc,
48 just as on the cc command line. By default, the Makefile.PL will
49 search through the library path determined by Configure. That path
50 can be augmented by including arguments of the form B<-L/another/library/path>
51 in the extra-libraries argument.
57 =item B<-n> I<module_name>
59 Specifies a name to be used for the extension, e.g., S<-n RPC::DCE>
63 Allows an extension to be created for a header even if that header is
64 not found in /usr/include.
68 Omit C<constant()> from the .xs file and corresponding specialised
69 C<AUTOLOAD> from the .pm file.
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.
81 # Default behavior, extension is Rusers
84 # Same, but extension is RUSERS
85 h2xs -n RUSERS rpcsvc/rusers
87 # Extension is rpcsvc::rusers. Still finds <rpcsvc/rusers.h>
90 # Extension is ONC::RPC. Still finds <rpcsvc/rusers.h>
91 h2xs -n ONC::RPC rpcsvc/rusers
93 # Without constant() or AUTOLOAD
96 # Creates templates for an extension named RPC
99 # Extension is ONC::RPC.
102 # Makefile.PL will look for library -lrpc in
103 # additional directory /opt/net/lib
104 h2xs rpcsvc/rusers -L/opt/net/lib -lrpc
109 No environment variables are used.
113 Larry Wall and others
117 L<perl>, L<ExtUtils::MakeMaker>, L<AutoLoader>
121 The usual warnings if it can't read or write the files involved.
130 die 'h2xs [-Acfh] [-n module_name] [headerfile [extra_libraries]]
131 -f Force creation of the extension even if the C header does not exist.
132 -n Specify a name to use for the extension (recommended).
133 -c Omit the constant() function and specialised AUTOLOAD from the XS file.
134 -A Omit all autoloading facilities (implies -c).
135 -h Display this help message
137 are any libraries that might be needed for loading the
138 extension, e.g. -lm would try to link in the math library.
143 getopts("Acfhn:") || usage;
146 $opt_c = 1 if $opt_A;
149 $extralibs = "@ARGV";
151 usage "Must supply header file or module name\n"
152 unless ($path_h or $opt_n);
157 if( $path_h =~ s#::#/#g && $opt_n ){
158 warn "Nesting of headerfile ignored with -n\n";
160 $path_h .= ".h" unless $path_h =~ /\.h$/;
161 $path_h = "/usr/include/$path_h" unless $path_h =~ m#^[./]#;
162 die "Can't find $path_h\n" if ( ! $opt_f && ! -f $path_h );
164 # Scan the header file (we should deal with nested header files)
165 # Record the names of simple #define constants into const_names
166 # Function prototypes are not (currently) processed.
167 open(CH, "<$path_h") || die "Can't open $path_h: $!\n";
169 if (/^#[ \t]*define\s+(\w+)\b\s*[^("]/) {
171 next if /^_.*_h_*$/i; # special case, but for what?
176 @const_names = sort keys %const_names;
180 $module = $opt_n || do {
189 chdir 'ext' if -d 'ext';
191 if( $module =~ /::/ ){
193 @modparts = split(/::/,$module);
194 $modfname = $modparts[-1];
195 $modpname = join('/',@modparts);
200 $modfname = $modpname = $module;
204 die "Won't overwrite existing ext/$modpname\n" if -e $modpname;
205 # quick hack, should really loop over @modparts
206 mkdir($modparts[0], 0777) if $nested;
207 mkdir($modpname, 0777);
208 chdir($modpname) || die "Can't chdir ext/$modpname: $!\n";
210 open(XS, ">$modfname.xs") || die "Can't create ext/$modpname/$modfname.xs: $!\n";
211 open(PM, ">$modfname.pm") || die "Can't create ext/$modpname/$modfname.pm: $!\n";
214 warn "Writing ext/$modpname/$modfname.pm\n";
229 if( $opt_c && ! $opt_A ){
230 # we won't have our own AUTOLOAD(), so we'll inherit it.
233 \@ISA = qw(Exporter AutoLoader DynaLoader);
237 # 1) we have our own AUTOLOAD(), so don't need to inherit it.
239 # 2) we don't want autoloading mentioned.
242 \@ISA = qw(Exporter DynaLoader);
247 # Items to export into callers namespace by default. Note: do not export
248 # names by default without a very good reason. Use EXPORT_OK instead.
249 # Do not simply export all your public functions/methods/constants.
255 print PM <<"END" unless $opt_c;
257 # This AUTOLOAD is used to 'autoload' constants from the constant()
258 # XS function. If a constant is not found then control is passed
259 # to the AUTOLOAD in AutoLoader.
262 (\$constname = \$AUTOLOAD) =~ s/.*:://;
263 \$val = constant(\$constname, \@_ ? \$_[0] : 0);
265 if (\$! =~ /Invalid/) {
266 \$AutoLoader::AUTOLOAD = \$AUTOLOAD;
267 goto &AutoLoader::AUTOLOAD;
270 (\$pack,\$file,\$line) = caller;
271 die "Your vendor has not defined $module macro \$constname, used at \$file line \$line.\n";
274 eval "sub \$AUTOLOAD { \$val }";
283 # Preloaded methods go here.
285 # Autoload methods go after __END__, and are processed by the autosplit program.
294 warn "Writing ext/$modpname/$modfname.xs\n";
304 $h =~ s#^/usr/include/##;
317 croak("$module::%s not implemented on this architecture", s);
330 my(@AZ, @az, @under);
332 foreach(@const_names){
333 @AZ = 'A' .. 'Z' if !@AZ && /^[A-Z]/;
334 @az = 'a' .. 'z' if !@az && /^[a-z]/;
335 @under = '_' if !@under && /^_/;
338 foreach $letter (@AZ, @az, @under) {
340 last if $letter eq 'a' && !@const_names;
342 print XS " case '$letter':\n";
344 while (substr($const_names[0],0,1) eq $letter) {
345 $name = shift(@const_names);
347 if (strEQ(name, "$name"))
372 # Now switch from C to XS by issuing the first MODULE declaration:
375 MODULE = $module PACKAGE = $module
379 # If a constant() function was written then output a corresponding
381 print XS <<"END" unless $opt_c;
393 warn "Writing ext/$modpname/Makefile.PL\n";
394 open(PL, ">Makefile.PL") || die "Can't create ext/$modpname/Makefile.PL: $!\n";
397 use ExtUtils::MakeMaker;
398 # See lib/ExtUtils/MakeMaker.pm for details of how to influence
399 # the contents of the Makefile that is written.
401 print PL "WriteMakefile(\n";
402 print PL " 'NAME' => '$module',\n";
403 print PL " 'VERSION' => '0.1',\n";
404 print PL " 'LIBS' => ['$extralibs'], # e.g., '-lm' \n";
405 print PL " 'DEFINE' => '', # e.g., '-DHAVE_SOMETHING' \n";
406 print PL " 'INC' => '', # e.g., '-I/usr/include/other' \n";
410 system '/bin/ls > MANIFEST';