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})
35 my %files = map {$_, 1} @_;
39 my ($self, $filename) = @_;
41 # File name, minus the suffix
42 (my $file_base = $filename) =~ s/\.[^.]+$//;
43 return "$file_base$self->{config}{obj_ext}";
46 sub arg_include_dirs {
48 return map {"-I$_"} @_;
51 sub arg_nolink { '-c' }
54 my ($self, $file) = @_;
58 sub arg_share_object_file {
59 my ($self, $file) = @_;
60 return ($self->split_like_shell($self->{config}{lddlflags}), '-o', $file);
64 my ($self, $file) = @_;
69 my ($self, %args) = @_;
70 die "Missing 'source' argument to compile()" unless defined $args{source};
72 my $cf = $self->{config}; # For convenience
74 $args{object_file} ||= $self->object_file($args{source});
76 my @include_dirs = $self->arg_include_dirs
77 (@{$args{include_dirs} || []},
80 my @extra_compiler_flags = $self->split_like_shell($args{extra_compiler_flags});
81 my @cccdlflags = $self->split_like_shell($cf->{cccdlflags});
82 my @ccflags = $self->split_like_shell($cf->{ccflags});
83 my @optimize = $self->split_like_shell($cf->{optimize});
84 my @flags = (@include_dirs, @cccdlflags, @extra_compiler_flags,
87 $self->arg_object_file($args{object_file}),
90 my @cc = $self->split_like_shell($cf->{cc});
92 $self->do_system(@cc, @flags, $args{source})
93 or die "error building $args{object_file} from '$args{source}'";
95 return $args{object_file};
100 return $self->{have_compiler} if defined $self->{have_compiler};
102 my $tmpfile = File::Spec->catfile(File::Spec->tmpdir, 'compilet.c');
105 open FH, "> $tmpfile" or die "Can't create $tmpfile: $!";
106 print FH "int boot_compilet() { return 1; }\n";
110 my ($obj_file, @lib_files);
112 $obj_file = $self->compile(source => $tmpfile);
113 @lib_files = $self->link(objects => $obj_file, module_name => 'compilet');
116 my $result = $self->{have_compiler} = $@ ? 0 : 1;
118 foreach (grep defined, $tmpfile, $obj_file, @lib_files) {
125 my ($self, $dl_file) = @_;
126 $dl_file =~ s/\.[^.]+$//;
128 return "$dl_file.$self->{config}{dlext}";
133 my ($self, $dl_file) = @_;
134 $dl_file =~ s/\.[^.]+$//;
136 return "$dl_file$self->{config}{_exe}";
139 sub need_prelink { 0 }
142 my ($self, %args) = @_;
144 ($args{dl_file} = $args{dl_name}) =~ s/.*::// unless $args{dl_file};
146 require ExtUtils::Mksymlists;
147 ExtUtils::Mksymlists::Mksymlists( # dl. abbrev for dynamic library
148 DL_VARS => $args{dl_vars} || [],
149 DL_FUNCS => $args{dl_funcs} || {},
150 FUNCLIST => $args{dl_func_list} || [],
151 IMPORTS => $args{dl_imports} || {},
152 NAME => $args{dl_name},
153 DLBASE => $args{dl_base},
154 FILE => $args{dl_file},
157 # Mksymlists will create one of these files
158 return grep -e, map "$args{dl_file}.$_", qw(ext def opt);
162 my ($self, %args) = @_;
163 return $self->_do_link('lib_file', lddl => 1, %args);
166 sub link_executable {
167 my ($self, %args) = @_;
168 return $self->_do_link('exe_file', lddl => 0, %args);
172 my ($self, $type, %args) = @_;
174 my $cf = $self->{config}; # For convenience
176 my $objects = delete $args{objects};
177 $objects = [$objects] unless ref $objects;
178 my $out = $args{$type} || $self->$type($objects->[0]);
182 $self->prelink(%args,
183 dl_name => $args{module_name}) if $args{lddl} && $self->need_prelink;
185 my @linker_flags = $self->split_like_shell($args{extra_linker_flags});
186 my @output = $args{lddl} ? $self->arg_share_object_file($out) : $self->arg_exec_file($out);
187 my @shrp = $self->split_like_shell($cf->{shrpenv});
188 my @ld = $self->split_like_shell($cf->{ld});
189 $self->do_system(@shrp, @ld, @output, @$objects, @linker_flags)
190 or die "error building $out from @$objects";
192 return wantarray ? ($out, @temp_files) : $out;
197 my ($self, @cmd) = @_;
198 print "@cmd\n" if !$self->{quiet};
199 return !system(@cmd);
202 sub split_like_shell {
203 my ($self, $string) = @_;
205 return () unless defined($string);
206 return @$string if UNIVERSAL::isa($string, 'ARRAY');
207 $string =~ s/^\s+|\s+$//g;
208 return () unless length($string);
210 return Text::ParseWords::shellwords($string);
213 # if building perl, perl's main source directory
215 # N.B. makemaker actually searches regardless of PERL_CORE, but
216 # only squawks at not finding it if PERL_CORE is set
218 return unless $ENV{PERL_CORE};
220 my $Updir = File::Spec->updir;
223 # Try up to 5 levels upwards
226 -f File::Spec->catfile($dir,"config_h.SH")
228 -f File::Spec->catfile($dir,"perl.h")
230 -f File::Spec->catfile($dir,"lib","Exporter.pm")
235 $dir = File::Spec->catdir($dir, $Updir);
238 warn "PERL_CORE is set but I can't find your perl source!\n";
242 # directory of perl's include files
246 $self->perl_src() || File::Spec->catdir($self->{config}{archlibexp},"CORE");