Skip to content

Commit

Permalink
Handle c++ compiler (cxx) independent of c compiler (cc)
Browse files Browse the repository at this point in the history
  • Loading branch information
rehsack authored and xdg committed Jul 27, 2010
1 parent d3c1cb9 commit e653d24
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
41 changes: 37 additions & 4 deletions lib/ExtUtils/CBuilder/Base.pm
Expand Up @@ -8,10 +8,18 @@ use Config;
use Text::ParseWords;
use IO::File;
use Data::Dumper;$Data::Dumper::Indent=1;
use IPC::Cmd qw(can_run);

use vars qw($VERSION);
$VERSION = '0.2704';

my %cc2cxx = (
cc => [ 'c++', 'CC', ], # http://developers.sun.com/sunstudio/documentation/product/compiler.jsp
gcc => [ 'g++' ], # http://gcc.gnu.org/
xlc => [ 'xlC' ], # http://publib.boulder.ibm.com/infocenter/comphelp/v101v121/index.jsp
xlc_r => [ 'xlC_r' ], # http://publib.boulder.ibm.com/infocenter/comphelp/v101v121/index.jsp
);

sub new {
my $class = shift;
my $self = bless {@_}, $class;
Expand All @@ -23,6 +31,26 @@ sub new {
$self->{config}{$k} = $v unless exists $self->{config}{$k};
}
$self->{config}{cc} = $ENV{CC} if exists $ENV{CC};
$self->{config}{ccflags} = $ENV{CFLAGS} if exists $ENV{CFLAGS};
$self->{config}{cxx} = $ENV{CXX} if exists $ENV{CXX};
$self->{config}{cxxflags} = $ENV{CXXFLAGS} if exists $ENV{CXXFLAGS};
$self->{config}{ld} = $ENV{LD} if exists $ENV{LD};
$self->{config}{ldflags} = $ENV{LDFLAGS} if exists $ENV{LDFLAGS};

unless ( exists $self->{config}{cxx} ) {
my $knowncc = basename($self->{config}{cc});
foreach my $cxx (@{$cc2cxx{$knowncc}}) {
if( can_run( $cxx ) ) {
$self->{config}{cxx} = $cxx;
last;
}
}
unless ( exists $self->{config}{cxx} ) {
$self->{config}{cxx} = $self->{config}{cc};
$self->{config}{cxxflags} = join( ' ', '-x c++', $self->{config}{cflags} );
}
}

return $self;
}

Expand All @@ -48,6 +76,10 @@ sub cleanup {
}
}

sub get_config {
return %{ $_[0]->{config} };
}

sub object_file {
my ($self, $filename) = @_;

Expand Down Expand Up @@ -108,8 +140,7 @@ sub compile {
my @extra_compiler_flags =
$self->split_like_shell($args{extra_compiler_flags});
my @cccdlflags = $self->split_like_shell($cf->{cccdlflags});
my @ccflags = $self->split_like_shell($cf->{ccflags});
push @ccflags, qw/-x c++/ if $args{'C++'};
my @ccflags = $self->split_like_shell($args{'C++'} ? $cf->{cxxflags} : $cf->{ccflags});
my @optimize = $self->split_like_shell($cf->{optimize});
my @flags = (
@include_dirs,
Expand All @@ -121,6 +152,7 @@ sub compile {
@optimize,
$self->arg_object_file($object_file),
);
my @cc = $self->split_like_shell($args{'C++'} ? $cf->{cxx} : $cf->{cc});

$self->do_system(@cc, @flags, $args{source})
or die "error building $object_file from '$args{source}'";
Expand All @@ -130,7 +162,8 @@ sub compile {

sub have_compiler {
my ($self, $is_cplusplus) = @_;
return $self->{have_compiler} if defined $self->{have_compiler};
my $have_compiler_flag = $is_cplusplus ? "have_cxx" : "have_cc";
return $self->{$have_compiler_flag} if defined $self->{$have_compiler_flag};

my $result;
my $attempts = 3;
Expand Down Expand Up @@ -171,7 +204,7 @@ sub have_compiler {
last DIR if $result;
}

return $self->{have_compiler} = $result;
return $self->{$have_compiler_flag} = $result;
}

sub have_cplusplus {
Expand Down
8 changes: 4 additions & 4 deletions t/00-have-compiler.t
Expand Up @@ -29,17 +29,17 @@ my $run_perl = "$perl -e1 --";
$b->{config}{cc} = $bogus_path;
$b->{config}{ld} = $bogus_path;

$b->{have_compiler} = undef;
$b->{have_cc} = undef;
is( $b->have_compiler, 0, "have_compiler: fake missing cc" );
$b->{have_compiler} = undef;
$b->{have_cxx} = undef;
is( $b->have_cplusplus, 0, "have_cplusplus: fake missing c++" );

# test found compiler
$b->{config}{cc} = $run_perl;
$b->{config}{ld} = $run_perl;
$b->{have_compiler} = undef;
$b->{have_cc} = undef;
is( $b->have_compiler, 1, "have_compiler: fake present cc" );
$b->{have_compiler} = undef;
$b->{have_cxx} = undef;
is( $b->have_cplusplus, 1, "have_cpp_compiler: fake present c++" );

# test missing cpp compiler
Expand Down

0 comments on commit e653d24

Please sign in to comment.