1package Config::AutoConf; 2 3use warnings; 4use strict; 5 6use base 'Exporter'; 7 8our @EXPORT = ( '$LIBEXT', '$EXEEXT' ); 9 10use constant QUOTE => do { $^O eq "MSWin32" ? q["] : q['] }; 11 12use Config; 13use Carp qw/croak/; 14 15use File::Temp qw/tempfile/; 16use File::Basename; 17use File::Spec; 18use Text::ParseWords qw//; 19 20use Capture::Tiny qw/capture/; 21 22# in core since 5.7.3 23eval "use Scalar::Util qw/looks_like_number/;"; 24__PACKAGE__->can("looks_like_number") or eval <<'EOP'; 25=begin private 26 27=head2 looks_like_number 28 29=end private 30 31=cut 32 33# from PP part of Params::Util 34sub looks_like_number { 35 local $_ = shift; 36 37 # checks from perlfaq4 38 return 0 if !defined($_); 39 if (ref($_)) { 40 return overload::Overloaded($_) ? defined(0 + $_) : 0; 41 } 42 return 1 if (/^[+-]?[0-9]+$/); # is a +/- integer 43 return 1 if (/^([+-]?)(?=[0-9]|\.[0-9])[0-9]*(\.[0-9]*)?([Ee]([+-]?[0-9]+))?$/); # a C float 44 return 1 if ($] >= 5.008 and /^(Inf(inity)?|NaN)$/i) or ($] >= 5.006001 and /^Inf$/i); 45 46 0; 47} 48EOP 49 50eval "use File::Slurp::Tiny qw/read_file/;"; 51__PACKAGE__->can("read_file") or eval <<'EOP'; 52=begin private 53 54=head2 read_file 55 56=end private 57 58=cut 59 60sub read_file { 61 my $fn = shift; 62 local $@ = ""; 63 open( my $fh, "<", $fn ) or croak "Error opening $fn: $!"; 64 my $fc = <$fh>; 65 close($fh) or croak "I/O error closing $fn: $!"; 66 return $fc; 67} 68EOP 69 70# PA-RISC1.1-thread-multi 71my %special_dlext = ( 72 darwin => ".dylib", 73 MSWin32 => ".dll", 74 ( $Config{archname} =~ m/PA-RISC/i ? ( "hpux" => ".sl" ) : () ), 75); 76 77our ( $LIBEXT, $EXEEXT ); 78 79defined $LIBEXT 80 or $LIBEXT = 81 defined $Config{so} ? "." . $Config{so} 82 : defined $special_dlext{$^O} ? $special_dlext{$^O} 83 : ".so"; 84defined $EXEEXT 85 or $EXEEXT = ( $^O eq "MSWin32" ) ? ".exe" : ""; 86 87=encoding UTF-8 88 89=head1 NAME 90 91Config::AutoConf - A module to implement some of AutoConf macros in pure perl. 92 93=cut 94 95our $VERSION = '0.311'; 96$VERSION = eval $VERSION; 97 98=head1 ABSTRACT 99 100With this module I pretend to simulate some of the tasks AutoConf 101macros do. To detect a command, to detect a library, etc. 102 103=head1 SYNOPSIS 104 105 use Config::AutoConf; 106 107 Config::AutoConf->check_prog("agrep"); 108 my $grep = Config::AutoConf->check_progs("agrep", "egrep", "grep"); 109 110 Config::AutoConf->check_header("ncurses.h"); 111 my $curses = Config::AutoConf->check_headers("ncurses.h","curses.h"); 112 113 Config::AutoConf->check_prog_awk; 114 Config::AutoConf->check_prog_egrep; 115 116 Config::AutoConf->check_cc(); 117 118 Config::AutoConf->check_lib("ncurses", "tgoto"); 119 120 Config::AutoConf->check_file("/etc/passwd"); # -f && -r 121 122=head1 DESCRIPTION 123 124Config::AutoConf is intended to provide the same opportunities to Perl 125developers as L<GNU Autoconf|http://www.gnu.org/software/autoconf/> 126does for Shell developers. 127 128As Perl is the second most deployed language (mind: every Unix comes 129with Perl, several mini-computers have Perl and even lot's of Windows 130machines run Perl software - which requires deployed Perl there, too), 131this gives wider support than Shell based probes. 132 133The API is leaned against GNU Autoconf, but we try to make the API 134(especially optional arguments) more Perl'ish than m4 abilities allow 135to the original. 136 137=head1 FUNCTIONS 138 139=cut 140 141my $glob_instance; 142 143=head2 new 144 145This function instantiates a new instance of Config::AutoConf, eg. to 146configure child components. The constructor adds also values set via 147environment variable C<PERL5_AUTOCONF_OPTS>. 148 149=cut 150 151sub new 152{ 153 my $class = shift; 154 ref $class and $class = ref $class; 155 my %args = @_; 156 157 my %flags = map { 158 my ( $k, $v ) = split( "=", $_, 2 ); 159 defined $v or $v = 1; 160 ( $k, $v ) 161 } split( ":", $ENV{PERL5_AC_OPTS} ) if ( $ENV{PERL5_AC_OPTS} ); 162 163 my %instance = ( 164 msg_prefix => 'configure: ', 165 lang => "C", 166 lang_stack => [], 167 lang_supported => { 168 "C" => $class->can("check_prog_cc"), 169 }, 170 cache => {}, 171 defines => {}, 172 extra_libs => [], 173 extra_lib_dirs => [], 174 extra_include_dirs => [], 175 extra_preprocess_flags => [], 176 extra_compile_flags => { 177 "C" => [], 178 }, 179 extra_link_flags => [], 180 logfile => "config.log", 181 c_ac_flags => {%flags}, 182 %args 183 ); 184 bless( \%instance, $class ); 185} 186 187=head2 check_file 188 189This function checks if a file exists in the system and is readable by 190the user. Returns a boolean. You can use '-f $file && -r $file' so you 191don't need to use a function call. 192 193=cut 194 195sub check_file 196{ 197 my ( $self, $file ) = @_; 198 -f $file && -r $file; 199} 200 201=head2 check_files 202 203This function checks if a set of files exist in the system and are 204readable by the user. Returns a boolean. 205 206=cut 207 208sub check_files 209{ 210 my $self = shift; 211 212 for (@_) 213 { 214 return 0 unless $self->check_file($_); 215 } 216 217 1; 218} 219 220sub _sanitize_prog 221{ 222 my ( $self, $prog ) = @_; 223 ( scalar Text::ParseWords::shellwords $prog) > 1 and $prog = QUOTE . $prog . QUOTE; 224 $prog; 225} 226 227my @exe_exts = ( $^O eq "MSWin32" ? qw(.exe .com .bat .cmd) : ("") ); 228 229=head2 check_prog( $prog, \@dirlist?, \%options? ) 230 231This function checks for a program with the supplied name. In success 232returns the full path for the executable; 233 234An optional array reference containing a list of directories to be searched 235instead of $PATH is gracefully honored. 236 237If the very last parameter contains a hash reference, C<CODE> references 238to I<action_on_true> or I<action_on_false> are executed, respectively. 239 240=cut 241 242sub check_prog 243{ 244 my $self = shift; 245 # sanitize ac_prog 246 my $ac_prog = _sanitize( shift @_ ); 247 248 my $options = {}; 249 scalar @_ > 1 and ref $_[-1] eq "HASH" and $options = pop @_; 250 251 my @dirlist; 252 @_ and scalar @_ > 1 and @dirlist = @_; 253 @_ and scalar @_ == 1 and ref $_[0] eq "ARRAY" and @dirlist = @{ $_[0] }; 254 @dirlist or @dirlist = split( /$Config{path_sep}/, $ENV{PATH} ); 255 256 for my $p (@dirlist) 257 { 258 for my $e (@exe_exts) 259 { 260 my $cmd = $self->_sanitize_prog( File::Spec->catfile( $p, $ac_prog . $e ) ); 261 -x $cmd 262 and $options->{action_on_true} 263 and ref $options->{action_on_true} eq "CODE" 264 and $options->{action_on_true}->(); 265 -x $cmd and return $cmd; 266 } 267 } 268 269 $options->{action_on_false} 270 and ref $options->{action_on_false} eq "CODE" 271 and $options->{action_on_false}->(); 272 273 return; 274} 275 276=head2 check_progs(progs, [dirlist]) 277 278This function takes a list of program names. Returns the full path for 279the first found on the system. Returns undef if none was found. 280 281An optional array reference containing a list of directories to be searched 282instead of $PATH is gracefully honored. 283 284If the very last parameter contains a hash reference, C<CODE> references 285to I<action_on_true> or I<action_on_false> are executed, respectively. The 286name of the I<$prog> to check and the found full path are passed as first 287and second argument to the I<action_on_true> callback. 288 289=cut 290 291sub check_progs 292{ 293 my $self = shift; 294 295 my $options = {}; 296 scalar @_ > 1 and ref $_[-1] eq "HASH" and $options = pop @_; 297 298 my @dirlist; 299 scalar @_ > 1 and ref $_[-1] eq "ARRAY" and @dirlist = @{ pop @_ }; 300 @dirlist or @dirlist = split( /$Config{path_sep}/, $ENV{PATH} ); 301 302 my @progs = @_; 303 foreach my $prog (@progs) 304 { 305 defined $prog or next; 306 307 my $ans = $self->check_prog( $prog, \@dirlist ); 308 $ans 309 and $options->{action_on_true} 310 and ref $options->{action_on_true} eq "CODE" 311 and $options->{action_if_true}->( $prog, $ans ); 312 313 $ans and return $ans; 314 } 315 316 $options->{action_on_false} 317 and ref $options->{action_on_false} eq "CODE" 318 and $options->{action_on_false}->(); 319 320 return; 321} 322 323sub _append_prog_args 324{ 325 my ( $self, $prog ) = @_; 326 join( " ", $self->_sanitize_prog($prog), @_ ); 327} 328 329=head2 check_prog_yacc 330 331From the autoconf documentation, 332 333 If `bison' is found, set [...] `bison -y'. 334 Otherwise, if `byacc' is found, set [...] `byacc'. 335 Otherwise set [...] `yacc'. The result of this test can be influenced 336 by setting the variable YACC or the cache variable ac_cv_prog_YACC. 337 338Returns the full path, if found. 339 340=cut 341 342sub check_prog_yacc 343{ 344 my $self = shift; 345 346 # my ($self, $cache_name, $message, $check_sub) = @_; 347 348 my $cache_name = $self->_cache_name( "prog", "YACC" ); 349 $self->check_cached( 350 $cache_name, 351 "for yacc", 352 sub { 353 defined $ENV{YACC} and return $ENV{YACC}; 354 my $binary = $self->check_progs(qw/bison byacc yacc/); 355 defined $binary 356 and $binary =~ /bison(?:\.(?:exe|com|bat|cmd))?$/ 357 and $binary = $self->_append_prog_args( $binary, "-y" ); 358 return $binary; 359 } 360 ); 361} 362 363=head2 check_prog_awk 364 365From the autoconf documentation, 366 367 Check for `gawk', `mawk', `nawk', and `awk', in that order, and 368 set output [...] to the first one that is found. It tries 369 `gawk' first because that is reported to be the best implementation. 370 The result can be overridden by setting the variable AWK or the 371 cache variable ac_cv_prog_AWK. 372 373Note that it returns the full path, if found. 374 375=cut 376 377sub check_prog_awk 378{ 379 my $self = shift; 380 my $cache_name = $self->_cache_name( "prog", "AWK" ); 381 $self->check_cached( $cache_name, "for awk", sub { $ENV{AWK} || $self->check_progs(qw/gawk mawk nawk awk/) } ); 382} 383 384=head2 check_prog_egrep 385 386From the autoconf documentation, 387 388 Check for `grep -E' and `egrep', in that order, and [...] output 389 [...] the first one that is found. The result can be overridden by 390 setting the EGREP variable and is cached in the ac_cv_path_EGREP 391 variable. 392 393Note that it returns the full path, if found. 394 395=cut 396 397sub check_prog_egrep 398{ 399 my $self = shift; 400 401 my $cache_name = $self->_cache_name( "prog", "EGREP" ); 402 $self->check_cached( 403 $cache_name, 404 "for egrep", 405 sub { 406 defined $ENV{EGREP} and return $ENV{EGREP}; 407 my $grep; 408 $grep = $self->check_progs("egrep") and return $grep; 409 410 if ( $grep = $self->check_prog("grep") ) 411 { 412 # check_run - Capture::Tiny, Open3 ... ftw! 413 my $ans = `echo a | ($grep -E '(a|b)') 2>/dev/null`; 414 chomp $ans; 415 $ans eq "a" and return $self->_append_prog_args( $grep, "-E" ); 416 } 417 } 418 ); 419} 420 421=head2 check_prog_lex 422 423From the autoconf documentation, 424 425 If flex is found, set output [...] to ‘flex’ and [...] to -lfl, if that 426 library is in a standard place. Otherwise set output [...] to ‘lex’ and 427 [...] to -ll, if found. If [...] packages [...] ship the generated 428 file.yy.c alongside the source file.l, this [...] allows users without a 429 lexer generator to still build the package even if the timestamp for 430 file.l is inadvertently changed. 431 432Note that it returns the full path, if found. 433 434The structure $self->{lex} is set with attributes 435 436 prog => $LEX 437 lib => $LEXLIB 438 root => $lex_root 439 440=cut 441 442sub check_prog_lex 443{ 444 my $self = shift->_get_instance; 445 my $cache_name = $self->_cache_name( "prog", "LEX" ); 446 my $lex = $self->check_cached( $cache_name, "for lex", sub { $ENV{LEX} || $self->check_progs(qw/flex lex/) } ); 447 if ($lex) 448 { 449 defined $self->{lex}->{prog} or $self->{lex}->{prog} = $lex; 450 my $lex_root_var = $self->check_cached( 451 "ac_cv_prog_lex_root", 452 "for lex output file root", 453 sub { 454 my ( $fh, $filename ) = tempfile( "testXXXXXX", SUFFIX => '.l' ); 455 my $src = <<'EOLEX'; 456%% 457a { ECHO; } 458b { REJECT; } 459c { yymore (); } 460d { yyless (1); } 461e { /* IRIX 6.5 flex 2.5.4 underquotes its yyless argument. */ 462 yyless ((input () != 0)); } 463f { unput (yytext[0]); } 464. { BEGIN INITIAL; } 465%% 466#ifdef YYTEXT_POINTER 467extern char *yytext; 468#endif 469int 470main (void) 471{ 472 return ! yylex () + ! yywrap (); 473} 474EOLEX 475 476 print {$fh} $src; 477 close $fh; 478 479 my ( $stdout, $stderr, $exit ) = 480 capture { system( $lex, $filename ); }; 481 chomp $stdout; 482 unlink $filename; 483 -f "lex.yy.c" and return "lex.yy"; 484 -f "lexyy.c" and return "lexyy"; 485 $self->msg_error("cannot find output from $lex; giving up"); 486 } 487 ); 488 defined $self->{lex}->{root} or $self->{lex}->{root} = $lex_root_var; 489 490 my $conftest = read_file( $lex_root_var . ".c" ); 491 unlink $lex_root_var . ".c"; 492 493 $cache_name = $self->_cache_name( "lib", "lex" ); 494 my $check_sub = sub { 495 my @save_libs = @{ $self->{extra_libs} }; 496 my $have_lib = 0; 497 foreach my $libstest ( undef, qw(-lfl -ll) ) 498 { 499 # XXX would local work on array refs? can we omit @save_libs? 500 $self->{extra_libs} = [@save_libs]; 501 defined($libstest) and unshift( @{ $self->{extra_libs} }, $libstest ); 502 $self->link_if_else($conftest) 503 and ( $have_lib = defined($libstest) ? $libstest : "none required" ) 504 and last; 505 } 506 $self->{extra_libs} = [@save_libs]; 507 508 if ($have_lib) 509 { 510 $self->define_var( _have_lib_define_name("lex"), $have_lib, "defined when lex library is available" ); 511 } 512 else 513 { 514 $self->define_var( _have_lib_define_name("lex"), undef, "defined when lex library is available" ); 515 } 516 return $have_lib; 517 }; 518 519 my $lex_lib = $self->check_cached( $cache_name, "lex library", $check_sub ); 520 defined $self->{lex}->{lib} or $self->{lex}->{lib} = $lex_lib; 521 } 522 523 $lex; 524} 525 526=head2 check_prog_sed 527 528From the autoconf documentation, 529 530 Set output variable [...] to a Sed implementation that conforms to Posix 531 and does not have arbitrary length limits. Report an error if no 532 acceptable Sed is found. See Limitations of Usual Tools, for more 533 information about portability problems with Sed. 534 535 The result of this test can be overridden by setting the SED variable and 536 is cached in the ac_cv_path_SED variable. 537 538Note that it returns the full path, if found. 539 540=cut 541 542sub check_prog_sed 543{ 544 my $self = shift; 545 my $cache_name = $self->_cache_name( "prog", "SED" ); 546 $self->check_cached( $cache_name, "for sed", sub { $ENV{SED} || $self->check_progs(qw/gsed sed/) } ); 547} 548 549=head2 check_prog_pkg_config 550 551Checks for C<pkg-config> program. No additional tests are made for it ... 552 553=cut 554 555sub check_prog_pkg_config 556{ 557 my $self = shift->_get_instance(); 558 my $cache_name = $self->_cache_name( "prog", "PKG_CONFIG" ); 559 $self->check_cached( $cache_name, "for pkg-config", sub { $self->check_prog("pkg-config") } ); 560} 561 562=head2 check_prog_cc 563 564Determine a C compiler to use. Currently the probe is delegated to L<ExtUtils::CBuilder>. 565 566=cut 567 568sub check_prog_cc 569{ 570 my $self = shift; 571 my $cache_name = $self->_cache_name( "prog", "CC" ); 572 573 $self->check_cached( 574 $cache_name, 575 "for cc", 576 sub { 577 $self->{lang_supported}->{C} = undef; 578 eval "use ExtUtils::CBuilder;"; 579 $@ and return; 580 my $cb = ExtUtils::CBuilder->new( quiet => 1 ); 581 $cb->have_compiler or return; 582 $self->{lang_supported}->{C} = "ExtUtils::CBuilder"; 583 $cb->{config}->{cc}; 584 } 585 ); 586} 587 588=head2 check_cc 589 590(Deprecated) Old name of L</check_prog_cc>. 591 592=cut 593 594sub check_cc { shift->check_prog_cc(@_) } 595 596=head2 check_valid_compiler 597 598This function checks for a valid compiler for the currently active language. 599At the very moment only C<C> is understood (corresponding to your compiler 600default options, e.g. -std=gnu89). 601 602=cut 603 604sub check_valid_compiler 605{ 606 my $self = shift->_get_instance; 607 my $lang = $self->{lang}; 608 $lang eq "C" or $self->msg_error("Language $lang is not supported"); 609 $self->check_prog_cc; 610} 611 612=head2 check_valid_compilers(;\@) 613 614Checks for valid compilers for each given language. When unspecified 615defaults to C<[ "C" ]>. 616 617=cut 618 619sub check_valid_compilers 620{ 621 my $self = shift; 622 for my $lang ( @{ $_[0] } ) 623 { 624 $self->push_lang($lang); 625 my $supp = $self->check_valid_compiler; 626 $self->pop_lang($lang); 627 $supp or return 0; 628 } 629 630 1; 631} 632 633=head2 msg_checking 634 635Prints "Checking @_ ..." 636 637=cut 638 639sub msg_checking 640{ 641 my $self = shift->_get_instance(); 642 $self->{quiet} 643 or print "Checking " . join( " ", @_ ) . "... "; 644 $self->_add_log_entry( "Checking " . join( " ", @_, "..." ) ); 645 return; 646} 647 648=head2 msg_result 649 650Prints result \n 651 652=cut 653 654my @_num_to_msg = qw/no yes/; 655 656sub _neat 657{ 658 defined $_[0] or return ""; 659 looks_like_number( $_[0] ) and defined $_num_to_msg[ $_[0] ] and return $_num_to_msg[ $_[0] ]; 660 $_[0]; 661} 662 663sub msg_result 664{ 665 my $self = shift->_get_instance(); 666 $self->{quiet} 667 or print join( " ", map { _neat $_ } @_ ), "\n"; 668 $self->_add_log_entry( join( " ", map { _neat $_ } @_ ), "\n" ); 669 return; 670} 671 672=head2 msg_notice 673 674Prints "configure: " @_ to stdout 675 676=cut 677 678sub msg_notice 679{ 680 my $self = shift->_get_instance(); 681 $self->{quiet} 682 or print $self->{msg_prefix} . join( " ", @_ ) . "\n"; 683 $self->_add_log_entry( $self->{msg_prefix} . join( " ", @_ ) . "\n" ); 684 return; 685} 686 687=head2 msg_warn 688 689Prints "configure: " @_ to stderr 690 691=cut 692 693sub msg_warn 694{ 695 my $self = shift->_get_instance(); 696 print STDERR $self->{msg_prefix} . join( " ", @_ ) . "\n"; 697 $self->_add_log_entry( "WARNING: " . $self->{msg_prefix} . join( " ", @_ ) . "\n" ); 698 return; 699} 700 701=head2 msg_error 702 703Prints "configure: " @_ to stderr and exits with exit code 0 (tells 704toolchain to stop here and report unsupported environment) 705 706=cut 707 708sub msg_error 709{ 710 my $self = shift->_get_instance(); 711 print STDERR $self->{msg_prefix} . join( " ", @_ ) . "\n"; 712 $self->_add_log_entry( "ERROR: " . $self->{msg_prefix} . join( " ", @_ ) . "\n" ); 713 exit(0); # #toolchain agreement: prevents configure stage to finish 714} 715 716=head2 msg_failure 717 718Prints "configure: " @_ to stderr and exits with exit code 0 (tells 719toolchain to stop here and report unsupported environment). Additional 720details are provides in config.log (probably more information in a 721later stage). 722 723=cut 724 725sub msg_failure 726{ 727 my $self = shift->_get_instance(); 728 print STDERR $self->{msg_prefix} . join( " ", @_ ) . "\n"; 729 $self->_add_log_entry( "FAILURE: " . $self->{msg_prefix} . join( " ", @_ ) . "\n" ); 730 exit(0); # #toolchain agreement: prevents configure stage to finish 731} 732 733=head2 define_var( $name, $value [, $comment ] ) 734 735Defines a check variable for later use in further checks or code to compile. 736Returns the value assigned value 737 738=cut 739 740sub define_var 741{ 742 my $self = shift->_get_instance(); 743 my ( $name, $value, $comment ) = @_; 744 745 defined($name) or croak("Need a name to add a define"); 746 $self->{defines}->{$name} = [ $value, $comment ]; 747 $value; 748} 749 750=head2 write_config_h( [$target] ) 751 752Writes the defined constants into given target: 753 754 Config::AutoConf->write_config_h( "config.h" ); 755 756=cut 757 758sub write_config_h 759{ 760 my $self = shift->_get_instance(); 761 my $tgt; 762 763 defined( $_[0] ) 764 ? ( 765 ref( $_[0] ) 766 ? $tgt = $_[0] 767 : open( $tgt, ">", $_[0] ) 768 ) 769 : open( $tgt, ">", "config.h" ); 770 771 my $conf_h = <<'EOC'; 772/** 773 * Generated from Config::AutoConf 774 * 775 * Do not edit this file, all modifications will be lost, 776 * modify Makefile.PL or Build.PL instead. 777 * 778 * Inspired by GNU AutoConf. 779 * 780 * (c) 2011 Alberto Simoes & Jens Rehsack 781 */ 782#ifndef __CONFIG_H__ 783 784EOC 785 786 while ( my ( $defname, $defcnt ) = each( %{ $self->{defines} } ) ) 787 { 788 if ( $defcnt->[0] ) 789 { 790 defined $defcnt->[1] and $conf_h .= "/* " . $defcnt->[1] . " */\n"; 791 $conf_h .= join( " ", "#define", $defname, $defcnt->[0] ) . "\n"; 792 } 793 else 794 { 795 defined $defcnt->[1] and $conf_h .= "/* " . $defcnt->[1] . " */\n"; 796 $conf_h .= "/* " . join( " ", "#undef", $defname ) . " */\n\n"; 797 } 798 } 799 $conf_h .= "#endif /* ?__CONFIG_H__ */\n"; 800 801 print {$tgt} $conf_h; 802 803 return; 804} 805 806=head2 push_lang(lang [, implementor ]) 807 808Puts the current used language on the stack and uses specified language 809for subsequent operations until ending pop_lang call. 810 811=cut 812 813sub push_lang 814{ 815 my $self = shift->_get_instance(); 816 817 push @{ $self->{lang_stack} }, [ $self->{lang} ]; 818 819 $self->_set_language(@_); 820} 821 822=head2 pop_lang([ lang ]) 823 824Pops the currently used language from the stack and restores previously used 825language. If I<lang> specified, it's asserted that the current used language 826equals to specified language (helps finding control flow bugs). 827 828=cut 829 830sub pop_lang 831{ 832 my $self = shift->_get_instance(); 833 834 scalar( @{ $self->{lang_stack} } ) > 0 or croak("Language stack empty"); 835 defined( $_[0] ) 836 and $self->{lang} ne $_[0] 837 and croak( "pop_lang( $_[0] ) doesn't match language in use (" . $self->{lang} . ")" ); 838 839 $self->_set_language( @{ pop @{ $self->{lang_stack} } } ); 840} 841 842=head2 lang_call( [prologue], function ) 843 844Builds program which simply calls given function. 845When given, prologue is prepended otherwise, the default 846includes are used. 847 848=cut 849 850sub lang_call 851{ 852 my ( $self, $prologue, $function ) = @_; 853 ref $self or $self = $self->_get_instance(); 854 855 defined($prologue) or $prologue = $self->_default_includes(); 856 $prologue .= <<"_ACEOF"; 857/* Override any GCC internal prototype to avoid an error. 858 Use char because int might match the return type of a GCC 859 builtin and then its argument prototype would still apply. */ 860#ifdef __cplusplus 861extern "C" { 862#endif 863char $function (); 864#ifdef __cplusplus 865} 866#endif 867_ACEOF 868 my $body = "return $function ();"; 869 $body = $self->_build_main($body); 870 871 $self->_fill_defines() . "\n$prologue\n\n$body\n"; 872} 873 874=head2 lang_build_program( prologue, body ) 875 876Builds program for current chosen language. If no prologue is given 877(I<undef>), the default headers are used. If body is missing, default 878body is used. 879 880Typical call of 881 882 Config::AutoConf->lang_build_program( "const char hw[] = \"Hello, World\\n\";", 883 "fputs (hw, stdout);" ) 884 885will create 886 887 const char hw[] = "Hello, World\n"; 888 889 /* Override any gcc2 internal prototype to avoid an error. */ 890 #ifdef __cplusplus 891 extern "C" { 892 #endif 893 894 int 895 main (int argc, char **argv) 896 { 897 (void)argc; 898 (void)argv; 899 fputs (hw, stdout);; 900 return 0; 901 } 902 903 #ifdef __cplusplus 904 } 905 #endif 906 907=cut 908 909sub lang_build_program 910{ 911 my ( $self, $prologue, $body ) = @_; 912 ref $self or $self = $self->_get_instance(); 913 914 defined($prologue) or $prologue = $self->_default_includes(); 915 defined($body) or $body = ""; 916 $body = $self->_build_main($body); 917 918 $self->_fill_defines() . "\n$prologue\n\n$body\n"; 919} 920 921=head2 lang_build_bool_test (prologue, test, [@decls]) 922 923Builds a static test which will fail to compile when test 924evaluates to false. If C<@decls> is given, it's prepended 925before the test code at the variable definition place. 926 927=cut 928 929sub lang_build_bool_test 930{ 931 my ( $self, $prologue, $test, @decls ) = @_; 932 ref $self or $self = $self->_get_instance(); 933 934 defined($test) or $test = "1"; 935 my $test_code = <<ACEOF; 936 static int test_array [($test) ? 1 : -1 ]; 937 test_array [0] = 0 938ACEOF 939 if (@decls) 940 { 941 $test_code = join( "\n", @decls, $test_code ); 942 } 943 $self->lang_build_program( $prologue, $test_code ); 944} 945 946=head2 push_includes 947 948Adds given list of directories to preprocessor/compiler 949invocation. This is not proved to allow adding directories 950which might be created during the build. 951 952=cut 953 954sub push_includes 955{ 956 my ( $self, @includes ) = @_; 957 ref $self or $self = $self->_get_instance(); 958 959 push( @{ $self->{extra_include_dirs} }, @includes ); 960 961 return; 962} 963 964=head2 push_preprocess_flags 965 966Adds given flags to the parameter list for preprocessor invocation. 967 968=cut 969 970sub push_preprocess_flags 971{ 972 my ( $self, @cpp_flags ) = @_; 973 ref $self or $self = $self->_get_instance(); 974 975 push( @{ $self->{extra_preprocess_flags} }, @cpp_flags ); 976 977 return; 978} 979 980=head2 push_compiler_flags 981 982Adds given flags to the parameter list for compiler invocation. 983 984=cut 985 986sub push_compiler_flags 987{ 988 my ( $self, @compiler_flags ) = @_; 989 ref $self or $self = $self->_get_instance(); 990 my $lang = $self->{lang}; 991 992 if ( scalar(@compiler_flags) && ( ref( $compiler_flags[-1] ) eq "HASH" ) ) 993 { 994 my $lang_opt = pop(@compiler_flags); 995 defined( $lang_opt->{lang} ) or croak("Missing lang attribute in language options"); 996 $lang = $lang_opt->{lang}; 997 defined( $self->{lang_supported}->{$lang} ) or croak("Unsupported language '$lang'"); 998 } 999 1000 push( @{ $self->{extra_compile_flags}->{$lang} }, @compiler_flags ); 1001 1002 return; 1003} 1004 1005=head2 push_libraries 1006 1007Adds given list of libraries to the parameter list for linker invocation. 1008 1009=cut 1010 1011sub push_libraries 1012{ 1013 my ( $self, @libs ) = @_; 1014 ref $self or $self = $self->_get_instance(); 1015 1016 push( @{ $self->{extra_libs} }, @libs ); 1017 1018 return; 1019} 1020 1021=head2 push_library_paths 1022 1023Adds given list of library paths to the parameter list for linker invocation. 1024 1025=cut 1026 1027sub push_library_paths 1028{ 1029 my ( $self, @libdirs ) = @_; 1030 ref $self or $self = $self->_get_instance(); 1031 1032 push( @{ $self->{extra_lib_dirs} }, @libdirs ); 1033 1034 return; 1035} 1036 1037=head2 push_link_flags 1038 1039Adds given flags to the parameter list for linker invocation. 1040 1041=cut 1042 1043sub push_link_flags 1044{ 1045 my ( $self, @link_flags ) = @_; 1046 ref $self or $self = $self->_get_instance(); 1047 1048 push( @{ $self->{extra_link_flags} }, @link_flags ); 1049 1050 return; 1051} 1052 1053=head2 compile_if_else( $src, \%options? ) 1054 1055This function tries to compile specified code and returns a boolean value 1056containing check success state. 1057 1058If the very last parameter contains a hash reference, C<CODE> references 1059to I<action_on_true> or I<action_on_false> are executed, respectively. 1060 1061=cut 1062 1063sub compile_if_else 1064{ 1065 my ( $self, $src ) = @_; 1066 ref $self or $self = $self->_get_instance(); 1067 1068 my $options = {}; 1069 scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_; 1070 1071 my $builder = $self->_get_builder(); 1072 1073 my ( $fh, $filename ) = tempfile( 1074 "testXXXXXX", 1075 SUFFIX => '.c', 1076 , UNLINK => 0 1077 ); 1078 1079 print {$fh} $src; 1080 close $fh; 1081 1082 my ( $obj_file, $outbuf, $errbuf, $exception ); 1083 ( $outbuf, $errbuf ) = capture 1084 { 1085 eval { 1086 $obj_file = $builder->compile( 1087 source => $filename, 1088 include_dirs => $self->{extra_include_dirs}, 1089 extra_compiler_flags => $self->_get_extra_compiler_flags() 1090 ); 1091 }; 1092 1093 $exception = $@; 1094 }; 1095 1096 unlink $filename; 1097 unlink $obj_file if $obj_file; 1098 1099 if ( $exception || !$obj_file ) 1100 { 1101 $self->_add_log_lines( "compile stage failed" . ( $exception ? " - " . $exception : "" ) ); 1102 $errbuf 1103 and $self->_add_log_lines($errbuf); 1104 $self->_add_log_lines( "failing program is:\n" . $src ); 1105 $outbuf 1106 and $self->_add_log_lines( "stdout was :\n" . $outbuf ); 1107 1108 $options->{action_on_false} 1109 and ref $options->{action_on_false} eq "CODE" 1110 and $options->{action_on_false}->(); 1111 1112 return 0; 1113 } 1114 1115 $options->{action_on_true} 1116 and ref $options->{action_on_true} eq "CODE" 1117 and $options->{action_on_true}->(); 1118 1119 1; 1120} 1121 1122=head2 link_if_else( $src, \%options? ) 1123 1124This function tries to compile and link specified code and returns a boolean 1125value containing check success state. 1126 1127If the very last parameter contains a hash reference, C<CODE> references 1128to I<action_on_true> or I<action_on_false> are executed, respectively. 1129 1130=cut 1131 1132sub link_if_else 1133{ 1134 my ( $self, $src ) = @_; 1135 ref $self or $self = $self->_get_instance(); 1136 my $options = {}; 1137 scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_; 1138 1139 my $builder = $self->_get_builder(); 1140 1141 my ( $fh, $filename ) = tempfile( "testXXXXXX", SUFFIX => '.c' ); 1142 1143 print {$fh} $src; 1144 close $fh; 1145 1146 my ( $obj_file, $outbuf, $errbuf, $exception ); 1147 ( $outbuf, $errbuf ) = capture 1148 { 1149 eval { 1150 $obj_file = $builder->compile( 1151 source => $filename, 1152 include_dirs => $self->{extra_include_dirs}, 1153 extra_compiler_flags => $self->_get_extra_compiler_flags() 1154 ); 1155 }; 1156 1157 $exception = $@; 1158 }; 1159 1160 if ( $exception || !$obj_file ) 1161 { 1162 $self->_add_log_lines( "compile stage failed" . ( $exception ? " - " . $exception : "" ) ); 1163 $errbuf 1164 and $self->_add_log_lines($errbuf); 1165 $self->_add_log_lines( "failing program is:\n" . $src ); 1166 $outbuf 1167 and $self->_add_log_lines( "stdout was :\n" . $outbuf ); 1168 1169 unlink $filename; 1170 unlink $obj_file if $obj_file; 1171 1172 $options->{action_on_false} 1173 and ref $options->{action_on_false} eq "CODE" 1174 and $options->{action_on_false}->(); 1175 1176 return 0; 1177 } 1178 1179 my $exe_file; 1180 ( $outbuf, $errbuf ) = capture 1181 { 1182 eval { 1183 $exe_file = $builder->link_executable( 1184 objects => $obj_file, 1185 extra_linker_flags => $self->_get_extra_linker_flags() 1186 ); 1187 }; 1188 1189 $exception = $@; 1190 }; 1191 unlink $filename; 1192 unlink $obj_file if $obj_file; 1193 unlink $exe_file if $exe_file; 1194 1195 if ( $exception || !$exe_file ) 1196 { 1197 $self->_add_log_lines( "link stage failed" . ( $exception ? " - " . $exception : "" ) ); 1198 $errbuf 1199 and $self->_add_log_lines($errbuf); 1200 $self->_add_log_lines( "failing program is:\n" . $src ); 1201 $outbuf 1202 and $self->_add_log_lines( "stdout was :\n" . $outbuf ); 1203 1204 $options->{action_on_false} 1205 and ref $options->{action_on_false} eq "CODE" 1206 and $options->{action_on_false}->(); 1207 1208 return 0; 1209 } 1210 1211 $options->{action_on_true} 1212 and ref $options->{action_on_true} eq "CODE" 1213 and $options->{action_on_true}->(); 1214 1215 1; 1216} 1217 1218=head2 check_cached( $cache-key, $check-title, \&check-call, \%options? ) 1219 1220Retrieves the result of a previous L</check_cached> invocation from 1221C<cache-key>, or (when called for the first time) populates the cache 1222by invoking C<\&check_call>. 1223 1224If the very last parameter contains a hash reference, C<CODE> references 1225to I<action_on_true> or I<action_on_false> are executed on B<every> call 1226to check_cached (not just the first cache-populating invocation), respectively. 1227 1228=cut 1229 1230sub check_cached 1231{ 1232 my ( $self, $cache_name, $message, $check_sub ) = @_; 1233 ref $self or $self = $self->_get_instance(); 1234 my $options = {}; 1235 scalar @_ > 4 and ref $_[-1] eq "HASH" and $options = pop @_; 1236 1237 $self->msg_checking($message); 1238 1239 defined $ENV{$cache_name} 1240 and not defined $self->{cache}->{$cache_name} 1241 and $self->{cache}->{$cache_name} = $ENV{$cache_name}; 1242 1243 my @cached_result; 1244 defined( $self->{cache}->{$cache_name} ) and push @cached_result, "(cached)"; 1245 defined( $self->{cache}->{$cache_name} ) or $self->{cache}->{$cache_name} = $check_sub->(); 1246 1247 $self->msg_result( @cached_result, $self->{cache}->{$cache_name} ); 1248 1249 $options->{action_on_true} 1250 and ref $options->{action_on_true} eq "CODE" 1251 and $self->{cache}->{$cache_name} 1252 and $options->{action_on_true}->(); 1253 1254 $options->{action_on_false} 1255 and ref $options->{action_on_false} eq "CODE" 1256 and !$self->{cache}->{$cache_name} 1257 and $options->{action_on_false}->(); 1258 1259 $self->{cache}->{$cache_name}; 1260} 1261 1262=head2 cache_val 1263 1264This function returns the value of a previously check_cached call. 1265 1266=cut 1267 1268sub cache_val 1269{ 1270 my ( $self, $cache_name ) = @_; 1271 ref $self or $self = $self->_get_instance(); 1272 defined $self->{cache}->{$cache_name} or return; 1273 $self->{cache}->{$cache_name}; 1274} 1275 1276=head2 check_decl( $symbol, \%options? ) 1277 1278This method actually tests whether symbol is defined as a macro or can be 1279used as an r-value, not whether it is really declared, because it is much 1280safer to avoid introducing extra declarations when they are not needed. 1281In order to facilitate use of C++ and overloaded function declarations, it 1282is possible to specify function argument types in parentheses for types 1283which can be zero-initialized: 1284 1285 Config::AutoConf->check_decl("basename(char *)") 1286 1287This method caches its result in the C<ac_cv_decl_E<lt>set langE<gt>>_symbol 1288variable. 1289 1290If the very last parameter contains a hash reference, C<CODE> references 1291to I<action_on_true> or I<action_on_false> are executed, respectively. 1292When a I<prologue> exists in the optional hash at end, it will be favoured 1293over C<default includes> (represented by L</_default_includes>). If any of 1294I<action_on_cache_true>, I<action_on_cache_false> is defined, both callbacks 1295are passed to L</check_cached> as I<action_on_true> or I<action_on_false> to 1296C<check_cached>, respectively. 1297 1298=cut 1299 1300sub check_decl 1301{ 1302 my ( $self, $symbol ) = @_; 1303 $self = $self->_get_instance(); 1304 my $options = {}; 1305 scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_; 1306 1307 defined($symbol) or return croak("No symbol to check for"); 1308 ref($symbol) eq "" or return croak("No symbol to check for"); 1309 ( my $sym_plain = $symbol ) =~ s/ *\(.*//; 1310 my $sym_call = $symbol; 1311 $sym_call =~ s/\(/((/; 1312 $sym_call =~ s/\)/) 0)/; 1313 $sym_call =~ s/,/) 0, (/g; 1314 1315 my $cache_name = $self->_cache_name( "decl", $self->{lang}, $symbol ); 1316 my $check_sub = sub { 1317 1318 my $body = <<ACEOF; 1319#ifndef $sym_plain 1320 (void) $sym_call; 1321#endif 1322ACEOF 1323 my $conftest = $self->lang_build_program( $options->{prologue}, $body ); 1324 1325 my $have_decl = $self->compile_if_else( 1326 $conftest, 1327 { 1328 ( $options->{action_on_true} ? ( action_on_true => $options->{action_on_true} ) : () ), 1329 ( $options->{action_on_false} ? ( action_on_false => $options->{action_on_false} ) : () ) 1330 } 1331 ); 1332 1333 $have_decl; 1334 }; 1335 1336 $self->check_cached( 1337 $cache_name, 1338 "whether $symbol is declared", 1339 $check_sub, 1340 { 1341 ( $options->{action_on_cache_true} ? ( action_on_true => $options->{action_on_cache_true} ) : () ), 1342 ( $options->{action_on_cache_false} ? ( action_on_false => $options->{action_on_cache_false} ) : () ) 1343 } 1344 ); 1345} 1346 1347=head2 check_decls( symbols, \%options? ) 1348 1349For each of the symbols (with optional function argument types for C++ 1350overloads), run L<check_decl>. 1351 1352Contrary to GNU autoconf, this method does not declare HAVE_DECL_symbol 1353macros for the resulting C<confdefs.h>, because it differs as C<check_decl> 1354between compiling languages. 1355 1356If the very last parameter contains a hash reference, C<CODE> references 1357to I<action_on_true> or I<action_on_false> are executed, respectively. 1358When a I<prologue> exists in the optional hash at end, it will be favoured 1359over C<default includes> (represented by L</_default_includes>). If any of 1360I<action_on_cache_true>, I<action_on_cache_false> is defined, both callbacks 1361are passed to L</check_cached> as I<action_on_true> or I<action_on_false> to 1362C<check_cached>, respectively. 1363Given callbacks for I<action_on_symbol_true> or I<action_on_symbol_false> are 1364called for each symbol checked using L</check_decl> receiving the symbol as 1365first argument. 1366 1367=cut 1368 1369sub check_decls 1370{ 1371 my ( $self, $symbols ) = @_; 1372 $self = $self->_get_instance(); 1373 my $options = {}; 1374 scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_; 1375 1376 my %pass_options; 1377 defined $options->{prologue} and $pass_options{prologue} = $options->{prologue}; 1378 defined $options->{action_on_cache_true} and $pass_options{action_on_cache_true} = $options->{action_on_cache_true}; 1379 defined $options->{action_on_cache_false} and $pass_options{action_on_cache_false} = $options->{action_on_cache_false}; 1380 1381 my $have_syms = 1; 1382 foreach my $symbol (@$symbols) 1383 { 1384 $have_syms &= $self->check_decl( 1385 $symbol, 1386 { 1387 %pass_options, 1388 ( 1389 $options->{action_on_symbol_true} && "CODE" eq ref $options->{action_on_symbol_true} 1390 ? ( action_on_true => sub { $options->{action_on_symbol_true}->($symbol) } ) 1391 : () 1392 ), 1393 ( 1394 $options->{action_on_symbol_false} && "CODE" eq ref $options->{action_on_symbol_false} 1395 ? ( action_on_false => sub { $options->{action_on_symbol_false}->($symbol) } ) 1396 : () 1397 ), 1398 } 1399 ); 1400 } 1401 1402 $have_syms 1403 and $options->{action_on_true} 1404 and ref $options->{action_on_true} eq "CODE" 1405 and $options->{action_on_true}->(); 1406 1407 $options->{action_on_false} 1408 and ref $options->{action_on_false} eq "CODE" 1409 and !$have_syms 1410 and $options->{action_on_false}->(); 1411 1412 $have_syms; 1413} 1414 1415sub _have_func_define_name 1416{ 1417 my $func = $_[0]; 1418 my $have_name = "HAVE_" . uc($func); 1419 $have_name =~ tr/_A-Za-z0-9/_/c; 1420 $have_name; 1421} 1422 1423=head2 check_func( $function, \%options? ) 1424 1425This method actually tests whether I<$funcion> can be linked into a program 1426trying to call I<$function>. This method caches its result in the 1427ac_cv_func_FUNCTION variable. 1428 1429If the very last parameter contains a hash reference, C<CODE> references 1430to I<action_on_true> or I<action_on_false> are executed, respectively. 1431If any of I<action_on_cache_true>, I<action_on_cache_false> is defined, 1432both callbacks are passed to L</check_cached> as I<action_on_true> or 1433I<action_on_false> to C<check_cached>, respectively. 1434 1435Returns: True if the function was found, false otherwise 1436 1437=cut 1438 1439sub check_func 1440{ 1441 my ( $self, $function ) = @_; 1442 $self = $self->_get_instance(); 1443 my $options = {}; 1444 scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_; 1445 1446 # Build the name of the cache variable. 1447 my $cache_name = $self->_cache_name( 'func', $function ); 1448 # Wrap the actual check in a closure so that we can use check_cached. 1449 my $check_sub = sub { 1450 my $have_func = $self->link_if_else( 1451 $self->lang_call( q{}, $function ), 1452 { 1453 ( $options->{action_on_true} ? ( action_on_true => $options->{action_on_true} ) : () ), 1454 ( $options->{action_on_false} ? ( action_on_false => $options->{action_on_false} ) : () ) 1455 } 1456 ); 1457 $have_func; 1458 }; 1459 1460 # Run the check and cache the results. 1461 return $self->check_cached( 1462 $cache_name, 1463 "for $function", 1464 $check_sub, 1465 { 1466 ( $options->{action_on_cache_true} ? ( action_on_true => $options->{action_on_cache_true} ) : () ), 1467 ( $options->{action_on_cache_false} ? ( action_on_false => $options->{action_on_cache_false} ) : () ) 1468 } 1469 ); 1470} 1471 1472=head2 check_funcs( \@functions-list, $action-if-true?, $action-if-false? ) 1473 1474The same as check_func, but takes a list of functions in I<\@functions-list> 1475to look for and checks for each in turn. Define HAVE_FUNCTION for each 1476function that was found. 1477 1478If the very last parameter contains a hash reference, C<CODE> references 1479to I<action_on_true> or I<action_on_false> are executed, respectively. 1480If any of I<action_on_cache_true>, I<action_on_cache_false> is defined, 1481both callbacks are passed to L</check_cached> as I<action_on_true> or 1482I<action_on_false> to C<check_cached>, respectively. Given callbacks 1483for I<action_on_function_true> or I<action_on_function_false> are called for 1484each symbol checked using L</check_func> receiving the symbol as first 1485argument. 1486 1487=cut 1488 1489sub check_funcs 1490{ 1491 my ( $self, $functions_ref ) = @_; 1492 $self = $self->_get_instance(); 1493 1494 my $options = {}; 1495 scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_; 1496 1497 my %pass_options; 1498 defined $options->{action_on_cache_true} and $pass_options{action_on_cache_true} = $options->{action_on_cache_true}; 1499 defined $options->{action_on_cache_false} and $pass_options{action_on_cache_false} = $options->{action_on_cache_false}; 1500 1501 # Go through the list of functions and call check_func for each one. We 1502 # generate new closures for the found and not-found functions that pass in 1503 # the relevant function name. 1504 my $have_funcs = 1; 1505 for my $function ( @{$functions_ref} ) 1506 { 1507 1508 # Build the code reference to run when a function was found. This defines 1509 # a HAVE_FUNCTION symbol, plus runs the current $action-if-true if there is 1510 # one. 1511 $pass_options{action_on_true} = sub { 1512 # XXX think about doing this always (move to check_func) 1513 $self->define_var( _have_func_define_name($function), 1, "Defined when $function is available" ); 1514 1515 # Run the user-provided hook, if there is one. 1516 defined $options->{action_on_function_true} 1517 and ref $options->{action_on_function_true} eq "CODE" 1518 and $options->{action_on_function_true}->($function); 1519 }; 1520 1521 defined $options->{action_on_function_false} 1522 and ref $options->{action_on_function_false} eq "CODE" 1523 and $pass_options{action_on_false} = sub { $options->{action_on_function_false}->($function); }; 1524 1525 $have_funcs &= check_func( $self, $function, \%pass_options ); 1526 } 1527 1528 $have_funcs 1529 and $options->{action_on_true} 1530 and ref $options->{action_on_true} eq "CODE" 1531 and $options->{action_on_true}->(); 1532 1533 $options->{action_on_false} 1534 and ref $options->{action_on_false} eq "CODE" 1535 and !$have_funcs 1536 and $options->{action_on_false}->(); 1537 1538 return $have_funcs; 1539} 1540 1541sub _have_type_define_name 1542{ 1543 my $type = $_[0]; 1544 my $have_name = "HAVE_" . uc($type); 1545 $have_name =~ tr/*/P/; 1546 $have_name =~ tr/_A-Za-z0-9/_/c; 1547 $have_name; 1548} 1549 1550=head2 check_type( $symbol, \%options? ) 1551 1552Check whether type is defined. It may be a compiler builtin type or defined 1553by the includes. In C, type must be a type-name, so that the expression 1554C<sizeof (type)> is valid (but C<sizeof ((type))> is not). 1555 1556If I<type> type is defined, preprocessor macro HAVE_I<type> (in all 1557capitals, with "*" replaced by "P" and spaces and dots replaced by 1558underscores) is defined. 1559 1560This method caches its result in the C<ac_cv_type_>type variable. 1561 1562If the very last parameter contains a hash reference, C<CODE> references 1563to I<action_on_true> or I<action_on_false> are executed, respectively. 1564When a I<prologue> exists in the optional hash at end, it will be favoured 1565over C<default includes> (represented by L</_default_includes>). If any of 1566I<action_on_cache_true>, I<action_on_cache_false> is defined, both callbacks 1567are passed to L</check_cached> as I<action_on_true> or I<action_on_false> to 1568C<check_cached>, respectively. 1569 1570=cut 1571 1572sub check_type 1573{ 1574 my ( $self, $type ) = @_; 1575 $self = $self->_get_instance(); 1576 my $options = {}; 1577 scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_; 1578 1579 defined($type) or return croak("No type to check for"); 1580 ref($type) eq "" or return croak("No type to check for"); 1581 1582 my $cache_name = $self->_cache_type_name( "type", $type ); 1583 my $check_sub = sub { 1584 1585 my $body = <<ACEOF; 1586 if( sizeof ($type) ) 1587 return 0; 1588ACEOF 1589 my $conftest = $self->lang_build_program( $options->{prologue}, $body ); 1590 1591 my $have_type = $self->compile_if_else( 1592 $conftest, 1593 { 1594 ( $options->{action_on_true} ? ( action_on_true => $options->{action_on_true} ) : () ), 1595 ( $options->{action_on_false} ? ( action_on_false => $options->{action_on_false} ) : () ) 1596 } 1597 ); 1598 $self->define_var( _have_type_define_name($type), $have_type ? $have_type : undef, "defined when $type is available" ); 1599 $have_type; 1600 }; 1601 1602 $self->check_cached( 1603 $cache_name, 1604 "for $type", 1605 $check_sub, 1606 { 1607 ( $options->{action_on_cache_true} ? ( action_on_true => $options->{action_on_cache_true} ) : () ), 1608 ( $options->{action_on_cache_false} ? ( action_on_false => $options->{action_on_cache_false} ) : () ) 1609 } 1610 ); 1611} 1612 1613=head2 check_types( \@type-list, \%options? ) 1614 1615For each type in I<@type-list>, call L<check_type> is called to check 1616for type and return the accumulated result (accumulation op is binary and). 1617 1618If the very last parameter contains a hash reference, C<CODE> references 1619to I<action_on_true> or I<action_on_false> are executed, respectively. 1620When a I<prologue> exists in the optional hash at end, it will be favoured 1621over C<default includes> (represented by L</_default_includes>). If any of 1622I<action_on_cache_true>, I<action_on_cache_false> is defined, both callbacks 1623are passed to L</check_cached> as I<action_on_true> or I<action_on_false> to 1624C<check_cached>, respectively. 1625Given callbacks for I<action_on_type_true> or I<action_on_type_false> are 1626called for each symbol checked using L</check_type> receiving the symbol as 1627first argument. 1628 1629=cut 1630 1631sub check_types 1632{ 1633 my ( $self, $types ) = @_; 1634 $self = $self->_get_instance(); 1635 my $options = {}; 1636 scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_; 1637 1638 my %pass_options; 1639 defined $options->{prologue} and $pass_options{prologue} = $options->{prologue}; 1640 defined $options->{action_on_cache_true} and $pass_options{action_on_cache_true} = $options->{action_on_cache_true}; 1641 defined $options->{action_on_cache_false} and $pass_options{action_on_cache_false} = $options->{action_on_cache_false}; 1642 1643 my $have_types = 1; 1644 foreach my $type (@$types) 1645 { 1646 $have_types &= $self->check_type( 1647 $type, 1648 { 1649 %pass_options, 1650 ( 1651 $options->{action_on_type_true} && "CODE" eq ref $options->{action_on_type_true} 1652 ? ( action_on_true => sub { $options->{action_on_type_true}->($type) } ) 1653 : () 1654 ), 1655 ( 1656 $options->{action_on_type_false} && "CODE" eq ref $options->{action_on_type_false} 1657 ? ( action_on_false => sub { $options->{action_on_type_false}->($type) } ) 1658 : () 1659 ), 1660 } 1661 ); 1662 } 1663 1664 $have_types 1665 and $options->{action_on_true} 1666 and ref $options->{action_on_true} eq "CODE" 1667 and $options->{action_on_true}->(); 1668 1669 $options->{action_on_false} 1670 and ref $options->{action_on_false} eq "CODE" 1671 and !$have_types 1672 and $options->{action_on_false}->(); 1673 1674 $have_types; 1675} 1676 1677sub _compute_int_compile 1678{ 1679 my ( $self, $expr, $prologue, @decls ) = @_; 1680 $self = $self->_get_instance(); 1681 1682 my ( $body, $conftest, $compile_result ); 1683 1684 my ( $low, $mid, $high ) = ( 0, 0, 0 ); 1685 if ( $self->compile_if_else( $self->lang_build_bool_test( $prologue, "((long int)($expr)) >= 0", @decls ) ) ) 1686 { 1687 $low = $mid = 0; 1688 while (1) 1689 { 1690 if ( $self->compile_if_else( $self->lang_build_bool_test( $prologue, "((long int)($expr)) <= $mid", @decls ) ) ) 1691 { 1692 $high = $mid; 1693 last; 1694 } 1695 $low = $mid + 1; 1696 # avoid overflow 1697 if ( $low <= $mid ) 1698 { 1699 $low = 0; 1700 last; 1701 } 1702 $mid = $low * 2; 1703 } 1704 } 1705 elsif ( $self->compile_if_else( $self->lang_build_bool_test( $prologue, "((long int)($expr)) < 0", @decls ) ) ) 1706 { 1707 $high = $mid = -1; 1708 while (1) 1709 { 1710 if ( $self->compile_if_else( $self->lang_build_bool_test( $prologue, "((long int)($expr)) >= $mid", @decls ) ) ) 1711 { 1712 $low = $mid; 1713 last; 1714 } 1715 $high = $mid - 1; 1716 # avoid overflow 1717 if ( $mid < $high ) 1718 { 1719 $high = 0; 1720 last; 1721 } 1722 $mid = $high * 2; 1723 } 1724 } 1725 1726 # perform binary search between $low and $high 1727 while ( $low <= $high ) 1728 { 1729 $mid = int( ( $high - $low ) / 2 + $low ); 1730 if ( $self->compile_if_else( $self->lang_build_bool_test( $prologue, "((long int)($expr)) < $mid", @decls ) ) ) 1731 { 1732 $high = $mid - 1; 1733 } 1734 elsif ( $self->compile_if_else( $self->lang_build_bool_test( $prologue, "((long int)($expr)) > $mid", @decls ) ) ) 1735 { 1736 $low = $mid + 1; 1737 } 1738 else 1739 { 1740 return $mid; 1741 } 1742 } 1743 1744 return; 1745} 1746 1747=head2 compute_int( $expression, @decls?, \%options ) 1748 1749Returns the value of the integer I<expression>. The value should fit in an 1750initializer in a C variable of type signed long. It should be possible 1751to evaluate the expression at compile-time. If no includes are specified, 1752the default includes are used. 1753 1754If the very last parameter contains a hash reference, C<CODE> references 1755to I<action_on_true> or I<action_on_false> are executed, respectively. 1756When a I<prologue> exists in the optional hash at end, it will be favoured 1757over C<default includes> (represented by L</_default_includes>). If any of 1758I<action_on_cache_true>, I<action_on_cache_false> is defined, both callbacks 1759are passed to L</check_cached> as I<action_on_true> or I<action_on_false> to 1760C<check_cached>, respectively. 1761 1762=cut 1763 1764sub compute_int 1765{ 1766 my $options = {}; 1767 scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_; 1768 my ( $self, $expr, @decls ) = @_; 1769 $self = $self->_get_instance(); 1770 1771 my $cache_name = $self->_cache_type_name( "compute_int", $self->{lang}, $expr ); 1772 my $check_sub = sub { 1773 my $val = $self->_compute_int_compile( $expr, $options->{prologue}, @decls ); 1774 1775 defined $val 1776 and $options->{action_on_true} 1777 and ref $options->{action_on_true} eq "CODE" 1778 and $options->{action_on_true}->(); 1779 1780 $options->{action_on_false} 1781 and ref $options->{action_on_false} eq "CODE" 1782 and !defined $val 1783 and $options->{action_on_false}->(); 1784 1785 $val; 1786 }; 1787 1788 $self->check_cached( 1789 $cache_name, 1790 "for compute result of ($expr)", 1791 $check_sub, 1792 { 1793 ( $options->{action_on_cache_true} ? ( action_on_true => $options->{action_on_cache_true} ) : () ), 1794 ( $options->{action_on_cache_false} ? ( action_on_false => $options->{action_on_cache_false} ) : () ) 1795 } 1796 ); 1797} 1798 1799sub _sizeof_type_define_name 1800{ 1801 my $type = $_[0]; 1802 my $have_name = "SIZEOF_" . uc($type); 1803 $have_name =~ tr/*/P/; 1804 $have_name =~ tr/_A-Za-z0-9/_/c; 1805 $have_name; 1806} 1807 1808=head2 check_sizeof_type( $type, \%options? ) 1809 1810Checks for the size of the specified type by compiling and define 1811C<SIZEOF_type> using the determined size. 1812 1813In opposition to GNU AutoConf, this method can determine size of structure 1814members, eg. 1815 1816 $ac->check_sizeof_type( "SV.sv_refcnt", { prologue => $include_perl } ); 1817 # or 1818 $ac->check_sizeof_type( "struct utmpx.ut_id", { prologue => "#include <utmpx.h>" } ); 1819 1820This method caches its result in the C<ac_cv_sizeof_E<lt>set langE<gt>>_type variable. 1821 1822If the very last parameter contains a hash reference, C<CODE> references 1823to I<action_on_true> or I<action_on_false> are executed, respectively. 1824When a I<prologue> exists in the optional hash at end, it will be favoured 1825over C<default includes> (represented by L</_default_includes>). If any of 1826I<action_on_cache_true>, I<action_on_cache_false> is defined, both callbacks 1827are passed to L</check_cached> as I<action_on_true> or I<action_on_false> to 1828C<check_cached>, respectively. 1829 1830=cut 1831 1832sub check_sizeof_type 1833{ 1834 my $options = {}; 1835 scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_; 1836 my ( $self, $type ) = @_; 1837 $self = $self->_get_instance(); 1838 defined($type) or return croak("No type to check for"); 1839 ref($type) eq "" or return croak("No type to check for"); 1840 1841 my $cache_name = $self->_cache_type_name( "sizeof", $self->{lang}, $type ); 1842 my $check_sub = sub { 1843 my @decls; 1844 if ( $type =~ m/^([^.]+)\.([^.]+)$/ ) 1845 { 1846 my $struct = $1; 1847 $type = "_ac_test_aggr.$2"; 1848 my $decl = "static $struct _ac_test_aggr;"; 1849 push( @decls, $decl ); 1850 } 1851 1852 my $typesize = $self->_compute_int_compile( "sizeof($type)", $options->{prologue}, @decls ); 1853 $self->define_var( 1854 _sizeof_type_define_name($type), 1855 $typesize ? $typesize : undef, 1856 "defined when sizeof($type) is available" 1857 ); 1858 1859 $typesize 1860 and $options->{action_on_true} 1861 and ref $options->{action_on_true} eq "CODE" 1862 and $options->{action_on_true}->(); 1863 1864 $options->{action_on_false} 1865 and ref $options->{action_on_false} eq "CODE" 1866 and !$typesize 1867 and $options->{action_on_false}->(); 1868 1869 $typesize; 1870 }; 1871 1872 $self->check_cached( 1873 $cache_name, 1874 "for size of $type", 1875 $check_sub, 1876 { 1877 ( $options->{action_on_cache_true} ? ( action_on_true => $options->{action_on_cache_true} ) : () ), 1878 ( $options->{action_on_cache_false} ? ( action_on_false => $options->{action_on_cache_false} ) : () ) 1879 } 1880 ); 1881} 1882 1883=head2 check_sizeof_types( type, \%options? ) 1884 1885For each type L<check_sizeof_type> is called to check for size of type. 1886 1887If I<action-if-found> is given, it is additionally executed when all of the 1888sizes of the types could determined. If I<action-if-not-found> is given, it 1889is executed when one size of the types could not determined. 1890 1891If the very last parameter contains a hash reference, C<CODE> references 1892to I<action_on_true> or I<action_on_false> are executed, respectively. 1893When a I<prologue> exists in the optional hash at end, it will be favoured 1894over C<default includes> (represented by L</_default_includes>). If any of 1895I<action_on_cache_true>, I<action_on_cache_false> is defined, both callbacks 1896are passed to L</check_cached> as I<action_on_true> or I<action_on_false> to 1897C<check_cached>, respectively. 1898Given callbacks for I<action_on_size_true> or I<action_on_size_false> are 1899called for each symbol checked using L</check_sizeof_type> receiving the 1900symbol as first argument. 1901 1902=cut 1903 1904sub check_sizeof_types 1905{ 1906 my $options = {}; 1907 scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_; 1908 my ( $self, $types ) = @_; 1909 $self = $self->_get_instance(); 1910 1911 my %pass_options; 1912 defined $options->{prologue} and $pass_options{prologue} = $options->{prologue}; 1913 defined $options->{action_on_cache_true} and $pass_options{action_on_cache_true} = $options->{action_on_cache_true}; 1914 defined $options->{action_on_cache_false} and $pass_options{action_on_cache_false} = $options->{action_on_cache_false}; 1915 1916 my $have_sizes = 1; 1917 foreach my $type (@$types) 1918 { 1919 $have_sizes &= !!( 1920 $self->check_sizeof_type( 1921 $type, 1922 { 1923 %pass_options, 1924 ( 1925 $options->{action_on_size_true} && "CODE" eq ref $options->{action_on_size_true} 1926 ? ( action_on_true => sub { $options->{action_on_size_true}->($type) } ) 1927 : () 1928 ), 1929 ( 1930 $options->{action_on_size_false} && "CODE" eq ref $options->{action_on_size_false} 1931 ? ( action_on_false => sub { $options->{action_on_size_false}->($type) } ) 1932 : () 1933 ), 1934 } 1935 ) 1936 ); 1937 } 1938 1939 $have_sizes 1940 and $options->{action_on_true} 1941 and ref $options->{action_on_true} eq "CODE" 1942 and $options->{action_on_true}->(); 1943 1944 $options->{action_on_false} 1945 and ref $options->{action_on_false} eq "CODE" 1946 and !$have_sizes 1947 and $options->{action_on_false}->(); 1948 1949 $have_sizes; 1950} 1951 1952sub _alignof_type_define_name 1953{ 1954 my $type = $_[0]; 1955 my $have_name = "ALIGNOF_" . uc($type); 1956 $have_name =~ tr/*/P/; 1957 $have_name =~ tr/_A-Za-z0-9/_/c; 1958 $have_name; 1959} 1960 1961=head2 check_alignof_type( type, \%options? ) 1962 1963Define ALIGNOF_type to be the alignment in bytes of type. I<type> must 1964be valid as a structure member declaration or I<type> must be a structure 1965member itself. 1966 1967This method caches its result in the C<ac_cv_alignof_E<lt>set langE<gt>>_type 1968variable, with I<*> mapped to C<p> and other characters not suitable for a 1969variable name mapped to underscores. 1970 1971If the very last parameter contains a hash reference, C<CODE> references 1972to I<action_on_true> or I<action_on_false> are executed, respectively. 1973When a I<prologue> exists in the optional hash at end, it will be favoured 1974over C<default includes> (represented by L</_default_includes>). If any of 1975I<action_on_cache_true>, I<action_on_cache_false> is defined, both callbacks 1976are passed to L</check_cached> as I<action_on_true> or I<action_on_false> to 1977C<check_cached>, respectively. 1978 1979=cut 1980 1981sub check_alignof_type 1982{ 1983 my $options = {}; 1984 scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_; 1985 my ( $self, $type ) = @_; 1986 $self = $self->_get_instance(); 1987 defined($type) or return croak("No type to check for"); 1988 ref($type) eq "" or return croak("No type to check for"); 1989 1990 my $cache_name = $self->_cache_type_name( "alignof", $self->{lang}, $type ); 1991 my $check_sub = sub { 1992 my @decls = ( 1993 "#ifndef offsetof", 1994 "# ifdef __ICC", 1995 "# define offsetof(type,memb) ((size_t)(((char *)(&((type*)0)->memb)) - ((char *)0)))", 1996 "# else", "# define offsetof(type,memb) ((size_t)&((type*)0)->memb)", 1997 "# endif", "#endif" 1998 ); 1999 2000 my ( $struct, $memb ); 2001 if ( $type =~ m/^([^.]+)\.([^.]+)$/ ) 2002 { 2003 $struct = $1; 2004 $memb = $2; 2005 } 2006 else 2007 { 2008 push( @decls, "typedef struct { char x; $type y; } ac__type_alignof_;" ); 2009 $struct = "ac__type_alignof_"; 2010 $memb = "y"; 2011 } 2012 2013 my $typealign = $self->_compute_int_compile( "offsetof($struct, $memb)", $options->{prologue}, @decls ); 2014 $self->define_var( 2015 _alignof_type_define_name($type), 2016 $typealign ? $typealign : undef, 2017 "defined when alignof($type) is available" 2018 ); 2019 2020 $typealign 2021 and $options->{action_on_true} 2022 and ref $options->{action_on_true} eq "CODE" 2023 and $options->{action_on_true}->(); 2024 2025 $options->{action_on_false} 2026 and ref $options->{action_on_false} eq "CODE" 2027 and !$typealign 2028 and $options->{action_on_false}->(); 2029 2030 $typealign; 2031 }; 2032 2033 $self->check_cached( 2034 $cache_name, 2035 "for align of $type", 2036 $check_sub, 2037 { 2038 ( $options->{action_on_cache_true} ? ( action_on_true => $options->{action_on_cache_true} ) : () ), 2039 ( $options->{action_on_cache_false} ? ( action_on_false => $options->{action_on_cache_false} ) : () ) 2040 } 2041 ); 2042} 2043 2044=head2 check_alignof_types (type, [action-if-found], [action-if-not-found], [prologue = default includes]) 2045 2046For each type L<check_alignof_type> is called to check for align of type. 2047 2048If I<action-if-found> is given, it is additionally executed when all of the 2049aligns of the types could determined. If I<action-if-not-found> is given, it 2050is executed when one align of the types could not determined. 2051 2052If the very last parameter contains a hash reference, C<CODE> references 2053to I<action_on_true> or I<action_on_false> are executed, respectively. 2054When a I<prologue> exists in the optional hash at end, it will be favoured 2055over C<default includes> (represented by L</_default_includes>). If any of 2056I<action_on_cache_true>, I<action_on_cache_false> is defined, both callbacks 2057are passed to L</check_cached> as I<action_on_true> or I<action_on_false> to 2058C<check_cached>, respectively. 2059Given callbacks for I<action_on_align_true> or I<action_on_align_false> are 2060called for each symbol checked using L</check_alignof_type> receiving the 2061symbol as first argument. 2062 2063=cut 2064 2065sub check_alignof_types 2066{ 2067 my $options = {}; 2068 scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_; 2069 my ( $self, $types ) = @_; 2070 $self = $self->_get_instance(); 2071 2072 my %pass_options; 2073 defined $options->{prologue} and $pass_options{prologue} = $options->{prologue}; 2074 defined $options->{action_on_cache_true} and $pass_options{action_on_cache_true} = $options->{action_on_cache_true}; 2075 defined $options->{action_on_cache_false} and $pass_options{action_on_cache_false} = $options->{action_on_cache_false}; 2076 2077 my $have_aligns = 1; 2078 foreach my $type (@$types) 2079 { 2080 $have_aligns &= !!( 2081 $self->check_alignof_type( 2082 $type, 2083 { 2084 %pass_options, 2085 ( 2086 $options->{action_on_align_true} && "CODE" eq ref $options->{action_on_align_true} 2087 ? ( action_on_true => sub { $options->{action_on_align_true}->($type) } ) 2088 : () 2089 ), 2090 ( 2091 $options->{action_on_align_false} && "CODE" eq ref $options->{action_on_align_false} 2092 ? ( action_on_false => sub { $options->{action_on_align_false}->($type) } ) 2093 : () 2094 ), 2095 } 2096 ) 2097 ); 2098 } 2099 2100 $have_aligns 2101 and $options->{action_on_true} 2102 and ref $options->{action_on_true} eq "CODE" 2103 and $options->{action_on_true}->(); 2104 2105 $options->{action_on_false} 2106 and ref $options->{action_on_false} eq "CODE" 2107 and !$have_aligns 2108 and $options->{action_on_false}->(); 2109 2110 $have_aligns; 2111} 2112 2113sub _have_member_define_name 2114{ 2115 my $member = $_[0]; 2116 my $have_name = "HAVE_" . uc($member); 2117 $have_name =~ tr/_A-Za-z0-9/_/c; 2118 $have_name; 2119} 2120 2121=head2 check_member( member, \%options? ) 2122 2123Check whether I<member> is in form of I<aggregate>.I<member> and 2124I<member> is a member of the I<aggregate> aggregate. 2125 2126which are used prior to the aggregate under test. 2127 2128 Config::AutoConf->check_member( 2129 "struct STRUCT_SV.sv_refcnt", 2130 { 2131 action_on_false => sub { Config::AutoConf->msg_failure( "sv_refcnt member required for struct STRUCT_SV" ); }, 2132 prologue => "#include <EXTERN.h>\n#include <perl.h>" 2133 } 2134 ); 2135 2136This function will return a true value (1) if the member is found. 2137 2138If I<aggregate> aggregate has I<member> member, preprocessor 2139macro HAVE_I<aggregate>_I<MEMBER> (in all capitals, with spaces 2140and dots replaced by underscores) is defined. 2141 2142This macro caches its result in the C<ac_cv_>aggr_member variable. 2143 2144If the very last parameter contains a hash reference, C<CODE> references 2145to I<action_on_true> or I<action_on_false> are executed, respectively. 2146When a I<prologue> exists in the optional hash at end, it will be favoured 2147over C<default includes> (represented by L</_default_includes>). If any of 2148I<action_on_cache_true>, I<action_on_cache_false> is defined, both callbacks 2149are passed to L</check_cached> as I<action_on_true> or I<action_on_false> to 2150C<check_cached>, respectively. 2151 2152=cut 2153 2154sub check_member 2155{ 2156 my $options = {}; 2157 scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_; 2158 my ( $self, $member ) = @_; 2159 $self = $self->_get_instance(); 2160 defined($member) or return croak("No type to check for"); 2161 ref($member) eq "" or return croak("No type to check for"); 2162 2163 $member =~ m/^([^.]+)\.([^.]+)$/ or return croak("check_member(\"struct foo.member\", \%options)"); 2164 my $type = $1; 2165 $member = $2; 2166 2167 my $cache_name = $self->_cache_type_name( "$type.$member" ); 2168 my $check_sub = sub { 2169 2170 my $body = <<ACEOF; 2171 static $type check_aggr; 2172 if( check_aggr.$member ) 2173 return 0; 2174ACEOF 2175 my $conftest = $self->lang_build_program( $options->{prologue}, $body ); 2176 2177 my $have_member = $self->compile_if_else( 2178 $conftest, 2179 { 2180 ( $options->{action_on_true} ? ( action_on_true => $options->{action_on_true} ) : () ), 2181 ( $options->{action_on_false} ? ( action_on_false => $options->{action_on_false} ) : () ) 2182 } 2183 ); 2184 $self->define_var( 2185 _have_member_define_name("$type.$member"), 2186 $have_member ? $have_member : undef, 2187 "defined when $type.$member is available" 2188 ); 2189 $have_member; 2190 }; 2191 2192 $self->check_cached( 2193 $cache_name, 2194 "for $type.$member", 2195 $check_sub, 2196 { 2197 ( $options->{action_on_cache_true} ? ( action_on_true => $options->{action_on_cache_true} ) : () ), 2198 ( $options->{action_on_cache_false} ? ( action_on_false => $options->{action_on_cache_false} ) : () ) 2199 } 2200 ); 2201} 2202 2203=head2 check_members( members, \%options? ) 2204 2205For each member L<check_member> is called to check for member of aggregate. 2206 2207This function will return a true value (1) if at least one member is found. 2208 2209If the very last parameter contains a hash reference, C<CODE> references 2210to I<action_on_true> or I<action_on_false> are executed, respectively. 2211When a I<prologue> exists in the optional hash at end, it will be favoured 2212over C<default includes> (represented by L</_default_includes>). If any of 2213I<action_on_cache_true>, I<action_on_cache_false> is defined, both callbacks 2214are passed to L</check_cached> as I<action_on_true> or I<action_on_false> to 2215C<check_cached>, respectively. 2216Given callbacks for I<action_on_member_true> or I<action_on_member_false> are 2217called for each symbol checked using L</check_member> receiving the symbol as 2218first argument. 2219 2220=cut 2221 2222sub check_members 2223{ 2224 my $options = {}; 2225 scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_; 2226 my ( $self, $members ) = @_; 2227 $self = $self->_get_instance(); 2228 2229 my %pass_options; 2230 defined $options->{prologue} and $pass_options{prologue} = $options->{prologue}; 2231 defined $options->{action_on_cache_true} and $pass_options{action_on_cache_true} = $options->{action_on_cache_true}; 2232 defined $options->{action_on_cache_false} and $pass_options{action_on_cache_false} = $options->{action_on_cache_false}; 2233 2234 my $have_members = 0; 2235 foreach my $member (@$members) 2236 { 2237 $have_members |= ( 2238 $self->check_member( 2239 $member, 2240 { 2241 %pass_options, 2242 ( 2243 $options->{action_on_member_true} && "CODE" eq ref $options->{action_on_member_true} 2244 ? ( action_on_true => sub { $options->{action_on_member_true}->($member) } ) 2245 : () 2246 ), 2247 ( 2248 $options->{action_on_member_false} && "CODE" eq ref $options->{action_on_member_false} 2249 ? ( action_on_false => sub { $options->{action_on_member_false}->($member) } ) 2250 : () 2251 ), 2252 } 2253 ) 2254 ); 2255 } 2256 2257 $have_members 2258 and $options->{action_on_true} 2259 and ref $options->{action_on_true} eq "CODE" 2260 and $options->{action_on_true}->(); 2261 2262 $options->{action_on_false} 2263 and ref $options->{action_on_false} eq "CODE" 2264 and !$have_members 2265 and $options->{action_on_false}->(); 2266 2267 $have_members; 2268} 2269 2270sub _have_header_define_name 2271{ 2272 my $header = $_[0]; 2273 my $have_name = "HAVE_" . uc($header); 2274 $have_name =~ tr/_A-Za-z0-9/_/c; 2275 return $have_name; 2276} 2277 2278sub _check_header 2279{ 2280 my $options = {}; 2281 scalar @_ > 4 and ref $_[-1] eq "HASH" and $options = pop @_; 2282 my ( $self, $header, $prologue, $body ) = @_; 2283 2284 $prologue .= <<"_ACEOF"; 2285 #include <$header> 2286_ACEOF 2287 my $conftest = $self->lang_build_program( $prologue, $body ); 2288 2289 $self->compile_if_else( $conftest, $options ); 2290} 2291 2292=head2 check_header( $header, \%options? ) 2293 2294This function is used to check if a specific header file is present in 2295the system: if we detect it and if we can compile anything with that 2296header included. Note that normally you want to check for a header 2297first, and then check for the corresponding library (not all at once). 2298 2299The standard usage for this module is: 2300 2301 Config::AutoConf->check_header("ncurses.h"); 2302 2303This function will return a true value (1) on success, and a false value 2304if the header is not present or not available for common usage. 2305 2306If the very last parameter contains a hash reference, C<CODE> references 2307to I<action_on_true> or I<action_on_false> are executed, respectively. 2308When a I<prologue> exists in the optional hash at end, it will be prepended 2309to the tested header. If any of I<action_on_cache_true>, 2310I<action_on_cache_false> is defined, both callbacks are passed to 2311L</check_cached> as I<action_on_true> or I<action_on_false> to 2312C<check_cached>, respectively. 2313 2314=cut 2315 2316sub check_header 2317{ 2318 my $options = {}; 2319 scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_; 2320 my ( $self, $header ) = @_; 2321 $self = $self->_get_instance(); 2322 defined($header) or return croak("No type to check for"); 2323 ref($header) eq "" or return croak("No type to check for"); 2324 2325 return 0 unless $header; 2326 my $cache_name = $self->_cache_name($header); 2327 my $check_sub = sub { 2328 my $prologue = defined $options->{prologue} ? $options->{prologue} : ""; 2329 2330 my $have_header = $self->_check_header( 2331 $header, 2332 $prologue, 2333 "", 2334 { 2335 ( $options->{action_on_true} ? ( action_on_true => $options->{action_on_true} ) : () ), 2336 ( $options->{action_on_false} ? ( action_on_false => $options->{action_on_false} ) : () ) 2337 } 2338 ); 2339 $self->define_var( 2340 _have_header_define_name($header), 2341 $have_header ? $have_header : undef, 2342 "defined when $header is available" 2343 ); 2344 2345 $have_header; 2346 }; 2347 2348 $self->check_cached( 2349 $cache_name, 2350 "for $header", 2351 $check_sub, 2352 { 2353 ( $options->{action_on_cache_true} ? ( action_on_true => $options->{action_on_cache_true} ) : () ), 2354 ( $options->{action_on_cache_false} ? ( action_on_false => $options->{action_on_cache_false} ) : () ) 2355 } 2356 ); 2357} 2358 2359=head2 check_headers 2360 2361This function uses check_header to check if a set of include files exist 2362in the system and can be included and compiled by the available compiler. 2363Returns the name of the first header file found. 2364 2365Passes an optional \%options hash to each L</check_header> call. 2366 2367=cut 2368 2369sub check_headers 2370{ 2371 my $options = {}; 2372 scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_; 2373 my $self = shift->_get_instance(); 2374 $self->check_header( $_, $options ) and return $_ for (@_); 2375 return; 2376} 2377 2378=head2 check_all_headers 2379 2380This function checks each given header for usability and returns true 2381when each header can be used -- otherwise false. 2382 2383If the very last parameter contains a hash reference, C<CODE> references 2384to I<action_on_true> or I<action_on_false> are executed, respectively. 2385Each of existing key/value pairs using I<prologue>, I<action_on_cache_true> 2386or I<action_on_cache_false> as key are passed-through to each call of 2387L</check_header>. 2388Given callbacks for I<action_on_header_true> or I<action_on_header_false> are 2389called for each symbol checked using L</check_header> receiving the symbol as 2390first argument. 2391 2392=cut 2393 2394sub check_all_headers 2395{ 2396 my $options = {}; 2397 scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_; 2398 my $self = shift->_get_instance(); 2399 @_ or return; 2400 2401 my %pass_options; 2402 defined $options->{prologue} and $pass_options{prologue} = $options->{prologue}; 2403 defined $options->{action_on_cache_true} and $pass_options{action_on_cache_true} = $options->{action_on_cache_true}; 2404 defined $options->{action_on_cache_false} and $pass_options{action_on_cache_false} = $options->{action_on_cache_false}; 2405 2406 my $all_headers = 1; 2407 foreach my $header (@_) 2408 { 2409 $all_headers &= $self->check_header( 2410 $header, 2411 { 2412 %pass_options, 2413 ( 2414 $options->{action_on_header_true} && "CODE" eq ref $options->{action_on_header_true} 2415 ? ( action_on_true => sub { $options->{action_on_header_true}->($header) } ) 2416 : () 2417 ), 2418 ( 2419 $options->{action_on_header_false} && "CODE" eq ref $options->{action_on_header_false} 2420 ? ( action_on_false => sub { $options->{action_on_header_false}->($header) } ) 2421 : () 2422 ), 2423 } 2424 ); 2425 } 2426 2427 $all_headers 2428 and $options->{action_on_true} 2429 and ref $options->{action_on_true} eq "CODE" 2430 and $options->{action_on_true}->(); 2431 2432 $options->{action_on_false} 2433 and ref $options->{action_on_false} eq "CODE" 2434 and !$all_headers 2435 and $options->{action_on_false}->(); 2436 2437 $all_headers; 2438} 2439 2440=head2 check_stdc_headers 2441 2442Checks for standard C89 headers, namely stdlib.h, stdarg.h, string.h and float.h. 2443If those are found, additional all remaining C89 headers are checked: assert.h, 2444ctype.h, errno.h, limits.h, locale.h, math.h, setjmp.h, signal.h, stddef.h, 2445stdio.h and time.h. 2446 2447Returns a false value if it fails. 2448 2449Passes an optional \%options hash to each L</check_all_headers> call. 2450 2451=cut 2452 2453my @ansi_c_headers = qw(stdlib stdarg string float assert ctype errno limits locale math setjmp signal stddef stdio time); 2454 2455sub check_stdc_headers 2456{ 2457 my $options = {}; 2458 scalar @_ > 1 and ref $_[-1] eq "HASH" and $options = pop @_; 2459 my $self = shift->_get_instance(); 2460 2461 # XXX for C++ the map should look like "c${_}" ... 2462 my @c_ansi_c_headers = map { "${_}.h" } @ansi_c_headers; 2463 my $rc = $self->check_all_headers( @c_ansi_c_headers, $options ); 2464 $rc and $self->define_var( "STDC_HEADERS", 1, "Define to 1 if you have the ANSI C header files." ); 2465 $rc; 2466} 2467 2468=head2 check_default_headers 2469 2470This function checks for some default headers, the std c89 headers and 2471sys/types.h, sys/stat.h, memory.h, strings.h, inttypes.h, stdint.h and unistd.h 2472 2473Passes an optional \%options hash to each L</check_all_headers> call. 2474 2475=cut 2476 2477sub check_default_headers 2478{ 2479 my $options = {}; 2480 scalar @_ > 1 and ref $_[-1] eq "HASH" and $options = pop @_; 2481 my $self = shift->_get_instance(); 2482 $self->check_stdc_headers($options) 2483 and $self->check_all_headers( qw(sys/types.h sys/stat.h memory.h strings.h inttypes.h stdint.h unistd.h), $options ); 2484} 2485 2486=head2 check_dirent_header 2487 2488Check for the following header files. For the first one that is found and 2489defines 'DIR', define the listed C preprocessor macro: 2490 2491 dirent.h HAVE_DIRENT_H 2492 sys/ndir.h HAVE_SYS_NDIR_H 2493 sys/dir.h HAVE_SYS_DIR_H 2494 ndir.h HAVE_NDIR_H 2495 2496The directory-library declarations in your source code should look 2497something like the following: 2498 2499 #include <sys/types.h> 2500 #ifdef HAVE_DIRENT_H 2501 # include <dirent.h> 2502 # define NAMLEN(dirent) strlen ((dirent)->d_name) 2503 #else 2504 # define dirent direct 2505 # define NAMLEN(dirent) ((dirent)->d_namlen) 2506 # ifdef HAVE_SYS_NDIR_H 2507 # include <sys/ndir.h> 2508 # endif 2509 # ifdef HAVE_SYS_DIR_H 2510 # include <sys/dir.h> 2511 # endif 2512 # ifdef HAVE_NDIR_H 2513 # include <ndir.h> 2514 # endif 2515 #endif 2516 2517Using the above declarations, the program would declare variables to be of 2518type C<struct dirent>, not C<struct direct>, and would access the length 2519of a directory entry name by passing a pointer to a C<struct dirent> to 2520the C<NAMLEN> macro. 2521 2522This method might be obsolescent, as all current systems with directory 2523libraries have C<<E<lt>dirent.hE<gt>>>. Programs supporting only newer OS 2524might not need to use this method. 2525 2526If the very last parameter contains a hash reference, C<CODE> references 2527to I<action_on_true> or I<action_on_false> are executed, respectively. 2528Each of existing key/value pairs using I<prologue>, I<action_on_header_true> 2529(as I<action_on_true> having the name of the tested header as first argument) 2530or I<action_on_header_false> (as I<action_on_false> having the name of the 2531tested header as first argument) as key are passed-through to each call of 2532L</_check_header>. 2533Given callbacks for I<action_on_cache_true> or I<action_on_cache_false> are 2534passed to the call of L</check_cached>. 2535 2536=cut 2537 2538sub check_dirent_header 2539{ 2540 my $options = {}; 2541 scalar @_ > 1 and ref $_[-1] eq "HASH" and $options = pop @_; 2542 my $self = shift->_get_instance(); 2543 2544 my %pass_options; 2545 defined $options->{prologue} and $pass_options{prologue} = $options->{prologue}; 2546 2547 my $cache_name = $self->_cache_name("header_dirent"); 2548 my $check_sub = sub { 2549 my $have_dirent; 2550 foreach my $header (qw(dirent.h sys/ndir.h sys/dir.h ndir.h)) 2551 { 2552 $have_dirent = $self->_check_header( 2553 $header, 2554 "#include <sys/types.h>\n", 2555 "if ((DIR *) 0) { return 0; }", 2556 { 2557 %pass_options, 2558 ( 2559 $options->{action_on_header_true} && "CODE" eq ref $options->{action_on_header_true} 2560 ? ( action_on_true => sub { $options->{action_on_header_true}->($header) } ) 2561 : () 2562 ), 2563 ( 2564 $options->{action_on_header_false} && "CODE" eq ref $options->{action_on_header_false} 2565 ? ( action_on_false => sub { $options->{action_on_header_false}->($header) } ) 2566 : () 2567 ), 2568 } 2569 ); 2570 $self->define_var( 2571 _have_header_define_name($header), 2572 $have_dirent ? $have_dirent : undef, 2573 "defined when $header is available" 2574 ); 2575 $have_dirent and $have_dirent = $header and last; 2576 } 2577 2578 $have_dirent 2579 and $options->{action_on_true} 2580 and ref $options->{action_on_true} eq "CODE" 2581 and $options->{action_on_true}->(); 2582 2583 $options->{action_on_false} 2584 and ref $options->{action_on_false} eq "CODE" 2585 and !$have_dirent 2586 and $options->{action_on_false}->(); 2587 2588 $have_dirent; 2589 }; 2590 2591 my $dirent_header = $self->check_cached( 2592 $cache_name, 2593 "for header defining DIR *", 2594 $check_sub, 2595 { 2596 ( $options->{action_on_cache_true} ? ( action_on__true => $options->{action_on_cache_true} ) : () ), 2597 ( $options->{action_on_cache_false} ? ( action_on__false => $options->{action_on_cache_false} ) : () ), 2598 } 2599 ); 2600 2601 $dirent_header; 2602} 2603 2604=head2 _check_perlapi_program 2605 2606This method provides the program source which is suitable to do basic 2607compile/link tests to prove perl development environment. 2608 2609=cut 2610 2611sub _check_perlapi_program 2612{ 2613 my $self = shift; 2614 2615 my $includes = $self->_default_includes_with_perl(); 2616 my $perl_check_body = <<'EOB'; 2617 I32 rc; 2618 SV *foo = newSVpv("Perl rocks", 11); 2619 rc = SvCUR(foo); 2620EOB 2621 $self->lang_build_program( $includes, $perl_check_body ); 2622} 2623 2624=head2 _check_compile_perlapi 2625 2626This method can be used from other checks to prove whether we have a perl 2627development environment or not (perl.h, reasonable basic checks - types, etc.) 2628 2629=cut 2630 2631sub _check_compile_perlapi 2632{ 2633 my $self = shift; 2634 2635 my $conftest = $self->_check_perlapi_program(); 2636 $self->compile_if_else($conftest); 2637} 2638 2639=head2 check_compile_perlapi 2640 2641This method can be used from other checks to prove whether we have a perl 2642development environment or not (perl.h, reasonable basic checks - types, etc.) 2643 2644=cut 2645 2646sub check_compile_perlapi 2647{ 2648 my $self = shift->_get_instance; 2649 my $cache_name = $self->_cache_name(qw(compile perlapi)); 2650 2651 $self->check_cached( $cache_name, "whether perlapi is accessible", sub { $self->_check_compile_perlapi } ); 2652} 2653 2654=head2 check_compile_perlapi_or_die 2655 2656Dies when not being able to compile using the Perl API 2657 2658=cut 2659 2660sub check_compile_perlapi_or_die 2661{ 2662 my $self = shift; 2663 $self->check_compile_perlapi(@_) or $self->msg_error("Cannot use Perl API - giving up"); 2664} 2665 2666=head2 check_linkable_xs_so 2667 2668Checks whether a dynamic loadable object containing an XS module can be 2669linked or not. Due the nature of the beast, this test currently always 2670succeed. 2671 2672=cut 2673 2674sub check_linkable_xs_so { 1 } 2675 2676=head2 check_linkable_xs_so_or_die 2677 2678Dies when L</check_linkable_xs_so> fails. 2679 2680=cut 2681 2682sub check_linkable_xs_so_or_die 2683{ 2684 my $self = shift; 2685 $self->check_linkable_xs_so(@_) or $self->msg_error("Cannot link XS dynamic loadable - giving up"); 2686} 2687 2688=head2 check_loadable_xs_so 2689 2690Checks whether a dynamic loadable object containing an XS module can be 2691loaded or not. Due the nature of the beast, this test currently always 2692succeed. 2693 2694=cut 2695 2696sub check_loadable_xs_so { 1 } 2697 2698=head2 check_loadable_xs_so_or_die 2699 2700Dies when L</check_loadable_xs_so> fails. 2701 2702=cut 2703 2704sub check_loadable_xs_so_or_die 2705{ 2706 my $self = shift; 2707 $self->check_loadable_xs_so(@_) or $self->msg_error("Cannot load XS dynamic loadable - giving up"); 2708} 2709 2710=head2 _check_link_perlapi 2711 2712This method can be used from other checks to prove whether we have a perl 2713development environment including a suitable libperl or not (perl.h, 2714reasonable basic checks - types, etc.) 2715 2716Caller must ensure that the linker flags are set appropriate (C<-lperl> 2717or similar). 2718 2719=cut 2720 2721sub _check_link_perlapi 2722{ 2723 my $self = shift; 2724 2725 my $conftest = $self->_check_perlapi_program(); 2726 my @save_libs = @{ $self->{extra_libs} }; 2727 my @save_extra_link_flags = @{ $self->{extra_link_flags} }; 2728 2729 my $libperl = $Config{libperl}; 2730 $libperl =~ s/^lib//; 2731 $libperl =~ s/\.[^\.]*$//; 2732 2733 push @{ $self->{extra_link_flags} }, "-L" . File::Spec->catdir( $Config{installarchlib}, "CORE" ); 2734 push @{ $self->{extra_libs} }, "$libperl"; 2735 if ( $Config{perllibs} ) 2736 { 2737 foreach my $perllib ( split( " ", $Config{perllibs} ) ) 2738 { 2739 $perllib =~ m/^\-l(\w+)$/ and push @{ $self->{extra_libs} }, "$1" and next; 2740 push @{ $self->{extra_link_flags} }, $perllib; 2741 } 2742 } 2743 2744 my $have_libperl = $self->link_if_else($conftest); 2745 2746 $have_libperl or $self->{extra_libs} = [@save_libs]; 2747 $have_libperl or $self->{extra_link_flags} = [@save_extra_link_flags]; 2748 2749 $have_libperl; 2750} 2751 2752=head2 check_link_perlapi 2753 2754This method can be used from other checks to prove whether we have a perl 2755development environment or not (perl.h, libperl.la, reasonable basic 2756checks - types, etc.) 2757 2758=cut 2759 2760sub check_link_perlapi 2761{ 2762 my $self = shift->_get_instance; 2763 my $cache_name = $self->_cache_name(qw(link perlapi)); 2764 2765 $self->check_cached( $cache_name, "whether perlapi is linkable", sub { $self->_check_link_perlapi } ); 2766} 2767 2768sub _have_lib_define_name 2769{ 2770 my $lib = $_[0]; 2771 my $have_name = "HAVE_LIB" . uc($lib); 2772 $have_name =~ tr/_A-Za-z0-9/_/c; 2773 return $have_name; 2774} 2775 2776=head2 check_lib( lib, func, @other-libs?, \%options? ) 2777 2778This function is used to check if a specific library includes some 2779function. Call it with the library name (without the lib portion), and 2780the name of the function you want to test: 2781 2782 Config::AutoConf->check_lib("z", "gzopen"); 2783 2784It returns 1 if the function exist, 0 otherwise. 2785 2786In case of function found, the HAVE_LIBlibrary (all in capitals) 2787preprocessor macro is defined with 1 and $lib together with @other_libs 2788are added to the list of libraries to link with. 2789 2790If linking with library results in unresolved symbols that would be 2791resolved by linking with additional libraries, give those libraries 2792as the I<other-libs> argument: e.g., C<[qw(Xt X11)]>. 2793Otherwise, this routine may fail to detect that library is present, 2794because linking the test program can fail with unresolved symbols. 2795The other-libraries argument should be limited to cases where it is 2796desirable to test for one library in the presence of another that 2797is not already in LIBS. 2798 2799This method caches its result in the C<ac_cv_lib_>lib_func variable. 2800 2801If the very last parameter contains a hash reference, C<CODE> references 2802to I<action_on_true> or I<action_on_false> are executed, respectively. 2803If any of I<action_on_cache_true>, I<action_on_cache_false> is defined, 2804both callbacks are passed to L</check_cached> as I<action_on_true> or 2805I<action_on_false> to C<check_cached>, respectively. 2806 2807It's recommended to use L<search_libs> instead of check_lib these days. 2808 2809=cut 2810 2811sub check_lib 2812{ 2813 my $options = {}; 2814 scalar @_ > 1 and ref $_[-1] eq "HASH" and $options = pop @_; 2815 my $self = shift->_get_instance(); 2816 my ( $lib, $func, @other_libs ) = @_; 2817 2818 return 0 unless $lib and $func; 2819 2820 scalar(@other_libs) == 1 2821 and ref( $other_libs[0] ) eq "ARRAY" 2822 and @other_libs = @{ $other_libs[0] }; 2823 2824 my $cache_name = $self->_cache_name( "lib", $lib, $func ); 2825 my $check_sub = sub { 2826 my $conftest = $self->lang_call( "", $func ); 2827 2828 my @save_libs = @{ $self->{extra_libs} }; 2829 push( @{ $self->{extra_libs} }, $lib, @other_libs ); 2830 my $have_lib = $self->link_if_else( 2831 $conftest, 2832 { 2833 ( $options->{action_on_true} ? ( action_on_true => $options->{action_on_true} ) : () ), 2834 ( $options->{action_on_false} ? ( action_on_false => $options->{action_on_false} ) : () ) 2835 } 2836 ); 2837 $self->{extra_libs} = [@save_libs]; 2838 2839 $have_lib 2840 and $self->define_var( _have_lib_define_name($lib), $have_lib, "defined when library $lib is available" ) 2841 and push( @{ $self->{extra_libs} }, $lib, @other_libs ); 2842 $have_lib 2843 or $self->define_var( _have_lib_define_name($lib), undef, "defined when library $lib is available" ); 2844 $have_lib; 2845 }; 2846 2847 $self->check_cached( 2848 $cache_name, 2849 "for $func in -l$lib", 2850 $check_sub, 2851 { 2852 ( $options->{action_on_cache_true} ? ( action_on_true => $options->{action_on_cache_true} ) : () ), 2853 ( $options->{action_on_cache_false} ? ( action_on_false => $options->{action_on_cache_false} ) : () ) 2854 } 2855 ); 2856} 2857 2858=head2 search_libs( function, search-libs, @other-libs?, \%options? ) 2859 2860Search for a library defining function if it's not already available. 2861This equates to calling 2862 2863 Config::AutoConf->link_if_else( 2864 Config::AutoConf->lang_call( "", "$function" ) ); 2865 2866first with no libraries, then for each library listed in search-libs. 2867I<search-libs> must be specified as an array reference to avoid 2868confusion in argument order. 2869 2870Prepend -llibrary to LIBS for the first library found to contain function. 2871 2872If linking with library results in unresolved symbols that would be 2873resolved by linking with additional libraries, give those libraries as 2874the I<other-libraries> argument: e.g., C<[qw(Xt X11)]>. Otherwise, this 2875method fails to detect that function is present, because linking the 2876test program always fails with unresolved symbols. 2877 2878The result of this test is cached in the ac_cv_search_function variable 2879as "none required" if function is already available, as C<0> if no 2880library containing function was found, otherwise as the -llibrary option 2881that needs to be prepended to LIBS. 2882 2883If the very last parameter contains a hash reference, C<CODE> references 2884to I<action_on_true> or I<action_on_false> are executed, respectively. 2885If any of I<action_on_cache_true>, I<action_on_cache_false> is defined, 2886both callbacks are passed to L</check_cached> as I<action_on_true> or 2887I<action_on_false> to C<check_cached>, respectively. Given callbacks 2888for I<action_on_lib_true> or I<action_on_lib_false> are called for 2889each library checked using L</link_if_else> receiving the library as 2890first argument and all C<@other_libs> subsequently. 2891 2892=cut 2893 2894sub search_libs 2895{ 2896 my $options = {}; 2897 scalar @_ > 1 and ref $_[-1] eq "HASH" and $options = pop @_; 2898 my $self = shift->_get_instance(); 2899 my ( $func, $libs, @other_libs ) = @_; 2900 2901 ( defined($libs) and "ARRAY" eq ref($libs) and scalar( @{$libs} ) > 0 ) 2902 or return 0; # XXX would prefer croak 2903 return 0 unless $func; 2904 2905 scalar(@other_libs) == 1 2906 and ref( $other_libs[0] ) eq "ARRAY" 2907 and @other_libs = @{ $other_libs[0] }; 2908 2909 my $cache_name = $self->_cache_name( "search", $func ); 2910 my $check_sub = sub { 2911 my $conftest = $self->lang_call( "", $func ); 2912 2913 my @save_libs = @{ $self->{extra_libs} }; 2914 my $have_lib = 0; 2915 foreach my $libstest ( undef, @$libs ) 2916 { 2917 # XXX would local work on array refs? can we omit @save_libs? 2918 $self->{extra_libs} = [@save_libs]; 2919 defined($libstest) and unshift( @{ $self->{extra_libs} }, $libstest, @other_libs ); 2920 $self->link_if_else( 2921 $conftest, 2922 { 2923 ( 2924 $options->{action_on_lib_true} && "CODE" eq ref $options->{action_on_lib_true} 2925 ? ( action_on_true => sub { $options->{action_on_lib_true}->( $libstest, @other_libs, @_ ) } ) 2926 : () 2927 ), 2928 ( 2929 $options->{action_on_lib_false} && "CODE" eq ref $options->{action_on_lib_false} 2930 ? ( action_on_false => sub { $options->{action_on_lib_false}->( $libstest, @other_libs, @_ ) } ) 2931 : () 2932 ), 2933 } 2934 ) 2935 and ( $have_lib = defined($libstest) ? $libstest : "none required" ) 2936 and last; 2937 } 2938 $self->{extra_libs} = [@save_libs]; 2939 2940 $have_lib eq "none required" or unshift( @{ $self->{extra_libs} }, $have_lib ); 2941 2942 $have_lib 2943 and $options->{action_on_true} 2944 and ref $options->{action_on_true} eq "CODE" 2945 and $options->{action_on_true}->(); 2946 2947 $options->{action_on_false} 2948 and ref $options->{action_on_false} eq "CODE" 2949 and !$have_lib 2950 and $options->{action_on_false}->(); 2951 2952 $have_lib; 2953 }; 2954 2955 return $self->check_cached( 2956 $cache_name, 2957 "for library containing $func", 2958 $check_sub, 2959 { 2960 ( $options->{action_on_cache_true} ? ( action_on_true => $options->{action_on_cache_true} ) : () ), 2961 ( $options->{action_on_cache_false} ? ( action_on_false => $options->{action_on_cache_false} ) : () ) 2962 } 2963 ); 2964} 2965 2966sub _check_lm_funcs { qw(log2 pow log10 log exp sqrt) } 2967 2968=head2 check_lm( \%options? ) 2969 2970This method is used to check if some common C<math.h> functions are 2971available, and if C<-lm> is needed. Returns the empty string if no 2972library is needed, or the "-lm" string if libm is needed. 2973 2974If the very last parameter contains a hash reference, C<CODE> references 2975to I<action_on_true> or I<action_on_false> are executed, respectively. 2976Each of existing key/value pairs using I<action_on_func_true> (as 2977I<action_on_true> having the name of the tested functions as first argument), 2978I<action_on_func_false> (as I<action_on_false> having the name of the tested 2979functions as first argument), I<action_on_func_lib_true> (as 2980I<action_on_lib_true> having the name of the tested functions as first 2981argument), I<action_on_func_lib_false> (as I<action_on_lib_false> having 2982the name of the tested functions as first argument) as key are passed- 2983through to each call of L</search_libs>. 2984Given callbacks for I<action_on_lib_true>, I<action_on_lib_false>, 2985I<action_on_cache_true> or I<action_on_cache_false> are passed to the 2986call of L</search_libs>. 2987 2988B<Note> that I<action_on_lib_true> and I<action_on_func_lib_true> or 2989I<action_on_lib_false> and I<action_on_func_lib_false> cannot be used 2990at the same time, respectively. 2991 2992=cut 2993 2994sub check_lm 2995{ 2996 my $options = {}; 2997 scalar @_ > 1 and ref $_[-1] eq "HASH" and $options = pop @_; 2998 my $self = shift->_get_instance(); 2999 3000 defined $options->{action_on_lib_true} 3001 and defined $options->{action_on_func_lib_true} 3002 and croak("action_on_lib_true and action_on_func_lib_true cannot be used together"); 3003 defined $options->{action_on_lib_false} 3004 and defined $options->{action_on_func_lib_false} 3005 and croak("action_on_lib_false and action_on_func_lib_false cannot be used together"); 3006 3007 my %pass_options; 3008 defined $options->{action_on_cache_true} and $pass_options{action_on_cache_true} = $options->{action_on_cache_true}; 3009 defined $options->{action_on_cache_false} and $pass_options{action_on_cache_false} = $options->{action_on_cache_false}; 3010 defined $options->{action_on_lib_true} and $pass_options{action_on_lib_true} = $options->{action_on_lib_true}; 3011 defined $options->{action_on_lib_false} and $pass_options{action_on_lib_false} = $options->{action_on_lib_false}; 3012 3013 my $fail = 0; 3014 my $required = ""; 3015 my @math_funcs = $self->_check_lm_funcs; 3016 for my $func (@math_funcs) 3017 { 3018 my $ans = $self->search_libs( 3019 $func, 3020 ['m'], 3021 { 3022 %pass_options, 3023 ( 3024 $options->{action_on_func_true} && "CODE" eq ref $options->{action_on_func_true} 3025 ? ( action_on_true => sub { $options->{action_on_func_true}->( $func, @_ ) } ) 3026 : () 3027 ), 3028 ( 3029 $options->{action_on_func_false} && "CODE" eq ref $options->{action_on_func_false} 3030 ? ( action_on_false => sub { $options->{action_on_func_false}->( $func, @_ ) } ) 3031 : () 3032 ), 3033 ( 3034 $options->{action_on_func_lib_true} && "CODE" eq ref $options->{action_on_func_lib_true} 3035 ? ( action_on_lib_true => sub { $options->{action_on_func_lib_true}->( $func, @_ ) } ) 3036 : () 3037 ), 3038 ( 3039 $options->{action_on_func_lib_false} && "CODE" eq ref $options->{action_on_func_lib_false} 3040 ? ( action_on_lib_false => sub { $options->{action_on_func_lib_false}->( $func, @_ ) } ) 3041 : () 3042 ), 3043 }, 3044 ); 3045 3046 $ans or $fail = 1; 3047 $ans ne "none required" and $required = $ans; 3048 } 3049 3050 !$fail 3051 and $options->{action_on_true} 3052 and ref $options->{action_on_true} eq "CODE" 3053 and $options->{action_on_true}->(); 3054 3055 $fail 3056 and $options->{action_on_false} 3057 and ref $options->{action_on_false} eq "CODE" 3058 and $options->{action_on_false}->(); 3059 3060 $required; 3061} 3062 3063=head2 pkg_config_package_flags($package, \%options?) 3064 3065Search for pkg-config flags for package as specified. The flags which are 3066extracted are C<--cflags> and C<--libs>. The extracted flags are appended 3067to the global C<extra_compile_flags> and C<extra_link_flags>, respectively. 3068 3069Call it with the package you're looking for and optional callback whether 3070found or not. 3071 3072If the very last parameter contains a hash reference, C<CODE> references 3073to I<action_on_true> or I<action_on_false> are executed, respectively. 3074If any of I<action_on_cache_true>, I<action_on_cache_false> is defined, 3075both callbacks are passed to L</check_cached> as I<action_on_true> or 3076I<action_on_false> to L</check_cached>, respectively. 3077 3078=cut 3079 3080my $_pkg_config_prog; 3081 3082sub _pkg_config_flag 3083{ 3084 defined $_pkg_config_prog or croak("pkg_config_prog required"); 3085 my @pkg_config_args = @_; 3086 my ( $stdout, $stderr, $exit ) = 3087 capture { system( $_pkg_config_prog, @pkg_config_args ); }; 3088 chomp $stdout; 3089 0 == $exit and return $stdout; 3090 return; 3091} 3092 3093sub pkg_config_package_flags 3094{ 3095 my $options = {}; 3096 scalar @_ > 1 and ref $_[-1] eq "HASH" and $options = pop @_; 3097 my ( $self, $package ) = @_; 3098 $self = $self->_get_instance(); 3099 3100 ( my $pkgpfx = $package ) =~ s/^(\w+).*?$/$1/; 3101 my $cache_name = $self->_cache_name( "pkg", $pkgpfx ); 3102 3103 defined $_pkg_config_prog or $_pkg_config_prog = $self->check_prog_pkg_config; 3104 my $check_sub = sub { 3105 my ( @pkg_cflags, @pkg_libs ); 3106 3107 ( my $ENV_CFLAGS = $package ) =~ s/^(\w+).*?$/$1_CFLAGS/; 3108 my $CFLAGS = 3109 defined $ENV{$ENV_CFLAGS} 3110 ? $ENV{$ENV_CFLAGS} 3111 : _pkg_config_flag( $package, "--cflags" ); 3112 $CFLAGS and @pkg_cflags = ( 3113 map { $_ =~ s/^\s+//; $_ =~ s/\s+$//; Text::ParseWords::shellwords $_; } 3114 split( m/\n/, $CFLAGS ) 3115 ) and push @{ $self->{extra_preprocess_flags} }, @pkg_cflags; 3116 3117 ( my $ENV_LIBS = $package ) =~ s/^(\w+).*?$/$1_LIBS/; 3118 # do not separate between libs and extra (for now) - they come with -l prepended 3119 my $LIBS = 3120 defined $ENV{$ENV_LIBS} 3121 ? $ENV{$ENV_LIBS} 3122 : _pkg_config_flag( $package, "--libs" ); 3123 $LIBS and @pkg_libs = ( 3124 map { $_ =~ s/^\s+//; $_ =~ s/\s+$//; Text::ParseWords::shellwords $_; } 3125 split( m/\n/, $LIBS ) 3126 ) and push @{ $self->{extra_link_flags} }, @pkg_libs; 3127 3128 my $pkg_config_flags = join( " ", @pkg_cflags, @pkg_libs ); 3129 3130 $pkg_config_flags 3131 and $options->{action_on_true} 3132 and ref $options->{action_on_true} eq "CODE" 3133 and $options->{action_on_true}->(); 3134 3135 $options->{action_on_false} 3136 and ref $options->{action_on_false} eq "CODE" 3137 and !$pkg_config_flags 3138 and $options->{action_on_false}->(); 3139 3140 $pkg_config_flags; 3141 }; 3142 3143 $self->check_cached( 3144 $cache_name, 3145 "for pkg-config package of $package", 3146 $check_sub, 3147 { 3148 ( $options->{action_on_cache_true} ? ( action_on_true => $options->{action_on_cache_true} ) : () ), 3149 ( $options->{action_on_cache_false} ? ( action_on_false => $options->{action_on_cache_false} ) : () ) 3150 } 3151 ); 3152} 3153 3154=head2 _check_mm_pureperl_build_wanted 3155 3156This method proves the C<_argv> attribute and (when set) the C<PERL_MM_OPT> 3157whether they contain I<PUREPERL_ONLY=(0|1)> or not. The attribute C<_force_xs> 3158is set as appropriate, which allows a compile test to bail out when C<Makefile.PL> 3159is called with I<PUREPERL_ONLY=0>. 3160 3161=cut 3162 3163sub _check_mm_pureperl_build_wanted 3164{ 3165 my $self = shift->_get_instance; 3166 3167 defined $ENV{PERL_MM_OPT} and my @env_args = split " ", $ENV{PERL_MM_OPT}; 3168 3169 foreach my $arg ( @{ $self->{_argv} }, @env_args ) 3170 { 3171 $arg =~ m/^PUREPERL_ONLY=(.*)$/ and return int($1); 3172 } 3173 3174 0; 3175} 3176 3177=head2 _check_mb_pureperl_build_wanted 3178 3179This method proves the C<_argv> attribute and (when set) the C<PERL_MB_OPT> 3180whether they contain I<--pureperl-only> or not. 3181 3182=cut 3183 3184sub _check_mb_pureperl_build_wanted 3185{ 3186 my $self = shift->_get_instance; 3187 3188 defined $ENV{PERL_MB_OPT} and my @env_args = split " ", $ENV{PERL_MB_OPT}; 3189 3190 foreach my $arg ( @{ $self->{_argv} }, @env_args ) 3191 { 3192 $arg eq "--pureperl-only" and return 1; 3193 } 3194 3195 0; 3196} 3197 3198=head2 _check_pureperl_required 3199 3200This method calls C<_check_mm_pureperl_build_wanted> when running under 3201L<ExtUtils::MakeMaker> (C<Makefile.PL>) or C<_check_mb_pureperl_build_wanted> 3202when running under a C<Build.PL> (L<Module::Build> compatible) environment. 3203 3204When neither is found (C<$0> contains neither C<Makefile.PL> nor C<Build.PL>), 3205simply 0 is returned. 3206 3207=cut 3208 3209sub _check_pureperl_required 3210{ 3211 my $self = shift; 3212 $0 =~ m/Makefile\.PL$/i and return $self->_check_mm_pureperl_build_wanted(@_); 3213 $0 =~ m/Build\.PL$/i and return $self->_check_mb_pureperl_build_wanted(@_); 3214 3215 0; 3216} 3217 3218=head2 check_pureperl_required 3219 3220This check method proves whether a pureperl build is wanted or not by 3221cached-checking C<< $self->_check_pureperl_required >>. 3222 3223=cut 3224 3225sub check_pureperl_required 3226{ 3227 my $self = shift->_get_instance; 3228 my $cache_name = $self->_cache_name(qw(pureperl required)); 3229 $self->check_cached( $cache_name, "whether pureperl is required", sub { $self->_check_pureperl_required } ); 3230} 3231 3232=head2 check_produce_xs_build 3233 3234This routine checks whether XS can be produced. Therefore it does 3235following checks in given order: 3236 3237=over 4 3238 3239=item * 3240 3241check pureperl environment variables (L</check_pureperl_required>) or 3242command line arguments and return false when pure perl is requested 3243 3244=item * 3245 3246check whether a compiler is available (L</check_valid_compilers>) and 3247return false if none found 3248 3249=item * 3250 3251check whether a test program accessing Perl API can be compiled and 3252die with error if not 3253 3254=back 3255 3256When all checks passed successfully, return a true value. 3257 3258If the very last parameter contains a hash reference, C<CODE> references 3259to I<action_on_true> or I<action_on_false> are executed, respectively. 3260 3261=cut 3262 3263sub check_produce_xs_build 3264{ 3265 my $options = {}; 3266 scalar @_ > 1 and ref $_[-1] eq "HASH" and $options = pop @_; 3267 my $self = shift->_get_instance; 3268 $self->check_pureperl_required() and return _on_return_callback_helper( 0, $options, "action_on_false" ); 3269 eval { $self->check_valid_compilers( $_[0] || [qw(C)] ) } or return _on_return_callback_helper( 0, $options, "action_on_false" ); 3270 # XXX necessary check for $Config{useshrlib}? (need to dicuss with eg. TuX, 99% likely return 0) 3271 $self->check_compile_perlapi_or_die(); 3272 3273 $options->{action_on_true} 3274 and ref $options->{action_on_true} eq "CODE" 3275 and $options->{action_on_true}->(); 3276 3277 return 1; 3278} 3279 3280=head2 check_produce_loadable_xs_build 3281 3282This routine proves whether XS should be built and it's possible to create 3283a dynamic linked object which can be loaded using Perl's Dynaloader. 3284 3285The extension over L</check_produce_xs_build> can be avoided by adding the 3286C<notest_loadable_xs> to C<$ENV{PERL5_AC_OPTS}>. 3287 3288If the very last parameter contains a hash reference, C<CODE> references 3289to I<action_on_true> or I<action_on_false> are executed, respectively. 3290 3291=cut 3292 3293sub check_produce_loadable_xs_build 3294{ 3295 my $self = shift->_get_instance; 3296 $self->check_produce_xs_build(@_) 3297 and !$self->{c_ac_flags}->{notest_loadable_xs} 3298 and $self->check_linkable_xs_so_or_die 3299 and $self->check_loadable_xs_so_or_die; 3300} 3301 3302# 3303# 3304# Auxiliary funcs 3305# 3306 3307=head2 _set_argv 3308 3309Intended to act as a helper for evaluating given command line arguments. 3310Stores given arguments in instances C<_argv> attribute. 3311 3312Call once at very begin of C<Makefile.PL> or C<Build.PL>: 3313 3314 Your::Pkg::Config::AutoConf->_set_args(@ARGV); 3315 3316=cut 3317 3318sub _set_argv 3319{ 3320 my ( $self, @argv ) = @_; 3321 $self = $self->_get_instance; 3322 $self->{_argv} = \@argv; 3323 return; 3324} 3325 3326sub _sanitize 3327{ 3328 # This is hard coded, and maybe a little stupid... 3329 my $x = shift; 3330 $x =~ s/ //g; 3331 $x =~ s/\///g; 3332 $x =~ s/\\//g; 3333 $x; 3334} 3335 3336sub _get_instance 3337{ 3338 ref $_[0] and return $_[0]; 3339 defined $glob_instance or $glob_instance = $_[0]->new(); 3340 $glob_instance; 3341} 3342 3343sub _get_builder 3344{ 3345 my $self = $_[0]->_get_instance(); 3346 3347 ref $self->{lang_supported}->{ $self->{lang} } eq "CODE" and $self->{lang_supported}->{ $self->{lang} }->($self); 3348 defined( $self->{lang_supported}->{ $self->{lang} } ) or croak( "Unsupported compile language \"" . $self->{lang} . "\"" ); 3349 3350 $self->{lang_supported}->{ $self->{lang} }->new(); 3351} 3352 3353sub _set_language 3354{ 3355 my $self = shift->_get_instance(); 3356 my ( $lang, $impl ) = @_; 3357 3358 defined($lang) or croak("Missing language"); 3359 3360 defined($impl) 3361 and defined( $self->{lang_supported}->{$lang} ) 3362 and $impl ne $self->{lang_supported}->{$lang} 3363 and croak( "Language implementor ($impl) doesn't match exisiting one (" . $self->{lang_supported}->{$lang} . ")" ); 3364 3365 defined($impl) 3366 and !defined( $self->{lang_supported}->{$lang} ) 3367 and $self->{lang_supported}->{$lang} = $impl; 3368 3369 ref $self->{lang_supported}->{$lang} eq "CODE" and $self->{lang_supported}->{$lang}->($self); 3370 defined( $self->{lang_supported}->{$lang} ) or croak("Unsupported language \"$lang\""); 3371 3372 defined( $self->{extra_compile_flags}->{$lang} ) or $self->{extra_compile_flags}->{$lang} = []; 3373 3374 $self->{lang} = $lang; 3375 3376 return; 3377} 3378 3379sub _on_return_callback_helper 3380{ 3381 my $callback = pop @_; 3382 my $options = pop @_; 3383 $options->{$callback} 3384 and ref $options->{$callback} eq "CODE" 3385 and $options->{$callback}->(); 3386 @_ and wantarray and return @_; 3387 1 == scalar @_ and return $_[0]; 3388 return; 3389} 3390 3391sub _fill_defines 3392{ 3393 my ( $self, $src, $action_if_true, $action_if_false ) = @_; 3394 ref $self or $self = $self->_get_instance(); 3395 3396 my $conftest = ""; 3397 while ( my ( $defname, $defcnt ) = each( %{ $self->{defines} } ) ) 3398 { 3399 $defcnt->[0] or next; 3400 defined $defcnt->[1] and $conftest .= "/* " . $defcnt->[1] . " */\n"; 3401 $conftest .= join( " ", "#define", $defname, $defcnt->[0] ) . "\n"; 3402 } 3403 $conftest .= "/* end of conftest.h */\n"; 3404 3405 $conftest; 3406} 3407 3408# 3409# default includes taken from autoconf/headers.m4 3410# 3411 3412=head2 _default_includes 3413 3414returns a string containing default includes for program prologue taken 3415from autoconf/headers.m4: 3416 3417 #include <stdio.h> 3418 #ifdef HAVE_SYS_TYPES_H 3419 # include <sys/types.h> 3420 #endif 3421 #ifdef HAVE_SYS_STAT_H 3422 # include <sys/stat.h> 3423 #endif 3424 #ifdef STDC_HEADERS 3425 # include <stdlib.h> 3426 # include <stddef.h> 3427 #else 3428 # ifdef HAVE_STDLIB_H 3429 # include <stdlib.h> 3430 # endif 3431 #endif 3432 #ifdef HAVE_STRING_H 3433 # if !defined STDC_HEADERS && defined HAVE_MEMORY_H 3434 # include <memory.h> 3435 # endif 3436 # include <string.h> 3437 #endif 3438 #ifdef HAVE_STRINGS_H 3439 # include <strings.h> 3440 #endif 3441 #ifdef HAVE_INTTYPES_H 3442 # include <inttypes.h> 3443 #endif 3444 #ifdef HAVE_STDINT_H 3445 # include <stdint.h> 3446 #endif 3447 #ifdef HAVE_UNISTD_H 3448 # include <unistd.h> 3449 #endif 3450 3451=cut 3452 3453my $_default_includes = <<"_ACEOF"; 3454#include <stdio.h> 3455#ifdef HAVE_SYS_TYPES_H 3456# include <sys/types.h> 3457#endif 3458#ifdef HAVE_SYS_STAT_H 3459# include <sys/stat.h> 3460#endif 3461#ifdef STDC_HEADERS 3462# include <stdlib.h> 3463# include <stddef.h> 3464#else 3465# ifdef HAVE_STDLIB_H 3466# include <stdlib.h> 3467# endif 3468#endif 3469#ifdef HAVE_STRING_H 3470# if !defined STDC_HEADERS && defined HAVE_MEMORY_H 3471# include <memory.h> 3472# endif 3473# include <string.h> 3474#endif 3475#ifdef HAVE_STRINGS_H 3476# include <strings.h> 3477#endif 3478#ifdef HAVE_INTTYPES_H 3479# include <inttypes.h> 3480#endif 3481#ifdef HAVE_STDINT_H 3482# include <stdint.h> 3483#endif 3484#ifdef HAVE_UNISTD_H 3485# include <unistd.h> 3486#endif 3487_ACEOF 3488 3489sub _default_includes { $_default_includes } 3490 3491sub _default_main { $_[0]->_build_main("") } 3492 3493my $_main_tpl = <<"_ACEOF"; 3494 int 3495 main () 3496 { 3497 %s; 3498 return 0; 3499 } 3500_ACEOF 3501 3502sub _build_main 3503{ 3504 my $self = shift->_get_instance(); 3505 my $body = shift || ""; 3506 sprintf( $_main_tpl, $body ); 3507} 3508 3509=head2 _default_includes_with_perl 3510 3511returns a string containing default includes for program prologue containing 3512I<_default_includes> plus 3513 3514 #include <EXTERN.h> 3515 #include <perl.h> 3516 3517=cut 3518 3519my $_include_perl = <<"_ACEOF"; 3520#include <EXTERN.h> 3521#include <perl.h> 3522#include <XSUB.h> /* for perl context in threaded perls */ 3523_ACEOF 3524 3525sub _default_includes_with_perl 3526{ 3527 join( "\n", $_[0]->_default_includes, $_include_perl ); 3528} 3529 3530sub _cache_prefix { "ac" } 3531 3532sub _cache_name 3533{ 3534 my ( $self, @names ) = @_; 3535 my $cache_name = join( "_", $self->_cache_prefix(), "cv", @names ); 3536 $cache_name =~ tr/_A-Za-z0-9/_/c; 3537 $cache_name; 3538} 3539 3540sub _get_log_fh 3541{ 3542 my $self = $_[0]->_get_instance(); 3543 unless ( defined( $self->{logfh} ) ) 3544 { 3545 my $open_mode = defined $self->{logfile_mode} ? $self->{logfile_mode} : ">"; 3546 open( my $fh, $open_mode, $self->{logfile} ) or croak "Could not open file $self->{logfile}: $!"; 3547 $self->{logfh} = [$fh]; 3548 } 3549 3550 $self->{logfh}; 3551} 3552 3553sub _add_log_entry 3554{ 3555 my ( $self, @logentries ) = @_; 3556 ref($self) or $self = $self->_get_instance(); 3557 $self->_get_log_fh(); 3558 foreach my $logentry (@logentries) 3559 { 3560 foreach my $fh ( @{ $self->{logfh} } ) 3561 { 3562 print {$fh} "$logentry"; 3563 } 3564 } 3565 3566 return; 3567} 3568 3569sub _add_log_lines 3570{ 3571 my ( $self, @logentries ) = @_; 3572 ref($self) or $self = $self->_get_instance(); 3573 $self->_get_log_fh(); 3574 my $logmsg = join( "\n", @logentries ) . "\n"; 3575 foreach my $fh ( @{ $self->{logfh} } ) 3576 { 3577 print {$fh} $logmsg; 3578 } 3579 3580 return; 3581} 3582 3583=head2 add_log_fh 3584 3585Push new file handles at end of log-handles to allow tee-ing log-output 3586 3587=cut 3588 3589sub add_log_fh 3590{ 3591 my ( $self, @newh ) = @_; 3592 $self->_get_log_fh(); 3593 SKIP_DUP: 3594 foreach my $fh (@newh) 3595 { 3596 foreach my $eh ( @{ $self->{logfh} } ) 3597 { 3598 $fh == $eh and next SKIP_DUP; 3599 } 3600 push @{ $self->{logfh} }, $fh; 3601 } 3602 return; 3603} 3604 3605=head2 delete_log_fh 3606 3607Removes specified log file handles. This method allows you to shoot 3608yourself in the foot - it doesn't prove whether the primary nor the last handle 3609is removed. Use with caution. 3610 3611=cut 3612 3613sub delete_log_fh 3614{ 3615 my ( $self, @xh ) = @_; 3616 $self->_get_log_fh(); 3617 SKIP_DUP: 3618 foreach my $fh (@xh) 3619 { 3620 foreach my $ih ( 0 .. $#{ $self->{logfh} } ) 3621 { 3622 $fh == $self->{logfh}->[$ih] or next; 3623 splice @{ $self->{logfh} }, $ih, 1; 3624 last; 3625 } 3626 } 3627 return; 3628} 3629 3630sub _cache_type_name 3631{ 3632 my ( $self, @names ) = @_; 3633 $self->_cache_name( map { $_ =~ tr/*/p/; $_ } @names ); 3634} 3635 3636sub _get_extra_compiler_flags 3637{ 3638 my $self = shift->_get_instance(); 3639 my @ppflags = @{ $self->{extra_preprocess_flags} }; 3640 my @cflags = @{ $self->{extra_compile_flags}->{ $self->{lang} } }; 3641 join( " ", @ppflags, @cflags ); 3642} 3643 3644sub _get_extra_linker_flags 3645{ 3646 my $self = shift->_get_instance(); 3647 my @libs = @{ $self->{extra_libs} }; 3648 my @ldflags = @{ $self->{extra_link_flags} }; 3649 join( " ", @ldflags, map { "-l$_" } @libs ); 3650} 3651 3652=head1 AUTHOR 3653 3654Alberto Simões, C<< <ambs@cpan.org> >> 3655 3656Jens Rehsack, C<< <rehsack@cpan.org> >> 3657 3658=head1 NEXT STEPS 3659 3660Although a lot of work needs to be done, these are the next steps I 3661intend to take. 3662 3663 - detect flex/lex 3664 - detect yacc/bison/byacc 3665 - detect ranlib (not sure about its importance) 3666 3667These are the ones I think not too much important, and will be 3668addressed later, or by request. 3669 3670 - detect an 'install' command 3671 - detect a 'ln -s' command -- there should be a module doing 3672 this kind of task. 3673 3674=head1 BUGS 3675 3676A lot. Portability is a pain. B<<Patches welcome!>>. 3677 3678Please report any bugs or feature requests to 3679C<bug-Config-AutoConf@rt.cpan.org>, or through the web interface at 3680L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Config-AutoConf>. We will 3681be notified, and then you'll automatically be notified of progress 3682on your bug as we make changes. 3683 3684=head1 SUPPORT 3685 3686You can find documentation for this module with the perldoc command. 3687 3688 perldoc Config::AutoConf 3689 3690You can also look for information at: 3691 3692=over 4 3693 3694=item * AnnoCPAN: Annotated CPAN documentation 3695 3696L<http://annocpan.org/dist/Config-AutoConf> 3697 3698=item * CPAN Ratings 3699 3700L<http://cpanratings.perl.org/l/Config-AutoConf> 3701 3702=item * MetaCPAN 3703 3704L<https://metacpan.org/release/Config-AutoConf> 3705 3706=item * Git Repository 3707 3708L<https://github.com/ambs/Config-AutoConf> 3709 3710=back 3711 3712=head1 ACKNOWLEDGEMENTS 3713 3714Michael Schwern for kind MacOS X help. 3715 3716Ken Williams for ExtUtils::CBuilder 3717 3718Peter Rabbitson for help on refactoring and making the API more Perl'ish 3719 3720=head1 COPYRIGHT & LICENSE 3721 3722Copyright 2004-2015 by the Authors 3723 3724This program is free software; you can redistribute it and/or modify it 3725under the same terms as Perl itself. 3726 3727=head1 SEE ALSO 3728 3729ExtUtils::CBuilder(3) 3730 3731=cut 3732 37331; # End of Config::AutoConf 3734