Commit | Line | Data |
---|---|---|
6b09c160 YST |
1 | package ExtUtils::CBuilder::Base; |
2 | ||
3 | use strict; | |
4 | use File::Spec; | |
5 | use File::Basename; | |
8a6e5c04 | 6 | use Cwd (); |
6b09c160 YST |
7 | use Config; |
8 | use Text::ParseWords; | |
c3fb68a3 | 9 | use IO::File; |
6b09c160 YST |
10 | |
11 | use vars qw($VERSION); | |
3698b7be | 12 | $VERSION = '0.260301'; |
6b09c160 YST |
13 | |
14 | sub new { | |
15 | my $class = shift; | |
16 | my $self = bless {@_}, $class; | |
17 | ||
18 | $self->{properties}{perl} = $class->find_perl_interpreter | |
19 | or warn "Warning: Can't locate your perl binary"; | |
20 | ||
21 | while (my ($k,$v) = each %Config) { | |
22 | $self->{config}{$k} = $v unless exists $self->{config}{$k}; | |
23 | } | |
24 | return $self; | |
25 | } | |
26 | ||
27 | sub find_perl_interpreter { | |
28 | my $perl; | |
29 | File::Spec->file_name_is_absolute($perl = $^X) | |
30 | or -f ($perl = $Config::Config{perlpath}) | |
31 | or ($perl = $^X); | |
32 | return $perl; | |
33 | } | |
34 | ||
35 | sub add_to_cleanup { | |
36 | my $self = shift; | |
da2b6f33 SH |
37 | foreach (@_) { |
38 | $self->{files_to_clean}{$_} = 1; | |
39 | } | |
40 | } | |
41 | ||
42 | sub cleanup { | |
43 | my $self = shift; | |
44 | foreach my $file (keys %{$self->{files_to_clean}}) { | |
45 | unlink $file; | |
46 | } | |
6b09c160 YST |
47 | } |
48 | ||
49 | sub object_file { | |
50 | my ($self, $filename) = @_; | |
51 | ||
52 | # File name, minus the suffix | |
53 | (my $file_base = $filename) =~ s/\.[^.]+$//; | |
54 | return "$file_base$self->{config}{obj_ext}"; | |
55 | } | |
56 | ||
57 | sub arg_include_dirs { | |
58 | my $self = shift; | |
59 | return map {"-I$_"} @_; | |
60 | } | |
61 | ||
62 | sub arg_nolink { '-c' } | |
63 | ||
64 | sub arg_object_file { | |
65 | my ($self, $file) = @_; | |
66 | return ('-o', $file); | |
67 | } | |
68 | ||
69 | sub arg_share_object_file { | |
70 | my ($self, $file) = @_; | |
71 | return ($self->split_like_shell($self->{config}{lddlflags}), '-o', $file); | |
72 | } | |
73 | ||
74 | sub arg_exec_file { | |
75 | my ($self, $file) = @_; | |
76 | return ('-o', $file); | |
77 | } | |
78 | ||
d1cf867f SP |
79 | sub arg_defines { |
80 | my ($self, %args) = @_; | |
81 | return map "-D$_=$args{$_}", keys %args; | |
82 | } | |
83 | ||
6b09c160 YST |
84 | sub compile { |
85 | my ($self, %args) = @_; | |
86 | die "Missing 'source' argument to compile()" unless defined $args{source}; | |
87 | ||
88 | my $cf = $self->{config}; # For convenience | |
89 | ||
90 | $args{object_file} ||= $self->object_file($args{source}); | |
91 | ||
92 | my @include_dirs = $self->arg_include_dirs | |
93 | (@{$args{include_dirs} || []}, | |
94 | $self->perl_inc()); | |
95 | ||
d1cf867f SP |
96 | my @defines = $self->arg_defines( %{$args{defines} || {}} ); |
97 | ||
6b09c160 YST |
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}); | |
d1cf867f | 102 | my @flags = (@include_dirs, @defines, @cccdlflags, @extra_compiler_flags, |
6b09c160 YST |
103 | $self->arg_nolink, |
104 | @ccflags, @optimize, | |
105 | $self->arg_object_file($args{object_file}), | |
106 | ); | |
107 | ||
108 | my @cc = $self->split_like_shell($cf->{cc}); | |
109 | ||
110 | $self->do_system(@cc, @flags, $args{source}) | |
111 | or die "error building $args{object_file} from '$args{source}'"; | |
112 | ||
113 | return $args{object_file}; | |
114 | } | |
115 | ||
116 | sub have_compiler { | |
117 | my ($self) = @_; | |
118 | return $self->{have_compiler} if defined $self->{have_compiler}; | |
6b09c160 | 119 | |
9015f106 DG |
120 | my $result; |
121 | my $attempts = 3; | |
122 | # tmpdir has issues for some people so fall back to current dir | |
123 | DIR: for my $dir ( File::Spec->tmpdir, '.' ) { | |
124 | ||
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"); | |
128 | if ( -e $tmpfile ) { | |
129 | redo DIR if $attempts--; | |
130 | next DIR; | |
131 | } | |
132 | ||
133 | { | |
134 | my $FH = IO::File->new("> $tmpfile") or die "Can't create $tmpfile: $!"; | |
135 | print $FH "int boot_compilet() { return 1; }\n"; | |
136 | } | |
137 | ||
138 | my ($obj_file, @lib_files); | |
139 | eval { | |
140 | local $^W = 0; | |
141 | $obj_file = $self->compile(source => $tmpfile); | |
142 | @lib_files = $self->link(objects => $obj_file, module_name => 'compilet'); | |
143 | }; | |
144 | $result = $@ ? 0 : 1; | |
145 | ||
146 | foreach (grep defined, $tmpfile, $obj_file, @lib_files) { | |
147 | 1 while unlink; | |
148 | } | |
149 | last DIR if $result; | |
6b09c160 | 150 | } |
9015f106 DG |
151 | |
152 | return $self->{have_compiler} = $result; | |
6b09c160 YST |
153 | } |
154 | ||
155 | sub lib_file { | |
156 | my ($self, $dl_file) = @_; | |
157 | $dl_file =~ s/\.[^.]+$//; | |
158 | $dl_file =~ tr/"//d; | |
159 | return "$dl_file.$self->{config}{dlext}"; | |
160 | } | |
161 | ||
162 | ||
163 | sub exe_file { | |
164 | my ($self, $dl_file) = @_; | |
165 | $dl_file =~ s/\.[^.]+$//; | |
166 | $dl_file =~ tr/"//d; | |
167 | return "$dl_file$self->{config}{_exe}"; | |
168 | } | |
169 | ||
170 | sub need_prelink { 0 } | |
171 | ||
d1cf867f SP |
172 | sub extra_link_args_after_prelink { return } |
173 | ||
6b09c160 YST |
174 | sub prelink { |
175 | my ($self, %args) = @_; | |
176 | ||
177 | ($args{dl_file} = $args{dl_name}) =~ s/.*::// unless $args{dl_file}; | |
178 | ||
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} || {}, | |
3fa7b0d2 RGS |
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 | |
d1cf867f | 188 | VERSION => (defined $args{dl_version} ? $args{dl_version} : '0.0'), |
6b09c160 YST |
189 | ); |
190 | ||
191 | # Mksymlists will create one of these files | |
192 | return grep -e, map "$args{dl_file}.$_", qw(ext def opt); | |
193 | } | |
194 | ||
195 | sub link { | |
196 | my ($self, %args) = @_; | |
197 | return $self->_do_link('lib_file', lddl => 1, %args); | |
198 | } | |
199 | ||
200 | sub link_executable { | |
201 | my ($self, %args) = @_; | |
202 | return $self->_do_link('exe_file', lddl => 0, %args); | |
203 | } | |
2bd31f1a | 204 | |
6b09c160 YST |
205 | sub _do_link { |
206 | my ($self, $type, %args) = @_; | |
207 | ||
208 | my $cf = $self->{config}; # For convenience | |
209 | ||
210 | my $objects = delete $args{objects}; | |
211 | $objects = [$objects] unless ref $objects; | |
212 | my $out = $args{$type} || $self->$type($objects->[0]); | |
213 | ||
214 | my @temp_files; | |
215 | @temp_files = | |
216 | $self->prelink(%args, | |
345dbb93 | 217 | dl_name => $args{module_name}) if $args{lddl} && $self->need_prelink; |
6b09c160 | 218 | |
d1cf867f SP |
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)); | |
222 | ||
6b09c160 YST |
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}); | |
2bd31f1a | 226 | |
6b09c160 YST |
227 | $self->do_system(@shrp, @ld, @output, @$objects, @linker_flags) |
228 | or die "error building $out from @$objects"; | |
229 | ||
230 | return wantarray ? ($out, @temp_files) : $out; | |
231 | } | |
232 | ||
233 | ||
234 | sub do_system { | |
235 | my ($self, @cmd) = @_; | |
236 | print "@cmd\n" if !$self->{quiet}; | |
237 | return !system(@cmd); | |
238 | } | |
239 | ||
240 | sub split_like_shell { | |
241 | my ($self, $string) = @_; | |
242 | ||
243 | return () unless defined($string); | |
244 | return @$string if UNIVERSAL::isa($string, 'ARRAY'); | |
245 | $string =~ s/^\s+|\s+$//g; | |
246 | return () unless length($string); | |
247 | ||
248 | return Text::ParseWords::shellwords($string); | |
249 | } | |
250 | ||
251 | # if building perl, perl's main source directory | |
252 | sub perl_src { | |
253 | # N.B. makemaker actually searches regardless of PERL_CORE, but | |
254 | # only squawks at not finding it if PERL_CORE is set | |
255 | ||
345dbb93 RGS |
256 | return unless $ENV{PERL_CORE}; |
257 | ||
ea2e6518 RGS |
258 | my $Updir = File::Spec->updir; |
259 | my $dir = File::Spec->curdir; | |
345dbb93 RGS |
260 | |
261 | # Try up to 5 levels upwards | |
33d1b122 | 262 | for (0..10) { |
345dbb93 RGS |
263 | if ( |
264 | -f File::Spec->catfile($dir,"config_h.SH") | |
265 | && | |
266 | -f File::Spec->catfile($dir,"perl.h") | |
267 | && | |
268 | -f File::Spec->catfile($dir,"lib","Exporter.pm") | |
269 | ) { | |
8a6e5c04 | 270 | return Cwd::realpath( $dir ); |
6b09c160 YST |
271 | } |
272 | ||
345dbb93 | 273 | $dir = File::Spec->catdir($dir, $Updir); |
6b09c160 | 274 | } |
ea2e6518 | 275 | |
345dbb93 | 276 | warn "PERL_CORE is set but I can't find your perl source!\n"; |
ea2e6518 | 277 | return ''; # return empty string if $ENV{PERL_CORE} but can't find dir ??? |
6b09c160 YST |
278 | } |
279 | ||
280 | # directory of perl's include files | |
281 | sub perl_inc { | |
282 | my $self = shift; | |
283 | ||
284 | $self->perl_src() || File::Spec->catdir($self->{config}{archlibexp},"CORE"); | |
285 | } | |
286 | ||
da2b6f33 SH |
287 | sub DESTROY { |
288 | my $self = shift; | |
8a6e5c04 | 289 | local($., $@, $!, $^E, $?); |
da2b6f33 SH |
290 | $self->cleanup(); |
291 | } | |
292 | ||
6b09c160 | 293 | 1; |