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