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 ExtUtils::CBuilder; 13 14use Config; 15use Carp qw/croak/; 16 17use File::Temp qw/tempfile/; 18use File::Basename; 19use File::Spec; 20use Text::ParseWords qw//; 21 22use Capture::Tiny qw/capture/; 23 24# in core since 5.7.3 25eval "use Scalar::Util qw/looks_like_number/;"; 26__PACKAGE__->can("looks_like_number") or eval <<'EOP'; 27=begin private 28 29=head2 looks_like_number 30 31=end private 32 33=cut 34 35# from PP part of Params::Util 36sub looks_like_number { 37 local $_ = shift; 38 39 # checks from perlfaq4 40 return 0 if !defined($_); 41 if (ref($_)) { 42 return overload::Overloaded($_) ? defined(0 + $_) : 0; 43 } 44 return 1 if (/^[+-]?[0-9]+$/); # is a +/- integer 45 return 1 if (/^([+-]?)(?=[0-9]|\.[0-9])[0-9]*(\.[0-9]*)?([Ee]([+-]?[0-9]+))?$/); # a C float 46 return 1 if ($] >= 5.008 and /^(Inf(inity)?|NaN)$/i) or ($] >= 5.006001 and /^Inf$/i); 47 48 0; 49} 50EOP 51 52eval "use File::Slurp::Tiny qw/read_file/;"; 53__PACKAGE__->can("read_file") or eval <<'EOP'; 54=begin private 55 56=head2 read_file 57 58=end private 59 60=cut 61 62sub read_file { 63 my $fn = shift; 64 local $@ = ""; 65 open( my $fh, "<", $fn ) or croak "Error opening $fn: $!"; 66 my $fc = <$fh>; 67 close($fh) or croak "I/O error closing $fn: $!"; 68 return $fc; 69} 70EOP 71 72# PA-RISC1.1-thread-multi 73my %special_dlext = ( 74 darwin => ".dylib", 75 MSWin32 => ".dll", 76 ($Config{archname} =~ m/PA-RISC/i ? ("hpux" => ".sl") : ()), 77); 78 79our ($LIBEXT, $EXEEXT); 80 81defined $LIBEXT 82 or $LIBEXT = defined $Config{so} ? "." . $Config{so} : 83 defined $special_dlext{$^O} ? $special_dlext{$^O} : ".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.305'; 96 97=head1 ABSTRACT 98 99With this module I pretend to simulate some of the tasks AutoConf 100macros do. To detect a command, to detect a library, etc. 101 102=head1 SYNOPSIS 103 104 use Config::AutoConf; 105 106 Config::AutoConf->check_prog("agrep"); 107 my $grep = Config::AutoConf->check_progs("agrep", "egrep", "grep"); 108 109 Config::AutoConf->check_header("ncurses.h"); 110 my $curses = Config::AutoConf->check_headers("ncurses.h","curses.h"); 111 112 Config::AutoConf->check_prog_awk; 113 Config::AutoConf->check_prog_egrep; 114 115 Config::AutoConf->check_cc(); 116 117 Config::AutoConf->check_lib("ncurses", "tgoto"); 118 119 Config::AutoConf->check_file("/etc/passwd"); # -f && -r 120 121=head1 FUNCTIONS 122 123=cut 124 125my $glob_instance; 126 127=head2 new 128 129This function instantiates a new instance of Config::AutoConf, eg. to 130configure child components. The contructor adds also values set via 131environment variable C<PERL5_AUTOCONF_OPTS>. 132 133=cut 134 135sub new { 136 my $class = shift; 137 ref $class and $class = ref $class; 138 my %args = @_; 139 140 my %flags = map { 141 my ($k, $v) = split("=", $_, 2); defined $v or $v = 1; ($k, $v) 142 } split( ":", $ENV{PERL5_AUTOCONF_OPTS} ) if($ENV{PERL5_AUTOCONF_OPTS}); 143 144 my %instance = ( 145 msg_prefix => 'configure: ', 146 lang => "C", 147 lang_stack => [], 148 lang_supported => { 149 "C" => "ExtUtils::CBuilder", 150 }, 151 cache => {}, 152 defines => {}, 153 extra_libs => [], 154 extra_lib_dirs => [], 155 extra_include_dirs => [], 156 extra_preprocess_flags => [], 157 extra_compile_flags => { 158 "C" => [], 159 }, 160 extra_link_flags => [], 161 logfile => "config.log", 162 c_ac_flags => {%flags}, 163 %args 164 ); 165 bless( \%instance, $class ); 166} 167 168=head2 check_file 169 170This function checks if a file exists in the system and is readable by 171the user. Returns a boolean. You can use '-f $file && -r $file' so you 172don't need to use a function call. 173 174=cut 175 176sub check_file { 177 my ($self, $file) = @_; 178 -f $file && -r $file; 179} 180 181 182=head2 check_files 183 184This function checks if a set of files exist in the system and are 185readable by the user. Returns a boolean. 186 187=cut 188 189sub check_files { 190 my $self = shift; 191 192 for (@_) { 193 return 0 unless $self->check_file($_) 194 } 195 196 1; 197} 198 199sub _sanitize_prog { 200 my ($self, $prog) = @_; 201 (scalar Text::ParseWords::shellwords $prog) > 1 and $prog = QUOTE . $prog . QUOTE; 202 $prog; 203} 204 205my @exe_exts = ( $^O eq "MSWin32" ? qw(.exe .com .bat .cmd) : ("") ); 206 207=head2 check_prog(prog,[dirlist]) 208 209This function checks for a program with the supplied name. In success 210returns the full path for the executable; 211 212An optional array reference containing a list of directories to be searched 213instead of $PATH is gracefully honored. 214 215=cut 216 217sub check_prog { 218 my $self = shift; 219 # sanitize ac_prog 220 my $ac_prog = _sanitize(shift @_); 221 my @dirlist; 222 @_ and scalar @_ > 1 and @dirlist = @_; 223 @_ and scalar @_ == 1 and ref $_[0] eq "ARRAY" and @dirlist = @{$_[0]}; 224 @dirlist or @dirlist = split(/$Config{path_sep}/,$ENV{PATH}); 225 226 for my $p (@dirlist) { 227 for my $e (@exe_exts) { 228 my $cmd = $self->_sanitize_prog(File::Spec->catfile($p,$ac_prog.$e)); 229 return $cmd if -x $cmd; 230 } 231 } 232 return; 233} 234 235=head2 check_progs(progs, [dirlist]) 236 237This function takes a list of program names. Returns the full path for 238the first found on the system. Returns undef if none was found. 239 240An optional array reference containing a list of directories to be searched 241instead of $PATH is gracefully honored. 242 243=cut 244 245sub check_progs { 246 my $self = shift; 247 my @dirlist; 248 scalar @_ > 1 and ref $_[-1] eq "ARRAY" and @dirlist = @{pop @_}; 249 @dirlist or @dirlist = split(/$Config{path_sep}/,$ENV{PATH}); 250 251 my @progs = @_; 252 for (@progs) { 253 defined $_ or next; 254 my $ans = $self->check_prog($_, \@dirlist); 255 return $ans if $ans; 256 } 257 return; 258} 259 260sub _append_prog_args { 261 my ($self, $prog) = @_; 262 join(" ", $self->_sanitize_prog($prog), @_); 263} 264 265=head2 check_prog_yacc 266 267From the autoconf documentation, 268 269 If `bison' is found, set [...] `bison -y'. 270 Otherwise, if `byacc' is found, set [...] `byacc'. 271 Otherwise set [...] `yacc'. The result of this test can be influenced 272 by setting the variable YACC or the cache variable ac_cv_prog_YACC. 273 274Returns the full path, if found. 275 276=cut 277 278sub check_prog_yacc { 279 my $self = shift; 280 281# my ($self, $cache_name, $message, $check_sub) = @_; 282 283 my $cache_name = $self->_cache_name("prog", "YACC"); 284 $self->check_cached( $cache_name, "for yacc", 285 sub { 286 defined $ENV{YACC} and return $ENV{YACC}; 287 my $binary = $self->check_progs(qw/bison byacc yacc/); 288 defined $binary and $binary =~ /bison(?:\.(?:exe|com|bat|cmd))?$/ 289 and $binary = $self->_append_prog_args($binary, "-y"); 290 return $binary; 291 } ); 292} 293 294=head2 check_prog_awk 295 296From the autoconf documentation, 297 298 Check for `gawk', `mawk', `nawk', and `awk', in that order, and 299 set output [...] to the first one that is found. It tries 300 `gawk' first because that is reported to be the best implementation. 301 The result can be overridden by setting the variable AWK or the 302 cache variable ac_cv_prog_AWK. 303 304Note that it returns the full path, if found. 305 306=cut 307 308sub check_prog_awk { 309 my $self = shift; 310 my $cache_name = $self->_cache_name("prog", "AWK"); 311 $self->check_cached( $cache_name, "for awk", 312 sub {$ENV{AWK} || $self->check_progs(qw/gawk mawk nawk awk/)} ); 313} 314 315 316=head2 check_prog_egrep 317 318From the autoconf documentation, 319 320 Check for `grep -E' and `egrep', in that order, and [...] output 321 [...] the first one that is found. The result can be overridden by 322 setting the EGREP variable and is cached in the ac_cv_path_EGREP 323 variable. 324 325Note that it returns the full path, if found. 326 327=cut 328 329sub check_prog_egrep { 330 my $self = shift; 331 332 my $cache_name = $self->_cache_name("prog", "EGREP"); 333 $self->check_cached( $cache_name, "for egrep", 334 sub { 335 defined $ENV{EGREP} and return $ENV{EGREP}; 336 my $grep; 337 $grep = $self->check_progs("egrep") and return $grep; 338 339 if ($grep = $self->check_prog("grep")) { 340 # check_run - Capture::Tiny, Open3 ... ftw! 341 my $ans = `echo a | ($grep -E '(a|b)') 2>/dev/null`; 342 chomp $ans; 343 $ans eq "a" and return $self->_append_prog_args($grep, "-E"); 344 } 345 } ); 346} 347 348=head2 check_prog_lex 349 350From the autoconf documentation, 351 352 If flex is found, set output [...] to ‘flex’ and [...] to -lfl, if that 353 library is in a standard place. Otherwise set output [...] to ‘lex’ and 354 [...] to -ll, if found. If [...] packages [...] ship the generated 355 file.yy.c alongside the source file.l, this [...] allows users without a 356 lexer generator to still build the package even if the timestamp for 357 file.l is inadvertently changed. 358 359Note that it returns the full path, if found. 360 361The structure $self->{lex} is set with attributes 362 363 prog => $LEX 364 lib => $LEXLIB 365 root => $lex_root 366 367=cut 368 369sub check_prog_lex { 370 my $self = shift->_get_instance; 371 my $cache_name = $self->_cache_name("prog", "LEX"); 372 my $lex = $self->check_cached( $cache_name, "for lex", 373 sub {$ENV{LEX} || $self->check_progs(qw/flex lex/)} ); 374 if($lex) { 375 defined $self->{lex}->{prog} or $self->{lex}->{prog} = $lex; 376 my $lex_root_var = $self->check_cached( "ac_cv_prog_lex_root", "for lex output file root", 377 sub { 378 my ($fh, $filename) = tempfile( "testXXXXXX", SUFFIX => '.l'); 379 my $src = <<'EOLEX'; 380%% 381a { ECHO; } 382b { REJECT; } 383c { yymore (); } 384d { yyless (1); } 385e { /* IRIX 6.5 flex 2.5.4 underquotes its yyless argument. */ 386 yyless ((input () != 0)); } 387f { unput (yytext[0]); } 388. { BEGIN INITIAL; } 389%% 390#ifdef YYTEXT_POINTER 391extern char *yytext; 392#endif 393int 394main (void) 395{ 396 return ! yylex () + ! yywrap (); 397} 398EOLEX 399 400 print {$fh} $src; 401 close $fh; 402 403 my ( $stdout, $stderr, $exit ) = 404 capture { system( $lex, $filename ); }; 405 chomp $stdout; 406 unlink $filename; 407 -f "lex.yy.c" and return "lex.yy"; 408 -f "lexyy.c" and return "lexyy"; 409 $self->msg_error("cannot find output from $lex; giving up"); 410 }); 411 defined $self->{lex}->{root} or $self->{lex}->{root} = $lex_root_var; 412 413 my $conftest = read_file($lex_root_var.".c"); 414 unlink $lex_root_var.".c"; 415 416 $cache_name = $self->_cache_name( "lib", "lex" ); 417 my $check_sub = sub { 418 my @save_libs = @{$self->{extra_libs}}; 419 my $have_lib = 0; 420 foreach my $libstest ( undef, qw(-lfl -ll) ) { 421 # XXX would local work on array refs? can we omit @save_libs? 422 $self->{extra_libs} = [ @save_libs ]; 423 defined( $libstest ) and unshift( @{$self->{extra_libs}}, $libstest ); 424 $self->link_if_else( $conftest ) 425 and ( $have_lib = defined( $libstest ) ? $libstest : "none required" ) 426 and last; 427 } 428 $self->{extra_libs} = [ @save_libs ]; 429 430 if( $have_lib ) { 431 $self->define_var( _have_lib_define_name( "lex" ), $have_lib, 432 "defined when lex library is available" ); 433 } 434 else { 435 $self->define_var( _have_lib_define_name( "lex" ), undef, 436 "defined when lex library is available" ); 437 } 438 return $have_lib; 439 }; 440 441 my $lex_lib = $self->check_cached( $cache_name, "lex library", $check_sub ); 442 defined $self->{lex}->{lib} or $self->{lex}->{lib} = $lex_lib; 443 } 444 445 $lex; 446} 447 448 449=head2 check_prog_sed 450 451From the autoconf documentation, 452 453 Set output variable [...] to a Sed implementation that conforms to Posix 454 and does not have arbitrary length limits. Report an error if no 455 acceptable Sed is found. See Limitations of Usual Tools, for more 456 information about portability problems with Sed. 457 458 The result of this test can be overridden by setting the SED variable and 459 is cached in the ac_cv_path_SED variable. 460 461Note that it returns the full path, if found. 462 463=cut 464 465sub check_prog_sed { 466 my $self = shift; 467 my $cache_name = $self->_cache_name("prog", "SED"); 468 $self->check_cached( $cache_name, "for sed", 469 sub {$ENV{SED} || $self->check_progs(qw/gsed sed/)} ); 470} 471 472 473=head2 check_prog_pkg_config 474 475Checks for C<pkg-config> program. No additional tests are made for it ... 476 477=cut 478 479sub check_prog_pkg_config { 480 my $self = shift->_get_instance(); 481 my $cache_name = $self->_cache_name("prog", "PKG_CONFIG"); 482 $self->check_cached( $cache_name, "for pkg-config", 483 sub {$self->check_prog("pkg-config")} ); 484} 485 486=head2 check_cc 487 488This function checks if you have a running C compiler. 489 490=cut 491 492sub check_cc { 493 ExtUtils::CBuilder->new(quiet => 1)->have_compiler; 494} 495 496=head2 msg_checking 497 498Prints "Checking @_ ..." 499 500=cut 501 502sub msg_checking { 503 my $self = shift->_get_instance(); 504 $self->{quiet} or 505 print "Checking " . join(" ", @_) . "... "; 506 $self->_add_log_entry( "Checking " . join( " ", @_, "..." ) ); 507 return; 508} 509 510=head2 msg_result 511 512Prints result \n 513 514=cut 515 516my @_num_to_msg = qw/no yes/; 517 518sub _neat 519{ 520 defined $_[0] or return ""; 521 looks_like_number( $_[0] ) and defined $_num_to_msg[$_[0]] and return $_num_to_msg[$_[0]]; 522 $_[0]; 523} 524 525sub msg_result { 526 my $self = shift->_get_instance(); 527 $self->{quiet} or 528 print join( " ", map { _neat $_ } @_ ), "\n"; 529 $self->_add_log_entry( join( " ", map { _neat $_ } @_ ), "\n" ); 530 return; 531} 532 533=head2 msg_notice 534 535Prints "configure: " @_ to stdout 536 537=cut 538 539sub msg_notice { 540 my $self = shift->_get_instance(); 541 $self->{quiet} or 542 print $self->{msg_prefix} . join( " ", @_ ) . "\n"; 543 $self->_add_log_entry( $self->{msg_prefix} . join( " ", @_ ) . "\n" ); 544 return; 545} 546 547=head2 msg_warn 548 549Prints "configure: " @_ to stderr 550 551=cut 552 553sub msg_warn { 554 my $self = shift->_get_instance(); 555 print STDERR $self->{msg_prefix} . join( " ", @_ ) . "\n"; 556 $self->_add_log_entry( "WARNING: " . $self->{msg_prefix} . join( " ", @_ ) . "\n" ); 557 return; 558} 559 560=head2 msg_error 561 562Prints "configure: " @_ to stderr and exits with exit code 0 (tells 563toolchain to stop here and report unsupported environment) 564 565=cut 566 567sub msg_error { 568 my $self = shift->_get_instance(); 569 print STDERR $self->{msg_prefix} . join( " ", @_ ) . "\n"; 570 $self->_add_log_entry( "ERROR: " . $self->{msg_prefix} . join( " ", @_ ) . "\n" ); 571 exit(0); # #toolchain agreement: prevents configure stage to finish 572} 573 574=head2 msg_failure 575 576Prints "configure: " @_ to stderr and exits with exit code 0 (tells 577toolchain to stop here and report unsupported environment). Additional 578details are provides in config.log (probably more information in a 579later stage). 580 581=cut 582 583sub msg_failure { 584 my $self = shift->_get_instance(); 585 print STDERR $self->{msg_prefix} . join( " ", @_ ) . "\n"; 586 $self->_add_log_entry( "FAILURE: " . $self->{msg_prefix} . join( " ", @_ ) . "\n" ); 587 exit(0); # #toolchain agreement: prevents configure stage to finish 588} 589 590=head2 define_var( $name, $value [, $comment ] ) 591 592Defines a check variable for later use in further checks or code to compile. 593 594=cut 595 596sub define_var { 597 my $self = shift->_get_instance(); 598 my ($name, $value, $comment) = @_; 599 600 defined( $name ) or croak( "Need a name to add a define" ); 601 602 $self->{defines}->{$name} = [ $value, $comment ]; 603 604 return; 605} 606 607=head2 write_config_h( [$target] ) 608 609Writes the defined constants into given target: 610 611 Config::AutoConf->write_config_h( "config.h" ); 612 613=cut 614 615sub write_config_h { 616 my $self = shift->_get_instance(); 617 my $tgt; 618 619 defined( $_[0] ) 620 ? ( ref( $_[0] ) 621 ? $tgt = $_[0] 622 : open( $tgt, ">", $_[0] ) ) 623 : open( $tgt, ">", "config.h" ); 624 625 my $conf_h = <<'EOC'; 626/** 627 * Generated from Config::AutoConf 628 * 629 * Do not edit this file, all modifications will be lost, 630 * modify Makefile.PL or Build.PL instead. 631 * 632 * Inspired by GNU AutoConf. 633 * 634 * (c) 2011 Alberto Simoes & Jens Rehsack 635 */ 636#ifndef __CONFIG_H__ 637 638EOC 639 640 while( my ($defname, $defcnt) = each( %{ $self->{defines} } ) ) { 641 if( $defcnt->[0] ) { 642 defined $defcnt->[1] and $conf_h .= "/* " . $defcnt->[1] . " */\n"; 643 $conf_h .= join( " ", "#define", $defname, $defcnt->[0] ) . "\n"; 644 } 645 else { 646 defined $defcnt->[1] and $conf_h .= "/* " . $defcnt->[1] . " */\n"; 647 $conf_h .= "/* " . join( " ", "#undef", $defname ) . " */\n\n"; 648 } 649 } 650 $conf_h .= "#endif /* ?__CONFIG_H__ */\n"; 651 652 print {$tgt} $conf_h; 653 654 return; 655} 656 657=head2 push_lang(lang [, implementor ]) 658 659Puts the current used language on the stack and uses specified language 660for subsequent operations until ending pop_lang call. 661 662=cut 663 664sub push_lang { 665 my $self = shift->_get_instance(); 666 667 push @{$self->{lang_stack}}, [ $self->{lang} ]; 668 669 $self->_set_language( @_ ); 670} 671 672=head2 pop_lang([ lang ]) 673 674Pops the currently used language from the stack and restores previously used 675language. If I<lang> specified, it's asserted that the current used language 676equals to specified language (helps finding control flow bugs). 677 678=cut 679 680sub pop_lang { 681 my $self = shift->_get_instance(); 682 683 scalar( @{$self->{lang_stack}} ) > 0 or croak( "Language stack empty" ); 684 defined( $_[0] ) and $self->{lang} ne $_[0] and 685 croak( "pop_lang( $_[0] ) doesn't match language in use (" . $self->{lang} . ")" ); 686 687 $self->_set_language( @{ pop @{ $self->{lang} } } ); 688} 689 690=head2 lang_call( [prologue], function ) 691 692Builds program which simply calls given function. 693When given, prologue is prepended otherwise, the default 694includes are used. 695 696=cut 697 698sub lang_call { 699 my ($self, $prologue, $function) = @_; 700 ref $self or $self = $self->_get_instance(); 701 702 defined( $prologue ) or $prologue = $self->_default_includes(); 703 $prologue .= <<"_ACEOF"; 704/* Override any GCC internal prototype to avoid an error. 705 Use char because int might match the return type of a GCC 706 builtin and then its argument prototype would still apply. */ 707#ifdef __cplusplus 708extern "C" { 709#endif 710char $function (); 711#ifdef __cplusplus 712} 713#endif 714_ACEOF 715 my $body = "return $function ();"; 716 $body = $self->_build_main( $body ); 717 718 $self->_fill_defines() . "\n$prologue\n\n$body\n"; 719} 720 721=head2 lang_build_program( prologue, body ) 722 723Builds program for current chosen language. If no prologue is given 724(I<undef>), the default headers are used. If body is missing, default 725body is used. 726 727Typical call of 728 729 Config::AutoConf->lang_build_program( "const char hw[] = \"Hello, World\\n\";", 730 "fputs (hw, stdout);" ) 731 732will create 733 734 const char hw[] = "Hello, World\n"; 735 736 /* Override any gcc2 internal prototype to avoid an error. */ 737 #ifdef __cplusplus 738 extern "C" { 739 #endif 740 741 int 742 main (int argc, char **argv) 743 { 744 (void)argc; 745 (void)argv; 746 fputs (hw, stdout);; 747 return 0; 748 } 749 750 #ifdef __cplusplus 751 } 752 #endif 753 754=cut 755 756sub lang_build_program { 757 my ($self, $prologue, $body) = @_; 758 ref $self or $self = $self->_get_instance(); 759 760 defined( $prologue ) or $prologue = $self->_default_includes(); 761 defined( $body ) or $body = ""; 762 $body = $self->_build_main( $body ); 763 764 $self->_fill_defines() . "\n$prologue\n\n$body\n"; 765} 766 767=head2 lang_build_bool_test (prologue, test, [@decls]) 768 769Builds a static test which will fail to compile when test 770evaluates to false. If C<@decls> is given, it's prepended 771before the test code at the variable definition place. 772 773=cut 774 775sub lang_build_bool_test { 776 my ($self, $prologue, $test, @decls) = @_; 777 ref $self or $self = $self->_get_instance(); 778 779 defined( $test ) or $test = "1"; 780 my $test_code = <<ACEOF; 781 static int test_array [($test) ? 1 : -1 ]; 782 test_array [0] = 0 783ACEOF 784 if( @decls ) { 785 $test_code = join( "\n", @decls, $test_code ); 786 } 787 $self->lang_build_program( $prologue, $test_code ); 788} 789 790=head2 push_includes 791 792Adds given list of directories to preprocessor/compiler 793invocation. This is not proved to allow adding directories 794which might be created during the build. 795 796=cut 797 798sub push_includes { 799 my ($self, @includes) = @_; 800 ref $self or $self = $self->_get_instance(); 801 802 push( @{$self->{extra_include_dirs}}, @includes ); 803 804 return; 805} 806 807=head2 push_preprocess_flags 808 809Adds given flags to the parameter list for preprocessor invocation. 810 811=cut 812 813sub push_preprocess_flags { 814 my ($self, @cpp_flags) = @_; 815 ref $self or $self = $self->_get_instance(); 816 817 push( @{$self->{extra_preprocess_flags}}, @cpp_flags ); 818 819 return; 820} 821 822=head2 push_compiler_flags 823 824Adds given flags to the parameter list for compiler invocation. 825 826=cut 827 828sub push_compiler_flags { 829 my ($self, @compiler_flags) = @_; 830 ref $self or $self = $self->_get_instance(); 831 my $lang = $self->{lang}; 832 833 if( scalar( @compiler_flags ) && ( ref($compiler_flags[-1]) eq "HASH" ) ) { 834 my $lang_opt = pop( @compiler_flags ); 835 defined( $lang_opt->{lang} ) or croak( "Missing lang attribute in language options" ); 836 $lang = $lang_opt->{lang}; 837 defined( $self->{lang_supported}->{$lang} ) or croak( "Unsupported language '$lang'" ); 838 } 839 840 push( @{$self->{extra_compile_flags}->{$lang}}, @compiler_flags ); 841 842 return; 843} 844 845=head2 push_libraries 846 847Adds given list of libraries to the parameter list for linker invocation. 848 849=cut 850 851sub push_libraries { 852 my ($self, @libs) = @_; 853 ref $self or $self = $self->_get_instance(); 854 855 push( @{$self->{extra_libs}}, @libs ); 856 857 return; 858} 859 860=head2 push_library_paths 861 862Adds given list of library paths to the parameter list for linker invocation. 863 864=cut 865 866sub push_library_paths { 867 my ($self, @libdirs) = @_; 868 ref $self or $self = $self->_get_instance(); 869 870 push( @{$self->{extra_lib_dirs}}, @libdirs ); 871 872 return; 873} 874 875=head2 push_link_flags 876 877Adds given flags to the parameter list for linker invocation. 878 879=cut 880 881sub push_link_flags { 882 my ($self, @link_flags) = @_; 883 ref $self or $self = $self->_get_instance(); 884 885 push( @{$self->{extra_link_flags}}, @link_flags ); 886 887 return; 888} 889 890=head2 compile_if_else( $src [, action-if-true [, action-if-false ] ] ) 891 892This function trys to compile specified code and runs action-if-true on success 893or action-if-false otherwise. 894 895Returns a boolean value containing check success state. 896 897=cut 898 899sub compile_if_else { 900 my ($self, $src, $action_if_true, $action_if_false) = @_; 901 ref $self or $self = $self->_get_instance(); 902 my $builder = $self->_get_builder(); 903 904 my ($fh, $filename) = tempfile("testXXXXXX", SUFFIX => '.c', , UNLINK => 0); 905 906 print {$fh} $src; 907 close $fh; 908 909 my ($obj_file, $outbuf, $errbuf, $exception); 910 ($outbuf, $errbuf) = capture { 911 eval { 912 $obj_file = $builder->compile( 913 source => $filename, 914 include_dirs => $self->{extra_include_dirs}, 915 extra_compiler_flags => $self->_get_extra_compiler_flags() ); 916 }; 917 918 $exception = $@; 919 }; 920 921 unlink $filename; 922 unlink $obj_file if $obj_file; 923 924 if ($exception || !$obj_file) { 925 $self->_add_log_lines( "compile stage failed" . ( $exception ? " - " . $exception : "" ) ); 926 $errbuf and 927 $self->_add_log_lines( $errbuf ); 928 $self->_add_log_lines( "failing program is:\n" . $src ); 929 $outbuf and 930 $self->_add_log_lines( "stdout was :\n" . $outbuf ); 931 932 defined( $action_if_false ) and "CODE" eq ref( $action_if_false ) and &{$action_if_false}(); 933 return 0; 934 } 935 936 defined( $action_if_true ) and "CODE" eq ref( $action_if_true ) and &{$action_if_true}(); 937 1; 938} 939 940=head2 link_if_else( $src [, action-if-true [, action-if-false ] ] ) 941 942This function trys to compile and link specified code and runs action-if-true on success 943or action-if-false otherwise. 944 945Returns a boolean value containing check success state. 946 947=cut 948 949sub link_if_else { 950 my ($self, $src, $action_if_true, $action_if_false) = @_; 951 ref $self or $self = $self->_get_instance(); 952 my $builder = $self->_get_builder(); 953 954 my ($fh, $filename) = tempfile( "testXXXXXX", SUFFIX => '.c'); 955 956 print {$fh} $src; 957 close $fh; 958 959 my ($obj_file, $outbuf, $errbuf, $exception); 960 ($outbuf, $errbuf) = capture { 961 eval { 962 $obj_file = $builder->compile( 963 source => $filename, 964 include_dirs => $self->{extra_include_dirs}, 965 extra_compiler_flags => $self->_get_extra_compiler_flags() ); 966 }; 967 968 $exception = $@; 969 }; 970 971 if ($exception || !$obj_file) { 972 $self->_add_log_lines( "compile stage failed" . ( $exception ? " - " . $exception : "" ) ); 973 $errbuf and 974 $self->_add_log_lines( $errbuf ); 975 $self->_add_log_lines( "failing program is:\n" . $src ); 976 $outbuf and 977 $self->_add_log_lines( "stdout was :\n" . $outbuf ); 978 979 unlink $filename; 980 unlink $obj_file if $obj_file; 981 defined( $action_if_false ) and "CODE" eq ref( $action_if_false ) and &{$action_if_false}(); 982 return 0; 983 } 984 985 my $exe_file; 986 ($outbuf, $errbuf) = capture { 987 eval { 988 $exe_file = $builder->link_executable( 989 objects => $obj_file, 990 extra_linker_flags => $self->_get_extra_linker_flags() ); 991 }; 992 993 $exception = $@; 994 }; 995 unlink $filename; 996 unlink $obj_file if $obj_file; 997 unlink $exe_file if $exe_file; 998 999 if ($exception || !$exe_file) { 1000 $self->_add_log_lines( "link stage failed" . ( $exception ? " - " . $exception : "" ) ); 1001 $errbuf and 1002 $self->_add_log_lines( $errbuf ); 1003 $self->_add_log_lines( "failing program is:\n" . $src ); 1004 $outbuf and 1005 $self->_add_log_lines( "stdout was :\n" . $outbuf ); 1006 1007 defined( $action_if_false ) and "CODE" eq ref( $action_if_false ) and &{$action_if_false}(); 1008 return 0; 1009 } 1010 1011 defined( $action_if_true ) and "CODE" eq ref( $action_if_true ) and &{$action_if_true}(); 1012 1; 1013} 1014 1015=head2 check_cached( cache-var, message, sub-to-check ) 1016 1017This function checks whether a specified cache variable is set or not, and if not 1018it's going to set it using specified sub-to-check. 1019 1020=cut 1021 1022sub check_cached { 1023 my ($self, $cache_name, $message, $check_sub) = @_; 1024 ref $self or $self = $self->_get_instance(); 1025 1026 $self->msg_checking( $message ); 1027 1028 defined $ENV{$cache_name} and not defined $self->{cache}->{$cache_name} 1029 and $self->{cache}->{$cache_name} = $ENV{$cache_name}; 1030 1031 if( defined($self->{cache}->{$cache_name}) ) { 1032 $self->msg_result( "(cached)", $self->{cache}->{$cache_name} ); 1033 } 1034 else { 1035 $self->{cache}->{$cache_name} = &{$check_sub}(); 1036 $self->msg_result( $self->{cache}->{$cache_name} ); 1037 } 1038 1039 $self->{cache}->{$cache_name}; 1040} 1041 1042=head2 cache_val 1043 1044This functions returns the value of a previously check_cached call. 1045 1046=cut 1047 1048sub cache_val { 1049 my ($self, $cache_name) = @_; 1050 ref $self or $self = $self->_get_instance(); 1051 defined $self->{cache}->{$cache_name} or return; 1052 $self->{cache}->{$cache_name}; 1053} 1054 1055=head2 check_decl( symbol, [action-if-found], [action-if-not-found], [prologue = default includes] ) 1056 1057If symbol (a function, variable or constant) is not declared in includes and 1058a declaration is needed, run the code ref given in I<action-if-not-found>, 1059otherwise I<action-if-found>. includes is a series of include directives, 1060defaulting to I<default includes>, which are used prior to the declaration 1061under test. 1062 1063This method actually tests whether symbol is defined as a macro or can be 1064used as an r-value, not whether it is really declared, because it is much 1065safer to avoid introducing extra declarations when they are not needed. 1066In order to facilitate use of C++ and overloaded function declarations, it 1067is possible to specify function argument types in parentheses for types 1068which can be zero-initialized: 1069 1070 Config::AutoConf->check_decl("basename(char *)") 1071 1072This method caches its result in the C<ac_cv_decl_E<lt>set langE<gt>>_symbol variable. 1073 1074=cut 1075 1076sub check_decl { 1077 my ($self, $symbol, $action_if_found, $action_if_not_found, $prologue) = @_; 1078 $self = $self->_get_instance(); 1079 defined( $symbol ) or return; # XXX prefer croak 1080 ref( $symbol ) eq "" or return; 1081 ( my $sym_plain = $symbol ) =~ s/ *\(.*//; 1082 my $sym_call = $symbol; 1083 $sym_call =~ s/\(/((/; 1084 $sym_call =~ s/\)/) 0)/; 1085 $sym_call =~ s/,/) 0, (/g; 1086 1087 my $cache_name = $self->_cache_name( "decl", $self->{lang}, $symbol ); 1088 my $check_sub = sub { 1089 1090 my $body = <<ACEOF; 1091#ifndef $sym_plain 1092 (void) $sym_call; 1093#endif 1094ACEOF 1095 my $conftest = $self->lang_build_program( $prologue, $body ); 1096 1097 my $have_decl = $self->compile_if_else( $conftest ); 1098 if( $have_decl ) { 1099 if( defined( $action_if_found ) and "CODE" eq ref( $action_if_found ) ) { 1100 &{$action_if_found}(); 1101 } 1102 } 1103 else { 1104 if( defined( $action_if_not_found ) and "CODE" eq ref( $action_if_not_found ) ) { 1105 &{$action_if_not_found}(); 1106 } 1107 } 1108 1109 $have_decl; 1110 }; 1111 1112 $self->check_cached( $cache_name, "whether $symbol is declared", $check_sub ); 1113} 1114 1115=head2 check_decls( symbols, [action-if-found], [action-if-not-found], [prologue = default includes] ) 1116 1117For each of the symbols (with optional function argument types for C++ 1118overloads), run L<check_decl>. If I<action-if-not-found> is given, it 1119is additional code to execute when one of the symbol declarations is 1120needed, otherwise I<action-if-found> is executed. 1121 1122Contrary to GNU autoconf, this method does not declare HAVE_DECL_symbol 1123macros for the resulting C<confdefs.h>, because it differs as C<check_decl> 1124between compiling languages. 1125 1126=cut 1127 1128sub check_decls { 1129 my ($self, $symbols, $action_if_found, $action_if_not_found, $prologue) = @_; 1130 $self = $self->_get_instance(); 1131 1132 my $have_syms = 1; 1133 foreach my $symbol (@$symbols) { 1134 $have_syms &= $self->check_decl( $symbol, undef, undef, $prologue ); 1135 } 1136 1137 if( $have_syms ) { 1138 if( defined( $action_if_found ) and "CODE" eq ref( $action_if_found ) ) { 1139 &{$action_if_found}(); 1140 } 1141 } 1142 else { 1143 if( defined( $action_if_not_found ) and "CODE" eq ref( $action_if_not_found ) ) { 1144 &{$action_if_not_found}(); 1145 } 1146 } 1147 1148 $have_syms; 1149} 1150 1151sub _have_type_define_name { 1152 my $type = $_[0]; 1153 my $have_name = "HAVE_" . uc($type); 1154 $have_name =~ tr/*/P/; 1155 $have_name =~ tr/_A-Za-z0-9/_/c; 1156 $have_name; 1157} 1158 1159=head2 check_type (type, [action-if-found], [action-if-not-found], [prologue = default includes]) 1160 1161Check whether type is defined. It may be a compiler builtin type or defined 1162by the includes. I<prologue> should be a series of include directives, 1163defaulting to I<default includes>, which are used prior to the type under 1164test. 1165 1166In C, type must be a type-name, so that the expression C<sizeof (type)> is 1167valid (but C<sizeof ((type))> is not) 1168 1169If I<type> type is defined, preprocessor macro HAVE_I<type> (in all 1170capitals, with "*" replaced by "P" and spaces and dots replaced by 1171underscores) is defined. 1172 1173This method caches its result in the C<ac_cv_type_>type variable. 1174 1175=cut 1176 1177sub check_type { 1178 my ($self, $type, $action_if_found, $action_if_not_found, $prologue) = @_; 1179 $self = $self->_get_instance(); 1180 defined $type or return; # XXX prefer croak 1181 ref $type eq "" or return; 1182 1183 my $cache_name = $self->_cache_type_name( "type", $type ); 1184 my $check_sub = sub { 1185 1186 my $body = <<ACEOF; 1187 if( sizeof ($type) ) 1188 return 0; 1189ACEOF 1190 my $conftest = $self->lang_build_program( $prologue, $body ); 1191 1192 my $have_type = $self->compile_if_else( $conftest ); 1193 $self->define_var( _have_type_define_name( $type ), $have_type ? $have_type : undef, "defined when $type is available" ); 1194 if( $have_type ) { 1195 if( defined( $action_if_found ) and "CODE" eq ref( $action_if_found ) ) { 1196 &{$action_if_found}(); 1197 } 1198 } 1199 else { 1200 if( defined( $action_if_not_found ) and "CODE" eq ref( $action_if_not_found ) ) { 1201 &{$action_if_not_found}(); 1202 } 1203 } 1204 1205 $have_type; 1206 }; 1207 1208 $self->check_cached( $cache_name, "for $type", $check_sub ); 1209} 1210 1211=head2 check_types (types, [action-if-found], [action-if-not-found], [prologue = default includes]) 1212 1213For each type L<check_type> is called to check for type. 1214 1215If I<action-if-found> is given, it is additionally executed when all of the 1216types are found. If I<action-if-not-found> is given, it is executed when one 1217of the types is not found. 1218 1219=cut 1220 1221sub check_types { 1222 my ($self, $types, $action_if_found, $action_if_not_found, $prologue) = @_; 1223 $self = $self->_get_instance(); 1224 1225 my $have_types = 1; 1226 foreach my $type (@$types) { 1227 $have_types &= $self->check_type( $type, undef, undef, $prologue ); 1228 } 1229 1230 if( $have_types ) { 1231 if( defined( $action_if_found ) and "CODE" eq ref( $action_if_found ) ) { 1232 &{$action_if_found}(); 1233 } 1234 } 1235 else { 1236 if( defined( $action_if_not_found ) and "CODE" eq ref( $action_if_not_found ) ) { 1237 &{$action_if_not_found}(); 1238 } 1239 } 1240 1241 $have_types; 1242} 1243 1244sub _compute_int_compile { 1245 my ($self, $expr, $prologue, @decls) = @_; 1246 $self = $self->_get_instance(); 1247 1248 my( $body, $conftest, $compile_result ); 1249 1250 my ($low, $mid, $high) = (0, 0, 0); 1251 if( $self->compile_if_else( $self->lang_build_bool_test( $prologue, "((long int)($expr)) >= 0", @decls ) ) ) { 1252 $low = $mid = 0; 1253 while( 1 ) { 1254 if( $self->compile_if_else( $self->lang_build_bool_test( $prologue, "((long int)($expr)) <= $mid", @decls ) ) ) { 1255 $high = $mid; 1256 last; 1257 } 1258 $low = $mid + 1; 1259 # avoid overflow 1260 if( $low <= $mid ) { 1261 $low = 0; 1262 last; 1263 } 1264 $mid = $low * 2; 1265 } 1266 } 1267 elsif( $self->compile_if_else( $self->lang_build_bool_test( $prologue, "((long int)($expr)) < 0", @decls ) ) ) { 1268 $high = $mid = -1; 1269 while( 1 ) { 1270 if( $self->compile_if_else( $self->lang_build_bool_test( $prologue, "((long int)($expr)) >= $mid", @decls ) ) ) { 1271 $low = $mid; 1272 last; 1273 } 1274 $high = $mid - 1; 1275 # avoid overflow 1276 if( $mid < $high ) { 1277 $high = 0; 1278 last; 1279 } 1280 $mid = $high * 2; 1281 } 1282 } 1283 1284 # perform binary search between $low and $high 1285 while( $low <= $high ) { 1286 $mid = int( ( $high - $low ) / 2 + $low ); 1287 if( $self->compile_if_else( $self->lang_build_bool_test( $prologue, "((long int)($expr)) < $mid", @decls ) ) ) { 1288 $high = $mid - 1; 1289 } 1290 elsif( $self->compile_if_else( $self->lang_build_bool_test( $prologue, "((long int)($expr)) > $mid", @decls ) ) ) { 1291 $low = $mid + 1; 1292 } 1293 else { 1294 return $mid; 1295 } 1296 } 1297 1298 return; 1299} 1300 1301=head2 compute_int (expression, [action-if-fails], [prologue = default includes], [@decls]) 1302 1303Returns the value of the integer I<expression>. The value should fit in an 1304initializer in a C variable of type signed long. It should be possible 1305to evaluate the expression at compile-time. If no includes are specified, 1306the default includes are used. 1307 1308Execute I<action-if-fails> if the value cannot be determined correctly. 1309 1310=cut 1311 1312sub compute_int { 1313 my ($self, $expr, $action_if_fails, $prologue, @decls) = @_; 1314 $self = $self->_get_instance(); 1315 1316 my $cache_name = $self->_cache_type_name( "compute_int", $self->{lang}, $expr ); 1317 my $check_sub = sub { 1318 1319 my $val = $self->_compute_int_compile( $expr, $prologue, @decls); 1320 unless( defined( $val ) ) { 1321 if( defined( $action_if_fails ) and "CODE" eq ref( $action_if_fails ) ) { 1322 &{$action_if_fails}(); 1323 } 1324 } 1325 1326 $val; 1327 }; 1328 1329 $self->check_cached( $cache_name, "for compute result of ($expr)", $check_sub ); 1330} 1331 1332sub _sizeof_type_define_name { 1333 my $type = $_[0]; 1334 my $have_name = "SIZEOF_" . uc($type); 1335 $have_name =~ tr/*/P/; 1336 $have_name =~ tr/_A-Za-z0-9/_/c; 1337 $have_name; 1338} 1339 1340=head2 check_sizeof_type (type, [action-if-found], [action-if-not-found], [prologue = default includes]) 1341 1342Checks for the size of the specified type by compiling. If no size can 1343determined, I<action-if-not-found> is invoked when given. Otherwise 1344I<action-if-found> is invoked and C<SIZEOF_type> is defined using the 1345determined size. 1346 1347In opposition to GNU AutoConf, this method can determine size of structure 1348members, eg. 1349 1350 $ac->check_sizeof_type( "SV.sv_refcnt", undef, undef, $include_perl ); 1351 # or 1352 $ac->check_sizeof_type( "struct utmpx.ut_id", undef, undef, "#include <utmpx.h>" ); 1353 1354This method caches its result in the C<ac_cv_sizeof_E<lt>set langE<gt>>_type variable. 1355 1356=cut 1357 1358sub check_sizeof_type { 1359 my ($self, $type, $action_if_found, $action_if_not_found, $prologue) = @_; 1360 $self = $self->_get_instance(); 1361 defined( $type ) or return; # XXX prefer croak 1362 ref( $type ) eq "" or return; 1363 1364 my $cache_name = $self->_cache_type_name( "sizeof", $self->{lang}, $type ); 1365 my $check_sub = sub { 1366 1367 my @decls; 1368 if( $type =~ m/^([^.]+)\.([^.]+)$/ ) { 1369 my $struct = $1; 1370 $type = "_ac_test_aggr.$2"; 1371 my $decl = "static $struct _ac_test_aggr;"; 1372 push( @decls, $decl ); 1373 } 1374 1375 my $typesize = $self->_compute_int_compile( "sizeof($type)", $prologue, @decls ); 1376 $self->define_var( _sizeof_type_define_name( $type ), $typesize ? $typesize : undef, "defined when sizeof($type) is available" ); 1377 if( $typesize ) { 1378 if( defined( $action_if_found ) and "CODE" eq ref( $action_if_found ) ) { 1379 &{$action_if_found}(); 1380 } 1381 } 1382 else { 1383 if( defined( $action_if_not_found ) and "CODE" eq ref( $action_if_not_found ) ) { 1384 &{$action_if_not_found}(); 1385 } 1386 } 1387 1388 $typesize; 1389 }; 1390 1391 $self->check_cached( $cache_name, "for size of $type", $check_sub ); 1392} 1393 1394=head2 check_sizeof_types (type, [action-if-found], [action-if-not-found], [prologue = default includes]) 1395 1396For each type L<check_sizeof_type> is called to check for size of type. 1397 1398If I<action-if-found> is given, it is additionally executed when all of the 1399sizes of the types could determined. If I<action-if-not-found> is given, it 1400is executed when one size of the types could not determined. 1401 1402=cut 1403 1404sub check_sizeof_types { 1405 my ($self, $types, $action_if_found, $action_if_not_found, $prologue) = @_; 1406 $self = $self->_get_instance(); 1407 1408 my $have_sizes = 1; 1409 foreach my $type (@$types) { 1410 $have_sizes &= ! ! ($self->check_sizeof_type ( $type, undef, undef, $prologue )); 1411 } 1412 1413 if( $have_sizes ) { 1414 if( defined( $action_if_found ) and "CODE" eq ref( $action_if_found ) ) { 1415 &{$action_if_found}(); 1416 } 1417 } 1418 else { 1419 if( defined( $action_if_not_found ) and "CODE" eq ref( $action_if_not_found ) ) { 1420 &{$action_if_not_found}(); 1421 } 1422 } 1423 1424 $have_sizes; 1425} 1426 1427sub _alignof_type_define_name { 1428 my $type = $_[0]; 1429 my $have_name = "ALIGNOF_" . uc($type); 1430 $have_name =~ tr/*/P/; 1431 $have_name =~ tr/_A-Za-z0-9/_/c; 1432 $have_name; 1433} 1434 1435=head2 check_alignof_type (type, [action-if-found], [action-if-not-found], [prologue = default includes]) 1436 1437Define ALIGNOF_type to be the alignment in bytes of type. I<type y;> must 1438be valid as a structure member declaration or I<type> must be a structure 1439member itself. 1440 1441This method caches its result in the C<ac_cv_alignof_E<lt>set langE<gt>>_type 1442variable, with I<*> mapped to C<p> and other characters not suitable for a 1443variable name mapped to underscores. 1444 1445=cut 1446 1447sub check_alignof_type { 1448 my ($self, $type, $action_if_found, $action_if_not_found, $prologue) = @_; 1449 $self = $self->_get_instance(); 1450 defined( $type ) or return; # XXX prefer croak 1451 ref( $type ) eq "" or return; 1452 1453 my $cache_name = $self->_cache_type_name( "alignof", $self->{lang}, $type ); 1454 my $check_sub = sub { 1455 1456 my @decls = ( 1457 "#ifndef offsetof", 1458 "# ifdef __ICC", 1459 "# define offsetof(type,memb) ((size_t)(((char *)(&((type*)0)->memb)) - ((char *)0)))", 1460 "# else", 1461 "# define offsetof(type,memb) ((size_t)&((type*)0)->memb)", 1462 "# endif", 1463 "#endif" 1464 ); 1465 1466 my ($struct, $memb); 1467 if( $type =~ m/^([^.]+)\.([^.]+)$/ ) { 1468 $struct = $1; 1469 $memb = $2; 1470 } 1471 else { 1472 push( @decls, "typedef struct { char x; $type y; } ac__type_alignof_;" ); 1473 $struct = "ac__type_alignof_"; 1474 $memb = "y"; 1475 } 1476 1477 my $typealign = $self->_compute_int_compile( "offsetof($struct, $memb)", $prologue, @decls ); 1478 $self->define_var( _alignof_type_define_name( $type ), $typealign ? $typealign : undef, "defined when alignof($type) is available" ); 1479 if( $typealign ) { 1480 if( defined( $action_if_found ) and "CODE" eq ref( $action_if_found ) ) { 1481 &{$action_if_found}(); 1482 } 1483 } 1484 else { 1485 if( defined( $action_if_not_found ) and "CODE" eq ref( $action_if_not_found ) ) { 1486 &{$action_if_not_found}(); 1487 } 1488 } 1489 1490 $typealign; 1491 }; 1492 1493 $self->check_cached( $cache_name, "for align of $type", $check_sub ); 1494} 1495 1496=head2 check_alignof_types (type, [action-if-found], [action-if-not-found], [prologue = default includes]) 1497 1498For each type L<check_alignof_type> is called to check for align of type. 1499 1500If I<action-if-found> is given, it is additionally executed when all of the 1501aligns of the types could determined. If I<action-if-not-found> is given, it 1502is executed when one align of the types could not determined. 1503 1504=cut 1505 1506sub check_alignof_types { 1507 my ($self, $types, $action_if_found, $action_if_not_found, $prologue) = @_; 1508 $self = $self->_get_instance(); 1509 1510 my $have_aligns = 1; 1511 foreach my $type (@$types) { 1512 $have_aligns &= ! ! ($self->check_alignof_type ( $type, undef, undef, $prologue )); 1513 } 1514 1515 if( $have_aligns ) { 1516 if( defined( $action_if_found ) and "CODE" eq ref( $action_if_found ) ) { 1517 &{$action_if_found}(); 1518 } 1519 } 1520 else { 1521 if( defined( $action_if_not_found ) and "CODE" eq ref( $action_if_not_found ) ) { 1522 &{$action_if_not_found}(); 1523 } 1524 } 1525 1526 $have_aligns; 1527} 1528 1529sub _have_member_define_name { 1530 my $member = $_[0]; 1531 my $have_name = "HAVE_" . uc($member); 1532 $have_name =~ tr/_A-Za-z0-9/_/c; 1533 $have_name; 1534} 1535 1536=head2 check_member (member, [action-if-found], [action-if-not-found], [prologue = default includes]) 1537 1538Check whether I<member> is in form of I<aggregate>.I<member> and 1539I<member> is a member of the I<aggregate> aggregate. I<prologue> 1540should be a series of include directives, defaulting to 1541I<default includes>, which are used prior to the aggregate under test. 1542 1543 Config::AutoConf->check_member( 1544 "struct STRUCT_SV.sv_refcnt", 1545 undef, 1546 sub { Config::AutoConf->msg_failure( "sv_refcnt member required for struct STRUCT_SV" ); } 1547 "#include <EXTERN.h>\n#include <perl.h>" 1548 ); 1549 1550If I<aggregate> aggregate has I<member> member, preprocessor 1551macro HAVE_I<aggregate>_I<MEMBER> (in all capitals, with spaces 1552and dots replaced by underscores) is defined. 1553 1554This macro caches its result in the C<ac_cv_>aggr_member variable. 1555 1556=cut 1557 1558sub check_member { 1559 my ($self, $member, $action_if_found, $action_if_not_found, $prologue) = @_; 1560 $self = $self->_get_instance(); 1561 defined( $member ) or return; # XXX prefer croak 1562 ref( $member ) eq "" or return; 1563 1564 $member =~ m/^([^.]+)\.([^.]+)$/ or return; 1565 my $type = $1; 1566 $member = $2; 1567 1568 my $cache_name = $self->_cache_type_name( "member", $type ); 1569 my $check_sub = sub { 1570 1571 my $body = <<ACEOF; 1572 static $type check_aggr; 1573 if( check_aggr.$member ) 1574 return 0; 1575ACEOF 1576 my $conftest = $self->lang_build_program( $prologue, $body ); 1577 1578 my $have_member = $self->compile_if_else( $conftest ); 1579 $self->define_var( _have_member_define_name( $member ), $have_member ? $have_member : undef, "defined when $member is available" ); 1580 if( $have_member ) { 1581 if( defined( $action_if_found ) and "CODE" eq ref( $action_if_found ) ) { 1582 &{$action_if_found}(); 1583 } 1584 } 1585 else { 1586 if( defined( $action_if_not_found ) and "CODE" eq ref( $action_if_not_found ) ) { 1587 &{$action_if_not_found}(); 1588 } 1589 } 1590 1591 $have_member; 1592 }; 1593 1594 $self->check_cached( $cache_name, "for $type.$member", $check_sub ); 1595} 1596 1597=head2 check_members (members, [action-if-found], [action-if-not-found], [prologue = default includes]) 1598 1599For each member L<check_member> is called to check for member of aggregate. 1600 1601If I<action-if-found> is given, it is additionally executed when all of the 1602aggregate members are found. If I<action-if-not-found> is given, it is 1603executed when one of the aggregate members is not found. 1604 1605=cut 1606 1607sub check_members { 1608 my ($self, $members, $action_if_found, $action_if_not_found, $prologue) = @_; 1609 $self = $self->_get_instance(); 1610 1611 my $have_members = 1; 1612 foreach my $member (@$members) { 1613 $have_members &= $self->check_member( $member, undef, undef, $prologue ); 1614 } 1615 1616 if( $have_members ) { 1617 if( defined( $action_if_found ) and "CODE" eq ref( $action_if_found ) ) { 1618 &{$action_if_found}(); 1619 } 1620 } 1621 else { 1622 if( defined( $action_if_not_found ) and "CODE" eq ref( $action_if_not_found ) ) { 1623 &{$action_if_not_found}(); 1624 } 1625 } 1626 1627 $have_members; 1628} 1629 1630=head2 check_headers 1631 1632This function uses check_header to check if a set of include files exist in the system and can 1633be included and compiled by the available compiler. Returns the name of the first header file found. 1634 1635=cut 1636 1637sub check_headers { 1638 my $self = shift; 1639 $self->check_header($_) and return $_ for(@_); 1640 return; 1641} 1642 1643sub _have_header_define_name { 1644 my $header = $_[0]; 1645 my $have_name = "HAVE_" . uc($header); 1646 $have_name =~ tr/_A-Za-z0-9/_/c; 1647 return $have_name; 1648} 1649 1650sub _check_header { 1651 my ($self, $header, $prologue, $body) = @_; 1652 1653 $prologue .= <<"_ACEOF"; 1654 #include <$header> 1655_ACEOF 1656 my $conftest = $self->lang_build_program( $prologue, $body ); 1657 1658 my $have_header = $self->compile_if_else( $conftest ); 1659 $have_header; 1660} 1661 1662=head2 check_header 1663 1664This function is used to check if a specific header file is present in 1665the system: if we detect it and if we can compile anything with that 1666header included. Note that normally you want to check for a header 1667first, and then check for the corresponding library (not all at once). 1668 1669The standard usage for this module is: 1670 1671 Config::AutoConf->check_header("ncurses.h"); 1672 1673This function will return a true value (1) on success, and a false value 1674if the header is not present or not available for common usage. 1675 1676=cut 1677 1678sub check_header { 1679 my $self = shift; 1680 my $header = shift; 1681 my $pre_inc = shift; 1682 1683 return 0 unless $header; 1684 my $cache_name = $self->_cache_name( $header ); 1685 my $check_sub = sub { 1686 my $prologue = ""; 1687 defined $pre_inc 1688 and $prologue .= "$pre_inc\n"; 1689 1690 my $have_header = $self->_check_header( $header, $prologue, "" ); 1691 $self->define_var( _have_header_define_name( $header ), $have_header ? $have_header : undef, "defined when $header is available" ); 1692 1693 return $have_header; 1694 }; 1695 1696 $self->check_cached( $cache_name, "for $header", $check_sub ); 1697} 1698 1699=head2 check_all_headers 1700 1701This function checks each given header for usability. 1702 1703=cut 1704 1705sub check_all_headers { 1706 my $self = shift->_get_instance(); 1707 @_ or return; 1708 my $rc = 1; 1709 foreach my $header (@_) { 1710 $rc &= $self->check_header( $header ); 1711 } 1712 $rc; 1713} 1714 1715=head2 check_stdc_headers 1716 1717Checks for standard C89 headers, namely stdlib.h, stdarg.h, string.h and float.h. 1718If those are found, additional all remaining C89 headers are checked: assert.h, 1719ctype.h, errno.h, limits.h, locale.h, math.h, setjmp.h, signal.h, stddef.h, 1720stdio.h and time.h. 1721 1722Returns a false value if it fails. 1723 1724=cut 1725 1726sub check_stdc_headers { 1727 my $self = shift->_get_instance(); 1728 my $rc = 0; 1729 if( $rc = $self->check_all_headers( qw(stdlib.h stdarg.h string.h float.h) ) ) { 1730 $rc &= $self->check_all_headers( qw/assert.h ctype.h errno.h limits.h/ ); 1731 $rc &= $self->check_all_headers( qw/locale.h math.h setjmp.h signal.h/ ); 1732 $rc &= $self->check_all_headers( qw/stddef.h stdio.h time.h/ ); 1733 } 1734 $rc and $self->define_var( "STDC_HEADERS", 1, "Define to 1 if you have the ANSI C header files." ); 1735 $rc; 1736} 1737 1738=head2 check_default_headers 1739 1740This function checks for some default headers, the std c89 haeders and 1741sys/types.h, sys/stat.h, memory.h, strings.h, inttypes.h, stdint.h and unistd.h 1742 1743=cut 1744 1745sub check_default_headers { 1746 my $self = shift->_get_instance(); 1747 my $rc = $self->check_stdc_headers() and $self->check_all_headers( qw(sys/types.h sys/stat.h memory.h strings.h inttypes.h stdint.h unistd.h) ); 1748 $rc; 1749} 1750 1751=head2 check_dirent_header 1752 1753Check for the following header files. For the first one that is found and 1754defines 'DIR', define the listed C preprocessor macro: 1755 1756 dirent.h HAVE_DIRENT_H 1757 sys/ndir.h HAVE_SYS_NDIR_H 1758 sys/dir.h HAVE_SYS_DIR_H 1759 ndir.h HAVE_NDIR_H 1760 1761The directory-library declarations in your source code should look 1762something like the following: 1763 1764 #include <sys/types.h> 1765 #ifdef HAVE_DIRENT_H 1766 # include <dirent.h> 1767 # define NAMLEN(dirent) strlen ((dirent)->d_name) 1768 #else 1769 # define dirent direct 1770 # define NAMLEN(dirent) ((dirent)->d_namlen) 1771 # ifdef HAVE_SYS_NDIR_H 1772 # include <sys/ndir.h> 1773 # endif 1774 # ifdef HAVE_SYS_DIR_H 1775 # include <sys/dir.h> 1776 # endif 1777 # ifdef HAVE_NDIR_H 1778 # include <ndir.h> 1779 # endif 1780 #endif 1781 1782Using the above declarations, the program would declare variables to be of 1783type C<struct dirent>, not C<struct direct>, and would access the length 1784of a directory entry name by passing a pointer to a C<struct dirent> to 1785the C<NAMLEN> macro. 1786 1787This macro might be obsolescent, as all current systems with directory 1788libraries have C<<E<lt>dirent.hE<gt>>>. Programs supporting only newer OS 1789might not need touse this macro. 1790 1791=cut 1792 1793sub check_dirent_header { 1794 my $self = shift->_get_instance(); 1795 1796 my $cache_name = $self->_cache_name( "header_dirent" ); 1797 my $check_sub = sub { 1798 1799 my $have_dirent; 1800 foreach my $header (qw(dirent.h sys/ndir.h sys/dir.h ndir.h)) { 1801 $have_dirent = $self->_check_header( $header, "#include <sys/types.h>\n", "if ((DIR *) 0) { return 0; }" ); 1802 $self->define_var( _have_header_define_name( $header ), $have_dirent ? $have_dirent : undef, "defined when $header is available" ); 1803 $have_dirent and $have_dirent = $header and last; 1804 } 1805 1806 $have_dirent; 1807 }; 1808 1809 1810 $self->check_cached( $cache_name, "for header defining DIR *", $check_sub ); 1811} 1812 1813sub _have_lib_define_name { 1814 my $lib = $_[0]; 1815 my $have_name = "HAVE_LIB" . uc($lib); 1816 $have_name =~ tr/_A-Za-z0-9/_/c; 1817 return $have_name; 1818} 1819 1820=head2 _check_perl_api_program 1821 1822This method provides the program source which is suitable to do basic 1823compile/link tests to prove perl development environment. 1824 1825=cut 1826 1827sub _check_perl_api_program { 1828 my $self = shift; 1829 1830 my $includes = $self->_default_includes_with_perl(); 1831 my $perl_check_body = <<'EOB'; 1832 I32 rc; 1833 SV *foo = newSVpv("Perl rocks", 11); 1834 rc = SvCUR(foo); 1835EOB 1836 $self->lang_build_program( $includes, $perl_check_body ); 1837} 1838 1839=head2 _check_compile_perl_api 1840 1841This method can be used from other checks to prove whether we have a perl 1842development environment or not (perl.h, reasonable basic checks - types, etc.) 1843 1844=cut 1845 1846sub _check_compile_perl_api { 1847 my $self = shift; 1848 1849 my $conftest = $self->_check_perl_api_program(); 1850 $self->compile_if_else($conftest); 1851} 1852 1853=head2 check_compile_perl_api 1854 1855This method can be used from other checks to prove whether we have a perl 1856development environment or not (perl.h, reasonable basic checks - types, etc.) 1857 1858=cut 1859 1860sub check_compile_perl_api { 1861 my $self = shift->_get_instance; 1862 my $cache_name = $self->_cache_name(qw(compile perl api)); 1863 1864 $self->check_cached( $cache_name, 1865 "whether perl api is accessible", 1866 sub { $self->_check_compile_perl_api } ); 1867} 1868 1869=head2 _check_link_perl_api 1870 1871This method can be used from other checks to prove whether we have a perl 1872development environment including a suitable libperl or not (perl.h, 1873reasonable basic checks - types, etc.) 1874 1875Caller must ensure that the linker flags are set appropriate (C<-lperl> 1876or similar). 1877 1878=cut 1879 1880sub _check_link_perl_api { 1881 my $self = shift; 1882 1883 my $conftest = $self->_check_perl_api_program(); 1884 my @save_libs = @{$self->{extra_libs}}; 1885 my @save_extra_link_flags = @{$self->{extra_link_flags}}; 1886 1887 push @{$self->{extra_link_flags}}, "-L" . File::Spec->catdir($Config{installarchlib}, "CORE"); 1888 push @{$self->{extra_libs}}, "perl"; 1889 if($Config{perllibs}) { 1890 foreach my $perllib (split(" ", $Config{perllibs})) { 1891 $perllib =~ m/^\-l(\w+)$/ and push @{$self->{extra_libs}}, "$1" and next; 1892 push @{$self->{extra_link_flags}}, $perllib; 1893 } 1894 } 1895 1896 my $have_libperl = $self->link_if_else( $conftest ); 1897 1898 $self->{extra_libs} = [ @save_libs ]; 1899 $self->{extra_link_flags} = [ @save_extra_link_flags ]; 1900 1901 $have_libperl; 1902} 1903 1904=head2 check_link_perl_api 1905 1906This method can be used from other checks to prove whether we have a perl 1907development environment or not (perl.h, libperl.la, reasonable basic 1908checks - types, etc.) 1909 1910=cut 1911 1912sub check_link_perl_api { 1913 my $self = shift->_get_instance; 1914 my $cache_name = $self->_cache_name(qw(link perl api)); 1915 1916 $self->check_cached( $cache_name, 1917 "whether perl api is linkable", 1918 sub { $self->_check_link_perl_api } ); 1919} 1920 1921=head2 check_lm( [ action-if-found ], [ action-if-not-found ] ) 1922 1923This method is used to check if some common C<math.h> functions are 1924available, and if C<-lm> is needed. Returns the empty string if no 1925library is needed, or the "-lm" string if libm is needed. 1926 1927Actions are only called at the end of the list of tests. If one fails, 1928I<action-if-not-found> is run. Otherwise, I<action-if-found> is run. 1929 1930=cut 1931 1932sub check_lm { 1933 my ($self, $aif, $ainf) = @_; 1934 ref($self) or $self = $self->_get_instance(); 1935 1936 my $fail = 0; 1937 my $required = ""; 1938 for my $func (qw(log2 pow log10 log exp sqrt)) { 1939 my $ans = $self->search_libs( $func, ['m'] ); 1940 1941 $ans or $fail = 1; 1942 ($ans ne "none required") and $required = $ans; 1943 } 1944 1945 if ($fail) { $ainf && $ainf->() } 1946 else { $aif && $aif->() } 1947 1948 $required; 1949} 1950 1951=head2 check_lib( lib, func, [ action-if-found ], [ action-if-not-found ], [ @other-libs ] ) 1952 1953This function is used to check if a specific library includes some 1954function. Call it with the library name (without the lib portion), and 1955the name of the function you want to test: 1956 1957 Config::AutoConf->check_lib("z", "gzopen"); 1958 1959It returns 1 if the function exist, 0 otherwise. 1960 1961I<action-if-found> and I<action-if-not-found> can be CODE references 1962whereby the default action in case of function found is to define 1963the HAVE_LIBlibrary (all in capitals) preprocessor macro with 1 and 1964add $lib to the list of libraries to link. 1965 1966If linking with library results in unresolved symbols that would be 1967resolved by linking with additional libraries, give those libraries 1968as the I<other-libs> argument: e.g., C<[qw(Xt X11)]>. 1969Otherwise, this routine may fail to detect that library is present, 1970because linking the test program can fail with unresolved symbols. 1971The other-libraries argument should be limited to cases where it is 1972desirable to test for one library in the presence of another that 1973is not already in LIBS. 1974 1975It's recommended to use L<search_libs> instead of check_lib these days. 1976 1977=cut 1978 1979sub check_lib { 1980 my ( $self, $lib, $func, $action_if_found, $action_if_not_found, @other_libs ) = @_; 1981 ref($self) or $self = $self->_get_instance(); 1982 1983 return 0 unless $lib; 1984 return 0 unless $func; 1985 1986 scalar( @other_libs ) == 1 and ref( $other_libs[0] ) eq "ARRAY" 1987 and @other_libs = @{ $other_libs[0] }; 1988 1989 my $cache_name = $self->_cache_name( "lib", $lib, $func ); 1990 my $check_sub = sub { 1991 my $conftest = $self->lang_call( "", $func ); 1992 1993 my @save_libs = @{$self->{extra_libs}}; 1994 push( @{$self->{extra_libs}}, $lib, @other_libs ); 1995 my $have_lib = $self->link_if_else( $conftest ); 1996 $self->{extra_libs} = [ @save_libs ]; 1997 1998 if( $have_lib ) { 1999 if( defined( $action_if_found ) and "CODE" eq ref( $action_if_found ) ) { 2000 &{$action_if_found}(); 2001 } 2002 else { 2003 $self->define_var( _have_lib_define_name( $lib ), $have_lib, 2004 "defined when library $lib is available" ); 2005 push( @{$self->{extra_libs}}, $lib ); 2006 } 2007 } 2008 else { 2009 if( defined( $action_if_not_found ) and "CODE" eq ref( $action_if_not_found ) ) { 2010 &{$action_if_not_found}(); 2011 } 2012 else { 2013 $self->define_var( _have_lib_define_name( $lib ), undef, 2014 "defined when library $lib is available" ); 2015 } 2016 } 2017 $have_lib; 2018 }; 2019 2020 $self->check_cached( $cache_name, "for $func in -l$lib", $check_sub ); 2021} 2022 2023=head2 search_libs( function, search-libs, [action-if-found], [action-if-not-found], [other-libs] ) 2024 2025Search for a library defining function if it's not already available. 2026This equates to calling 2027 2028 Config::AutoConf->link_if_else( 2029 Config::AutoConf->lang_call( "", "$function" ) ); 2030 2031first with no libraries, then for each library listed in search-libs. 2032I<search-libs> must be specified as an array reference to avoid 2033confusion in argument order. 2034 2035Prepend -llibrary to LIBS for the first library found to contain function, 2036and run I<action-if-found>. If the function is not found, run 2037I<action-if-not-found>. 2038 2039If linking with library results in unresolved symbols that would be 2040resolved by linking with additional libraries, give those libraries as 2041the I<other-libraries> argument: e.g., C<[qw(Xt X11)]>. Otherwise, this 2042method fails to detect that function is present, because linking the 2043test program always fails with unresolved symbols. 2044 2045The result of this test is cached in the ac_cv_search_function variable 2046as "none required" if function is already available, as C<0> if no 2047library containing function was found, otherwise as the -llibrary option 2048that needs to be prepended to LIBS. 2049 2050=cut 2051 2052sub search_libs { 2053 my ( $self, $func, $libs, $action_if_found, $action_if_not_found, @other_libs ) = @_; 2054 ref($self) or $self = $self->_get_instance(); 2055 2056 ( defined( $libs ) and "ARRAY" eq ref( $libs ) and scalar( @{$libs} ) > 0 ) 2057 or return 0; # XXX would prefer croak 2058 return 0 unless $func; 2059 2060 scalar( @other_libs ) == 1 and ref( $other_libs[0] ) eq "ARRAY" 2061 and @other_libs = @{ $other_libs[0] }; 2062 2063 my $cache_name = $self->_cache_name( "search", $func ); 2064 my $check_sub = sub { 2065 2066 my $conftest = $self->lang_call( "", $func ); 2067 2068 my @save_libs = @{$self->{extra_libs}}; 2069 my $have_lib = 0; 2070 foreach my $libstest ( undef, @$libs ) { 2071 # XXX would local work on array refs? can we omit @save_libs? 2072 $self->{extra_libs} = [ @save_libs ]; 2073 defined( $libstest ) and unshift( @{$self->{extra_libs}}, $libstest, @other_libs ); 2074 $self->link_if_else( $conftest ) and ( $have_lib = defined( $libstest ) ? $libstest : "none required" ) and last; 2075 } 2076 $self->{extra_libs} = [ @save_libs ]; 2077 if( $have_lib ) { 2078 $have_lib eq "none required" or unshift( @{$self->{extra_libs}}, $have_lib ); 2079 2080 if( defined( $action_if_found ) and "CODE" eq ref( $action_if_found ) ) { 2081 &{$action_if_found}(); 2082 } 2083 } 2084 else { 2085 if( defined( $action_if_not_found ) and "CODE" eq ref( $action_if_not_found ) ) { 2086 &{$action_if_not_found}(); 2087 } 2088 } 2089 2090 return $have_lib; 2091 }; 2092 2093 return $self->check_cached( $cache_name, "for library containing $func", $check_sub ); 2094} 2095 2096=head2 pkg_config_package_flags($package, [action-if-found], [action-if-not-found]) 2097 2098Search for pkg-config flags for package as specified. The flags which are 2099extracted are C<--cflags> and C<--libs>. The extracted flags are appended 2100to the global C<extra_compile_flags> and C<extra_link_flags>, respectively. 2101 2102Call it with the package you're looking for and optional callback whether 2103found or not. 2104 2105=cut 2106 2107my $_pkg_config_prog; 2108 2109sub _pkg_config_flag { 2110 defined $_pkg_config_prog or croak("pkg_config_prog required"); 2111 my @pkg_config_args = @_; 2112 my ( $stdout, $stderr, $exit ) = 2113 capture { system( $_pkg_config_prog, @pkg_config_args ); }; 2114 chomp $stdout; 2115 0 == $exit and return $stdout; 2116 return; 2117} 2118 2119sub pkg_config_package_flags { 2120 my ( $self, $package, $action_if_found, $action_if_not_found ) = @_; 2121 $self = $self->_get_instance(); 2122 (my $pkgpfx = $package) =~ s/^(\w+).*?$/$1/; 2123 my $cache_name = $self->_cache_name( "pkg", $pkgpfx ); 2124 defined $_pkg_config_prog or $_pkg_config_prog = $self->check_prog_pkg_config; 2125 my $check_sub = sub { 2126 my ( @pkg_cflags, @pkg_libs ); 2127 2128 (my $ENV_CFLAGS = $package) =~ s/^(\w+).*?$/$1_CFLAGS/; 2129 my $CFLAGS = defined $ENV{$ENV_CFLAGS} ? $ENV{$ENV_CFLAGS} 2130 : _pkg_config_flag($package, "--cflags"); 2131 $CFLAGS and @pkg_cflags = ( 2132 map { $_ =~ s/^\s+//; $_ =~ s/\s+$//; Text::ParseWords::shellwords $_; } 2133 split( m/\n/, $CFLAGS ) 2134 ) and push @{ $self->{extra_preprocess_flags} }, @pkg_cflags; 2135 2136 (my $ENV_LIBS = $package) =~ s/^(\w+).*?$/$1_LIBS/; 2137 # do not separate between libs and extra (for now) - they come with -l prepended 2138 my $LIBS = defined $ENV{$ENV_LIBS} ? $ENV{$ENV_LIBS} 2139 : _pkg_config_flag($package, "--libs"); 2140 $LIBS and @pkg_libs = ( 2141 map { $_ =~ s/^\s+//; $_ =~ s/\s+$//; Text::ParseWords::shellwords $_; } 2142 split( m/\n/, $LIBS ) 2143 ) and push @{ $self->{extra_link_flags} }, @pkg_libs; 2144 2145 join(" ", @pkg_cflags, @pkg_libs); 2146 }; 2147 2148 $self->check_cached( $cache_name, "for pkg-config package of $package", $check_sub ); 2149} 2150 2151=head2 _check_pureperl_build_wanted 2152 2153This method proves the C<_argv> attribute and (when set) the C<PERL_MM_OPT> 2154whether they contain I<PUREPERL_ONLY=(0|1)> or not. The attribute C<_force_xs> 2155is set appropriate, which allows a compile test to bail out when C<Makefile.PL> 2156is called with I<PUREPERL_ONLY=0>. 2157 2158=cut 2159 2160sub _check_mm_pureperl_build_wanted { 2161 my $self = shift->_get_instance; 2162 2163 defined $ENV{PERL_MM_OPT} and my @env_args = split " ", $ENV{PERL_MM_OPT}; 2164 2165 foreach my $arg ( @{$self->{_argv}}, @env_args ) { 2166 $arg =~ m/^PUREPERL_ONLY=(.*)$/ and return int($1); 2167 } 2168 2169 0; 2170} 2171 2172=head2 _check_pureperl_build_wanted 2173 2174This method proves the C<_argv> attribute and (when set) the C<PERL_MB_OPT> 2175whether they contain I<--pureperl-only> or not. 2176 2177=cut 2178 2179sub _check_mb_pureperl_build_wanted { 2180 my $self = shift->_get_instance; 2181 2182 defined $ENV{PERL_MB_OPT} and my @env_args = split " ", $ENV{PERL_MB_OPT}; 2183 2184 foreach my $arg ( @{$self->{_argv}}, @env_args ) { 2185 $arg eq "--pureperl-only" and return 1; 2186 } 2187 2188 0; 2189} 2190 2191=head2 _check_pureperl_build_wanted 2192 2193This method calls C<_check_mm_pureperl_build_wanted> when running under 2194L<ExtUtils::MakeMaker> (C<Makefile.PL>) or C<_check_mb_pureperl_build_wanted> 2195when running under a C<Build.PL> (L<Module::Build> compatible) environment. 2196 2197When neither is found (C<$0> contains neither C<Makefile.PL> nor C<Build.PL>), 2198simply 0 is returned. 2199 2200=cut 2201 2202sub _check_pureperl_build_wanted { 2203 $0 =~ m/Makefile\.PL$/i and goto \&_check_mm_pureperl_build_wanted; 2204 $0 =~ m/Build\.PL$/i and goto \&_check_mb_pureperl_build_wanted; 2205 2206 0; 2207} 2208 2209=head2 check_pureperl_build_wanted 2210 2211This check method proves whether a pureperl build is wanted or not by 2212cached-checking C<< $self->_check_pureperl_build_wanted >>. The result 2213might lead to further checks, eg. L</_check_compile_perl_api>. 2214 2215=cut 2216 2217sub check_pureperl_build_wanted { 2218 my $self = shift->_get_instance; 2219 my $cache_name = $self->_cache_name(qw(pureperl only wanted)); 2220 $self->check_cached( $cache_name, 2221 "whether pureperl shall be forced", 2222 sub { $self->_check_pureperl_build_wanted } ); 2223} 2224 2225=head2 check_sane_xs 2226 2227This routine checks whether XS can be sanely used. Therefore it does 2228following checks in given order: 2229 2230=over 4 2231 2232=item * 2233 2234check pureperl environment variables or command line arguments and disable 2235XS when pure perl is wanted in any way 2236 2237=item * 2238 2239check whether a compiler is available (C<check_cc>) and disable XS if none found 2240 2241=item * 2242 2243check whether a test program accessing Perl API can be compiled and 2244die with error if not 2245 2246=item * 2247 2248when C<ExtensivePerlAPI> is enabled, check wether perl extensions can 2249be linked or die with error otherwise 2250 2251=item * 2252 2253I<TODO> check whether a trivial XS can be loaded and die hard on error 2254 2255=back 2256 2257When all checks passed successfully, return a true value. 2258 2259=cut 2260 2261sub check_sane_xs { 2262 my $self = shift->_get_instance; 2263 my $pp = $self->check_pureperl_build_wanted(); 2264 $pp and return 0; 2265 $self->check_cc or return 0; 2266 # XXX necessary check for $Config{useshrlib}? 2267 $self->check_compile_perl_api() or return $self->msg_error("Cannot use Perl API - giving up"); 2268 if( $self->{c_ac_flags}->{ExtensivePerlAPI} ) { 2269 $self->check_compile_perl_api() or return $self->msg_error("Cannot link Perl API - giving up"); 2270 # XXX add a reasonable check compiling and trying to load an XS module 2271 } 2272 return 1; 2273} 2274 2275 2276# 2277# 2278# Auxiliary funcs 2279# 2280 2281=head2 _set_argv 2282 2283Intended to act as a helper for evaluating given command line arguments. 2284Stores given arguments in instances C<_argv> attribute. 2285 2286Call once at very begin of C<Makefile.PL> or C<Build.PL>: 2287 2288 Your::Pkg::Config::AutoConf->_set_args(@ARGV); 2289 2290=cut 2291 2292sub _set_argv { 2293 my ( $self, @argv ) = @_; 2294 $self = $self->_get_instance; 2295 $self->{_argv} = \@argv; 2296 return; 2297} 2298 2299sub _sanitize { 2300 # This is hard coded, and maybe a little stupid... 2301 my $x = shift; 2302 $x =~ s/ //g; 2303 $x =~ s/\///g; 2304 $x =~ s/\\//g; 2305 $x; 2306} 2307 2308sub _get_instance { 2309 ref $_[0] and return $_[0]; 2310 defined $glob_instance or $glob_instance = $_[0]->new(); 2311 $glob_instance; 2312} 2313 2314sub _get_builder { 2315 my $self = $_[0]->_get_instance(); 2316 defined( $self->{lang_supported}->{ $self->{lang} } ) or croak( "Unsupported compile language \"" . $self->{lang} . "\"" ); 2317 2318 my $builder = $self->{lang_supported}->{ $self->{lang} }->new(); 2319 2320 ## XXX - Temporarily. Will try to send upstream 2321 if ($self->{lang} eq "C") { 2322 $builder->{config}{ccflags} =~ s/-arch \S+//g; 2323 $builder->{config}{lddlflags} =~ s/-arch \S+//g; 2324 $builder->{config}{ldflags} =~ s/-arch \S+//g; 2325 } 2326 $builder; 2327} 2328 2329sub _set_language { 2330 my $self = shift->_get_instance(); 2331 my ($lang, $impl) = @_; 2332 2333 defined( $lang ) or croak( "Missing language" ); 2334 2335 defined( $impl ) and defined( $self->{lang_supported}->{$lang} ) 2336 and $impl ne $self->{lang_supported}->{$lang} 2337 and croak( "Language implementor ($impl) doesn't match exisiting one (" . $self->{lang_supported}->{$lang} . ")" ); 2338 2339 defined( $impl ) and !defined( $self->{lang_supported}->{$lang} ) 2340 and $self->{lang_supported}->{$lang} = $impl; 2341 2342 defined( $self->{lang_supported}->{$lang} ) or croak( "Unsupported language \"$lang\"" ); 2343 2344 defined( $self->{extra_compile_flags}->{$lang} ) or $self->{extra_compile_flags}->{$lang} = []; 2345 2346 $self->{lang} = $lang; 2347 2348 return; 2349} 2350 2351sub _fill_defines { 2352 my ($self, $src, $action_if_true, $action_if_false) = @_; 2353 ref $self or $self = $self->_get_instance(); 2354 2355 my $conftest = ""; 2356 while( my ($defname, $defcnt) = each( %{ $self->{defines} } ) ) { 2357 $defcnt->[0] or next; 2358 defined $defcnt->[1] and $conftest .= "/* " . $defcnt->[1] . " */\n"; 2359 $conftest .= join( " ", "#define", $defname, $defcnt->[0] ) . "\n"; 2360 } 2361 $conftest .= "/* end of conftest.h */\n"; 2362 2363 $conftest; 2364} 2365 2366# 2367# default includes taken from autoconf/headers.m4 2368# 2369 2370=head2 _default_includes 2371 2372returns a string containing default includes for program prologue taken 2373from autoconf/headers.m4: 2374 2375 #include <stdio.h> 2376 #ifdef HAVE_SYS_TYPES_H 2377 # include <sys/types.h> 2378 #endif 2379 #ifdef HAVE_SYS_STAT_H 2380 # include <sys/stat.h> 2381 #endif 2382 #ifdef STDC_HEADERS 2383 # include <stdlib.h> 2384 # include <stddef.h> 2385 #else 2386 # ifdef HAVE_STDLIB_H 2387 # include <stdlib.h> 2388 # endif 2389 #endif 2390 #ifdef HAVE_STRING_H 2391 # if !defined STDC_HEADERS && defined HAVE_MEMORY_H 2392 # include <memory.h> 2393 # endif 2394 # include <string.h> 2395 #endif 2396 #ifdef HAVE_STRINGS_H 2397 # include <strings.h> 2398 #endif 2399 #ifdef HAVE_INTTYPES_H 2400 # include <inttypes.h> 2401 #endif 2402 #ifdef HAVE_STDINT_H 2403 # include <stdint.h> 2404 #endif 2405 #ifdef HAVE_UNISTD_H 2406 # include <unistd.h> 2407 #endif 2408 2409=cut 2410 2411my $_default_includes = <<"_ACEOF"; 2412#include <stdio.h> 2413#ifdef HAVE_SYS_TYPES_H 2414# include <sys/types.h> 2415#endif 2416#ifdef HAVE_SYS_STAT_H 2417# include <sys/stat.h> 2418#endif 2419#ifdef STDC_HEADERS 2420# include <stdlib.h> 2421# include <stddef.h> 2422#else 2423# ifdef HAVE_STDLIB_H 2424# include <stdlib.h> 2425# endif 2426#endif 2427#ifdef HAVE_STRING_H 2428# if !defined STDC_HEADERS && defined HAVE_MEMORY_H 2429# include <memory.h> 2430# endif 2431# include <string.h> 2432#endif 2433#ifdef HAVE_STRINGS_H 2434# include <strings.h> 2435#endif 2436#ifdef HAVE_INTTYPES_H 2437# include <inttypes.h> 2438#endif 2439#ifdef HAVE_STDINT_H 2440# include <stdint.h> 2441#endif 2442#ifdef HAVE_UNISTD_H 2443# include <unistd.h> 2444#endif 2445_ACEOF 2446 2447 2448sub _default_includes { $_default_includes } 2449 2450sub _default_main { $_[0]->_build_main("") } 2451 2452my $_main_tpl = <<"_ACEOF"; 2453 int 2454 main () 2455 { 2456 %s; 2457 return 0; 2458 } 2459_ACEOF 2460 2461 2462sub _build_main { 2463 my $self = shift->_get_instance(); 2464 my $body = shift || ""; 2465 sprintf($_main_tpl, $body); 2466} 2467 2468=head2 _default_includes_with_perl 2469 2470returns a string containing default includes for program prologue containing 2471I<_default_includes> plus 2472 2473 #include <EXTERN.h> 2474 #include <perl.h> 2475 2476=cut 2477 2478my $_include_perl = <<"_ACEOF"; 2479#include <EXTERN.h> 2480#include <perl.h> 2481#include <XSUB.h> /* for perl context in threaded perls */ 2482_ACEOF 2483 2484sub _default_includes_with_perl { 2485 join( "\n", $_[0]->_default_includes, $_include_perl ); 2486} 2487 2488sub _cache_prefix { "ac" } 2489 2490sub _cache_name { 2491 my ($self, @names) = @_; 2492 my $cache_name = join( "_", $self->_cache_prefix(), "cv", @names ); 2493 $cache_name =~ tr/_A-Za-z0-9/_/c; 2494 $cache_name; 2495} 2496 2497sub _get_log_fh { 2498 my $self = $_[0]->_get_instance(); 2499 unless( defined( $self->{logfh} ) ) { 2500 my $open_mode = defined $self->{logfile_mode} ? $self->{logfile_mode} : ">"; 2501 open( my $fh, $open_mode, $self->{logfile} ) or croak "Could not open file $self->{logfile}: $!"; 2502 $self->{logfh} = [ $fh ]; 2503 } 2504 2505 $self->{logfh}; 2506} 2507 2508sub _add_log_entry { 2509 my ($self, @logentries) = @_; 2510 ref($self) or $self = $self->_get_instance(); 2511 $self->_get_log_fh(); 2512 foreach my $logentry (@logentries) { 2513 foreach my $fh (@{$self->{logfh}}) { 2514 print {$fh} "$logentry"; 2515 } 2516 } 2517 2518 return; 2519} 2520 2521sub _add_log_lines { 2522 my ($self, @logentries) = @_; 2523 ref($self) or $self = $self->_get_instance(); 2524 $self->_get_log_fh(); 2525 my $logmsg = join("\n", @logentries) . "\n"; 2526 foreach my $fh (@{$self->{logfh}}) { 2527 print {$fh} $logmsg; 2528 } 2529 2530 return; 2531} 2532 2533=head2 add_log_fh 2534 2535Push new file handles at end of log-handles to allow tee-ing log-output 2536 2537=cut 2538 2539sub add_log_fh { 2540 my ($self, @newh) = @_; 2541 $self->_get_log_fh(); 2542SKIP_DUP: 2543 foreach my $fh (@newh) { 2544 foreach my $eh (@{$self->{logfh}}) { 2545 $fh == $eh and next SKIP_DUP; 2546 } 2547 push @{$self->{logfh}}, $fh; 2548 } 2549 return; 2550} 2551 2552=head2 delete_log_fh 2553 2554Removes specified log file handles. This method allows you to shoot you 2555in your foot - it doesn't prove whether the primary nor the last handle 2556is removed. Use with caution. 2557 2558=cut 2559 2560sub delete_log_fh { 2561 my ($self, @xh) = @_; 2562 $self->_get_log_fh(); 2563SKIP_DUP: 2564 foreach my $fh (@xh) { 2565 foreach my $ih (0 .. $#{$self->{logfh}}) { 2566 $fh == $self->{logfh}->[$ih] or next; 2567 splice @{$self->{logfh}}, $ih, 1; 2568 last; 2569 } 2570 } 2571 return; 2572} 2573 2574sub _cache_type_name { 2575 my ($self, @names) = @_; 2576 $self->_cache_name( map { $_ =~ tr/*/p/; $_ } @names ); 2577} 2578 2579sub _get_extra_compiler_flags { 2580 my $self = shift->_get_instance(); 2581 my @ppflags = @{$self->{extra_preprocess_flags}}; 2582 my @cflags = @{$self->{extra_compile_flags}->{ $self->{lang} }}; 2583 join( " ", @ppflags, @cflags ); 2584} 2585 2586sub _get_extra_linker_flags { 2587 my $self = shift->_get_instance(); 2588 my @libs = @{$self->{extra_libs}}; 2589 my @ldflags = @{$self->{extra_link_flags}}; 2590 join( " ", @ldflags, map { "-l$_" } @libs ); 2591} 2592 2593=head1 AUTHOR 2594 2595Alberto Simões, C<< <ambs@cpan.org> >> 2596 2597Jens Rehsack, C<< <rehsack@cpan.org> >> 2598 2599=head1 NEXT STEPS 2600 2601Although a lot of work needs to be done, this is the next steps I 2602intent to take. 2603 2604 - detect flex/lex 2605 - detect yacc/bison/byacc 2606 - detect ranlib (not sure about its importance) 2607 2608These are the ones I think not too much important, and will be 2609addressed later, or by request. 2610 2611 - detect an 'install' command 2612 - detect a 'ln -s' command -- there should be a module doing 2613 this kind of task. 2614 2615=head1 BUGS 2616 2617A lot. Portability is a pain. B<<Patches welcome!>>. 2618 2619Please report any bugs or feature requests to 2620C<bug-extutils-autoconf@rt.cpan.org>, or through the web interface at 2621L<http://rt.cpan.org>. I will be notified, and then you'll automatically 2622be notified of progress on your bug as I make changes. 2623 2624=head1 ACKNOWLEDGEMENTS 2625 2626Michael Schwern for kind MacOS X help. 2627 2628Ken Williams for ExtUtils::CBuilder 2629 2630=head1 COPYRIGHT & LICENSE 2631 2632Copyright 2004-2011 by the Authors 2633 2634This program is free software; you can redistribute it and/or modify it 2635under the same terms as Perl itself. 2636 2637=head1 SEE ALSO 2638 2639ExtUtils::CBuilder(3) 2640 2641=cut 2642 26431; # End of Config::AutoConf 2644