1 package ExtUtils::CBuilder::Base;
14 my $self = bless {@_}, $class;
16 $self->{properties}{perl} = $class->find_perl_interpreter
17 or warn "Warning: Can't locate your perl binary";
19 while (my ($k,$v) = each %Config) {
20 $self->{config}{$k} = $v unless exists $self->{config}{$k};
25 sub find_perl_interpreter {
27 File::Spec->file_name_is_absolute($perl = $^X)
28 or -f ($perl = $Config::Config{perlpath})
36 $self->{files_to_clean}{$_} = 1;
42 foreach my $file (keys %{$self->{files_to_clean}}) {
48 my ($self, $filename) = @_;
50 # File name, minus the suffix
51 (my $file_base = $filename) =~ s/\.[^.]+$//;
52 return "$file_base$self->{config}{obj_ext}";
55 sub arg_include_dirs {
57 return map {"-I$_"} @_;
60 sub arg_nolink { '-c' }
63 my ($self, $file) = @_;
67 sub arg_share_object_file {
68 my ($self, $file) = @_;
69 return ($self->split_like_shell($self->{config}{lddlflags}), '-o', $file);
73 my ($self, $file) = @_;
78 my ($self, %args) = @_;
79 die "Missing 'source' argument to compile()" unless defined $args{source};
81 my $cf = $self->{config}; # For convenience
83 $args{object_file} ||= $self->object_file($args{source});
85 my @include_dirs = $self->arg_include_dirs
86 (@{$args{include_dirs} || []},
89 my @extra_compiler_flags = $self->split_like_shell($args{extra_compiler_flags});
90 my @cccdlflags = $self->split_like_shell($cf->{cccdlflags});
91 my @ccflags = $self->split_like_shell($cf->{ccflags});
92 my @optimize = $self->split_like_shell($cf->{optimize});
93 my @flags = (@include_dirs, @cccdlflags, @extra_compiler_flags,
96 $self->arg_object_file($args{object_file}),
99 my @cc = $self->split_like_shell($cf->{cc});
101 $self->do_system(@cc, @flags, $args{source})
102 or die "error building $args{object_file} from '$args{source}'";
104 return $args{object_file};
109 return $self->{have_compiler} if defined $self->{have_compiler};
111 my $tmpfile = File::Spec->catfile(File::Spec->tmpdir, 'compilet.c');
114 open FH, "> $tmpfile" or die "Can't create $tmpfile: $!";
115 print FH "int boot_compilet() { return 1; }\n";
119 my ($obj_file, @lib_files);
121 $obj_file = $self->compile(source => $tmpfile);
122 @lib_files = $self->link(objects => $obj_file, module_name => 'compilet');
125 my $result = $self->{have_compiler} = $@ ? 0 : 1;
127 foreach (grep defined, $tmpfile, $obj_file, @lib_files) {
134 my ($self, $dl_file) = @_;
135 $dl_file =~ s/\.[^.]+$//;
137 return "$dl_file.$self->{config}{dlext}";
142 my ($self, $dl_file) = @_;
143 $dl_file =~ s/\.[^.]+$//;
145 return "$dl_file$self->{config}{_exe}";
148 sub need_prelink { 0 }
151 my ($self, %args) = @_;
153 ($args{dl_file} = $args{dl_name}) =~ s/.*::// unless $args{dl_file};
155 require ExtUtils::Mksymlists;
156 ExtUtils::Mksymlists::Mksymlists( # dl. abbrev for dynamic library
157 DL_VARS => $args{dl_vars} || [],
158 DL_FUNCS => $args{dl_funcs} || {},
159 FUNCLIST => $args{dl_func_list} || [],
160 IMPORTS => $args{dl_imports} || {},
161 NAME => $args{dl_name},
162 DLBASE => $args{dl_base},
163 FILE => $args{dl_file},
166 # Mksymlists will create one of these files
167 return grep -e, map "$args{dl_file}.$_", qw(ext def opt);
171 my ($self, %args) = @_;
172 return $self->_do_link('lib_file', lddl => 1, %args);
175 sub link_executable {
176 my ($self, %args) = @_;
177 return $self->_do_link('exe_file', lddl => 0, %args);
181 my ($self, $type, %args) = @_;
183 my $cf = $self->{config}; # For convenience
185 my $objects = delete $args{objects};
186 $objects = [$objects] unless ref $objects;
187 my $out = $args{$type} || $self->$type($objects->[0]);
191 $self->prelink(%args,
192 dl_name => $args{module_name}) if $args{lddl} && $self->need_prelink;
194 my @linker_flags = $self->split_like_shell($args{extra_linker_flags});
195 my @output = $args{lddl} ? $self->arg_share_object_file($out) : $self->arg_exec_file($out);
196 my @shrp = $self->split_like_shell($cf->{shrpenv});
197 my @ld = $self->split_like_shell($cf->{ld});
199 $self->do_system(@shrp, @ld, @output, @$objects, @linker_flags)
200 or die "error building $out from @$objects";
202 return wantarray ? ($out, @temp_files) : $out;
207 my ($self, @cmd) = @_;
208 print "@cmd\n" if !$self->{quiet};
209 return !system(@cmd);
212 sub split_like_shell {
213 my ($self, $string) = @_;
215 return () unless defined($string);
216 return @$string if UNIVERSAL::isa($string, 'ARRAY');
217 $string =~ s/^\s+|\s+$//g;
218 return () unless length($string);
220 return Text::ParseWords::shellwords($string);
223 # if building perl, perl's main source directory
225 # N.B. makemaker actually searches regardless of PERL_CORE, but
226 # only squawks at not finding it if PERL_CORE is set
228 return unless $ENV{PERL_CORE};
230 my $Updir = File::Spec->updir;
233 # Try up to 5 levels upwards
236 -f File::Spec->catfile($dir,"config_h.SH")
238 -f File::Spec->catfile($dir,"perl.h")
240 -f File::Spec->catfile($dir,"lib","Exporter.pm")
245 $dir = File::Spec->catdir($dir, $Updir);
248 warn "PERL_CORE is set but I can't find your perl source!\n";
252 # directory of perl's include files
256 $self->perl_src() || File::Spec->catdir($self->{config}{archlibexp},"CORE");