blib/lib/ExtUtils/CChecker.pm

filter: Statementstmt 76.3% 113 / 148 Branchbran 51.6% 31 / 60 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 84.0% 21 / 25 Podpod 78.9% 15 / 19 totaltotal 70.4% 181 / 257 CC 36 SCAR 23.0
CC
36
Cov
69%
try_find_cflags_for
30.0
try_find_include_dirs_for
30.0
try_find_libs_for
20.0
CRAP 10.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-2024 -- leonerd@leonerd.org.uk
5  
6 package ExtUtils::CChecker 0.12;
7  
8 7 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 7 ▶use ExtUtils::CBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 C<ExtUtils::CChecker> - configure-time utilities for using C headers,
18 libraries, or OS features
19  
20 =head1 SYNOPSIS
21  
22 use Module::Build;
23 use ExtUtils::CChecker;
24  
25 my $cc = ExtUtils::CChecker->new;
26  
27 $cc->assert_compile_run(
28 diag => "no PF_MOONLASER",
29 source => <<'EOF' );
30 #include <stdio.h>
31 #include <sys/socket.h>
32 int main(int argc, char *argv[]) {
33 printf("PF_MOONLASER is %d\n", PF_MOONLASER);
34 return 0;
35 }
36 EOF
37  
38 Module::Build->new(
39 ...
40 )->create_build_script;
41  
42 =head1 DESCRIPTION
43  
44 Often Perl modules are written to wrap functionality found in existing C
45 headers, libraries, or to use OS-specific features. It is useful in the
46 F<Build.PL> or F<Makefile.PL> file to check for the existance of these
47 requirements before attempting to actually build the module.
48  
49 Objects in this class provide an extension around L<ExtUtils::CBuilder> to
50 simplify the creation of a F<.c> file, compiling, linking and running it, to
51 test if a certain feature is present.
52  
53 It may also be necessary to search for the correct library to link against,
54 or for the right include directories to find header files in. This class also
55 provides assistance here.
56  
57 =cut
58  
59 =head1 CONSTRUCTOR
60  
61 =cut
62  
63 =head2 new
64  
65 $cc = ExtUtils::CChecker->new( %args );
66  
67 Returns a new instance of a C<ExtUtils::CChecker> object. Takes the following
68 named parameters:
69  
70 =over 4
71  
72 =item defines_to => PATH
73  
74 If given, defined symbols will be written to a C preprocessor F<.h> file of
75 the given name, instead of by adding extra C<-DI<SYMBOL>> arguments to the
76 compiler flags.
77  
78 =item quiet => BOOL
79  
80 If given, sets the C<quiet> option to the underlying C<ExtUtils::CBuilder>
81 instance. If absent, defaults to enabled. To disable quietness, i.e. to print
82 more verbosely, pass a defined-but-false value, such as C<0>.
83  
84 =item config => HASH
85  
86 If given, passed through as the configuration of the underlying
87 C<ExtUtils::CBuilder> instance.
88  
89 =back
90  
91 =cut
92  
93 sub new
94 6 ▶{
Subroutine new: called CC=3 SCAR=11.6
Pod documented
95 6 my $class = shift;
96 6 my %args = @_;
97  
98 6 my $quiet = 1;
99 6 ▶ $quiet = 0 if defined $args{quiet} and !$args{quiet};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"quiet"} and not $args{"quiet"} 0 6 6
Condition33%1/3
defined $args{"quiet"} and not $args{"quiet"}
!ll&&!rl&&r
600
MC/DC0%0/2
defined $args{"quiet"} and not $args{"quiet"}
defined $args{"quiet"} not $args{"quiet"}
Truth table33%1/3
A and not(B)
A= defined $args{"quiet"}B= not $args{"quiet"}
ABresult
0X0
100
111
100  
101 my $cb = ExtUtils::CBuilder->new(
102 quiet => $quiet,
103 config => $args{config},
104 6 );
105  
106 return bless {
107 cb => $cb,
108 seq => 0,
109  
110 defines_to => $args{defines_to},
111  
112 6 include_dirs => [],
113 extra_compiler_flags => [],
114 extra_linker_flags => [],
115 }, $class;
116 }
117  
118 =head1 METHODS
119  
120 =cut
121  
122 =head2 include_dirs
123  
124 $dirs = $cc->include_dirs;
125  
126 Returns the currently-configured include directories in an ARRAY reference.
127  
128 =cut
129  
130 sub include_dirs
131 5 ▶{
Subroutine include_dirs: called CC=1 SCAR=0.0
Pod documented
132 5 my $self = shift;
133 # clone it just so caller can't modify ours
134 5 return [ @{ $self->{include_dirs} } ];
135 }
136  
137 =head2 extra_compiler_flags
138  
139 $flags = $cc->extra_compiler_flags;
140  
141 Returns the currently-configured extra compiler flags in an ARRAY reference.
142  
143 =cut
144  
145 sub extra_compiler_flags
146 7 ▶{
Subroutine extra_compiler_flags: called CC=1 SCAR=0.0
Pod documented
147 7 my $self = shift;
148 # clone it just so caller can't modify ours
149 7 return [ @{ $self->{extra_compiler_flags} } ];
150 }
151  
152 =head2 extra_linker_flags
153  
154 $flags = $cc->extra_linker_flags;
155  
156 Returns the currently-configured extra linker flags in an ARRAY reference.
157  
158 =cut
159  
160 sub extra_linker_flags
161 5 ▶{
Subroutine extra_linker_flags: called CC=1 SCAR=0.0
Pod documented
162 5 my $self = shift;
163 # clone it just so caller can't modify ours
164 5 return [ @{ $self->{extra_linker_flags} } ];
165 }
166  
167 =head2 push_include_dirs
168  
169 $cc->push_include_dirs( @dirs );
170  
171 Adds more include directories
172  
173 =cut
174  
175 sub push_include_dirs
176 2 ▶{
Subroutine push_include_dirs: called CC=1 SCAR=0.0
Pod documented
177 2 my $self = shift;
178 2 push @{ $self->{include_dirs} }, @_;
179 }
180  
181 =head2 push_extra_compiler_flags
182  
183 $cc->push_extra_compiler_flags( @flags );
184  
185 Adds more compiler flags
186  
187 =cut
188  
189 sub push_extra_compiler_flags
190 3 ▶{
Subroutine push_extra_compiler_flags: called CC=1 SCAR=0.0
Pod documented
191 3 my $self = shift;
192 3 push @{ $self->{extra_compiler_flags} }, @_;
193 }
194  
195 =head2 push_extra_linker_flags
196  
197 $cc->push_extra_linker_flags( @flags );
198  
199 Adds more linker flags
200  
201 =cut
202  
203 sub push_extra_linker_flags
204 2 ▶{
Subroutine push_extra_linker_flags: called CC=1 SCAR=0.0
Pod documented
205 2 my $self = shift;
206 2 push @{ $self->{extra_linker_flags} }, @_;
207 }
208  
209 sub cbuilder
210 12 ▶{
Subroutine cbuilder: called CC=1 SCAR=0.0
Pod undocumented
211 12 my $self = shift;
212 12 return $self->{cb};
213 }
214  
215 sub compile
216 7 ▶{
Subroutine compile: called CC=3 SCAR=11.0
Pod undocumented
217 7 my $self = shift;
218 7 my %args = @_;
219  
220 7 ▶ $args{include_dirs} = [ map { defined $_ ? @$_ : () } $self->{include_dirs}, $args{include_dirs} ];
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 7 7 14
221 7 ▶ $args{extra_compiler_flags} = [ map { defined $_ ? @$_ : () } $self->{extra_compiler_flags}, $args{extra_compiler_flags} ];
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 7 7 14
222  
223 7 $self->cbuilder->compile( %args );
224 }
225  
226 sub link_executable
227 5 ▶{
Subroutine link_executable: called CC=2 SCAR=6.9
Pod undocumented
228 5 my $self = shift;
229 5 my %args = @_;
230  
231 5 ▶ $args{extra_linker_flags} = [ map { defined $_ ? @$_ : () } $self->{extra_linker_flags}, $args{extra_linker_flags} ];
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 5 5 10
232  
233 5 $self->cbuilder->link_executable( %args );
234 }
235  
236 sub fail
237 1 ▶{
Subroutine fail: called CC=2 SCAR=7.0
Pod undocumented
238 1 my $self = shift;
239 1 my ( $diag ) = @_;
240  
241 1 ▶ my $message = defined $diag ? "OS unsupported - $diag\n" : "OS unsupported\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $diag ? : 1 0 1
242 1 die $message;
243 }
244  
245 =head2 define
246  
247 $cc->define( $symbol );
248  
249 Adds a new defined symbol directly; either by appending to the compiler flags
250 or writing it into the defines file.
251  
252 =cut
253  
254 sub define
255 2 ▶{
Subroutine define: called CC=4 SCAR=14.0
Pod documented
256 2 my $self = shift;
257 2 my ( $symbol ) = @_;
258  
259 2 ▶ if( $self->{defines_to} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'defines_to'}) { } 1 1 2
260 1 ▶ unless( $self->{defines_fh} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"defines_fh"}) 1 0 1
261 1 ▶ open $self->{defines_fh}, ">", $self->{defines_to} or croak "Cannot open $self->{defines_to} for writing - $!";
Branch: 1 branch
BranchTrueFalseTotal
open $self->{"defines_fh"}, ">", $self->{"defines_to"} or croak(qq[Cannot open $self->{"defines_to"} for writing - $!]) 1 0 1
262 1 $self->{defines_fh}->autoflush(1);
263 }
264  
265 1 $self->{defines_fh}->print( "#define $symbol /**/\n" );
266 }
267 else {
268 1 $self->push_extra_compiler_flags( "-D$symbol" );
269 }
270 }
271  
272 =head2 try_compile_run
273  
274 $success = $cc->try_compile_run( %args );
275  
276 $success = $cc->try_compile_run( $source );
277  
278 Try to compile, link, and execute a C program whose source is given. Returns
279 true if the program compiled and linked, and exited successfully. Returns
280 false if any of these steps fail.
281  
282 Takes the following named arguments. If a single argument is given, that is
283 taken as the source string.
284  
285 =over 4
286  
287 =item source => STRING
288  
289 The source code of the C program to try compiling, building, and running.
290  
291 =item extra_compiler_flags => ARRAY
292  
293 Optional. If specified, pass extra flags to the compiler.
294  
295 =item extra_linker_flags => ARRAY
296  
297 Optional. If specified, pass extra flags to the linker.
298  
299 =item define => STRING
300  
301 Optional. If specified, then the named symbol will be defined if the program
302 ran successfully. This will either on the C compiler commandline (by passing
303 an option C<-DI<SYMBOL>>), or in the C<defines_to> file.
304  
305 =back
306  
307 =cut
308  
309 sub try_compile_run
310 7 ▶{
Subroutine try_compile_run: called CC=11 SCAR=24.9
Pod documented
311 7 my $self = shift;
312 7 ▶ my %args = ( @_ == 1 ) ? ( source => $_[0] ) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 2 5 7
313  
314 7 ▶ defined $args{source} or croak "Expected 'source'";
Branch: 1 branch
BranchTrueFalseTotal
defined $args{"source"} or croak("Expected 'source'") 7 0 7
315  
316 7 my $seq = $self->{seq}++;
317  
318 7 my $test_source = "test-$$-$seq.c";
319  
320 7 ▶ open( my $test_source_fh, "> $test_source" ) or die "Cannot write $test_source - $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $test_source_fh, "> $test_source" or die "Cannot write $test_source - $!" 7 0 7
321  
322 7 print $test_source_fh $args{source};
323  
324 7 close $test_source_fh;
325  
326 7 my %compile_args = (
327 source => $test_source,
328 );
329  
330 7 ▶ $compile_args{include_dirs} = $args{include_dirs} if exists $args{include_dirs};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"include_dirs"} 0 7 7
331 7 ▶ $compile_args{extra_compiler_flags} = $args{extra_compiler_flags} if exists $args{extra_compiler_flags};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"extra_compiler_flags"} 0 7 7
332  
333 7 my $test_obj = eval { $self->compile( %compile_args ) };
334  
335 7 unlink $test_source;
336  
337 7 ▶ if( not defined $test_obj ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $test_obj) 2 5 7
338 2 return 0;
339 }
340  
341 5 my %link_args = (
342 objects => $test_obj,
343 );
344  
345 5 ▶ $link_args{extra_linker_flags} = $args{extra_linker_flags} if exists $args{extra_linker_flags};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"extra_linker_flags"} 0 5 5
346  
347 5 my $test_exe = eval { $self->link_executable( %link_args ) };
348  
349 5 unlink $test_obj;
350  
351 5 ▶ if( not defined $test_exe ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $test_exe) 0 5 5
352 0 return 0;
353 }
354  
355 5 ▶ if( system( "./$test_exe" ) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (system("./$test_exe") != 0) 0 5 5
356 0 unlink $test_exe;
357 0 return 0;
358 }
359  
360 5 unlink $test_exe;
361  
362 5 ▶ $self->define( $args{define} ) if defined $args{define};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"define"} 2 3 5
363  
364 5 return 1;
365 }
366  
367 =head2 assert_compile_run
368  
369 $cc->assert_compile_run( %args );
370  
371 Calls C<try_compile_run>. If it fails, die with an C<OS unsupported> message.
372 Useful to call from F<Build.PL> or F<Makefile.PL>.
373  
374 Takes one extra optional argument:
375  
376 =over 4
377  
378 =item diag => STRING
379  
380 If present, this string will be appended to the failure message if one is
381 generated. It may provide more useful information to the user on why the OS is
382 unsupported.
383  
384 =back
385  
386 =cut
387  
388 sub assert_compile_run
389 2 ▶{
Subroutine assert_compile_run: called CC=2 SCAR=6.9
Pod documented
390 2 my $self = shift;
391 2 my %args = @_;
392  
393 2 my $diag = delete $args{diag};
394 2 ▶ $self->try_compile_run( %args ) or $self->fail( $diag );
Branch: 1 branch
BranchTrueFalseTotal
$self->try_compile_run(%args) or $self->fail($diag) 1 1 2
395 }
396  
397 =head2 try_find_cflags_for
398  
399 $success = $cc->try_find_cflags_for( %args );
400  
401 I<Since version 0.11.>
402  
403 Try to compile, link and execute the given source, using extra compiler flags.
404  
405 When a usable combination is found, the flags are stored in the object for use
406 in further compile operations, or returned by C<extra_compiler_flags>. The
407 method then returns true.
408  
409 If no usable combination is found, it returns false.
410  
411 Takes the following extra arguments:
412  
413 =over 4
414  
415 =item source => STRING
416  
417 Source code to compile
418  
419 =item cflags => ARRAY of ARRAYs
420  
421 Gives a list of sets of flags. Each set of flags should be strings in its own
422 array reference.
423  
424 =item define => STRING
425  
426 Optional. If specified, then the named symbol will be defined if the program
427 ran successfully.
428  
429 =back
430  
431 =cut
432  
433 sub try_find_cflags_for
434 0 ▶{
Subroutine try_find_cflags_for: not called CC=5 SCAR=34.0
Pod documented
435 0 my $self = shift;
436 0 my %args = @_;
437  
438 0 ▶ ref( my $cflags = $args{cflags} ) eq "ARRAY" or croak "Expected 'cflags' as ARRAY ref";
Branch: 1 branch
BranchTrueFalseTotal
ref(my $cflags = $args{"cflags"}) eq "ARRAY" or croak("Expected 'cflags' as ARRAY ref") 0 0 0
439  
440 0 foreach my $f ( @$cflags ) {
441 0 ▶ ref $f eq "ARRAY" or croak "Expected 'cflags' element as ARRAY ref";
Branch: 1 branch
BranchTrueFalseTotal
ref $f eq "ARRAY" or croak("Expected 'cflags' element as ARRAY ref") 0 0 0
442  
443 0 ▶ $self->try_compile_run( %args, extra_compiler_flags => $f ) or next;
Branch: 1 branch
BranchTrueFalseTotal
$self->try_compile_run(%args, "extra_compiler_flags", $f) or next 0 0 0
444  
445 0 $self->push_extra_compiler_flags( @$f );
446  
447 0 return 1;
448 }
449  
450 0 return 0;
451 }
452  
453 =head2 try_find_include_dirs_for
454  
455 $success = $cc->try_find_include_dirs_for( %args );
456  
457 Try to compile, link and execute the given source, using extra include
458 directories.
459  
460 When a usable combination is found, the directories required are stored in the
461 object for use in further compile operations, or returned by C<include_dirs>.
462 The method then returns true.
463  
464 If no a usable combination is found, it returns false.
465  
466 Takes the following arguments:
467  
468 =over 4
469  
470 =item source => STRING
471  
472 Source code to compile
473  
474 =item dirs => ARRAY of ARRAYs
475  
476 Gives a list of sets of dirs. Each set of dirs should be strings in its own
477 array reference.
478  
479 =item define => STRING
480  
481 Optional. If specified, then the named symbol will be defined if the program
482 ran successfully. This will either on the C compiler commandline (by passing
483 an option C<-DI<SYMBOL>>), or in the C<defines_to> file.
484  
485 =back
486  
487 =cut
488  
489 sub try_find_include_dirs_for
490 0 ▶{
Subroutine try_find_include_dirs_for: not called CC=5 SCAR=34.0
Pod documented
491 0 my $self = shift;
492 0 my %args = @_;
493  
494 0 ▶ ref( my $dirs = $args{dirs} ) eq "ARRAY" or croak "Expected 'dirs' as ARRAY ref";
Branch: 1 branch
BranchTrueFalseTotal
ref(my $dirs = $args{"dirs"}) eq "ARRAY" or croak("Expected 'dirs' as ARRAY ref") 0 0 0
495  
496 0 foreach my $d ( @$dirs ) {
497 0 ▶ ref $d eq "ARRAY" or croak "Expected 'dirs' element as ARRAY ref";
Branch: 1 branch
BranchTrueFalseTotal
ref $d eq "ARRAY" or croak("Expected 'dirs' element as ARRAY ref") 0 0 0
498  
499 0 ▶ $self->try_compile_run( %args, include_dirs => $d ) or next;
Branch: 1 branch
BranchTrueFalseTotal
$self->try_compile_run(%args, "include_dirs", $d) or next 0 0 0
500  
501 0 $self->push_include_dirs( @$d );
502  
503 0 return 1;
504 }
505  
506 0 return 0;
507 }
508  
509 =head2 try_find_libs_for
510  
511 $success = $cc->try_find_libs_for( %args );
512  
513 Try to compile, link and execute the given source, when linked against a
514 given set of extra libraries.
515  
516 When a usable combination is found, the libraries required are stored in the
517 object for use in further link operations, or returned by
518 C<extra_linker_flags>. The method then returns true.
519  
520 If no usable combination is found, it returns false.
521  
522 Takes the following arguments:
523  
524 =over 4
525  
526 =item source => STRING
527  
528 Source code to compile
529  
530 =item libs => ARRAY of STRINGs
531  
532 Gives a list of sets of libraries. Each set of libraries should be
533 space-separated.
534  
535 =item define => STRING
536  
537 Optional. If specified, then the named symbol will be defined if the program
538 ran successfully. This will either on the C compiler commandline (by passing
539 an option C<-DI<SYMBOL>>), or in the C<defines_to> file.
540  
541 =back
542  
543 =cut
544  
545 sub try_find_libs_for
546 0 ▶{
Subroutine try_find_libs_for: not called CC=4 SCAR=30.0
Pod documented
547 0 my $self = shift;
548 0 my %args = @_;
549  
550 0 ▶ ref( my $libs = $args{libs} ) eq "ARRAY" or croak "Expected 'libs' as ARRAY ref";
Branch: 1 branch
BranchTrueFalseTotal
ref(my $libs = $args{"libs"}) eq "ARRAY" or croak("Expected 'libs' as ARRAY ref") 0 0 0
551  
552 0 foreach my $l ( @$libs ) {
553 0 my @extra_linker_flags = map { "-l$_" } split m/\s+/, $l;
554  
555 0 ▶ $self->try_compile_run( %args, extra_linker_flags => \@extra_linker_flags ) or next;
Branch: 1 branch
BranchTrueFalseTotal
$self->try_compile_run(%args, "extra_linker_flags", \@extra_linker_flags) or next 0 0 0
556  
557 0 $self->push_extra_linker_flags( @extra_linker_flags );
558  
559 0 return 1;
560 }
561  
562 0 return 0;
563 }
564  
565 =head2 find_cflags_for
566  
567 $cc->find_cflags_for( %args );
568  
569 =head2 find_include_dirs_for
570  
571 $cc->find_include_dirs_for( %args );
572  
573 =head2 find_libs_for
574  
575 $cc->find_libs_for( %args );
576  
577 Calls C<try_find_cflags_for>, C<try_find_include_dirs_for> or
578 C<try_find_libs_for> respectively. If it fails, die with an
579 C<OS unsupported> message.
580  
581 Each method takes one extra optional argument:
582  
583 =over 4
584  
585 =item diag => STRING
586  
587 If present, this string will be appended to the failure message if one is
588 generated. It may provide more useful information to the user on why the OS is
589 unsupported.
590  
591 =back
592  
593 =cut
594  
595 7 foreach ( qw( find_cflags_for find_libs_for find_include_dirs_for ) ) {
596 21 my $trymethod = "try_$_";
597  
598 0 ▶ my $code = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
599 0 my $self = shift;
600 0 my %args = @_;
601  
602 0 my $diag = delete $args{diag};
603 0 ▶ $self->$trymethod( %args ) or $self->fail( $diag );
Branch: 1 branch
BranchTrueFalseTotal
$self->$trymethod(%args) or $self->fail($diag) 0 0 0
604 21 };
605  
606 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
607 21 *$_ = $code;
608 }
609  
610 =head2 extend_module_build
611  
612 $cc->extend_module_build( $build );
613  
614 I<Since version 0.11.>
615  
616 Sets the appropriate arguments into the given L<Module::Build> instance.
617  
618 =cut
619  
620 sub extend_module_build
621 3 ▶{
Subroutine extend_module_build: called CC=4 SCAR=13.9
Pod documented
622 3 my $self = shift;
623 3 my ( $build ) = @_;
624  
625 3 foreach my $key (qw( include_dirs extra_compiler_flags extra_linker_flags )) {
626 9 ▶ my @vals = @{ $self->$key } or next;
Branch: 1 branch
BranchTrueFalseTotal
my(@vals) = @{$self->$key;} or next 6 3 9
627  
628 6 push @vals, @{ $build->$key };
629  
630 # Module::Build ->include_dirs wants an ARRAYref
631 6 ▶ $build->$key( $key eq "include_dirs" ? [ @vals ] : @vals );
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'include_dirs' ? : 2 4 6
632 }
633 }
634  
635 =head2 new_module_build
636  
637 $mb = $cc->new_module_build( %args );
638  
639 Construct and return a new L<Module::Build> object, preconfigured with the
640 C<include_dirs>, C<extra_compiler_flags> and C<extra_linker_flags> options
641 that have been configured on this object, by the above methods.
642  
643 This is provided as a simple shortcut for the common use case, that a
644 F<Build.PL> file is using the C<ExtUtils::CChecker> object to detect the
645 required arguments to pass.
646  
647 =cut
648  
649 sub new_module_build
650 3 ▶{
Subroutine new_module_build: called CC=1 SCAR=0.0
Pod documented
651 3 my $self = shift;
652 3 my %args = @_;
653  
654 3 require Module::Build;
655 3 my $build = Module::Build->new( %args );
656  
657 3 $self->extend_module_build( $build );
658  
659 3 return $build;
660 }
661  
662 =head1 EXAMPLES
663  
664 =head2 Socket Libraries
665  
666 Some operating systems provide the BSD sockets API in their primary F<libc>.
667 Others keep it in a separate library which should be linked against. The
668 following example demonstrates how this would be handled.
669  
670 use ExtUtils::CChecker;
671  
672 my $cc = ExtUtils::CChecker->new;
673  
674 $cc->find_libs_for(
675 diag => "no socket()",
676 libs => [ "", "socket nsl" ],
677 source => q[
678 #include <sys/socket.h>
679 int main(int argc, char *argv) {
680 int fd = socket(PF_INET, SOCK_STREAM, 0);
681 if(fd < 0)
682 return 1;
683 return 0;
684 }
685 ] );
686  
687 $cc->new_module_build(
688 module_name => "Your::Name::Here",
689 requires => {
690 'IO::Socket' => 0,
691 },
692 ...
693 )->create_build_script;
694  
695 By using the C<new_module_build> method, the detected C<extra_linker_flags>
696 value has been automatically passed into the new C<Module::Build> object.
697  
698 =head2 Testing For Optional Features
699  
700 Sometimes a function or ability may be optionally provided by the OS, or you
701 may wish your module to be useable when only partial support is provided,
702 without requiring it all to be present. In these cases it is traditional to
703 detect the presence of this optional feature in the F<Build.PL> script, and
704 define a symbol to declare this fact if it is found. The XS code can then use
705 this symbol to select between differing implementations. For example, the
706 F<Build.PL>:
707  
708 use ExtUtils::CChecker;
709  
710 my $cc = ExtUtils::CChecker->new;
711  
712 $cc->try_compile_run(
713 define => "HAVE_MANGO",
714 source => <<'EOF' );
715 #include <mango.h>
716 #include <unistd.h>
717 int main(void) {
718 if(mango() != 0)
719 exit(1);
720 exit(0);
721 }
722 EOF
723  
724 $cc->new_module_build(
725 ...
726 )->create_build_script;
727  
728 If the C code compiles and runs successfully, and exits with a true status,
729 the symbol C<HAVE_MANGO> will be defined on the compiler commandline. This
730 allows the XS code to detect it, for example
731  
732 int
733 mango()
734 CODE:
735 #ifdef HAVE_MANGO
736 RETVAL = mango();
737 #else
738 croak("mango() not implemented");
739 #endif
740 OUTPUT:
741 RETVAL
742  
743 This module will then still compile even if the operating system lacks this
744 particular function. Trying to invoke the function at runtime will simply
745 throw an exception.
746  
747 =head2 Linux Kernel Headers
748  
749 Operating systems built on top of the F<Linux> kernel often share a looser
750 association with their kernel version than most other operating systems. It
751 may be the case that the running kernel is newer, containing more features,
752 than the distribution's F<libc> headers would believe. In such circumstances
753 it can be difficult to make use of new socket options, C<ioctl()>s, etc..
754 without having the constants that define them and their parameter structures,
755 because the relevant header files are not visible to the compiler. In this
756 case, there may be little choice but to pull in some of the kernel header
757 files, which will provide the required constants and structures.
758  
759 The Linux kernel headers can be found using the F</lib/modules> directory. A
760 fragment in F<Build.PL> like the following, may be appropriate.
761  
762 chomp( my $uname_r = `uname -r` );
763  
764 my @dirs = (
765 [],
766 [ "/lib/modules/$uname_r/source/include" ],
767 );
768  
769 $cc->find_include_dirs_for(
770 diag => "no PF_MOONLASER",
771 dirs => \@dirs,
772 source => <<'EOF' );
773 #include <sys/socket.h>
774 #include <moon/laser.h>
775 int family = PF_MOONLASER;
776 struct laserwl lwl;
777 int main(int argc, char *argv[]) {
778 return 0;
779 }
780 EOF
781  
782 This fragment will first try to compile the program as it stands, hoping that
783 the F<libc> headers will be sufficient. If it fails, it will then try
784 including the kernel headers, which should make the constant and structure
785 visible, allowing the program to compile.
786  
787 =head2 Creating an C<#include> file
788  
789 Sometimes, rather than setting defined symbols on the compiler commandline, it
790 is preferrable to have them written to a C preprocessor include (F<.h>) file.
791 This may be beneficial for cross-platform portability concerns, as not all C
792 compilers may take extra C<-D> arguments on the command line, or platforms may
793 have small length restrictions on the length of a command line.
794  
795 use ExtUtils::CChecker;
796  
797 my $cc = ExtUtils::CChecker->new(
798 defines_to => "mymodule-config.h",
799 );
800  
801 $cc->try_compile_run(
802 define => "HAVE_MANGO",
803 source => <<'EOF' );
804 #include <mango.h>
805 #include <unistd.h>
806 #include "mymodule-config.h"
807 int main(void) {
808 if(mango() != 0)
809 exit(1);
810 exit(0);
811 }
812 EOF
813  
814 Because the F<mymodule-config.h> file is written and flushed after every
815 define operation, it will still be useable in later C fragments to test for
816 features detected in earlier ones.
817  
818 It is suggested not to name the file simply F<config.h>, as the core of Perl
819 itself has a file of that name containing its own compile-time detected
820 configuration. A confusion between the two could lead to surprising results.
821  
822 =head1 AUTHOR
823  
824 Paul Evans <leonerd@leonerd.org.uk>
825  
826 =cut
827  
828 7 0x55AA;
↑ Summary