Fix type mismatches in x2p's safe{alloc,realloc,free}.
[p5sagit/p5-mst-13.2.git] / utils / h2xs.PL
CommitLineData
4633a7c4 1#!/usr/local/bin/perl
2
3use Config;
4use File::Basename qw(&basename &dirname);
5
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
10# $startperl
11# to ensure Configure will look for $Config{startperl}.
12
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.
15chdir(dirname($0));
16($file = basename($0)) =~ s/\.PL$//;
17$file =~ s/\.pl$//
5ae7f1db 18 if ($^O eq 'VMS' or $^O eq 'os2'); # "case-forgiving"
4633a7c4 19
20open OUT,">$file" or die "Can't create $file: $!";
21
22print "Extracting $file (with variable substitutions)\n";
23
24# In this section, perl variables will be expanded during extraction.
25# You can use $Config{...} to use Configure variables.
26
27print OUT <<"!GROK!THIS!";
28$Config{'startperl'}
29 eval 'exec perl -S \$0 "\$@"'
30 if 0;
40000a8c 31!GROK!THIS!
32
4633a7c4 33# In the following, perl variables are not expanded during extraction.
34
35print OUT <<'!NO!SUBS!';
cf35f3c1 36
3edbfbe5 37=head1 NAME
38
39h2xs - convert .h C header files to Perl extensions
40
41=head1 SYNOPSIS
42
b73edd97 43B<h2xs> [B<-AOPXcdf>] [B<-v> version] [B<-n> module_name] [B<-p> prefix] [B<-s> sub] [headerfile [extra_libraries]]
f508c652 44
45B<h2xs> B<-h>
3edbfbe5 46
47=head1 DESCRIPTION
48
49I<h2xs> builds a Perl extension from any C header file. The extension will
50include functions which can be used to retrieve the value of any #define
51statement which was in the C header.
52
53The I<module_name> will be used for the name of the extension. If
54module_name is not supplied then the name of the header file will be used,
55with the first character capitalized.
56
57If the extension might need extra libraries, they should be included
58here. The extension Makefile.PL will take care of checking whether
59the libraries actually exist and how they should be loaded.
60The extra libraries should be specified in the form -lm -lposix, etc,
61just as on the cc command line. By default, the Makefile.PL will
62search through the library path determined by Configure. That path
63can be augmented by including arguments of the form B<-L/another/library/path>
64in the extra-libraries argument.
65
66=head1 OPTIONS
67
68=over 5
69
f508c652 70=item B<-A>
3edbfbe5 71
f508c652 72Omit all autoload facilities. This is the same as B<-c> but also removes the
73S<C<require AutoLoader>> statement from the .pm file.
3edbfbe5 74
b73edd97 75=item B<-F>
76
77Additional flags to specify to C preprocessor when scanning header for
78function declarations. Should not be used without B<-x>.
79
2920c5d2 80=item B<-O>
81
82Allows a pre-existing extension directory to be overwritten.
83
f508c652 84=item B<-P>
3edbfbe5 85
f508c652 86Omit the autogenerated stub POD section.
3edbfbe5 87
b73edd97 88=item B<-X>
89
90Omit the XS portion. Used to generate templates for a module which is not
91XS-based.
92
3edbfbe5 93=item B<-c>
94
95Omit C<constant()> from the .xs file and corresponding specialised
96C<AUTOLOAD> from the .pm file.
97
b73edd97 98=item B<-d>
99
100Turn on debugging messages.
101
f508c652 102=item B<-f>
3edbfbe5 103
f508c652 104Allows an extension to be created for a header even if that header is
105not found in /usr/include.
106
107=item B<-h>
108
109Print the usage, help and version for this h2xs and exit.
110
111=item B<-n> I<module_name>
112
113Specifies a name to be used for the extension, e.g., S<-n RPC::DCE>
114
ead2a595 115=item B<-p> I<prefix>
116
117Specify a prefix which should be removed from the Perl function names, e.g., S<-p sec_rgy_>
118This sets up the XS B<PREFIX> keyword and removes the prefix from functions that are
119autoloaded via the C<constant()> mechansim.
120
121=item B<-s> I<sub1,sub2>
122
123Create a perl subroutine for the specified macros rather than autoload with the constant() subroutine.
124These macros are assumed to have a return type of B<char *>, e.g., S<-s sec_rgy_wildcard_name,sec_rgy_wildcard_sid>.
125
f508c652 126=item B<-v> I<version>
127
128Specify a version number for this extension. This version number is added
129to the templates. The default is 0.01.
3edbfbe5 130
760ac839 131=item B<-x>
132
133Automatically generate XSUBs basing on function declarations in the
134header file. The package C<C::Scan> should be installed. If this
135option is specified, the name of the header file may look like
136C<NAME1,NAME2>. In this case NAME1 is used instead of the specified string,
b73edd97 137but XSUBs are emitted only for the declarations included from file NAME2.
760ac839 138
5273d82d 139Note that some types of arguments/return-values for functions may
140result in XSUB-declarations/typemap-entries which need
141hand-editing. Such may be objects which cannot be converted from/to a
142pointer (like C<long long>), pointers to functions, or arrays.
143
3edbfbe5 144=back
145
146=head1 EXAMPLES
147
148
149 # Default behavior, extension is Rusers
150 h2xs rpcsvc/rusers
151
152 # Same, but extension is RUSERS
153 h2xs -n RUSERS rpcsvc/rusers
154
155 # Extension is rpcsvc::rusers. Still finds <rpcsvc/rusers.h>
156 h2xs rpcsvc::rusers
157
158 # Extension is ONC::RPC. Still finds <rpcsvc/rusers.h>
159 h2xs -n ONC::RPC rpcsvc/rusers
160
161 # Without constant() or AUTOLOAD
162 h2xs -c rpcsvc/rusers
163
164 # Creates templates for an extension named RPC
165 h2xs -cfn RPC
166
167 # Extension is ONC::RPC.
168 h2xs -cfn ONC::RPC
169
170 # Makefile.PL will look for library -lrpc in
171 # additional directory /opt/net/lib
172 h2xs rpcsvc/rusers -L/opt/net/lib -lrpc
173
ead2a595 174 # Extension is DCE::rgynbase
175 # prefix "sec_rgy_" is dropped from perl function names
176 h2xs -n DCE::rgynbase -p sec_rgy_ dce/rgynbase
177
178 # Extension is DCE::rgynbase
179 # prefix "sec_rgy_" is dropped from perl function names
180 # subroutines are created for sec_rgy_wildcard_name and sec_rgy_wildcard_sid
181 h2xs -n DCE::rgynbase -p sec_rgy_ \
182 -s sec_rgy_wildcard_name,sec_rgy_wildcard_sid dce/rgynbase
3edbfbe5 183
5273d82d 184 # Make XS without defines in perl.h, but with function declarations
760ac839 185 # visible from perl.h. Name of the extension is perl1.
186 # When scanning perl.h, define -DEXT=extern -DdEXT= -DINIT(x)=
187 # Extra backslashes below because the string is passed to shell.
5273d82d 188 # Note that a directory with perl header files would
189 # be added automatically to include path.
190 h2xs -xAn perl1 -F "-DEXT=extern -DdEXT= -DINIT\(x\)=" perl.h
760ac839 191
192 # Same with function declaration in proto.h as visible from perl.h.
5273d82d 193 h2xs -xAn perl2 perl.h,proto.h
760ac839 194
3edbfbe5 195=head1 ENVIRONMENT
196
197No environment variables are used.
198
199=head1 AUTHOR
200
201Larry Wall and others
202
203=head1 SEE ALSO
204
f508c652 205L<perl>, L<perlxstut>, L<ExtUtils::MakeMaker>, and L<AutoLoader>.
3edbfbe5 206
207=head1 DIAGNOSTICS
208
760ac839 209The usual warnings if it cannot read or write the files involved.
3edbfbe5 210
211=cut
212
b73edd97 213my( $H2XS_VERSION ) = ' $Revision: 1.18 $ ' =~ /\$Revision:\s+([^\s]+)/;
f508c652 214my $TEMPLATE_VERSION = '0.01';
a0d0e21e 215
216use Getopt::Std;
217
e1666bf5 218sub usage{
219 warn "@_\n" if @_;
b73edd97 220 die "h2xs [-AOPXcdfh] [-v version] [-n module_name] [-p prefix] [-s subs] [headerfile [extra_libraries]]
f508c652 221version: $H2XS_VERSION
3edbfbe5 222 -A Omit all autoloading facilities (implies -c).
b73edd97 223 -F Additional flags for C preprocessor (used with -x).
2920c5d2 224 -O Allow overwriting of a pre-existing extension directory.
f508c652 225 -P Omit the stub POD section.
2920c5d2 226 -X Omit the XS portion.
b73edd97 227 -c Omit the constant() function and specialised AUTOLOAD from the XS file.
228 -d Turn on debugging messages.
229 -f Force creation of the extension even if the C header does not exist.
230 -h Display this help message
231 -n Specify a name to use for the extension (recommended).
232 -p Specify a prefix which should be removed from the Perl function names.
233 -s Create subroutines for specified macros.
f508c652 234 -v Specify a version number for this extension.
760ac839 235 -x Autogenerate XSUBs using C::Scan.
e1666bf5 236extra_libraries
237 are any libraries that might be needed for loading the
238 extension, e.g. -lm would try to link in the math library.
f508c652 239";
e1666bf5 240}
a0d0e21e 241
a0d0e21e 242
b73edd97 243getopts("AF:OPXcdfhn:p:s:v:x") || usage;
a0d0e21e 244
e1666bf5 245usage if $opt_h;
f508c652 246
247if( $opt_v ){
248 $TEMPLATE_VERSION = $opt_v;
249}
e1666bf5 250$opt_c = 1 if $opt_A;
ead2a595 251%const_xsub = map { $_,1 } split(/,+/, $opt_s) if $opt_s;
a0d0e21e 252
e1666bf5 253$path_h = shift;
a0d0e21e 254$extralibs = "@ARGV";
e1666bf5 255
256usage "Must supply header file or module name\n"
257 unless ($path_h or $opt_n);
258
a0d0e21e 259
260if( $path_h ){
e1666bf5 261 $name = $path_h;
262 if( $path_h =~ s#::#/#g && $opt_n ){
263 warn "Nesting of headerfile ignored with -n\n";
264 }
265 $path_h .= ".h" unless $path_h =~ /\.h$/;
760ac839 266 $fullpath = $path_h;
267 $path_h =~ s/,.*$// if $opt_x;
ead2a595 268 if ($^O eq 'VMS') { # Consider overrides of default location
269 if ($path_h !~ m![:>\[]!) {
270 my($hadsys) = ($path_h =~ s!^sys/!!i);
271 if ($ENV{'DECC$System_Include'}) { $path_h = "DECC\$System_Include:$path_h"; }
272 elsif ($ENV{'DECC$Library_Include'}) { $path_h = "DECC\$Library_Include:$path_h"; }
273 elsif ($ENV{'GNU_CC_Include'}) { $path_h = 'GNU_CC_Include:' .
274 ($hadsys ? '[vms]' : '[000000]') . $path_h; }
275 elsif ($ENV{'VAXC$Include'}) { $path_h = "VAXC\$_Include:$path_h"; }
276 else { $path_h = "Sys\$Library:$path_h"; }
277 }
278 }
279 elsif ($^O eq 'os2') {
5273d82d 280 $path_h = "/usr/include/$path_h"
281 if $path_h !~ m#^([a-z]:)?[./]#i and -r "/usr/include/$path_h";
282 }
283 else {
284 $path_h = "/usr/include/$path_h"
285 if $path_h !~ m#^[./]# and -r "/usr/include/$path_h";
ead2a595 286 }
5273d82d 287
288 if (!$opt_c) {
289 die "Can't find $path_h\n" if ( ! $opt_f && ! -f $path_h );
290 # Scan the header file (we should deal with nested header files)
291 # Record the names of simple #define constants into const_names
292 # Function prototypes are not (currently) processed.
293 open(CH, "<$path_h") || die "Can't open $path_h: $!\n";
294 while (<CH>) {
b73edd97 295 if (/^#[ \t]*define\s+([\$\w]+)\b\s*[^("]/) {
296 print "Matched $_ ($1)\n" if $opt_d;
e1666bf5 297 $_ = $1;
298 next if /^_.*_h_*$/i; # special case, but for what?
760ac839 299 if (defined $opt_p) {
5273d82d 300 if (!/^$opt_p(\d)/) {
301 ++$prefix{$_} if s/^$opt_p//;
302 }
303 else {
304 warn "can't remove $opt_p prefix from '$_'!\n";
305 }
ead2a595 306 }
e1666bf5 307 $const_names{$_}++;
5273d82d 308 }
309 }
310 close(CH);
311 @const_names = sort keys %const_names;
e1666bf5 312 }
a0d0e21e 313}
314
e1666bf5 315
a0d0e21e 316$module = $opt_n || do {
317 $name =~ s/\.h$//;
318 if( $name !~ /::/ ){
319 $name =~ s#^.*/##;
320 $name = "\u$name";
321 }
322 $name;
323};
324
8e07c86e 325(chdir 'ext', $ext = 'ext/') if -d 'ext';
a0d0e21e 326
327if( $module =~ /::/ ){
328 $nested = 1;
329 @modparts = split(/::/,$module);
330 $modfname = $modparts[-1];
331 $modpname = join('/',@modparts);
332}
333else {
334 $nested = 0;
335 @modparts = ();
336 $modfname = $modpname = $module;
337}
338
339
2920c5d2 340if ($opt_O) {
341 warn "Overwriting existing $ext$modpname!!!\n" if -e $modpname;
342} else {
343 die "Won't overwrite existing $ext$modpname\n" if -e $modpname;
344}
c07a80fd 345if( $nested ){
346 $modpath = "";
347 foreach (@modparts){
348 mkdir("$modpath$_", 0777);
349 $modpath .= "$_/";
350 }
351}
a0d0e21e 352mkdir($modpname, 0777);
8e07c86e 353chdir($modpname) || die "Can't chdir $ext$modpname: $!\n";
a0d0e21e 354
5273d82d 355my %types_seen;
356my %std_types;
357my $fdecls;
358my $fdecls_parsed;
359
2920c5d2 360if( ! $opt_X ){ # use XS, unless it was disabled
361 open(XS, ">$modfname.xs") || die "Can't create $ext$modpname/$modfname.xs: $!\n";
5273d82d 362 if ($opt_x) {
363 require C::Scan; # Run-time directive
364 require Config; # Run-time directive
365 warn "Scanning typemaps...\n";
366 get_typemap();
367 my $c;
368 my $filter;
369 my $filename = $path_h;
370 my $addflags = $opt_F || '';
371 if ($fullpath =~ /,/) {
372 $filename = $`;
373 $filter = $';
374 }
375 warn "Scanning $filename for functions...\n";
376 $c = new C::Scan 'filename' => $filename, 'filename_filter' => $filter,
377 'add_cppflags' => $addflags;
378 $c->set('includeDirs' => ["$Config::Config{archlib}/CORE"]);
379
380 $fdecls_parsed = $c->get('parsed_fdecls');
381 $fdecls = $c->get('fdecls');
382 }
2920c5d2 383}
5273d82d 384
8e07c86e 385open(PM, ">$modfname.pm") || die "Can't create $ext$modpname/$modfname.pm: $!\n";
a0d0e21e 386
a0d0e21e 387$" = "\n\t";
8e07c86e 388warn "Writing $ext$modpname/$modfname.pm\n";
a0d0e21e 389
a0d0e21e 390print PM <<"END";
391package $module;
392
2920c5d2 393use strict;
394END
395
396if( $opt_X || $opt_c || $opt_A ){
397 # we won't have our own AUTOLOAD(), so won't have $AUTOLOAD
398 print PM <<'END';
399use vars qw($VERSION @ISA @EXPORT);
400END
401}
402else{
403 # we'll have an AUTOLOAD(), and it will have $AUTOLOAD and
404 # will want Carp.
405 print PM <<'END';
406use Carp;
407use vars qw($VERSION @ISA @EXPORT $AUTOLOAD);
408END
409}
410
411print PM <<'END';
412
a0d0e21e 413require Exporter;
2920c5d2 414END
415
416print PM <<"END" if ! $opt_X; # use DynaLoader, unless XS was disabled
a0d0e21e 417require DynaLoader;
3edbfbe5 418END
419
2920c5d2 420# require autoloader if XS is disabled.
421# if XS is enabled, require autoloader unless autoloading is disabled.
422if( $opt_X || (! $opt_A) ){
3edbfbe5 423 print PM <<"END";
424require AutoLoader;
425END
426}
427
2920c5d2 428if( $opt_X || ($opt_c && ! $opt_A) ){
3edbfbe5 429 # we won't have our own AUTOLOAD(), so we'll inherit it.
2920c5d2 430 if( ! $opt_X ) { # use DynaLoader, unless XS was disabled
431 print PM <<"END";
e1666bf5 432
a0d0e21e 433\@ISA = qw(Exporter AutoLoader DynaLoader);
3edbfbe5 434END
2920c5d2 435 }
436 else{
437 print PM <<"END";
438
439\@ISA = qw(Exporter AutoLoader);
440END
441 }
3edbfbe5 442}
443else{
444 # 1) we have our own AUTOLOAD(), so don't need to inherit it.
445 # or
446 # 2) we don't want autoloading mentioned.
2920c5d2 447 if( ! $opt_X ){ # use DynaLoader, unless XS was disabled
448 print PM <<"END";
3edbfbe5 449
450\@ISA = qw(Exporter DynaLoader);
451END
2920c5d2 452 }
453 else{
454 print PM <<"END";
455
456\@ISA = qw(Exporter);
457END
458 }
3edbfbe5 459}
e1666bf5 460
3edbfbe5 461print PM<<"END";
e1666bf5 462# Items to export into callers namespace by default. Note: do not export
463# names by default without a very good reason. Use EXPORT_OK instead.
464# Do not simply export all your public functions/methods/constants.
a0d0e21e 465\@EXPORT = qw(
e1666bf5 466 @const_names
a0d0e21e 467);
f508c652 468\$VERSION = '$TEMPLATE_VERSION';
469
e1666bf5 470END
471
2920c5d2 472print PM <<"END" unless $opt_c or $opt_X;
a0d0e21e 473sub AUTOLOAD {
3edbfbe5 474 # This AUTOLOAD is used to 'autoload' constants from the constant()
475 # XS function. If a constant is not found then control is passed
476 # to the AUTOLOAD in AutoLoader.
e1666bf5 477
2920c5d2 478 my \$constname;
a0d0e21e 479 (\$constname = \$AUTOLOAD) =~ s/.*:://;
2920c5d2 480 my \$val = constant(\$constname, \@_ ? \$_[0] : 0);
a0d0e21e 481 if (\$! != 0) {
482 if (\$! =~ /Invalid/) {
483 \$AutoLoader::AUTOLOAD = \$AUTOLOAD;
484 goto &AutoLoader::AUTOLOAD;
485 }
486 else {
2920c5d2 487 croak "Your vendor has not defined $module macro \$constname";
a0d0e21e 488 }
489 }
490 eval "sub \$AUTOLOAD { \$val }";
491 goto &\$AUTOLOAD;
492}
493
a0d0e21e 494END
a0d0e21e 495
2920c5d2 496if( ! $opt_X ){ # print bootstrap, unless XS is disabled
497 print PM <<"END";
f508c652 498bootstrap $module \$VERSION;
2920c5d2 499END
500}
501
502if( $opt_P ){ # if POD is disabled
503 $after = '__END__';
504}
505else {
506 $after = '=cut';
507}
508
509print PM <<"END";
a0d0e21e 510
e1666bf5 511# Preloaded methods go here.
a0d0e21e 512
2920c5d2 513# Autoload methods go after $after, and are processed by the autosplit program.
a0d0e21e 514
5151;
e1666bf5 516__END__
a0d0e21e 517END
a0d0e21e 518
f508c652 519$author = "A. U. Thor";
520$email = 'a.u.thor@a.galaxy.far.far.away';
521
5273d82d 522my $const_doc = '';
523my $fdecl_doc = '';
524if (@const_names and not $opt_P) {
525 $const_doc = <<EOD;
b73edd97 526\n=head1 Exported constants
5273d82d 527
528 @{[join "\n ", @const_names]}
529
530EOD
531}
532if (defined $fdecls and @$fdecls and not $opt_P) {
533 $fdecl_doc = <<EOD;
b73edd97 534\n=head1 Exported functions
5273d82d 535
536 @{[join "\n ", @$fdecls]}
537
538EOD
539}
540
f508c652 541$pod = <<"END" unless $opt_P;
542## Below is the stub of documentation for your module. You better edit it!
543#
544#=head1 NAME
545#
546#$module - Perl extension for blah blah blah
547#
548#=head1 SYNOPSIS
549#
550# use $module;
551# blah blah blah
552#
553#=head1 DESCRIPTION
554#
555#Stub documentation for $module was created by h2xs. It looks like the
556#author of the extension was negligent enough to leave the stub
557#unedited.
558#
559#Blah blah blah.
5273d82d 560#$const_doc$fdecl_doc
f508c652 561#=head1 AUTHOR
562#
563#$author, $email
564#
565#=head1 SEE ALSO
566#
567#perl(1).
568#
569#=cut
570END
571
572$pod =~ s/^\#//gm unless $opt_P;
573print PM $pod unless $opt_P;
574
a0d0e21e 575close PM;
576
e1666bf5 577
2920c5d2 578if( ! $opt_X ){ # print XS, unless it is disabled
8e07c86e 579warn "Writing $ext$modpname/$modfname.xs\n";
e1666bf5 580
a0d0e21e 581print XS <<"END";
4633a7c4 582#ifdef __cplusplus
583extern "C" {
584#endif
a0d0e21e 585#include "EXTERN.h"
586#include "perl.h"
587#include "XSUB.h"
4633a7c4 588#ifdef __cplusplus
589}
590#endif
a0d0e21e 591
592END
593if( $path_h ){
594 my($h) = $path_h;
595 $h =~ s#^/usr/include/##;
ead2a595 596 if ($^O eq 'VMS') { $h =~ s#.*vms\]#sys/# or $h =~ s#.*[:>\]]##; }
a0d0e21e 597print XS <<"END";
598#include <$h>
599
600END
601}
602
603if( ! $opt_c ){
604print XS <<"END";
605static int
606not_here(s)
607char *s;
608{
609 croak("$module::%s not implemented on this architecture", s);
610 return -1;
611}
612
613static double
614constant(name, arg)
615char *name;
616int arg;
617{
618 errno = 0;
619 switch (*name) {
620END
621
e1666bf5 622my(@AZ, @az, @under);
623
624foreach(@const_names){
625 @AZ = 'A' .. 'Z' if !@AZ && /^[A-Z]/;
626 @az = 'a' .. 'z' if !@az && /^[a-z]/;
627 @under = '_' if !@under && /^_/;
628}
629
a0d0e21e 630foreach $letter (@AZ, @az, @under) {
631
e1666bf5 632 last if $letter eq 'a' && !@const_names;
a0d0e21e 633
634 print XS " case '$letter':\n";
635 my($name);
e1666bf5 636 while (substr($const_names[0],0,1) eq $letter) {
637 $name = shift(@const_names);
ead2a595 638 $macro = $prefix{$name} ? "$opt_p$name" : $name;
639 next if $const_xsub{$macro};
a0d0e21e 640 print XS <<"END";
641 if (strEQ(name, "$name"))
ead2a595 642#ifdef $macro
643 return $macro;
a0d0e21e 644#else
645 goto not_there;
646#endif
647END
648 }
649 print XS <<"END";
650 break;
651END
652}
653print XS <<"END";
654 }
655 errno = EINVAL;
656 return 0;
657
658not_there:
659 errno = ENOENT;
660 return 0;
661}
662
e1666bf5 663END
664}
665
ead2a595 666$prefix = "PREFIX = $opt_p" if defined $opt_p;
e1666bf5 667# Now switch from C to XS by issuing the first MODULE declaration:
668print XS <<"END";
a0d0e21e 669
ead2a595 670MODULE = $module PACKAGE = $module $prefix
671
672END
673
674foreach (sort keys %const_xsub) {
675 print XS <<"END";
676char *
677$_()
678
679 CODE:
680#ifdef $_
681 RETVAL = $_;
682#else
683 croak("Your vendor has not defined the $module macro $_");
684#endif
685
686 OUTPUT:
687 RETVAL
a0d0e21e 688
e1666bf5 689END
ead2a595 690}
e1666bf5 691
692# If a constant() function was written then output a corresponding
693# XS declaration:
694print XS <<"END" unless $opt_c;
695
a0d0e21e 696double
697constant(name,arg)
698 char * name
699 int arg
700
701END
a0d0e21e 702
5273d82d 703my %seen_decl;
704
705
ead2a595 706sub print_decl {
707 my $fh = shift;
708 my $decl = shift;
709 my ($type, $name, $args) = @$decl;
5273d82d 710 return if $seen_decl{$name}++; # Need to do the same for docs as well?
711
ead2a595 712 my @argnames = map {$_->[1]} @$args;
713 my @argtypes = map { normalize_type( $_->[0] ) } @$args;
5273d82d 714 my @argarrays = map { $_->[4] || '' } @$args;
ead2a595 715 my $numargs = @$args;
716 if ($numargs and $argtypes[-1] eq '...') {
717 $numargs--;
718 $argnames[-1] = '...';
719 }
720 local $" = ', ';
721 $type = normalize_type($type);
722
723 print $fh <<"EOP";
724
725$type
726$name(@argnames)
727EOP
728
729 for $arg (0 .. $numargs - 1) {
730 print $fh <<"EOP";
5273d82d 731 $argtypes[$arg] $argnames[$arg]$argarrays[$arg]
ead2a595 732EOP
733 }
734}
735
5273d82d 736# Should be called before any actual call to normalize_type().
737sub get_typemap {
738 # We do not want to read ./typemap by obvios reasons.
739 my @tm = qw(../../../typemap ../../typemap ../typemap);
740 my $stdtypemap = "$Config::Config{privlib}/ExtUtils/typemap";
741 unshift @tm, $stdtypemap;
742 my $proto_re = "[" . quotemeta('\$%&*@;') . "]" ;
743 my $image;
744
745 foreach $typemap (@tm) {
746 next unless -e $typemap ;
747 # skip directories, binary files etc.
748 warn " Scanning $typemap\n";
749 warn("Warning: ignoring non-text typemap file '$typemap'\n"), next
750 unless -T $typemap ;
751 open(TYPEMAP, $typemap)
752 or warn ("Warning: could not open typemap file '$typemap': $!\n"), next;
753 my $mode = 'Typemap';
754 while (<TYPEMAP>) {
755 next if /^\s*\#/;
756 if (/^INPUT\s*$/) { $mode = 'Input'; next; }
757 elsif (/^OUTPUT\s*$/) { $mode = 'Output'; next; }
758 elsif (/^TYPEMAP\s*$/) { $mode = 'Typemap'; next; }
759 elsif ($mode eq 'Typemap') {
760 next if /^\s*($|\#)/ ;
761 if ( ($type, $image) =
762 /^\s*(.*?\S)\s+(\S+)\s*($proto_re*)\s*$/o
763 # This may reference undefined functions:
764 and not ($image eq 'T_PACKED' and $typemap eq $stdtypemap)) {
765 normalize_type($type);
766 }
767 }
768 }
769 close(TYPEMAP) or die "Cannot close $typemap: $!";
770 }
771 %std_types = %types_seen;
772 %types_seen = ();
773}
774
ead2a595 775
776sub normalize_type {
5273d82d 777 my $ignore_mods = '(?:\b(?:__const__|static|inline|__inline__)\b\s*)*';
ead2a595 778 my $type = shift;
779 $type =~ s/$ignore_mods//go;
5273d82d 780 $type =~ s/([\]\[()])/ \1 /g;
ead2a595 781 $type =~ s/\s+/ /g;
782 $type =~ s/\s+$//;
783 $type =~ s/^\s+//;
784 $type =~ s/\b\*/ */g;
785 $type =~ s/\*\b/* /g;
786 $type =~ s/\*\s+(?=\*)/*/g;
5273d82d 787 $types_seen{$type}++
788 unless $type eq '...' or $type eq 'void' or $std_types{$type};
ead2a595 789 $type;
790}
791
792if ($opt_x) {
5273d82d 793 for $decl (@$fdecls_parsed) { print_decl(\*XS, $decl) }
ead2a595 794}
795
a0d0e21e 796close XS;
5273d82d 797
798if (%types_seen) {
799 my $type;
800 warn "Writing $ext$modpname/typemap\n";
801 open TM, ">typemap" or die "Cannot open typemap file for write: $!";
802
803 for $type (keys %types_seen) {
804 print TM $type, "\t" x (6 - int((length $type)/8)), "T_PTROBJ\n"
805 }
806
807 close TM or die "Cannot close typemap file for write: $!";
808}
809
2920c5d2 810} # if( ! $opt_X )
e1666bf5 811
8e07c86e 812warn "Writing $ext$modpname/Makefile.PL\n";
813open(PL, ">Makefile.PL") || die "Can't create $ext$modpname/Makefile.PL: $!\n";
a0d0e21e 814
a0d0e21e 815print PL <<'END';
816use ExtUtils::MakeMaker;
817# See lib/ExtUtils/MakeMaker.pm for details of how to influence
42793c05 818# the contents of the Makefile that is written.
a0d0e21e 819END
42793c05 820print PL "WriteMakefile(\n";
821print PL " 'NAME' => '$module',\n";
c07a80fd 822print PL " 'VERSION_FROM' => '$modfname.pm', # finds \$VERSION\n";
2920c5d2 823if( ! $opt_X ){ # print C stuff, unless XS is disabled
824 print PL " 'LIBS' => ['$extralibs'], # e.g., '-lm' \n";
825 print PL " 'DEFINE' => '', # e.g., '-DHAVE_SOMETHING' \n";
826 print PL " 'INC' => '', # e.g., '-I/usr/include/other' \n";
827}
a0d0e21e 828print PL ");\n";
f508c652 829close(PL) || die "Can't close $ext$modpname/Makefile.PL: $!\n";
830
831warn "Writing $ext$modpname/test.pl\n";
832open(EX, ">test.pl") || die "Can't create $ext$modpname/test.pl: $!\n";
833print EX <<'_END_';
834# Before `make install' is performed this script should be runnable with
835# `make test'. After `make install' it should work as `perl test.pl'
836
837######################### We start with some black magic to print on failure.
838
839# Change 1..1 below to 1..last_test_to_print .
840# (It may become useful if the test is moved to ./t subdirectory.)
841
5ae7f1db 842BEGIN { $| = 1; print "1..1\n"; }
f508c652 843END {print "not ok 1\n" unless $loaded;}
844_END_
845print EX <<_END_;
846use $module;
847_END_
848print EX <<'_END_';
849$loaded = 1;
850print "ok 1\n";
851
852######################### End of black magic.
853
854# Insert your test code below (better if it prints "ok 13"
855# (correspondingly "not ok 13") depending on the success of chunk 13
856# of the test code):
e1666bf5 857
f508c652 858_END_
859close(EX) || die "Can't close $ext$modpname/test.pl: $!\n";
a0d0e21e 860
c07a80fd 861warn "Writing $ext$modpname/Changes\n";
862open(EX, ">Changes") || die "Can't create $ext$modpname/Changes: $!\n";
863print EX "Revision history for Perl extension $module.\n\n";
864print EX "$TEMPLATE_VERSION ",scalar localtime,"\n";
865print EX "\t- original version; created by h2xs $H2XS_VERSION\n\n";
866close(EX) || die "Can't close $ext$modpname/Changes: $!\n";
867
868warn "Writing $ext$modpname/MANIFEST\n";
5ae7f1db 869open(MANI,'>MANIFEST') or die "Can't create MANIFEST: $!";
870@files = <*>;
871if (!@files) {
872 eval {opendir(D,'.');};
873 unless ($@) { @files = readdir(D); closedir(D); }
874}
875if (!@files) { @files = map {chomp && $_} `ls`; }
55497cff 876if ($^O eq 'VMS') {
877 foreach (@files) {
878 # Clip trailing '.' for portability -- non-VMS OSs don't expect it
879 s%\.$%%;
880 # Fix up for case-sensitive file systems
881 s/$modfname/$modfname/i && next;
882 $_ = "\U$_" if $_ eq 'manifest' or $_ eq 'changes';
883 $_ = 'Makefile.PL' if $_ = 'makefile.pl';
884 }
885}
5ae7f1db 886print MANI join("\n",@files);
887close MANI;
40000a8c 888!NO!SUBS!
4633a7c4 889
890close OUT or die "Can't close $file: $!";
891chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
892exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';