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};
27 sub find_perl_interpreter {
29 File::Spec->file_name_is_absolute($perl = $^X)
30 or -f ($perl = $Config::Config{perlpath})
38 $self->{files_to_clean}{$_} = 1;
44 foreach my $file (keys %{$self->{files_to_clean}}) {
50 my ($self, $filename) = @_;
52 # File name, minus the suffix
53 (my $file_base = $filename) =~ s/\.[^.]+$//;
54 return "$file_base$self->{config}{obj_ext}";
57 sub arg_include_dirs {
59 return map {"-I$_"} @_;
62 sub arg_nolink { '-c' }
65 my ($self, $file) = @_;
69 sub arg_share_object_file {
70 my ($self, $file) = @_;
71 return ($self->split_like_shell($self->{config}{lddlflags}), '-o', $file);
75 my ($self, $file) = @_;
80 my ($self, %args) = @_;
81 return map "-D$_=$args{$_}", keys %args;
85 my ($self, %args) = @_;
86 die "Missing 'source' argument to compile()" unless defined $args{source};
88 my $cf = $self->{config}; # For convenience
90 $args{object_file} ||= $self->object_file($args{source});
92 my @include_dirs = $self->arg_include_dirs
93 (@{$args{include_dirs} || []},
96 my @defines = $self->arg_defines( %{$args{defines} || {}} );
98 my @extra_compiler_flags = $self->split_like_shell($args{extra_compiler_flags});
99 my @cccdlflags = $self->split_like_shell($cf->{cccdlflags});
100 my @ccflags = $self->split_like_shell($cf->{ccflags});
101 my @optimize = $self->split_like_shell($cf->{optimize});
102 my @flags = (@include_dirs, @defines, @cccdlflags, @extra_compiler_flags,
105 $self->arg_object_file($args{object_file}),
108 my @cc = $self->split_like_shell($cf->{cc});
110 $self->do_system(@cc, @flags, $args{source})
111 or die "error building $args{object_file} from '$args{source}'";
113 return $args{object_file};
118 return $self->{have_compiler} if defined $self->{have_compiler};
122 # tmpdir has issues for some people so fall back to current dir
123 DIR: for my $dir ( File::Spec->tmpdir, '.' ) {
125 # don't clobber existing files (rare, but possible)
126 my $rand = int(rand(2**31));
127 my $tmpfile = File::Spec->catfile($dir, "compilet-$rand.c");
129 redo DIR if $attempts--;
134 my $FH = IO::File->new("> $tmpfile") or die "Can't create $tmpfile: $!";
135 print $FH "int boot_compilet() { return 1; }\n";
138 my ($obj_file, @lib_files);
141 $obj_file = $self->compile(source => $tmpfile);
142 @lib_files = $self->link(objects => $obj_file, module_name => 'compilet');
144 $result = $@ ? 0 : 1;
146 foreach (grep defined, $tmpfile, $obj_file, @lib_files) {
152 return $self->{have_compiler} = $result;
156 my ($self, $dl_file) = @_;
157 $dl_file =~ s/\.[^.]+$//;
159 return "$dl_file.$self->{config}{dlext}";
164 my ($self, $dl_file) = @_;
165 $dl_file =~ s/\.[^.]+$//;
167 return "$dl_file$self->{config}{_exe}";
170 sub need_prelink { 0 }
172 sub extra_link_args_after_prelink { return }
175 my ($self, %args) = @_;
177 ($args{dl_file} = $args{dl_name}) =~ s/.*::// unless $args{dl_file};
179 require ExtUtils::Mksymlists;
180 ExtUtils::Mksymlists::Mksymlists( # dl. abbrev for dynamic library
181 DL_VARS => $args{dl_vars} || [],
182 DL_FUNCS => $args{dl_funcs} || {},
183 FUNCLIST => $args{dl_func_list} || [],
184 IMPORTS => $args{dl_imports} || {},
185 NAME => $args{dl_name}, # Name of the Perl module
186 DLBASE => $args{dl_base}, # Basename of DLL file
187 FILE => $args{dl_file}, # Dir + Basename of symlist file
188 VERSION => (defined $args{dl_version} ? $args{dl_version} : '0.0'),
191 # Mksymlists will create one of these files
192 return grep -e, map "$args{dl_file}.$_", qw(ext def opt);
196 my ($self, %args) = @_;
197 return $self->_do_link('lib_file', lddl => 1, %args);
200 sub link_executable {
201 my ($self, %args) = @_;
202 return $self->_do_link('exe_file', lddl => 0, %args);
206 my ($self, $type, %args) = @_;
208 my $cf = $self->{config}; # For convenience
210 my $objects = delete $args{objects};
211 $objects = [$objects] unless ref $objects;
212 my $out = $args{$type} || $self->$type($objects->[0]);
216 $self->prelink(%args,
217 dl_name => $args{module_name}) if $args{lddl} && $self->need_prelink;
219 my @linker_flags = ($self->split_like_shell($args{extra_linker_flags}),
220 $self->extra_link_args_after_prelink(%args, dl_name => $args{module_name},
221 prelink_res => \@temp_files));
223 my @output = $args{lddl} ? $self->arg_share_object_file($out) : $self->arg_exec_file($out);
224 my @shrp = $self->split_like_shell($cf->{shrpenv});
225 my @ld = $self->split_like_shell($cf->{ld});
227 $self->do_system(@shrp, @ld, @output, @$objects, @linker_flags)
228 or die "error building $out from @$objects";
230 return wantarray ? ($out, @temp_files) : $out;
235 my ($self, @cmd) = @_;
236 print "@cmd\n" if !$self->{quiet};
237 return !system(@cmd);
240 sub split_like_shell {
241 my ($self, $string) = @_;
243 return () unless defined($string);
244 return @$string if UNIVERSAL::isa($string, 'ARRAY');
245 $string =~ s/^\s+|\s+$//g;
246 return () unless length($string);
248 return Text::ParseWords::shellwords($string);
251 # if building perl, perl's main source directory
253 # N.B. makemaker actually searches regardless of PERL_CORE, but
254 # only squawks at not finding it if PERL_CORE is set
256 return unless $ENV{PERL_CORE};
258 my $Updir = File::Spec->updir;
259 my $dir = File::Spec->curdir;
261 # Try up to 5 levels upwards
264 -f File::Spec->catfile($dir,"config_h.SH")
266 -f File::Spec->catfile($dir,"perl.h")
268 -f File::Spec->catfile($dir,"lib","Exporter.pm")
270 return Cwd::realpath( $dir );
273 $dir = File::Spec->catdir($dir, $Updir);
276 warn "PERL_CORE is set but I can't find your perl source!\n";
277 return ''; # return empty string if $ENV{PERL_CORE} but can't find dir ???
280 # directory of perl's include files
284 $self->perl_src() || File::Spec->catdir($self->{config}{archlibexp},"CORE");
289 local($., $@, $!, $^E, $?);