This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: $^CHILD_ERROR_NATIVE issues (with attachment)
[perl5.git] / lib / ExtUtils / CBuilder / Base.pm
CommitLineData
6b09c160
YST
1package ExtUtils::CBuilder::Base;
2
3use strict;
4use File::Spec;
5use File::Basename;
6use Config;
7use Text::ParseWords;
8
9use vars qw($VERSION);
345dbb93 10$VERSION = '0.12';
6b09c160
YST
11
12sub new {
13 my $class = shift;
14 my $self = bless {@_}, $class;
15
16 $self->{properties}{perl} = $class->find_perl_interpreter
17 or warn "Warning: Can't locate your perl binary";
18
19 while (my ($k,$v) = each %Config) {
20 $self->{config}{$k} = $v unless exists $self->{config}{$k};
21 }
22 return $self;
23}
24
25sub find_perl_interpreter {
26 my $perl;
27 File::Spec->file_name_is_absolute($perl = $^X)
28 or -f ($perl = $Config::Config{perlpath})
29 or ($perl = $^X);
30 return $perl;
31}
32
33sub add_to_cleanup {
34 my $self = shift;
da2b6f33
SH
35 foreach (@_) {
36 $self->{files_to_clean}{$_} = 1;
37 }
38}
39
40sub cleanup {
41 my $self = shift;
42 foreach my $file (keys %{$self->{files_to_clean}}) {
43 unlink $file;
44 }
6b09c160
YST
45}
46
47sub object_file {
48 my ($self, $filename) = @_;
49
50 # File name, minus the suffix
51 (my $file_base = $filename) =~ s/\.[^.]+$//;
52 return "$file_base$self->{config}{obj_ext}";
53}
54
55sub arg_include_dirs {
56 my $self = shift;
57 return map {"-I$_"} @_;
58}
59
60sub arg_nolink { '-c' }
61
62sub arg_object_file {
63 my ($self, $file) = @_;
64 return ('-o', $file);
65}
66
67sub arg_share_object_file {
68 my ($self, $file) = @_;
69 return ($self->split_like_shell($self->{config}{lddlflags}), '-o', $file);
70}
71
72sub arg_exec_file {
73 my ($self, $file) = @_;
74 return ('-o', $file);
75}
76
77sub compile {
78 my ($self, %args) = @_;
79 die "Missing 'source' argument to compile()" unless defined $args{source};
80
81 my $cf = $self->{config}; # For convenience
82
83 $args{object_file} ||= $self->object_file($args{source});
84
85 my @include_dirs = $self->arg_include_dirs
86 (@{$args{include_dirs} || []},
87 $self->perl_inc());
88
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,
94 $self->arg_nolink,
95 @ccflags, @optimize,
96 $self->arg_object_file($args{object_file}),
97 );
98
99 my @cc = $self->split_like_shell($cf->{cc});
100
101 $self->do_system(@cc, @flags, $args{source})
102 or die "error building $args{object_file} from '$args{source}'";
103
104 return $args{object_file};
105}
106
107sub have_compiler {
108 my ($self) = @_;
109 return $self->{have_compiler} if defined $self->{have_compiler};
110
111 my $tmpfile = File::Spec->catfile(File::Spec->tmpdir, 'compilet.c');
112 {
113 local *FH;
114 open FH, "> $tmpfile" or die "Can't create $tmpfile: $!";
115 print FH "int boot_compilet() { return 1; }\n";
116 close FH;
117 }
118
119 my ($obj_file, @lib_files);
120 eval {
121 $obj_file = $self->compile(source => $tmpfile);
122 @lib_files = $self->link(objects => $obj_file, module_name => 'compilet');
123 };
124 warn $@ if $@;
125 my $result = $self->{have_compiler} = $@ ? 0 : 1;
126
127 foreach (grep defined, $tmpfile, $obj_file, @lib_files) {
128 1 while unlink;
129 }
130 return $result;
131}
132
133sub lib_file {
134 my ($self, $dl_file) = @_;
135 $dl_file =~ s/\.[^.]+$//;
136 $dl_file =~ tr/"//d;
137 return "$dl_file.$self->{config}{dlext}";
138}
139
140
141sub exe_file {
142 my ($self, $dl_file) = @_;
143 $dl_file =~ s/\.[^.]+$//;
144 $dl_file =~ tr/"//d;
145 return "$dl_file$self->{config}{_exe}";
146}
147
148sub need_prelink { 0 }
149
150sub prelink {
151 my ($self, %args) = @_;
152
153 ($args{dl_file} = $args{dl_name}) =~ s/.*::// unless $args{dl_file};
154
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},
164 );
165
166 # Mksymlists will create one of these files
167 return grep -e, map "$args{dl_file}.$_", qw(ext def opt);
168}
169
170sub link {
171 my ($self, %args) = @_;
172 return $self->_do_link('lib_file', lddl => 1, %args);
173}
174
175sub link_executable {
176 my ($self, %args) = @_;
177 return $self->_do_link('exe_file', lddl => 0, %args);
178}
2bd31f1a 179
6b09c160
YST
180sub _do_link {
181 my ($self, $type, %args) = @_;
182
183 my $cf = $self->{config}; # For convenience
184
185 my $objects = delete $args{objects};
186 $objects = [$objects] unless ref $objects;
187 my $out = $args{$type} || $self->$type($objects->[0]);
188
189 my @temp_files;
190 @temp_files =
191 $self->prelink(%args,
345dbb93 192 dl_name => $args{module_name}) if $args{lddl} && $self->need_prelink;
6b09c160
YST
193
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});
2bd31f1a 198
6b09c160
YST
199 $self->do_system(@shrp, @ld, @output, @$objects, @linker_flags)
200 or die "error building $out from @$objects";
201
202 return wantarray ? ($out, @temp_files) : $out;
203}
204
205
206sub do_system {
207 my ($self, @cmd) = @_;
208 print "@cmd\n" if !$self->{quiet};
209 return !system(@cmd);
210}
211
212sub split_like_shell {
213 my ($self, $string) = @_;
214
215 return () unless defined($string);
216 return @$string if UNIVERSAL::isa($string, 'ARRAY');
217 $string =~ s/^\s+|\s+$//g;
218 return () unless length($string);
219
220 return Text::ParseWords::shellwords($string);
221}
222
223# if building perl, perl's main source directory
224sub perl_src {
225 # N.B. makemaker actually searches regardless of PERL_CORE, but
226 # only squawks at not finding it if PERL_CORE is set
227
345dbb93
RGS
228 return unless $ENV{PERL_CORE};
229
230 my $Updir = File::Spec->updir;
231 my $dir = $Updir;
232
233 # Try up to 5 levels upwards
234 for (1..5) {
235 if (
236 -f File::Spec->catfile($dir,"config_h.SH")
237 &&
238 -f File::Spec->catfile($dir,"perl.h")
239 &&
240 -f File::Spec->catfile($dir,"lib","Exporter.pm")
241 ) {
242 return $dir;
6b09c160
YST
243 }
244
345dbb93 245 $dir = File::Spec->catdir($dir, $Updir);
6b09c160 246 }
345dbb93
RGS
247
248 warn "PERL_CORE is set but I can't find your perl source!\n";
6b09c160
YST
249 return;
250}
251
252# directory of perl's include files
253sub perl_inc {
254 my $self = shift;
255
256 $self->perl_src() || File::Spec->catdir($self->{config}{archlibexp},"CORE");
257}
258
da2b6f33
SH
259sub DESTROY {
260 my $self = shift;
261 $self->cleanup();
262}
263
6b09c160 2641;