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