1 package ExtUtils::CBuilder::Base;
11 use vars qw($VERSION);
16 my $self = bless {@_}, $class;
18 $self->{properties}{perl} = $class->find_perl_interpreter
19 or warn "Warning: Can't locate your perl binary";
21 while (my ($k,$v) = each %Config) {
22 $self->{config}{$k} = $v unless exists $self->{config}{$k};
24 $self->{config}{cc} = $ENV{CC} if exists $ENV{CC};
28 sub find_perl_interpreter {
30 File::Spec->file_name_is_absolute($perl = $^X)
31 or -f ($perl = $Config::Config{perlpath})
39 $self->{files_to_clean}{$_} = 1;
45 foreach my $file (keys %{$self->{files_to_clean}}) {
51 my ($self, $filename) = @_;
53 # File name, minus the suffix
54 (my $file_base = $filename) =~ s/\.[^.]+$//;
55 return "$file_base$self->{config}{obj_ext}";
58 sub arg_include_dirs {
60 return map {"-I$_"} @_;
63 sub arg_nolink { '-c' }
66 my ($self, $file) = @_;
70 sub arg_share_object_file {
71 my ($self, $file) = @_;
72 return ($self->split_like_shell($self->{config}{lddlflags}), '-o', $file);
76 my ($self, $file) = @_;
81 my ($self, %args) = @_;
82 return map "-D$_=$args{$_}", keys %args;
86 my ($self, %args) = @_;
87 die "Missing 'source' argument to compile()" unless defined $args{source};
89 my $cf = $self->{config}; # For convenience
91 $args{object_file} ||= $self->object_file($args{source});
93 my @include_dirs = $self->arg_include_dirs
94 (@{$args{include_dirs} || []},
97 my @defines = $self->arg_defines( %{$args{defines} || {}} );
99 my @extra_compiler_flags = $self->split_like_shell($args{extra_compiler_flags});
100 my @cccdlflags = $self->split_like_shell($cf->{cccdlflags});
101 my @ccflags = $self->split_like_shell($cf->{ccflags});
102 push @ccflags, qw/-x c++/ if $args{'C++'};
103 my @optimize = $self->split_like_shell($cf->{optimize});
104 my @flags = (@include_dirs, @defines, @cccdlflags, @extra_compiler_flags,
107 $self->arg_object_file($args{object_file}),
110 my @cc = $self->split_like_shell($cf->{cc});
112 $self->do_system(@cc, @flags, $args{source})
113 or die "error building $args{object_file} from '$args{source}'";
115 return $args{object_file};
119 my ($self, $is_cplusplus) = @_;
120 return $self->{have_compiler} if defined $self->{have_compiler};
124 # tmpdir has issues for some people so fall back to current dir
125 DIR: for my $dir ( File::Spec->tmpdir, '.' ) {
127 # don't clobber existing files (rare, but possible)
128 my $rand = int(rand(2**31));
129 my $tmpfile = File::Spec->catfile($dir, "compilet-$rand.c");
130 $tmpfile .= "c" if $is_cplusplus;
132 redo DIR if $attempts--;
137 my $FH = IO::File->new("> $tmpfile") or die "Can't create $tmpfile: $!";
138 if ( $is_cplusplus ) {
139 print $FH "class Bogus { public: int boot_compilet() { return 1; } };\n";
142 print $FH "int boot_compilet() { return 1; }\n";
146 my ($obj_file, @lib_files);
149 local $self->{quiet} = 1;
150 $obj_file = $self->compile('C++' => $is_cplusplus, source => $tmpfile);
151 @lib_files = $self->link(objects => $obj_file, module_name => 'compilet');
153 $result = $@ ? 0 : 1;
155 foreach (grep defined, $tmpfile, $obj_file, @lib_files) {
161 return $self->{have_compiler} = $result;
170 my ($self, $dl_file) = @_;
171 $dl_file =~ s/\.[^.]+$//;
173 return "$dl_file.$self->{config}{dlext}";
178 my ($self, $dl_file) = @_;
179 $dl_file =~ s/\.[^.]+$//;
181 return "$dl_file$self->{config}{_exe}";
184 sub need_prelink { 0 }
186 sub extra_link_args_after_prelink { return }
189 my ($self, %args) = @_;
191 ($args{dl_file} = $args{dl_name}) =~ s/.*::// unless $args{dl_file};
193 require ExtUtils::Mksymlists;
194 ExtUtils::Mksymlists::Mksymlists( # dl. abbrev for dynamic library
195 DL_VARS => $args{dl_vars} || [],
196 DL_FUNCS => $args{dl_funcs} || {},
197 FUNCLIST => $args{dl_func_list} || [],
198 IMPORTS => $args{dl_imports} || {},
199 NAME => $args{dl_name}, # Name of the Perl module
200 DLBASE => $args{dl_base}, # Basename of DLL file
201 FILE => $args{dl_file}, # Dir + Basename of symlist file
202 VERSION => (defined $args{dl_version} ? $args{dl_version} : '0.0'),
205 # Mksymlists will create one of these files
206 return grep -e, map "$args{dl_file}.$_", qw(ext def opt);
210 my ($self, %args) = @_;
211 return $self->_do_link('lib_file', lddl => 1, %args);
214 sub link_executable {
215 my ($self, %args) = @_;
216 return $self->_do_link('exe_file', lddl => 0, %args);
220 my ($self, $type, %args) = @_;
222 my $cf = $self->{config}; # For convenience
224 my $objects = delete $args{objects};
225 $objects = [$objects] unless ref $objects;
226 my $out = $args{$type} || $self->$type($objects->[0]);
230 $self->prelink(%args,
231 dl_name => $args{module_name}) if $args{lddl} && $self->need_prelink;
233 my @linker_flags = ($self->split_like_shell($args{extra_linker_flags}),
234 $self->extra_link_args_after_prelink(%args, dl_name => $args{module_name},
235 prelink_res => \@temp_files));
237 my @output = $args{lddl} ? $self->arg_share_object_file($out) : $self->arg_exec_file($out);
238 my @shrp = $self->split_like_shell($cf->{shrpenv});
239 my @ld = $self->split_like_shell($cf->{ld});
241 $self->do_system(@shrp, @ld, @output, @$objects, @linker_flags)
242 or die "error building $out from @$objects";
244 return wantarray ? ($out, @temp_files) : $out;
249 my ($self, @cmd) = @_;
250 print "@cmd\n" if !$self->{quiet};
251 return !system(@cmd);
254 sub split_like_shell {
255 my ($self, $string) = @_;
257 return () unless defined($string);
258 return @$string if UNIVERSAL::isa($string, 'ARRAY');
259 $string =~ s/^\s+|\s+$//g;
260 return () unless length($string);
262 return Text::ParseWords::shellwords($string);
265 # if building perl, perl's main source directory
267 # N.B. makemaker actually searches regardless of PERL_CORE, but
268 # only squawks at not finding it if PERL_CORE is set
270 return unless $ENV{PERL_CORE};
272 my $Updir = File::Spec->updir;
273 my $dir = File::Spec->curdir;
275 # Try up to 5 levels upwards
278 -f File::Spec->catfile($dir,"config_h.SH")
280 -f File::Spec->catfile($dir,"perl.h")
282 -f File::Spec->catfile($dir,"lib","Exporter.pm")
284 return Cwd::realpath( $dir );
287 $dir = File::Spec->catdir($dir, $Updir);
290 warn "PERL_CORE is set but I can't find your perl source!\n";
291 return ''; # return empty string if $ENV{PERL_CORE} but can't find dir ???
294 # directory of perl's include files
298 $self->perl_src() || File::Spec->catdir($self->{config}{archlibexp},"CORE");
303 local($., $@, $!, $^E, $?);