ExtUtils-CBuilder

 view release on metacpan or  search on metacpan

lib/ExtUtils/CBuilder/Base.pm  view on Meta::CPAN

package ExtUtils::CBuilder::Base;
use strict;
use warnings;
use File::Spec;
use File::Basename;
use Cwd ();
use Config;
use Text::ParseWords;
use IPC::Cmd qw(can_run);
use File::Temp qw(tempfile);

our $VERSION = '0.280236'; # VERSION


# More details about C/C++ compilers:
# http://developers.sun.com/sunstudio/documentation/product/compiler.jsp
# http://gcc.gnu.org/
# http://publib.boulder.ibm.com/infocenter/comphelp/v101v121/index.jsp
# http://msdn.microsoft.com/en-us/vstudio/default.aspx

my %cc2cxx = (
    # first line order is important to support wrappers like in pkgsrc
    cc => [ 'c++', 'CC', 'aCC', 'cxx', ], # Sun Studio, HP ANSI C/C++ Compilers
    gcc => [ 'g++' ], # GNU Compiler Collection
    xlc => [ 'xlC' ], # IBM C/C++ Set, xlc without thread-safety
    xlc_r => [ 'xlC_r' ], # IBM C/C++ Set, xlc with thread-safety
    cl    => [ 'cl' ], # Microsoft Visual Studio
);

sub new {
  my $class = shift;
  my $self = bless {@_}, $class;

  $self->{properties}{perl} = $class->find_perl_interpreter
    or warn "Warning: Can't locate your perl binary";

  while (my ($k,$v) = each %Config) {
    $self->{config}{$k} = $v unless exists $self->{config}{$k};
  }
  $self->{config}{cc} = $ENV{CC} if defined $ENV{CC};
  $self->{config}{ccflags} = join(" ", $self->{config}{ccflags}, $ENV{CFLAGS})
     if defined $ENV{CFLAGS};
  $self->{config}{cxx} = $ENV{CXX} if defined $ENV{CXX};
  $self->{config}{cxxflags} = $ENV{CXXFLAGS} if defined $ENV{CXXFLAGS};
  $self->{config}{ld} = $ENV{LD} if defined $ENV{LD};
  $self->{config}{ldflags} = join(" ", $self->{config}{ldflags}, $ENV{LDFLAGS})
     if defined $ENV{LDFLAGS};

  unless ( exists $self->{config}{cxx} ) {

    my ($ccbase, $ccpath, $ccsfx ) = fileparse($self->{config}{cc}, qr/\.[^.]*/);

    ## If the path is just "cc", fileparse returns $ccpath as "./"
    $ccpath = "" if $self->{config}{cc} =~ /^\Q$ccbase$ccsfx\E$/;
      
    foreach my $cxx (@{$cc2cxx{$ccbase}}) {
      my $cxx1 = File::Spec->catfile( $ccpath, $cxx . $ccsfx);

      if( can_run( $cxx1 ) ) {
        $self->{config}{cxx} = $cxx1;
	last;
      }
      my $cxx2 = $cxx . $ccsfx;

      if( can_run( $cxx2 ) ) {
        $self->{config}{cxx} = $cxx2;
	last;
      }

      if( can_run( $cxx ) ) {
        $self->{config}{cxx} = $cxx;
	last;
      }
    }
    unless ( exists $self->{config}{cxx} ) {
      $self->{config}{cxx} = $self->{config}{cc};
      my $cflags = $self->{config}{ccflags};
      $self->{config}{cxxflags} = '-x c++';
      $self->{config}{cxxflags} .= " $cflags" if defined $cflags;
    }
  }

  return $self;
}

sub find_perl_interpreter {
  my $perl;
  File::Spec->file_name_is_absolute($perl = $^X)
    or -f ($perl = $Config::Config{perlpath})
    or ($perl = $^X); # XXX how about using IPC::Cmd::can_run here?
  return $perl;
}

sub add_to_cleanup {
  my $self = shift;
  foreach (@_) {
    $self->{files_to_clean}{$_} = 1;
  }
}

sub cleanup {
  my $self = shift;
  foreach my $file (keys %{$self->{files_to_clean}}) {
    unlink $file;
  }
}



( run in 1.792 second using v1.01-cache-2.11-cpan-f56aa216473 )