This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl 5.000
[perl5.git] / h2xs
CommitLineData
a0d0e21e
LW
1#!/usr/bin/perl
2'di ';
3'ds 00 \"';
4'ig 00 ';
5
6use Getopt::Std;
7
8$usage='h2xs [-Aachfm] [-n module_name] [headerfile [extra_libraries]]
9 -a Omit AutoLoad facilities from .pm file.
10 -c Omit the constant() function from the XS file.
11 -A Equivalent to -a -c
12 -f Force creation of the extension even if the C header does not exist.
13 -m Also create an old-style Makefile.SH
14 -h help
15 -n Specify a name to use for the extension.
16extra_libraries are any libraries that might be needed for loading
17 the extension, e.g. -lm would try to link in the math library.
18';
19
20sub usage{ die "Usage: $usage\n" }
21
22getopts("fhcaAmn:") || &usage;
23
24&usage if $opt_h;
25
26if( @ARGV ){
27 $path_h = shift;
28}
29elsif( ! @ARGV && ! $opt_n ){
30 die "Must supply header file or module name\n";
31}
32
33$extralibs = "@ARGV";
34if( $opt_A ){
35 $opt_a = $opt_c = 1;
36}
37$write_makefile_sh = ($opt_m) ? 1 : 0;
38
39if( $path_h ){
40 $name = $path_h;
41 if( $path_h =~ s#::#/#g && $opt_n ){
42 warn "Nesting of headerfile ignored with -n\n";
43 }
44 $path_h .= ".h" unless $path_h =~ /\.h$/;
45 $path_h = "/usr/include/$path_h" unless $path_h =~ m#^[./]#;
46 die "Can't find $path_h\n" if( ! $opt_f && ! -f $path_h );
47}
48
49$module = $opt_n || do {
50 $name =~ s/\.h$//;
51 if( $name !~ /::/ ){
52 $name =~ s#^.*/##;
53 $name = "\u$name";
54 }
55 $name;
56};
57
58chdir 'ext' if -d 'ext';
59
60if( $module =~ /::/ ){
61 $nested = 1;
62 @modparts = split(/::/,$module);
63 $modfname = $modparts[-1];
64 $modpname = join('/',@modparts);
65}
66else {
67 $nested = 0;
68 @modparts = ();
69 $modfname = $modpname = $module;
70}
71
72
73die "Won't overwrite existing ext/$modpname\n" if -e $modpname;
74# quick hack, should really loop over @modparts
75mkdir($modparts[0], 0777) if $nested;
76mkdir($modpname, 0777);
77chdir($modpname) || die "Can't chdir ext/$modpname: $!\n";
78
79open(XS, ">$modfname.xs") || die "Can't create ext/$modpname/$modfname.xs: $!\n";
80open(PM, ">$modfname.pm") || die "Can't create ext/$modpname/$modfname.pm: $!\n";
81
82
83if( -r $path_h ){
84 open(CH, "<$path_h") || die "Can't open $path_h: $!\n";
85 while (<CH>) {
86 if (/^#[ \t]*define\s+(\w+)\b\s*[^("]/) {
87 $_ = $1;
88 next if /^_.*_h_*$/i;
89 $names{$_}++;
90 @AZ = 'A' .. 'Z' if !@AZ && /^[A-Z]/;
91 @az = 'a' .. 'z' if !@az && /^[a-z]/;
92 @under = '_' if !@under && /^_/;
93 }
94 }
95 close(CH);
96 @names = sort keys %names;
97}
98
99$" = "\n\t";
100warn "Writing ext/$modpname/$modfname.pm\n";
101
102if( ! $opt_a ){
103print PM <<"END";
104package $module;
105
106require Exporter;
107require AutoLoader;
108require DynaLoader;
109\@ISA = qw(Exporter AutoLoader DynaLoader);
110# Items to export into callers namespace by default
111# (move infrequently used names to \@EXPORT_OK below)
112\@EXPORT = qw(
113 @names
114);
115# Other items we are prepared to export if requested
116\@EXPORT_OK = qw(
117);
118
119sub AUTOLOAD {
120 if (\@_ > 1) {
121 \$AutoLoader::AUTOLOAD = \$AUTOLOAD;
122 goto &AutoLoader::AUTOLOAD;
123 }
124 local(\$constname);
125 (\$constname = \$AUTOLOAD) =~ s/.*:://;
126 \$val = constant(\$constname, \@_ ? \$_[0] : 0);
127 if (\$! != 0) {
128 if (\$! =~ /Invalid/) {
129 \$AutoLoader::AUTOLOAD = \$AUTOLOAD;
130 goto &AutoLoader::AUTOLOAD;
131 }
132 else {
133 (\$pack,\$file,\$line) = caller;
134 die "Your vendor has not defined $module macro \$constname, used at \$file line \$line.\n";
135 }
136 }
137 eval "sub \$AUTOLOAD { \$val }";
138 goto &\$AUTOLOAD;
139}
140
141bootstrap $module;
142
143# Preloaded methods go here. Autoload methods go after __END__, and are
144# processed by the autosplit program.
145
1461;
147__END__
148END
149}
150else{
151print PM <<"END";
152package $module;
153
154require Exporter;
155require DynaLoader;
156\@ISA = qw(Exporter DynaLoader);
157# Items to export into callers namespace by default
158\@EXPORT = qw();
159# Other items we are prepared to export if requested
160\@EXPORT_OK = qw();
161
162
163bootstrap $module;
164
1651;
166END
167}
168
169close PM;
170
171warn "Writing ext/$modpname/$modfname.xs\n";
172print XS <<"END";
173#include "EXTERN.h"
174#include "perl.h"
175#include "XSUB.h"
176
177END
178if( $path_h ){
179 my($h) = $path_h;
180 $h =~ s#^/usr/include/##;
181print XS <<"END";
182#include <$h>
183
184END
185}
186
187if( ! $opt_c ){
188print XS <<"END";
189static int
190not_here(s)
191char *s;
192{
193 croak("$module::%s not implemented on this architecture", s);
194 return -1;
195}
196
197static double
198constant(name, arg)
199char *name;
200int arg;
201{
202 errno = 0;
203 switch (*name) {
204END
205
206foreach $letter (@AZ, @az, @under) {
207
208 last if $letter eq 'a' && !@names;
209
210 print XS " case '$letter':\n";
211 my($name);
212 while (substr($names[0],0,1) eq $letter) {
213 $name = shift(@names);
214 print XS <<"END";
215 if (strEQ(name, "$name"))
216#ifdef $name
217 return $name;
218#else
219 goto not_there;
220#endif
221END
222 }
223 print XS <<"END";
224 break;
225END
226}
227print XS <<"END";
228 }
229 errno = EINVAL;
230 return 0;
231
232not_there:
233 errno = ENOENT;
234 return 0;
235}
236
237
238MODULE = $module PACKAGE = $module
239
240double
241constant(name,arg)
242 char * name
243 int arg
244
245END
246}
247else{
248print XS <<"END";
249
250MODULE = $module PACKAGE = $module
251
252END
253}
254
255close XS;
256
257{
258warn "Writing ext/$modpname/Makefile.PL\n";
259open(PL, ">Makefile.PL") || die "Can't create ext/$modpname/Makefile.PL: $!\n";
260
261# Ideally this should have a #!../.. ... miniperl etc header
262print PL <<'END';
263use ExtUtils::MakeMaker;
264# See lib/ExtUtils/MakeMaker.pm for details of how to influence
265# the contents of the Makefile being created.
266END
267print PL "&writeMakefile(\n";
268print PL " 'potential_libs' => '$extralibs', # e.g., '-lm' \n";
269print PL " 'INC' => '', # e.g., '-I/usr/include/other' \n";
270print PL " 'DISTNAME' => 'myname',\n";
271print PL " 'VERSION' => '0.1',\n";
272print PL ");\n";
273}
274
275if ($write_makefile_sh){
276warn "Writing ext/$modpname/Makefile.SH\n";
277open(MF, ">Makefile.SH") || die "Can't create ext/$modpname/Makefile.SH: $!\n";
278print MF <<'END';
279: This forces SH files to create target in same directory as SH file.
280: This is so that make depend always knows where to find SH derivatives.
281
282case "$0" in
283*/*) cd `expr X$0 : 'X\(.*\)/'` ;;
284esac
285
286if test -f config.sh; then TOP=.;
287elif test -f ../config.sh; then TOP=..;
288elif test -f ../../config.sh; then TOP=../..;
289elif test -f ../../../config.sh; then TOP=../../..;
290elif test -f ../../../../config.sh; then TOP=../../../..;
291else
292 echo "Can't find config.sh."; exit 1
293fi
294
295: Find absolute path name for TOP. This is needed when we cd to TOP
296: to run perl on autosplit.
297oldpwd=`pwd`; cd $TOP; ABSTOP=`pwd`; cd $oldpwd
298
299case $CONFIG in
300'')
301 . $TOP/config.sh
302 ;;
303esac
304
305: Find out directory name. This is also the extension name.
306ext=`pwd | $sed -e 's@.*/@@'`
307
308: This extension might have its own typemap
309if test -f typemap; then
310 exttypemap='typemap'
311else
312 exttypemap=''
313fi
314
315: This extension might need additional libraries.
316END
317print MF "potential_libs=\"$extralibs\"\n";
318print MF <<'END';
319. $TOP/ext/util/extliblist
320
321: This extension might need bootstrap support
322if test -f ${ext}_BS; then
323 bootdep=${ext}_BS
324else
325 bootdep=''
326fi
327
328case "$dlsrc" in
329dl_aix*)
330 echo "#!" > $ext.exp
331 echo "boot_$ext" >> $ext.exp
332 ;;
333esac
334
335echo "Extracting ext/$ext/Makefile (with variable substitutions)"
336: This section of the file will have variable substitutions done on it.
337: Move anything that needs config subs from !NO!SUBS! section to !GROK!THIS!.
338: Protect any dollar signs and backticks that you do not want interpreted
339: by putting a backslash in front. You may delete these comments.
340$spitshell >Makefile << !GROK!THIS!
341#
342# This Makefile is for the $ext extension to perl.
343#
344CC = $cc
345RANLIB = $ranlib
346TOP = $TOP
347ABSTOP = $ABSTOP
348LDFLAGS = $ldflags
349CLDFLAGS = $ldflags
350SMALL = $small
351LARGE = $large $split
352
353# To use an alternate make, set \\$altmake in config.sh.
354MAKE = ${altmake-make}
355
356EXT = $ext
357
358# $ext might have its own typemap
359EXTTYPEMAP = $exttypemap
360
361# $ext might have its own bootstrap support
362BOOTDEP = $bootdep
363BOOTSTRAP = $ext.bs
364
365# The following are used to build and install shared libraries for
366# dynamic loading.
367LDDLFLAGS = $lddlflags
368CCDLFLAGS = $ccdlflags
369CCCDLFLAGS = $cccdlflags
370SO = $so
371
372# $ext might need to be linked with some extra libraries.
373# EXTRALIBS = full list of libraries needed for static linking.
374# Only those libraries that actually exist are included.
375# DYNLOADLIBS = list of those libraries that are needed but can be
376# linked in dynamically on this platform. On SunOS, for
377# example, this would be .so* libraries, but not archive
378# libraries. The bootstrap file is installed only if
379# this list is not empty.
380# STATLOADLIBS = list of those libraries which must be statically
381# linked into the shared library. On SunOS 4.1.3,
382# for example, I have only an archive version of
383# -lm, and it must be linked in statically.
384EXTRALIBS = $extralibs
385DYNALOADLIBS = $dynaloadlibs
386STATLOADLIBS = $statloadlibs
387
388!GROK!THIS!
389
390$spitshell >>Makefile <<'!NO!SUBS!'
391
392# Where to put things:
393AUTO = $(TOP)/lib/auto
394INSTALLBOOT = $(AUTO)/$(EXT)/$(EXT).bs
395INSTALLDYNAMIC = $(AUTO)/$(EXT)/$(EXT).$(SO)
396INSTALLSTATIC = $(EXT).a
397INSTALLPM = $(TOP)/lib/$(EXT).pm
398
399PERL = $(ABSTOP)/miniperl
400XSUBPP = $(TOP)/ext/xsubpp
401SHELL = /bin/sh
402CCCMD = `sh $(shellflags) $(TOP)/cflags $@`
403
404.c.o:
405 $(CCCMD) $(CCCDLFLAGS) -I$(TOP) $*.c
406
407all: dynamic
408# Phony target to force checking subdirectories.
409FORCE:
410
411# Target for Dynamic Loading:
412dynamic: $(INSTALLDYNAMIC) $(INSTALLPM) $(INSTALLBOOT)
413
414$(INSTALLDYNAMIC): $(EXT).o
415 @test -d $(AUTO) || mkdir $(AUTO)
416 @test -d $(AUTO)/$(EXT) || mkdir $(AUTO)/$(EXT)
417 ld $(LDDLFLAGS) -o $@ $(EXT).o $(STATLOADLIBS)
418
419$(BOOTSTRAP): Makefile $(BOOTDEP)
420 $(PERL) -I$(TOP)/lib $(TOP)/ext/util/mkbootstrap $(DYNALOADLIBS)
421 touch $(BOOTSTRAP)
422
423$(INSTALLBOOT): $(BOOTSTRAP)
424 @test ! -s $(BOOTSTRAP) || cp $(BOOTSTRAP) $@
425
426# Target for Static Loading:
427static: $(INSTALLSTATIC) $(INSTALLPM)
428
429$(INSTALLSTATIC): $(EXT).o
430 ar cr $@ $(EXT).o
431 $(RANLIB) $@
432 echo $(EXTRALIBS) >> $(TOP)/ext.libs
433
434$(EXT).c: $(EXT).xs $(XSUBPP) $(TOP)/ext/typemap $(EXTTYPEMAP) $(TOP)/cflags Makefile
435 $(PERL) $(XSUBPP) $(EXT).xs >tmp
436 mv tmp $@
437
438END
439if( ! $opt_a ){
440print MF <<'END';
441$(INSTALLPM): $(EXT).pm
442 rm -f $@
443 cp $(EXT).pm $@
444 cd $(TOP); $(PERL) autosplit $(EXT)
445END
446}
447else {
448print MF <<'END';
449$(INSTALLPM): $(EXT).pm
450 cp $(EXT).pm $@
451END
452}
453print MF <<'END';
454
455clean:
456 rm -f *.o *.a mon.out core $(EXT).c so_locations $(BOOTSTRAP) $(EXT).exp
457
458realclean: clean
459 rm -f makefile Makefile
460 rm -f $(INSTALLPM) $(INSTALLDYNAMIC) $(INSTALLSTATIC) $(INSTALLBOOT)
461 rm -rf $(AUTO)/$(EXT)
462
463purge: realclean
464
465$(EXT).o : $(TOP)/EXTERN.h
466$(EXT).o : $(TOP)/perl.h
467$(EXT).o : $(TOP)/embed.h
468$(EXT).o : $(TOP)/config.h
469$(EXT).o : $(TOP)/unixish.h
470$(EXT).o : $(TOP)/handy.h
471$(EXT).o : $(TOP)/regexp.h
472$(EXT).o : $(TOP)/sv.h
473$(EXT).o : $(TOP)/util.h
474$(EXT).o : $(TOP)/form.h
475$(EXT).o : $(TOP)/gv.h
476$(EXT).o : $(TOP)/cv.h
477$(EXT).o : $(TOP)/opcode.h
478$(EXT).o : $(TOP)/op.h
479$(EXT).o : $(TOP)/cop.h
480$(EXT).o : $(TOP)/av.h
481$(EXT).o : $(TOP)/hv.h
482$(EXT).o : $(TOP)/mg.h
483$(EXT).o : $(TOP)/scope.h
484$(EXT).o : $(TOP)/pp.h
485$(EXT).o : $(TOP)/proto.h
486$(EXT).o : $(TOP)/XSUB.h
487
488Makefile: Makefile.SH $(TOP)/config.sh ; /bin/sh Makefile.SH
489$(TOP)/config.h: $(TOP)/config.sh; cd $(TOP); /bin/sh config_h.SH
490$(TOP)/embed.h: $(TOP)/config.sh; cd $(TOP); /bin/sh embed_h.SH
491$(TOP)/cflags: $(TOP)/config.sh; cd $(TOP); /bin/sh cflags.SH
492
493!NO!SUBS!
494chmod 755 Makefile
495$eunicefix Makefile
496
497END
498close MF;
499}
500
501system '/bin/ls > MANIFEST';
502
503# this needs fixing
504# system '[ -f Makefile.SH ] && sh Makefile.SH';
505# system '[ -f Makefile.PL ] && perl Makefile.PL';
506
507
508##############################################################################
509
510 # These next few lines are legal in both Perl and nroff.
511
512.00 ; # finish .ig
513
514'di \" finish diversion--previous line must be blank
515.nr nl 0-1 \" fake up transition to first page again
516.nr % 0 \" start at page 1
517'; __END__ ############# From here on it's a standard manual page ############
518.TH H2XS 1 "August 9, 1994"
519.AT 3
520.SH NAME
521h2xs \- convert .h C header files to Perl extensions
522.SH SYNOPSIS
523.B h2xs [-Aachfm] [-n module_name] [headerfile [extra_libraries]]
524.SH DESCRIPTION
525.I h2xs
526builds a Perl extension from any C header file. The extension will include
527functions which can be used to retrieve the value of any #define statement
528which was in the C header.
529.PP
530The
531.I module_name
532will be used for the name of the extension. If module_name is not supplied
533then the name of the header file will be used, with the first character
534capitalized.
535.PP
536If the extension might need extra libraries, they should be included
537here. The extension Makefile.SH will take care of checking whether
538the libraries actually exist and how they should be loaded.
539The extra libraries should be specified in the form -lm -lposix, etc,
540just as on the cc command line. By default, the Makefile.SH will
541search through the library path determined by Configure. That path
542can be augmented by including arguments of the form -L/another/library/path
543in the extra-libraries argument.
544.SH OPTIONS
545.TP
546.B \-f
547Allows an extension to be created for a header even if that
548header is not found in /usr/include.
549.TP
550.B \-a
551Omit AutoLoad(), AUTOLOAD, and autosplit from the .pm and Makefile files.
552.TP
553.B \-c
554Omit constant() from the .xs file.
555.TP
556.B \-n module_name
557Specifies a name to be used for the extension.
558.TP
559.B \-A
560Turns on both -a and -c.
561.TP
562.B \-m
563Causes an old-style Makefile.SH to be created.
564.SH EXAMPLES
565.nf
566
567 # Default behavior, extension is Rusers
568 h2xs rpcsvc/rusers
569
570 # Same, but extension is RUSERS
571 h2xs -n RUSERS rpcsvc/rusers
572
573 # Extension is rpcsvc::rusers. Still finds <rpcsvc/rusers.h>
574 h2xs rpcsvc::rusers
575
576 # Extension is ONC::RPC. Still finds <rpcsvc/rusers.h>
577 h2xs -n ONC::RPC rpcsvc/rusers
578
579 # Without AUTOLOAD, AutoLoad, autosplit
580 h2xs -a rpcsvc/rusers
581
582 # Creates templates for an extension named RPC
583 h2xs -Afn RPC
584
585 # Extension is ONC::RPC.
586 h2xs -An ONC::RPC
587
588 # Makefile.SH will look for library -lrpc in
589 # additional directory /opt/net/lib
590 h2xs rpcsvc/rusers -L/opt/net/lib -lrpc
591
592.fi
593.SH ENVIRONMENT
594No environment variables are used.
595.SH AUTHOR
596Larry Wall
597.SH "SEE ALSO"
598perl(1)
599.SH DIAGNOSTICS
600The usual warnings if it can't read or write the files involved.
601.ex