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};
120 my $tmpfile = File::Spec->catfile(File::Spec->tmpdir, 'compilet.c');
122 my $FH = IO::File->new("> $tmpfile") or die "Can't create $tmpfile: $!";
123 print $FH "int boot_compilet() { return 1; }\n";
126 my ($obj_file, @lib_files);
128 $obj_file = $self->compile(source => $tmpfile);
129 @lib_files = $self->link(objects => $obj_file, module_name => 'compilet');
132 my $result = $self->{have_compiler} = $@ ? 0 : 1;
134 foreach (grep defined, $tmpfile, $obj_file, @lib_files) {
141 my ($self, $dl_file) = @_;
142 $dl_file =~ s/\.[^.]+$//;
144 return "$dl_file.$self->{config}{dlext}";
149 my ($self, $dl_file) = @_;
150 $dl_file =~ s/\.[^.]+$//;
152 return "$dl_file$self->{config}{_exe}";
155 sub need_prelink { 0 }
157 sub extra_link_args_after_prelink { return }
160 my ($self, %args) = @_;
162 ($args{dl_file} = $args{dl_name}) =~ s/.*::// unless $args{dl_file};
164 require ExtUtils::Mksymlists;
165 ExtUtils::Mksymlists::Mksymlists( # dl. abbrev for dynamic library
166 DL_VARS => $args{dl_vars} || [],
167 DL_FUNCS => $args{dl_funcs} || {},
168 FUNCLIST => $args{dl_func_list} || [],
169 IMPORTS => $args{dl_imports} || {},
170 NAME => $args{dl_name}, # Name of the Perl module
171 DLBASE => $args{dl_base}, # Basename of DLL file
172 FILE => $args{dl_file}, # Dir + Basename of symlist file
173 VERSION => (defined $args{dl_version} ? $args{dl_version} : '0.0'),
176 # Mksymlists will create one of these files
177 return grep -e, map "$args{dl_file}.$_", qw(ext def opt);
181 my ($self, %args) = @_;
182 return $self->_do_link('lib_file', lddl => 1, %args);
185 sub link_executable {
186 my ($self, %args) = @_;
187 return $self->_do_link('exe_file', lddl => 0, %args);
191 my ($self, $type, %args) = @_;
193 my $cf = $self->{config}; # For convenience
195 my $objects = delete $args{objects};
196 $objects = [$objects] unless ref $objects;
197 my $out = $args{$type} || $self->$type($objects->[0]);
201 $self->prelink(%args,
202 dl_name => $args{module_name}) if $args{lddl} && $self->need_prelink;
204 my @linker_flags = ($self->split_like_shell($args{extra_linker_flags}),
205 $self->extra_link_args_after_prelink(%args, dl_name => $args{module_name},
206 prelink_res => \@temp_files));
208 my @output = $args{lddl} ? $self->arg_share_object_file($out) : $self->arg_exec_file($out);
209 my @shrp = $self->split_like_shell($cf->{shrpenv});
210 my @ld = $self->split_like_shell($cf->{ld});
212 $self->do_system(@shrp, @ld, @output, @$objects, @linker_flags)
213 or die "error building $out from @$objects";
215 return wantarray ? ($out, @temp_files) : $out;
220 my ($self, @cmd) = @_;
221 print "@cmd\n" if !$self->{quiet};
222 return !system(@cmd);
225 sub split_like_shell {
226 my ($self, $string) = @_;
228 return () unless defined($string);
229 return @$string if UNIVERSAL::isa($string, 'ARRAY');
230 $string =~ s/^\s+|\s+$//g;
231 return () unless length($string);
233 return Text::ParseWords::shellwords($string);
236 # if building perl, perl's main source directory
238 # N.B. makemaker actually searches regardless of PERL_CORE, but
239 # only squawks at not finding it if PERL_CORE is set
241 return unless $ENV{PERL_CORE};
243 my $Updir = File::Spec->updir;
244 my $dir = File::Spec->curdir;
246 # Try up to 5 levels upwards
249 -f File::Spec->catfile($dir,"config_h.SH")
251 -f File::Spec->catfile($dir,"perl.h")
253 -f File::Spec->catfile($dir,"lib","Exporter.pm")
255 return Cwd::realpath( $dir );
258 $dir = File::Spec->catdir($dir, $Updir);
261 warn "PERL_CORE is set but I can't find your perl source!\n";
262 return ''; # return empty string if $ENV{PERL_CORE} but can't find dir ???
265 # directory of perl's include files
269 $self->perl_src() || File::Spec->catdir($self->{config}{archlibexp},"CORE");
274 local($., $@, $!, $^E, $?);