blib/lib/ExtUtils/H2PM.pm

filter: Statementstmt 87.2% 218 / 250 Branchbran 61.9% 52 / 84 Conditioncond 60.0% 9 / 15 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 94.2% 33 / 35 Podpod 82.3% 14 / 17 totaltotal 80.2% 329 / 410 CC 49 SCAR 20.8
CC
49
Cov
80%
__ANON__
29.4
__ANON__
11.1
gen_perl
10.7
CRAP 8.0
↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2010-2023 -- leonerd@leonerd.org.uk
5  
6 package ExtUtils::H2PM 0.11;
7  
8 10 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 our @EXPORT = qw(
15 module
16 include
17 constant
18  
19 structure
20 member_numeric
21 member_strarray
22 member_constant
23  
24 no_export use_export use_export_ok
25  
26 gen_output
27 write_output
28  
29 include_path
30 define
31 );
32  
33 10 ▶use ExtUtils::CBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 10 ▶use List::Util 1.39 qw( pairs ); # ->key
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 =head1 NAME
38  
39 C<ExtUtils::H2PM> - automatically generate perl modules to wrap C header files
40  
41 =head1 DESCRIPTION
42  
43 This module assists in generating wrappers around system functionallity, such
44 as C<socket()> types or C<ioctl()> calls, where the only interesting features
45 required are the values of some constants or layouts of structures normally
46 only known to the C header files. Rather than writing an entire XS module just
47 to contain some constants and pack/unpack functions, this module allows the
48 author to generate, at module build time, a pure perl module containing
49 constant declarations and structure utility functions. The module then
50 requires no XS module to be loaded at run time.
51  
52 In comparison to F<h2ph>, C<C::Scan::Constants>, and so on, this module works
53 by generating a small C program containing C<printf()> lines to output the
54 values of the constants, compiling it, and running it. This allows it to
55 operate without needing tricky syntax parsing or guessing of the contents of
56 C header files.
57  
58 It can also automatically build pack/unpack functions for simple structure
59 layouts, whose members are all simple integer or character array fields.
60 It is not intended as a full replacement of arbitrary code written in XS
61 modules. If structures should contain pointers, or require special custom
62 handling, then likely an XS module will need to be written.
63  
64 =cut
65  
66 10 my $output = "";
67  
68 10 my @preamble;
69 my @fragments;
70  
71 10 my $done_carp;
72  
73 10 my @perlcode;
74 10 my @genblocks;
75  
76 10 my $export_mode; use_export_ok();
77 10 my @exports; my @exports_ok;
78  
79 =head1 FUNCTIONS
80  
81 =cut
82  
83 sub push_export
84 26 ▶{
Subroutine push_export: called CC=3 SCAR=11.4
Pod undocumented
85 26 my $name = shift;
86  
87 26 ▶ if( $export_mode eq "OK" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($export_mode eq 'OK') { } 23 3 26
elsif ($export_mode) { } 0 3 3
88 23 push @exports_ok, $name;
89 }
90 elsif( $export_mode ) {
91 0 push @exports, $name;
92 }
93 }
94  
95 =head2 module
96  
97 module $name;
98  
99 Sets the name of the perl module to generate. This will apply a C<package>
100 header.
101  
102 =cut
103  
104 10 my $modulename;
105 sub module
106 18 ▶{
Subroutine module: called CC=2 SCAR=7.0
Pod documented
107 18 $modulename = shift;
108  
109 18 ▶ $output .= gen_perl() if @fragments;
Branch: 1 branch
BranchTrueFalseTotal
if @fragments 0 18 18
110  
111 18 $output .= "package $modulename;\n" .
112 "# This module was generated automatically by ExtUtils::H2PM from $0\n" .
113 "\n";
114  
115 18 undef $done_carp;
116 }
117  
118 =head2 include
119  
120 include $file;
121  
122 Adds a file to the list of headers which will be included by the C program, to
123 obtain the constants or structures from
124  
125 =cut
126  
127 sub include
128 18 ▶{
Subroutine include: called CC=2 SCAR=7.6
Pod documented
129 18 my ( $file, %params ) = @_;
130  
131 # undocumented but useful for testing
132 18 ▶ if( $params{local} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'local'}) { } 18 0 18
133 18 push @preamble, qq[#include "$file"];
134 }
135 else {
136 0 push @preamble, "#include <$file>";
137 }
138 }
139  
140 # undocumented so far
141 sub perlcode
142 0 ▶{
Subroutine perlcode: not called CC=1 SCAR=6.9
Pod undocumented
143 0 my ( $code ) = @_;
144 0 push @perlcode, $code;
145 }
146  
147 =head2 constant
148  
149 constant $name, %args;
150  
151 Adds a numerical constant.
152  
153 The following additional named arguments are also recognised:
154  
155 =over 4
156  
157 =item * name => STRING
158  
159 Use the given name for the generated constant function. If not specified, the
160 C name for the constant will be used.
161  
162 =item * ifdef => STRING
163  
164 If present, guard the constant with an C<#ifdef STRING> preprocessor macro. If
165 the given string is not defined, no constant will be generated.
166  
167 =back
168  
169 =cut
170  
171 sub constant
172 9 ▶{
Subroutine constant: called CC=3 SCAR=11.0
Pod documented
173 9 my $constname = shift;
174 9 my %args = @_;
175  
176 9 ▶ my $name = $args{name} || $constname;
Condition66%2/3
$args{'name'} || $constname
l!l&&r!l&&!r
180
MC/DC0%0/2
$args{'name'} || $constname
$args{'name'} $constname
Truth table66%2/3
A || B
A= $args{'name'}B= $constname
ABresult
1X1
011
000
177  
178 9 push @fragments, qq{ printf("$constname=%ld\\n", (long)$constname);};
179  
180 9 ▶ if( my $symbol = $args{ifdef} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $symbol = $args{"ifdef"}) 1 8 9
181 1 $fragments[-1] = "#ifdef $symbol\n$fragments[-1]\n#endif";
182 }
183  
184 9 ▶ push @genblocks, [ $constname => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
185 9 my ( $result ) = @_;
186 9 ▶ return () unless defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 1 8 9
187  
188 8 push_export $name;
189 8 "use constant $name => $result;";
190 9 } ];
191 }
192  
193 =head2 structure
194  
195 structure $name, %args;
196  
197 Adds a structure definition. This requires a named argument, C<members>. This
198 should be an ARRAY ref containing an even number of name-definition pairs. The
199 first of each pair should be a member name. The second should be one of the
200 following structure member definitions.
201  
202 The following additional named arguments are also recognised:
203  
204 =over 4
205  
206 =item * pack_func => STRING
207  
208 =item * unpack_func => STRING
209  
210 Use the given names for the generated pack or unpack functions.
211  
212 =item * with_tail => BOOL
213  
214 If true, the structure is a header with more data behind it. The pack function
215 takes an optional extra string value for the data tail, and the unpack
216 function will return an extra string value containing it.
217  
218 =item * no_length_check => BOOL
219  
220 If true, the generated unpack function will not first check the length of its
221 argument before attempting to unpack it. If the buffer is not long enough to
222 unpack all the required values, the remaining ones will not be returned. This
223 may be useful, for example, in cases where various versions of a structure
224 have been designed, later versions adding extra members, but where the exact
225 version found may not be easy to determine beforehand.
226  
227 =item * arg_style => STRING
228  
229 Defines the style in which the functions take arguments or return values.
230 Defaults to C<list>, which take or return a list of values in the given order.
231 The other allowed value is C<hashref>, where the pack function takes a HASH
232 reference and the unpack function returns one. Each will consist of keys named
233 after the structure members. If a data tail is included, it will use the hash
234 key of C<_tail>.
235  
236 =item * ifdef => STRING
237  
238 If present, guard the structure with an C<#ifdef STRING> preprocessor macro.
239 If the given string is not defined, no functions will be generated.
240  
241 =back
242  
243 =cut
244  
245 sub structure
246 10 ▶{
Subroutine structure: called CC=8 SCAR=20.8
Pod documented
247 10 my ( $name, %params ) = @_;
248  
249 10 ( my $basename = $name ) =~ s/^struct //;
250  
251 10 ▶ my $packfunc = $params{pack_func} || "pack_$basename";
Condition100%2/2
$params{'pack_func'} || "pack_$basename"
l!l
19
MC/DC100%1/1
$params{'pack_func'} || "pack_$basename"
$params{'pack_func'}
Truth table100%2/2
A
A= $params{'pack_func'}
Aresult
11
00
252 10 ▶ my $unpackfunc = $params{unpack_func} || "unpack_$basename";
Condition100%2/2
$params{'unpack_func'} || "unpack_$basename"
l!l
19
MC/DC100%1/1
$params{'unpack_func'} || "unpack_$basename"
$params{'unpack_func'}
Truth table100%2/2
A
A= $params{'unpack_func'}
Aresult
11
00
253  
254 10 my $with_tail = $params{with_tail};
255 10 my $no_length_check = $params{no_length_check};
256  
257 10 ▶ my $arg_style = $params{arg_style} || "list";
Condition100%2/2
$params{'arg_style'} || 'list'
l!l
19
MC/DC100%1/1
$params{'arg_style'} || 'list'
$params{'arg_style'}
Truth table100%2/2
A
A= $params{'arg_style'}
Aresult
11
00
258  
259 10 my @membernames;
260 my @argnames;
261 10 my @memberhandlers;
262  
263 10 my $argindex = 0;
264 10 my @members = @{ $params{members} };
265 10 foreach ( pairs @members ) {
266 20 my $memname = $_->key;
267 20 my $handler = $_->value;
268  
269 20 push @membernames, $memname;
270 20 push @memberhandlers, $handler;
271  
272 20 $handler->{set_names}->( $basename, $memname );
273  
274 20 my $wasindex = $argindex;
275 20 $handler->{set_arg}( $argindex );
276  
277 20 ▶ push @argnames, $memname if $argindex > $wasindex;
Branch: 1 branch
BranchTrueFalseTotal
if $argindex > $wasindex 19 1 20
278 }
279  
280 10 ▶ push @fragments, "#ifdef $params{ifdef}" if $params{ifdef};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"ifdef"} 1 9 10
281 push @fragments,
282 " {",
283 " $name $basename;",
284 qq[ printf("$basename=%lu,", (unsigned long)sizeof($basename));],
285 10 ( map { " " . $_->{gen_c}->() } @memberhandlers ),
286 qq[ printf("\\n");],
287 " }";
288 10 ▶ push @fragments, "#endif" if $params{ifdef};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"ifdef"} 1 9 10
289  
290 10 ▶ push @genblocks, [ $basename => sub {
Subroutine __ANON__: called CC=11 SCAR=24.0
291 10 my ( $result ) = @_;
292 10 ▶ return () unless defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 1 9 10
293  
294 9 my @result = split m/,/, $result;
295  
296 9 my $curpos = 0;
297  
298 9 my $format = "";
299  
300 9 my $sizeof = shift @result;
301  
302 9 my ( @postargs, @preret );
303  
304 9 foreach my $def ( @result ) {
305 19 my $handler = shift @memberhandlers;
306  
307 19 $format .= $handler->{gen_format}( $def, $curpos, \@postargs, \@preret ) . " ";
308 }
309  
310 9 ▶ if( $curpos < $sizeof ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curpos < $sizeof) 2 7 9
311 2 $format .= "x" . ( $sizeof - $curpos );
312 }
313  
314 9 my $eq = "==";
315 9 ▶ if( $with_tail ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($with_tail) 1 8 9
316 1 $format .= "a*";
317 1 $eq = ">=";
318 }
319  
320 9 ▶ unshift( @perlcode, "use Carp;" ), $done_carp++ unless $done_carp;
Branch: 1 branch
BranchTrueFalseTotal
unless $done_carp 9 0 9
321  
322 9 my ( @argcode, @retcode );
323 9 ▶ if( $arg_style eq "list" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg_style eq 'list') { } 8 1 9
elsif ($arg_style eq 'hashref') { } 1 0 1
324 8 ▶ my $members = join( ", ", @argnames, ( $with_tail ? "[tail]" : () ) );
Branch: 1 branch
BranchTrueFalseTotal
$with_tail ? : 1 7 8
325  
326 8 @argcode = (
327 qq{ \@_ $eq $argindex or croak "usage: $packfunc($members)";},
328 qq{ my \@v = \@_;} );
329 8 @retcode = (
330 qq{ \@v;} );
331 }
332 elsif( $arg_style eq "hashref" ) {
333 1 ▶ my $qmembers = join( ", ", map { "'$_'" } @membernames, ( $with_tail ? "_tail" : () ) );
Branch: 1 branch
BranchTrueFalseTotal
$with_tail ? : 0 1 1
334  
335 1 @argcode = (
336 qq{ ref(\$_[0]) eq "HASH" or croak "usage: $packfunc(\\%args)";},
337 qq( my \@v = \@{\$_[0]}{$qmembers};) );
338 1 @retcode = (
339 # Seems we can't easily do this without a temporary
340 qq( my %ret; \@ret{$qmembers} = \@v;),
341 qq{ \\%ret;} );
342 }
343 else {
344 0 carp "Unrecognised arg_style $arg_style";
345 }
346  
347 9 push_export $packfunc;
348 9 push_export $unpackfunc;
349  
350 9 ▶ join( "\n",
Branch: 1 branch
BranchTrueFalseTotal
$no_length_check ? : 1 8 9
351 "",
352 "sub $packfunc",
353 "{",
354 @argcode,
355 @postargs,
356 qq{ pack "$format", \@v;},
357 "}",
358 "",
359 "sub $unpackfunc",
360 "{",
361 ( $no_length_check ? '' :
362 qq{ length \$_[0] $eq $sizeof or croak "$unpackfunc: expected $sizeof bytes, got " . length \$_[0];}
363 ),
364 qq{ my \@v = unpack "$format", \$_[0];},
365 @preret,
366 @retcode,
367 "}"
368 );
369 10 } ];
370 }
371  
372 =pod
373  
374 The following structure member definitions are allowed:
375  
376 =over 4
377  
378 =cut
379  
380 my %struct_formats = (
381 map {
382 10 my $bytes = length( pack "$_", 0 );
383 30 "${bytes}u" => uc $_,
384 "${bytes}s" => lc $_
385 } qw( C S L )
386 );
387  
388 10 ▶if( eval { pack "Q", 0 } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { "\0\0\0\0\0\0\0\0" } }) 10 0 10
389 10 my $bytes = length( pack "Q", 0 );
390 10 $struct_formats{"${bytes}u"} = "Q";
391 10 $struct_formats{"${bytes}s"} = "q";
392 }
393  
394 =item * member_numeric
395  
396 The field contains a single signed or unsigned number. Its size and signedness
397 will be automatically detected.
398  
399 =cut
400  
401 10 my $done_u64;
402  
403 sub member_numeric
404 18 ▶{
Subroutine member_numeric: called CC=1 SCAR=1.5
Pod documented
405 18 my $varname;
406 my $membername;
407 18 my $argindex;
408  
409 return {
410 18 ▶ set_names => sub { ( $varname, $membername ) = @_; },
Subroutine __ANON__: called CC=1 SCAR=0.0
411 17 ▶ set_arg => sub { $argindex = $_[0]++; },
Subroutine __ANON__: called CC=1 SCAR=0.0
412  
413 18 ▶ gen_c => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
414 18 qq{printf("$membername@%ld+%lu%c,", } .
415 "(long)((char*)&$varname.$membername-(char*)&$varname), " . # offset
416 "(unsigned long)sizeof($varname.$membername), " . # size
417 "($varname.$membername=-1)>0?'u':'s'" . # signedness
418 ");";
419 },
420 17 ▶ gen_format => sub {
Subroutine __ANON__: called CC=9 SCAR=33.8
421 17 my ( $def, undef, $postarg, $preret ) = @_;
422 # ( undef, curpos ) = @_;
423  
424 17 ▶ my ( $member, $offs, $size, $sign ) = $def =~ m/^([\w.]+)@(\d+)\+(\d+)([us])$/
Branch: 1 branch
BranchTrueFalseTotal
() = $def =~ /^([\w.]+)@(\d+)\+(\d+)([us])$/u or die "Could not parse member definition out of '${def}'" 17 0 17
425 or die "Could not parse member definition out of '$def'";
426  
427 17 ▶ $member eq $membername or die "Expected definition of $membername but found $member instead";
Branch: 1 branch
BranchTrueFalseTotal
$member eq $membername or die "Expected definition of $membername but found $member instead" 17 0 17
428  
429 17 my $format = "";
430 17 ▶ if( $offs > $_[1] ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($offs > $_[1]) { } 0 17 17
elsif ($offs < $_[1]) { } 0 17 17
431 0 my $pad = $offs - $_[1];
432  
433 0 $format .= "x" x $pad;
434 0 $_[1] += $pad;
435 }
436 elsif( $offs < $_[1] ) {
437 0 die "Err.. need to go backwards for structure $varname member $member";
438 }
439  
440 17 ▶ if( exists $struct_formats{"$size$sign"} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $struct_formats{"$size$sign"}) { } 17 0 17
elsif ($size == 8 and $sign eq 'u') { } 0 0 0
441 17 $format .= $struct_formats{"$size$sign"};
442 }
443 ▶ elsif( $size == 8 and $sign eq "u" ) {
Condition0%0/3
$size == 8 and $sign eq "u"
!ll&&!rl&&r
000
MC/DC0%0/2
$size == 8 and $sign eq "u"
$size == 8 $sign eq "u"
Truth table0%0/3
A and B
A= $size == 8B= $sign eq "u"
ABresult
0X0
100
111
444 # 64bit int on a 64bit-challenged perl. We'll have to improvise
445  
446 0 ▶ unless( $done_u64 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done_u64) 0 0 0
447 0 my $hilo = pack("S",0x0201) eq "\x02\x01" ? "\$hi, \$lo" : "\$lo, \$hi";
448  
449 0 perlcode join "\n",
450 "require Math::BigInt;",
451 "",
452 "sub __pack_u64 {",
453 " my ( \$hi, \$lo ) = ( int(\$_[0] / 2**32), \$_[0] & 0xffffffff );",
454 " pack( \"L L\", $hilo );",
455 "}",
456 "",
457 "sub __unpack_u64 {",
458 " length \$_[0] == 8 or return undef;", # in case of no_length_check
459 " my ( $hilo ) = unpack( \"L L\", \$_[0] );",
460 " return \$lo if \$hi == 0;",
461 " my \$n = Math::BigInt->new(\$hi); \$n <<= 32; \$n |= \$lo;",
462 " return \$n;",
463 "}",
464 "";
465  
466 0 $done_u64++;
467 }
468  
469 0 push @$postarg, " \$v[$argindex] = __pack_u64( \$v[$argindex] );";
470 0 push @$preret, " \$v[$argindex] = __unpack_u64( \$v[$argindex] );";
471
472  
473 0 $format .= "a8";
474 }
475 else {
476 0 die "Cannot find a pack format for size $size sign $sign";
477 }
478  
479 17 $_[1] += $size;
480 17 return $format;
481 },
482 18 };
483 }
484  
485 =item * member_strarray
486  
487 The field contains a NULL-padded string of characters. Its size will be
488 automatically detected.
489  
490 =cut
491  
492 sub member_strarray
493 2 ▶{
Subroutine member_strarray: called CC=1 SCAR=0.3
Pod documented
494 2 my $varname;
495 my $membername;
496 2 my $argindex;
497  
498 return {
499 2 ▶ set_names => sub { ( $varname, $membername ) = @_; },
Subroutine __ANON__: called CC=1 SCAR=0.0
500 2 ▶ set_arg => sub { $argindex = $_[0]++; },
Subroutine __ANON__: called CC=1 SCAR=0.0
501  
502 2 ▶ gen_c => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
503 2 qq{printf("$membername@%ld+%lu,", } .
504 "(long)((char*)&$varname.$membername-(char*)&$varname), " . # offset
505 "(unsigned long)sizeof($varname.$membername)" . # size
506 ");";
507 },
508 2 ▶ gen_format => sub {
Subroutine __ANON__: called CC=5 SCAR=18.9
509 2 my ( $def ) = @_;
510  
511 2 ▶ my ( $member, $offs, $size ) = $def =~ m/^([\w.]+)@(\d+)\+(\d+)$/
Branch: 1 branch
BranchTrueFalseTotal
() = $def =~ /^([\w.]+)@(\d+)\+(\d+)$/u or die "Could not parse member definition out of '${def}'" 2 0 2
512 or die "Could not parse member definition out of '$def'";
513  
514 2 ▶ $member eq $membername or die "Expected definition of $membername but found $member instead";
Branch: 1 branch
BranchTrueFalseTotal
$member eq $membername or die "Expected definition of $membername but found $member instead" 2 0 2
515  
516 2 my $format = "";
517 2 ▶ if( $offs > $_[1] ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($offs > $_[1]) { } 0 2 2
elsif ($offs < $_[1]) { } 0 2 2
518 0 my $pad = $offs - $_[1];
519  
520 0 $format .= "x" x $pad;
521 0 $_[1] += $pad;
522 }
523 elsif( $offs < $_[1] ) {
524 0 die "Err.. need to go backwards for structure $varname member $member";
525 }
526  
527 2 $format .= "Z$size";
528 2 $_[1] += $size;
529  
530 2 return $format;
531 },
532 2 };
533 }
534  
535 =item * member_constant($code)
536  
537 The field contains a single number as for C<member_numeric>. Instead of
538 consuming/returning a value in the arguments list, this member will be packed
539 from an expression, or asserted that it contains the given value. The string
540 C<$code> will be inserted into the generated pack and unpack functions, so it
541 can be used for constants generated by the C<constant> directive.
542  
543 =cut
544  
545 sub member_constant
546 1 ▶{
Subroutine member_constant: called CC=1 SCAR=0.0
Pod documented
547 1 my $value = shift;
548  
549 1 my $constant = member_numeric;
550  
551 1 my $arg_index;
552 1 ▶ $constant->{set_arg} = sub { $arg_index = $_[0] }; # no inc
Subroutine __ANON__: called CC=1 SCAR=0.0
553  
554 1 my $gen_format = delete $constant->{gen_format};
555 1 ▶ $constant->{gen_format} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
556 1 my ( $def, undef, $postarg, $preret ) = @_;
557  
558 1 ▶ my ( $member ) = $def =~ m/^([\w.]+)@/
Branch: 1 branch
BranchTrueFalseTotal
my($member) = $def =~ /^([\w.]+)@/u or die "Could not parse member definition out of '${def}'" 1 0 1
559 or die "Could not parse member definition out of '$def'";
560  
561 1 push @$postarg, " splice \@v, $arg_index, 0, $value;";
562  
563 1 my $format = $gen_format->( @_ );
564  
565 1 push @$preret, " splice( \@v, $arg_index, 1 ) == $value or croak \"expected $member == $value\";";
566  
567 1 return $format;
568 1 };
569  
570 1 $constant;
571 }
572  
573 =back
574  
575 The structure definition results in two new functions being created,
576 C<pack_$name> and C<unpack_$name>, where C<$name> is the name of the structure
577 (with the leading C<struct> prefix stripped). These behave similarly to the
578 familiar functions such as C<pack_sockaddr_in>; the C<pack_> function will
579 take a list of fields and return a packed string, the C<unpack_> function will
580 take a string and return a list of fields.
581  
582 =cut
583  
584 =head2 no_export
585  
586 =head2 use_export
587  
588 =head2 use_export_ok
589  
590 no_export;
591 use_export;
592 use_export_ok;
593  
594 Controls the export behaviour of the generated symbols. C<no_export> creates
595 symbols that are not exported by their package, they must be used fully-
596 qualified. C<use_export> creates symbols that are exported by default.
597 C<use_export_ok> creates symbols that are exported if they are specifically
598 requested at C<use> time.
599  
600 The mode can be changed at any time to affect only the symbols that follow
601 it. It defaults to C<use_export_ok>.
602  
603 =cut
604  
605 1 ▶sub no_export { $export_mode = 0 }
Subroutine no_export: called CC=1 SCAR=0.0
Pod documented
606 1 ▶sub use_export { $export_mode = 1 }
Subroutine use_export: called CC=1 SCAR=0.0
Pod documented
607 10 ▶sub use_export_ok { $export_mode = "OK" }
Subroutine use_export_ok: called CC=1 SCAR=0.0
Pod documented
608  
609 10 my $cbuilder = ExtUtils::CBuilder->new( quiet => 1 );
610 10 my %compile_args;
611 my %link_args;
612  
613 10 ▶if( my $mb = eval { require Module::Build and Module::Build->current } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mb = eval { do { require Module::Build and "Module::Build"->current } }) 10 0 10
Condition33%1/3
require Module::Build && 'Module::Build'->current
!ll&&!rl&&r
0010
MC/DC0%0/2
require Module::Build && 'Module::Build'->current
require Module::Build 'Module::Build'->current
Truth table33%1/3
A && B
A= require Module::BuildB= 'Module::Build'->current
ABresult
0X0
100
111
614 10 $compile_args{include_dirs} = $mb->include_dirs;
615 10 $compile_args{extra_compiler_flags} = $mb->extra_compiler_flags;
616  
617 10 $link_args{extra_linker_flags} = $mb->extra_linker_flags;
618 }
619  
620 sub gen_perl
621 18 ▶{
Subroutine gen_perl: called CC=10 SCAR=23.7
Pod undocumented
622 18 ▶ return "" unless @fragments;
Branch: 1 branch
BranchTrueFalseTotal
unless @fragments 0 18 18
623  
624 18 my $c_file = join "\n",
625 "#include <stdio.h>",
626 @preamble,
627 "",
628 "int main(void) {",
629 @fragments,
630 " return 0;",
631 "}\n";
632  
633 18 undef @preamble;
634 18 undef @fragments;
635  
636 18 ▶ die "Cannot generate a C file yet - no module name\n" unless defined $modulename;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $modulename 0 18 18
637  
638 18 my $tempname = "gen-$modulename";
639  
640 18 my $sourcename = "$tempname.c";
641 {
642 18 ▶ open( my $source_fh, "> $sourcename" ) or die "Cannot write $sourcename - $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $source_fh, "> $sourcename" or die "Cannot write $sourcename - $!" 18 0 18
643 18 print $source_fh $c_file;
644 }
645  
646 18 my $objname = eval { $cbuilder->compile( source => $sourcename, %compile_args ) };
647  
648 18 unlink $sourcename;
649  
650 18 ▶ if( !defined $objname ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $objname) 0 18 18
651 0 die "Failed to compile source\n";
652 }
653  
654 18 my $exename = eval { $cbuilder->link_executable( objects => $objname, %link_args ) };
655  
656 18 unlink $objname;
657  
658 18 ▶ if( !defined $exename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $exename) 0 18 18
659 0 die "Failed to link executable\n";
660 }
661  
662 18 my $output;
663 {
664 18 ▶ open( my $runh, "./$exename |" ) or die "Cannot pipeopen $exename - $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $runh, "./$exename |" or die "Cannot pipeopen $exename - $!" 18 0 18
665  
666 18 local $/;
667 18 $output = <$runh>;
668 }
669  
670 18 unlink $exename;
671  
672 18 my %results = map { m/^(\w+)=(.*)$/ } split m/\n/, $output;
673  
674 18 my $perl = "";
675  
676 18 my @bodylines;
677  
678 # Evaluate these first, so they have a chance to push_export()
679 18 foreach my $genblock ( @genblocks ) {
680 19 my ( $key, $code ) = @$genblock;
681  
682 19 push @bodylines, $code->( $results{$key} );
683 }
684  
685 18 ▶ if( @exports ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@exports) 0 18 18
686 0 $perl .= "push \@EXPORT, " . join( ", ", map { "'$_'" } @exports ) . ";\n";
687 0 undef @exports;
688 }
689  
690 18 ▶ if( @exports_ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@exports_ok) 13 5 18
691 13 $perl .= "push \@EXPORT_OK, " . join( ", ", map { "'$_'" } @exports_ok ) . ";\n";
692 13 undef @exports_ok;
693 }
694  
695 18 $perl .= join "", map { "$_\n" } @bodylines;
696  
697 18 undef @genblocks;
698  
699 18 my @thisperlcode = @perlcode;
700 18 undef @perlcode;
701  
702 18 return join "\n", @thisperlcode, $perl;
703 }
704  
705 =head2 gen_output
706  
707 $perl = gen_output;
708  
709 Returns the generated perl code. This is used internally for testing purposes
710 but normally would not be necessary; see instead C<write_output>.
711  
712 =cut
713  
714 sub gen_output
715 18 ▶{
Subroutine gen_output: called CC=1 SCAR=0.0
Pod documented
716 18 my $ret = $output . gen_perl . "\n1;\n";
717 18 $output = "";
718  
719 18 return $ret;
720 }
721  
722 =head2 write_output
723  
724 write_output $filename;
725  
726 Write the generated perl code into the named file. This would normally be used
727 as the last function in the containing script, to generate the output file. In
728 the case of C<ExtUtils::MakeMaker> or C<Module::Build> invoking the script,
729 the path to the file to be generated should be given in C<$ARGV[0]>. Normally,
730 therefore, the script would end with
731  
732 write_output $ARGV[0];
733  
734 =cut
735  
736 sub write_output
737 0 ▶{
Subroutine write_output: not called CC=2 SCAR=17.9
Pod documented
738 0 my ( $filename ) = @_;
739  
740 0 my $output = gen_output();
741  
742 0 ▶ open( my $outfile, ">", $filename ) or die "Cannot write '$filename' - $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $outfile, ">", $filename or die "Cannot write '${filename}' - $!" 0 0 0
743  
744 0 print $outfile $output;
745 }
746  
747 =head2 include_path
748  
749 include_path $path
750  
751 Adds an include path to the list of paths used by the compiler
752  
753 =cut
754  
755 sub include_path
756 1 ▶{
Subroutine include_path: called CC=1 SCAR=0.0
Pod documented
757 1 my ( $path ) = @_;
758  
759 1 push @{ $compile_args{include_dirs} }, $path;
760 }
761  
762 =head2 define
763  
764 define $symbol;
765 define $symbol, $value;
766  
767 Adds a symbol to be defined on the compiler's commandline, by using the C<-D>
768 option. This is sometimes required to turn on particular optional parts of the
769 included files. An optional value can also be specified.
770  
771 =cut
772  
773 sub define
774 1 ▶{
Subroutine define: called CC=2 SCAR=7.9
Pod documented
775 1 my ( $symbol, $value ) = @_;
776  
777 1 ▶ if( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 1 0 1
778 1 push @{ $compile_args{extra_compiler_flags} }, "-D$symbol=$value";
779 }
780 else {
781 0 push @{ $compile_args{extra_compiler_flags} }, "-D$symbol";
782 }
783 }
784  
785 =head1 EXAMPLES
786  
787 Normally this module would be used by another module at build time, to
788 construct the relevant constants and structure functions from system headers.
789  
790 For example, suppose your operating system defines a new type of socket, which
791 has its own packet and address families, and perhaps some new socket options
792 which are valid on this socket. We can build a module to contain the relevant
793 constants and structure functions by writing, for example:
794  
795 #!/usr/bin/perl
796  
797 use ExtUtils::H2PM;
798  
799 module "Socket::Moonlaser";
800  
801 include "moon/laser.h";
802  
803 constant "AF_MOONLASER";
804 constant "PF_MOONLASER";
805  
806 constant "SOL_MOONLASER";
807  
808 constant "MOONLASER_POWER", name => "POWER";
809 constant "MOONLASER_WAVELENGTH", name => "WAVELENGTH";
810  
811 structure "struct laserwl",
812 members => [
813 lwl_nm_coarse => member_numeric,
814 lwl_nm_fine => member_numeric,
815 ];
816  
817 write_output $ARGV[0];
818  
819 If we save this script as, say, F<lib/Socket/Moonlaser.pm.PL>, then when the
820 distribution is built, the script will be used to generate the contents of the
821 file F<lib/Socket/Moonlaser.pm>. Once installed, any other code can simply
822  
823 use Socket::Moonlaser qw( AF_MOONLASER );
824  
825 to import a constant.
826  
827 The method described above doesn't allow us any room to actually include other
828 code in the module. Perhaps, as well as these simple constants, we'd like to
829 include functions, documentation, etc... To allow this, name the script
830 instead something like F<lib/Socket/Moonlaser_const.pm.PL>, so that this is
831 the name used for the generated output. The code can then be included in the
832 actual F<lib/Socket/Moonlaser.pm> (which will just be a normal perl module) by
833  
834 package Socket::Moonlaser;
835  
836 use Socket::Moonlaser_const;
837  
838 sub get_power
839 {
840 getsockopt( $_[0], SOL_MOONLASER, POWER );
841 }
842  
843 sub set_power
844 {
845 setsockopt( $_[0], SOL_MOONLASER, POWER, $_[1] );
846 }
847  
848 sub get_wavelength
849 {
850 my $wl = getsockopt( $_[0], SOL_MOONLASER, WAVELENGTH );
851 defined $wl or return;
852 unpack_laserwl( $wl );
853 }
854  
855 sub set_wavelength
856 {
857 my $wl = pack_laserwl( $_[1], $_[2] );
858 setsockopt( $_[0], SOL_MOONLASER, WAVELENGTH, $wl );
859 }
860  
861 1;
862  
863 Sometimes, the actual C structure layout may not exactly match the semantics
864 we wish to present to perl modules using this extension wrapper. Socket
865 address structures typically contain their address family as the first member,
866 whereas this detail isn't exposed by, for example, the C<sockaddr_in> and
867 C<sockaddr_un> functions. To cope with this case, the low-level structure
868 packing and unpacking functions can be generated with a different name, and
869 wrapped in higher-level functions in the main code. For example, in
870 F<Moonlaser_const.pm.PL>:
871  
872 no_export;
873  
874 structure "struct sockaddr_ml",
875 pack_func => "_pack_sockaddr_ml",
876 unpack_func => "_unpack_sockaddr_ml",
877 members => [
878 ml_family => member_numeric,
879 ml_lat_deg => member_numeric,
880 ml_long_deg => member_numeric,
881 ml_lat_fine => member_numeric,
882 ml_long_fine => member_numeric,
883 ];
884  
885 This will generate a pack/unpack function pair taking or returning five
886 arguments; these functions will not be exported. In our main F<Moonlaser.pm>
887 file we can wrap these to actually expose a different API:
888  
889 sub pack_sockaddr_ml
890 {
891 @_ == 2 or croak "usage: pack_sockaddr_ml(lat, long)";
892 my ( $lat, $long ) = @_;
893  
894 return _pack_sockaddr_ml( AF_MOONLASER, int $lat, int $long,
895 ($lat - int $lat) * 1_000_000, ($long - int $long) * 1_000_000);
896 }
897  
898 sub unpack_sockaddr_ml
899 {
900 my ( $family, $lat, $long, $lat_fine, $long_fine ) =
901 _unpack_sockaddr_ml( $_[0] );
902  
903 $family == AF_MOONLASER or croak "expected family AF_MOONLASER";
904  
905 return ( $lat + $lat_fine/1_000_000, $long + $long_fine/1_000_000 );
906 }
907  
908 Sometimes, a structure will contain members which are themselves structures.
909 Suppose a different definition of the above address, which at the C layer is
910 defined as
911  
912 struct angle
913 {
914 short deg;
915 unsigned long fine;
916 };
917  
918 struct sockaddr_ml
919 {
920 short ml_family;
921 struct angle ml_lat, ml_long;
922 };
923  
924 We can instead "flatten" this structure tree to obtain the five fields by
925 naming the sub-members of the outer structure:
926  
927 structure "struct sockaddr_ml",
928 members => [
929 "ml_family" => member_numeric,
930 "ml_lat.deg" => member_numeric,
931 "ml_lat.fine" => member_numeric,
932 "ml_long.deg" => member_numeric,
933 "ml_long.fine" => member_numeric,
934 ];
935  
936 =head1 TODO
937  
938 =over 4
939  
940 =item *
941  
942 Consider more structure members. With strings comes the requirement to have
943 members that store a size. This requires cross-referential members. And while
944 we're at it it might be nice to have constant members; fill in constants
945 without consuming arguments when packing, assert the right value on unpacking.
946  
947 =back
948  
949 =head1 AUTHOR
950  
951 Paul Evans <leonerd@leonerd.org.uk>
952  
953 =cut
954  
955 10 0x55AA;
↑ Summary