package ExtUtils::Constant;
use vars qw (@ISA $VERSION %XS_Constant %XS_TypeSet @EXPORT_OK %EXPORT_TAGS);
-$VERSION = '0.07';
+$VERSION = '0.10';
=head1 NAME
A fixed length thing, given as a [pointer, length] pair. If you know the
length of a string at compile time you may use this instead of I<PV>
-=item PVN
+=item SV
A B<mortal> SV.
=cut
-require 5.006; # I think, for [:cntrl:] in REGEXP
-use warnings;
+if ($] >= 5.006) {
+ eval "use warnings; 1" or die $@;
+}
use strict;
use Carp;
%EXPORT_TAGS = ( 'all' => [ qw(
XS_constant constant_types return_clause memEQ_clause C_stringify
- C_constant autoload WriteConstants
+ C_constant autoload WriteConstants WriteMakefileSnippet
) ] );
@EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
s/\t/\\t/g;
s/\f/\\f/g;
s/\a/\\a/g;
- s/([[:cntrl:]])/sprintf "\\%03o", ord $1/ge;
- s/\177/\\177/g; # DEL doesn't seem to be a [:cntrl:]
+ unless ($] < 5.006) {
+ # This will elict a warning on 5.005_03 about [: :] being reserved unless
+ # I cheat
+ my $cheat = '([[:^print:]])';
+ s/$cheat/sprintf "\\%03o", ord $1/ge;
+ } else {
+ require POSIX;
+ s/([^A-Za-z0-9_])/POSIX::isprint($1) ? $1 : sprintf "\\%03o", ord $1/ge;
+ }
$_;
}
#ifndef NVTYPE
typedef double NV; /* 5.6 and later define NVTYPE, and typedef NV to it. */
#endif
+#ifndef aTHX_
+#define aTHX_ /* 5.6 or later define this for threading support. */
+#endif
+#ifndef pTHX_
+#define pTHX_ /* 5.6 or later define this for threading support. */
+#endif
EOT
return join '', @lines;
return $clause;
}
-=item return_clause VALUE, TYPE, INDENT, MACRO, DEFAULT, PRE, POST, PRE, POST
+=item return_clause
+
+return_clause VALUE, TYPE, INDENT, MACRO, DEFAULT, PRE, POST, PRE, POST
A function to return a suitable C<#ifdef> clause. I<MACRO> defaults to
I<VALUE> when not defined. If I<TYPE> is aggregate (eg I<PVN> expects both
##ifdef thingy
if (ref $macro) {
$clause = $macro->[0];
- } else {
+ } elsif ($macro ne "1") {
$clause = "#ifdef $macro\n";
}
$clause .= assign ($indent, $type, $pre, $post,
ref $value ? @$value : $value);
- ##else
- $clause .= "#else\n";
+ if (ref $macro or $macro ne "1") {
+ ##else
+ $clause .= "#else\n";
- # return PERL_constant_NOTDEF;
- if (!defined $default) {
- $clause .= "${indent}return PERL_constant_NOTDEF;\n";
- } else {
- my @default = ref $default ? @$default : $default;
- $type = shift @default;
- $clause .= assign ($indent, $type, $def_pre, $def_post, @default);
- }
+ # return PERL_constant_NOTDEF;
+ if (!defined $default) {
+ $clause .= "${indent}return PERL_constant_NOTDEF;\n";
+ } else {
+ my @default = ref $default ? @$default : $default;
+ $type = shift @default;
+ $clause .= assign ($indent, $type, $def_pre, $def_post, @default);
+ }
- ##endif
- if (ref $macro) {
- $clause .= $macro->[1];
- } else {
- $clause .= "#endif\n";
+ ##endif
+ if (ref $macro) {
+ $clause .= $macro->[1];
+ } else {
+ $clause .= "#endif\n";
+ }
}
return $clause
}
=item params WHAT
An internal function. I<WHAT> should be a hashref of types the constant
-function will return. I<params> returns the list of flags C<$use_iv, $use_nv,
-$use_pv, $use_sv> to show which combination of pointers will be needed in the
-C argument list.
+function will return. I<params> returns a hashref keyed IV NV PV SV to show
+which combination of pointers will be needed in the C argument list.
=cut
foreach (sort keys %$what) {
warn "ExtUtils::Constant doesn't know how to handle values of type $_" unless defined $XS_Constant{$_};
}
- my $use_iv = $what->{IV} || $what->{UV} || $what->{PVN};
- my $use_nv = $what->{NV};
- my $use_pv = $what->{PV} || $what->{PVN};
- my $use_sv = $what->{SV};
- return ($use_iv, $use_nv, $use_pv, $use_sv);
+ my $params = {};
+ $params->{IV} = 1 if $what->{IV} || $what->{UV} || $what->{PVN};
+ $params->{NV} = 1 if $what->{NV};
+ $params->{PV} = 1 if $what->{PV} || $what->{PVN};
+ $params->{SV} = 1 if $what->{SV};
+ return $params;
}
=item dump_names
-dump_names PACKAGE, SUBNAME, DEFAULT_TYPE, TYPES, INDENT, BREAKOUT, ITEM...
+dump_names DEFAULT_TYPE, TYPES, INDENT, OPTIONS, ITEM...
An internal function to generate the embedded perl code that will regenerate
-the constant subroutines. Parameters are the same as for C_constant.
+the constant subroutines. I<DEFAULT_TYPE>, I<TYPES> and I<ITEM>s are the
+same as for C_constant. I<INDENT> is treated as number of spaces to indent
+by. I<OPTIONS> is a hashref of options. Currently only C<declare_types> is
+recognised. If the value is true a C<$types> is always declared in the perl
+code generated, if defined and false never declared, and if undefined C<$types>
+is only declared if the values in I<TYPES> as passed in cannot be inferred from
+I<DEFAULT_TYPES> and the I<ITEM>s.
=cut
sub dump_names {
- my ($package, $subname, $default_type, $what, $indent, $breakout, @items)
- = @_;
- my (@simple, @complex);
+ my ($default_type, $what, $indent, $options, @items) = @_;
+ my $declare_types = $options->{declare_types};
+ $indent = ' ' x ($indent || 0);
+
+ my $result;
+ my (@simple, @complex, %used_types);
foreach (@items) {
- my $type = $_->{type} || $default_type;
+ my $type;
+ if (ref $_) {
+ $type = $_->{type} || $default_type;
+ } else {
+ $_ = {name=>$_};
+ $type = $default_type;
+ }
+ $used_types{$type}++;
if ($type eq $default_type and 0 == ($_->{name} =~ tr/A-Za-z0-9_//c)
and !defined ($_->{macro}) and !defined ($_->{value})
and !defined ($_->{default}) and !defined ($_->{pre})
push @complex, $_;
}
}
- my $result = <<"EOT";
- /* When generated this function returned values for the list of names given
- in this section of perl code. Rather than manually editing these functions
- to add or remove constants, which would result in this comment and section
- of code becoming inaccurate, we recommend that you edit this section of
- code, and use it to regenerate a new set of constant functions which you
- then use to replace the originals.
- Regenerate these constant functions by feeding this entire source file to
- perl -x
-
-#!$^X -w
-use ExtUtils::Constant qw (constant_types C_constant XS_constant);
-
-EOT
- $result .= 'my $types = {map {($_, 1)} qw(' . join (" ", sort keys %$what)
- . ")};\n";
- $result .= wrap ("my \@names = (qw(",
- " ", join (" ", sort @simple) . ")");
+ if (!defined $declare_types) {
+ # Do they pass in any types we weren't already using?
+ foreach (keys %$what) {
+ next if $used_types{$_};
+ $declare_types++; # Found one in $what that wasn't used.
+ last; # And one is enough to terminate this loop
+ }
+ }
+ if ($declare_types) {
+ $result = $indent . 'my $types = {map {($_, 1)} qw('
+ . join (" ", sort keys %$what) . ")};\n";
+ }
+ $result .= wrap ($indent . "my \@names = (qw(",
+ $indent . " ", join (" ", sort @simple) . ")");
if (@complex) {
foreach my $item (sort {$a->{name} cmp $b->{name}} @complex) {
my $name = C_stringify $item->{name};
- my $line = ",\n {name=>\"$name\"";
+ my $line = ",\n$indent {name=>\"$name\"";
$line .= ", type=>\"$item->{type}\"" if defined $item->{type};
foreach my $thing (qw (macro value default pre post def_pre def_post)) {
my $value = $item->{$thing};
}
$result .= ");\n";
+ $result;
+}
+
+
+=item dogfood
+
+dogfood PACKAGE, SUBNAME, DEFAULT_TYPE, TYPES, INDENT, BREAKOUT, ITEM...
+
+An internal function to generate the embedded perl code that will regenerate
+the constant subroutines. Parameters are the same as for C_constant.
+
+=cut
+
+sub dogfood {
+ my ($package, $subname, $default_type, $what, $indent, $breakout, @items)
+ = @_;
+ my $result = <<"EOT";
+ /* When generated this function returned values for the list of names given
+ in this section of perl code. Rather than manually editing these functions
+ to add or remove constants, which would result in this comment and section
+ of code becoming inaccurate, we recommend that you edit this section of
+ code, and use it to regenerate a new set of constant functions which you
+ then use to replace the originals.
+
+ Regenerate these constant functions by feeding this entire source file to
+ perl -x
+
+#!$^X -w
+use ExtUtils::Constant qw (constant_types C_constant XS_constant);
+
+EOT
+ $result .= dump_names ($default_type, $what, 0, {declare_types=>1}, @items);
$result .= <<'EOT';
print constant_types(); # macro defs
to be used to determine if a constant is to be defined.
+A "macro" 1 signals that the constant is always defined, so the C<#if>/C<#endif>
+test is omitted.
+
=item default
Default value to use (instead of C<croak>ing with "your vendor has not
sub C_constant {
my ($package, $subname, $default_type, $what, $indent, $breakout, @items)
= @_;
- my $namelen;
- if (ref $breakout) {
- $namelen = $$breakout;
- } else {
- $breakout ||= 3;
- }
$package ||= 'Foo';
$subname ||= 'constant';
# I'm not using this. But a hashref could be used for full formatting without
# breaking this API
# $indent ||= 0;
- $default_type ||= 'IV';
- if (!ref $what) {
- # Convert line of the form IV,UV,NV to hash
- $what = {map {$_ => 1} split /,\s*/, ($what || '')};
- # Figure out what types we're dealing with, and assign all unknowns to the
- # default type
- }
- my %items;
- foreach (@items) {
- my $name;
- if (ref $_) {
- my $orig = $_;
- # Make a copy which is a normalised version of the ref passed in.
- $name = $_->{name};
- my ($type, $macro, $value) = @$_{qw (type macro value)};
- $type ||= $default_type;
- $what->{$type} = 1;
- $_ = {name=>$name, type=>$type};
-
- undef $macro if defined $macro and $macro eq $name;
- $_->{macro} = $macro if defined $macro;
- undef $value if defined $value and $value eq $name;
- $_->{value} = $value if defined $value;
- foreach my $key (qw(default pre post def_pre def_post)) {
- my $value = $orig->{$key};
- $_->{$key} = $value if defined $value;
- # warn "$key $value";
- }
- } else {
- $name = $_;
- $_ = {name=>$_, type=>$default_type};
- $what->{$default_type} = 1;
+
+ my ($namelen, $items);
+ if (ref $breakout) {
+ # We are called recursively. We trust @items to be normalised, $what to
+ # be a hashref, and pinch %$items from our parent to save recalculation.
+ ($namelen, $items) = @$breakout;
+ } else {
+ $breakout ||= 3;
+ $default_type ||= 'IV';
+ if (!ref $what) {
+ # Convert line of the form IV,UV,NV to hash
+ $what = {map {$_ => 1} split /,\s*/, ($what || '')};
+ # Figure out what types we're dealing with, and assign all unknowns to the
+ # default type
}
- warn "ExtUtils::Constant doesn't know how to handle values of type $_ used in macro $name" unless defined $XS_Constant{$_->{type}};
- if (exists $items{$name}) {
- die "Multiple definitions for macro $name";
+ foreach (@items) {
+ my $name;
+ if (ref $_) {
+ my $orig = $_;
+ # Make a copy which is a normalised version of the ref passed in.
+ $name = $_->{name};
+ my ($type, $macro, $value) = @$_{qw (type macro value)};
+ $type ||= $default_type;
+ $what->{$type} = 1;
+ $_ = {name=>$name, type=>$type};
+
+ undef $macro if defined $macro and $macro eq $name;
+ $_->{macro} = $macro if defined $macro;
+ undef $value if defined $value and $value eq $name;
+ $_->{value} = $value if defined $value;
+ foreach my $key (qw(default pre post def_pre def_post)) {
+ my $value = $orig->{$key};
+ $_->{$key} = $value if defined $value;
+ # warn "$key $value";
+ }
+ } else {
+ $name = $_;
+ $_ = {name=>$_, type=>$default_type};
+ $what->{$default_type} = 1;
+ }
+ warn "ExtUtils::Constant doesn't know how to handle values of type $_ used in macro $name" unless defined $XS_Constant{$_->{type}};
+ if (exists $items->{$name}) {
+ die "Multiple definitions for macro $name";
+ }
+ $items->{$name} = $_;
}
- $items{$name} = $_;
}
- my ($use_iv, $use_nv, $use_pv, $use_sv) = params ($what);
+ my $params = params ($what);
my ($body, @subs) = "static int\n$subname (pTHX_ const char *name";
$body .= ", STRLEN len" unless defined $namelen;
- $body .= ", IV *iv_return" if $use_iv;
- $body .= ", NV *nv_return" if $use_nv;
- $body .= ", const char **pv_return" if $use_pv;
- $body .= ", SV **sv_return" if $use_sv;
+ $body .= ", IV *iv_return" if $params->{IV};
+ $body .= ", NV *nv_return" if $params->{NV};
+ $body .= ", const char **pv_return" if $params->{PV};
+ $body .= ", SV **sv_return" if $params->{SV};
$body .= ") {\n";
if (defined $namelen) {
my $comment = 'When generated this function returned values for the list'
. ' of names given here. However, subsequent manual editing may have'
. ' added or removed some.';
- $body .= switch_clause (2, $comment, $namelen, \%items, @items);
+ $body .= switch_clause (2, $comment, $namelen, $items, @items);
} else {
# We are the top level.
$body .= " /* Initially switch on the length of the name. */\n";
- $body .= dump_names ($package, $subname, $default_type, $what, $indent,
- $breakout, @items);
+ $body .= dogfood ($package, $subname, $default_type, $what, $indent,
+ $breakout, @items);
$body .= " switch (len) {\n";
# Need to group names of the same length
my @by_length;
$default, $pre, $post, $def_pre, $def_post);
$body .= " }\n";
} elsif (@{$by_length[$i]} < $breakout) {
- $body .= switch_clause (4, '', $i, \%items, @{$by_length[$i]});
+ $body .= switch_clause (4, '', $i, $items, @{$by_length[$i]});
} else {
- push @subs, C_constant ($package, "${subname}_$i", $default_type,
- $what, $indent, \$i, @{$by_length[$i]});
+ # Only use the minimal set of parameters actually needed by the types
+ # of the names of this length.
+ my $what = {};
+ foreach (@{$by_length[$i]}) {
+ $what->{$_->{type}} = 1;
+ }
+ $params = params ($what);
+ push @subs, C_constant ($package, "${subname}_$i", $default_type, $what,
+ $indent, [$i, $items], @{$by_length[$i]});
$body .= " return ${subname}_$i (aTHX_ name";
- $body .= ", iv_return" if $use_iv;
- $body .= ", nv_return" if $use_nv;
- $body .= ", pv_return" if $use_pv;
- $body .= ", sv_return" if $use_sv;
+ $body .= ", iv_return" if $params->{IV};
+ $body .= ", nv_return" if $params->{NV};
+ $body .= ", pv_return" if $params->{PV};
+ $body .= ", sv_return" if $params->{SV};
$body .= ");\n";
}
$body .= " break;\n";
# Convert line of the form IV,UV,NV to hash
$what = {map {$_ => 1} split /,\s*/, ($what)};
}
- my ($use_iv, $use_nv, $use_pv, $use_sv) = params ($what);
+ my $params = params ($what);
my $type;
my $xs = <<"EOT";
int type;
EOT
- if ($use_iv) {
+ if ($params->{IV}) {
$xs .= " IV iv;\n";
} else {
$xs .= " /* IV\t\tiv;\tUncomment this if you need to return IVs */\n";
}
- if ($use_nv) {
+ if ($params->{NV}) {
$xs .= " NV nv;\n";
} else {
$xs .= " /* NV\t\tnv;\tUncomment this if you need to return NVs */\n";
}
- if ($use_pv) {
+ if ($params->{PV}) {
$xs .= " const char *pv;\n";
} else {
$xs .=
PPCODE:
EOT
- if ($use_iv xor $use_nv) {
+ if ($params->{IV} xor $params->{NV}) {
$xs .= << "EOT";
/* Change this to $C_subname(aTHX_ s, len, &iv, &nv);
if you need to return both NVs and IVs */
EOT
}
$xs .= " type = $C_subname(aTHX_ s, len";
- $xs .= ', &iv' if $use_iv;
- $xs .= ', &nv' if $use_nv;
- $xs .= ', &pv' if $use_pv;
- $xs .= ', &sv' if $use_sv;
+ $xs .= ', &iv' if $params->{IV};
+ $xs .= ', &nv' if $params->{NV};
+ $xs .= ', &pv' if $params->{PV};
+ $xs .= ', &sv' if $params->{SV};
$xs .= ");\n";
$xs .= << "EOT";
}
+=item WriteMakefileSnippet
+
+WriteMakefileSnippet ATTRIBUTE =E<gt> VALUE [, ...]
+
+An function to generate perl code for Makefile.PL that will regenerate
+the constant subroutines. Parameters are named as passed to C<WriteConstants>,
+with the addition of C<INDENT> to specify the number of leading spaces
+(default 2).
+
+Currently only C<INDENT>, C<NAME>, C<DEFAULT_TYPE>, C<NAMES>, C<C_FILE> and
+C<XS_FILE> are recognised.
+
+=cut
+
+sub WriteMakefileSnippet {
+ my %args = @_;
+ my $indent = $args{INDENT} || 2;
+
+ my $result = <<"EOT";
+ExtUtils::Constant::WriteConstants(
+ NAME => '$args{NAME}',
+ NAMES => \\\@names,
+ DEFAULT_TYPE => '$args{DEFAULT_TYPE}',
+EOT
+ foreach (qw (C_FILE XS_FILE)) {
+ next unless exists $args{$_};
+ $result .= sprintf " %-12s => '%s',\n",
+ $_, $args{$_};
+ }
+ $result .= <<'EOT';
+ );
+EOT
+
+ $result =~ s/^/' 'x$indent/gem;
+ return dump_names ($args{DEFAULT_TYPE}, undef, $indent, undef,
+ @{$args{NAMES}})
+ . $result;
+}
+
=item WriteConstants ATTRIBUTE =E<gt> VALUE [, ...]
Writes a file of C code and a file of XS code which you should C<#include>
=item SUBNAME
The perl visible name of the XS subroutine generated which will return the
-constants. The default is C<constant>.
+constants. The default is C<constant>.
=item C_SUBNAME