This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade ExtUtils::CBuilder from version 0.280223 to 0.280224
[perl5.git] / dist / ExtUtils-CBuilder / lib / ExtUtils / CBuilder / Base.pm
CommitLineData
6b09c160 1package ExtUtils::CBuilder::Base;
83dcc064 2$ExtUtils::CBuilder::Base::VERSION = '0.280224';
6b09c160 3use strict;
83dcc064 4use warnings;
6b09c160
YST
5use File::Spec;
6use File::Basename;
8a6e5c04 7use Cwd ();
6b09c160
YST
8use Config;
9use Text::ParseWords;
06e8058f
CBW
10use IPC::Cmd qw(can_run);
11use File::Temp qw(tempfile);
6b09c160 12
06e8058f
CBW
13# More details about C/C++ compilers:
14# http://developers.sun.com/sunstudio/documentation/product/compiler.jsp
15# http://gcc.gnu.org/
16# http://publib.boulder.ibm.com/infocenter/comphelp/v101v121/index.jsp
17# http://msdn.microsoft.com/en-us/vstudio/default.aspx
18
19my %cc2cxx = (
20 # first line order is important to support wrappers like in pkgsrc
21 cc => [ 'c++', 'CC', 'aCC', 'cxx', ], # Sun Studio, HP ANSI C/C++ Compilers
22 gcc => [ 'g++' ], # GNU Compiler Collection
23 xlc => [ 'xlC' ], # IBM C/C++ Set, xlc without thread-safety
24 xlc_r => [ 'xlC_r' ], # IBM C/C++ Set, xlc with thread-safety
25 cl => [ 'cl' ], # Microsoft Visual Studio
26);
6b09c160
YST
27
28sub new {
29 my $class = shift;
30 my $self = bless {@_}, $class;
31
32 $self->{properties}{perl} = $class->find_perl_interpreter
33 or warn "Warning: Can't locate your perl binary";
34
35 while (my ($k,$v) = each %Config) {
36 $self->{config}{$k} = $v unless exists $self->{config}{$k};
37 }
06e8058f 38 $self->{config}{cc} = $ENV{CC} if defined $ENV{CC};
011e8fb4
NT
39 $self->{config}{ccflags} = join(" ", $self->{config}{ccflags}, $ENV{CFLAGS})
40 if defined $ENV{CFLAGS};
06e8058f
CBW
41 $self->{config}{cxx} = $ENV{CXX} if defined $ENV{CXX};
42 $self->{config}{cxxflags} = $ENV{CXXFLAGS} if defined $ENV{CXXFLAGS};
43 $self->{config}{ld} = $ENV{LD} if defined $ENV{LD};
011e8fb4
NT
44 $self->{config}{ldflags} = join(" ", $self->{config}{ldflags}, $ENV{LDFLAGS})
45 if defined $ENV{LDFLAGS};
06e8058f
CBW
46
47 unless ( exists $self->{config}{cxx} ) {
48 my ($ccpath, $ccbase, $ccsfx ) = fileparse($self->{config}{cc}, qr/\.[^.]*/);
49 foreach my $cxx (@{$cc2cxx{$ccbase}}) {
50 if( can_run( File::Spec->catfile( $ccpath, $cxx, $ccsfx ) ) ) {
51 $self->{config}{cxx} = File::Spec->catfile( $ccpath, $cxx, $ccsfx );
52 last;
53 }
54 if( can_run( File::Spec->catfile( $cxx, $ccsfx ) ) ) {
55 $self->{config}{cxx} = File::Spec->catfile( $cxx, $ccsfx );
56 last;
57 }
58 if( can_run( $cxx ) ) {
59 $self->{config}{cxx} = $cxx;
60 last;
61 }
62 }
63 unless ( exists $self->{config}{cxx} ) {
64 $self->{config}{cxx} = $self->{config}{cc};
d247f55d 65 my $cflags = $self->{config}{ccflags};
06e8058f
CBW
66 $self->{config}{cxxflags} = '-x c++';
67 $self->{config}{cxxflags} .= " $cflags" if defined $cflags;
68 }
69 }
70
6b09c160
YST
71 return $self;
72}
73
74sub find_perl_interpreter {
75 my $perl;
76 File::Spec->file_name_is_absolute($perl = $^X)
77 or -f ($perl = $Config::Config{perlpath})
06e8058f 78 or ($perl = $^X); # XXX how about using IPC::Cmd::can_run here?
6b09c160
YST
79 return $perl;
80}
81
82sub add_to_cleanup {
83 my $self = shift;
da2b6f33
SH
84 foreach (@_) {
85 $self->{files_to_clean}{$_} = 1;
86 }
87}
88
89sub cleanup {
90 my $self = shift;
91 foreach my $file (keys %{$self->{files_to_clean}}) {
92 unlink $file;
93 }
6b09c160
YST
94}
95
06e8058f
CBW
96sub get_config {
97 return %{ $_[0]->{config} };
98}
99
6b09c160
YST
100sub object_file {
101 my ($self, $filename) = @_;
102
103 # File name, minus the suffix
104 (my $file_base = $filename) =~ s/\.[^.]+$//;
105 return "$file_base$self->{config}{obj_ext}";
106}
107
108sub arg_include_dirs {
109 my $self = shift;
110 return map {"-I$_"} @_;
111}
112
113sub arg_nolink { '-c' }
114
115sub arg_object_file {
116 my ($self, $file) = @_;
117 return ('-o', $file);
118}
119
120sub arg_share_object_file {
121 my ($self, $file) = @_;
122 return ($self->split_like_shell($self->{config}{lddlflags}), '-o', $file);
123}
124
125sub arg_exec_file {
126 my ($self, $file) = @_;
127 return ('-o', $file);
128}
129
d1cf867f
SP
130sub arg_defines {
131 my ($self, %args) = @_;
4392616c 132 return map "-D$_=$args{$_}", sort keys %args;
d1cf867f
SP
133}
134
6b09c160
YST
135sub compile {
136 my ($self, %args) = @_;
137 die "Missing 'source' argument to compile()" unless defined $args{source};
4392616c 138
6b09c160 139 my $cf = $self->{config}; # For convenience
4392616c 140
06e8058f
CBW
141 my $object_file = $args{object_file}
142 ? $args{object_file}
143 : $self->object_file($args{source});
144
4392616c 145 my $include_dirs_ref =
06e8058f
CBW
146 (exists($args{include_dirs}) && ref($args{include_dirs}) ne "ARRAY")
147 ? [ $args{include_dirs} ]
148 : $args{include_dirs};
149 my @include_dirs = $self->arg_include_dirs(
150 @{ $include_dirs_ref || [] },
151 $self->perl_inc(),
152 );
4392616c 153
d1cf867f 154 my @defines = $self->arg_defines( %{$args{defines} || {}} );
4392616c 155
06e8058f
CBW
156 my @extra_compiler_flags =
157 $self->split_like_shell($args{extra_compiler_flags});
6b09c160 158 my @cccdlflags = $self->split_like_shell($cf->{cccdlflags});
06e8058f 159 my @ccflags = $self->split_like_shell($args{'C++'} ? $cf->{cxxflags} : $cf->{ccflags});
6b09c160 160 my @optimize = $self->split_like_shell($cf->{optimize});
06e8058f
CBW
161 my @flags = (
162 @include_dirs,
163 @defines,
164 @cccdlflags,
165 @extra_compiler_flags,
166 $self->arg_nolink,
167 @ccflags,
168 @optimize,
169 $self->arg_object_file($object_file),
170 );
171 my @cc = $self->split_like_shell($args{'C++'} ? $cf->{cxx} : $cf->{cc});
4392616c 172
6b09c160 173 $self->do_system(@cc, @flags, $args{source})
06e8058f 174 or die "error building $object_file from '$args{source}'";
6b09c160 175
06e8058f 176 return $object_file;
6b09c160
YST
177}
178
179sub have_compiler {
506098d4 180 my ($self, $is_cplusplus) = @_;
06e8058f
CBW
181 my $have_compiler_flag = $is_cplusplus ? "have_cxx" : "have_cc";
182 my $suffix = $is_cplusplus ? ".cc" : ".c";
183 return $self->{$have_compiler_flag} if defined $self->{$have_compiler_flag};
6b09c160 184
9015f106
DG
185 my $result;
186 my $attempts = 3;
187 # tmpdir has issues for some people so fall back to current dir
9015f106 188
06e8058f
CBW
189 # don't clobber existing files (rare, but possible)
190 my ( $FH, $tmpfile ) = tempfile( "compilet-XXXXX", SUFFIX => $suffix );
191 binmode $FH;
9015f106 192
06e8058f
CBW
193 if ( $is_cplusplus ) {
194 print $FH "class Bogus { public: int boot_compilet() { return 1; } };\n";
195 }
196 else {
197 print $FH "int boot_compilet() { return 1; }\n";
198 }
ef9721fc 199 close $FH;
06e8058f
CBW
200
201 my ($obj_file, @lib_files);
202 eval {
203 local $^W = 0;
204 local $self->{quiet} = 1;
205 $obj_file = $self->compile('C++' => $is_cplusplus, source => $tmpfile);
206 @lib_files = $self->link(objects => $obj_file, module_name => 'compilet');
207 };
208 $result = $@ ? 0 : 1;
209
210 foreach (grep defined, $tmpfile, $obj_file, @lib_files) {
211 1 while unlink;
6b09c160 212 }
9015f106 213
06e8058f 214 return $self->{$have_compiler_flag} = $result;
6b09c160
YST
215}
216
506098d4
DG
217sub have_cplusplus {
218 push @_, 1;
219 goto &have_compiler;
220}
221
6b09c160 222sub lib_file {
5e4a7f81 223 my ($self, $dl_file, %args) = @_;
6b09c160
YST
224 $dl_file =~ s/\.[^.]+$//;
225 $dl_file =~ tr/"//d;
4392616c 226
5e4a7f81
BF
227 if (defined $args{module_name} and length $args{module_name}) {
228 # Need to create with the same name as DynaLoader will load with.
229 require DynaLoader;
230 if (defined &DynaLoader::mod2fname) {
231 my $lib = DynaLoader::mod2fname([split /::/, $args{module_name}]);
232 my ($dev, $lib_dir, undef) = File::Spec->splitpath($dl_file);
233 $dl_file = File::Spec->catpath($dev, $lib_dir, $lib);
234 }
235 }
4392616c 236
5e4a7f81
BF
237 $dl_file .= ".$self->{config}{dlext}";
238
239 return $dl_file;
6b09c160
YST
240}
241
242
243sub exe_file {
244 my ($self, $dl_file) = @_;
245 $dl_file =~ s/\.[^.]+$//;
246 $dl_file =~ tr/"//d;
247 return "$dl_file$self->{config}{_exe}";
248}
249
250sub need_prelink { 0 }
251
d1cf867f
SP
252sub extra_link_args_after_prelink { return }
253
6b09c160
YST
254sub prelink {
255 my ($self, %args) = @_;
06e8058f
CBW
256
257 my ($dl_file_out, $mksymlists_args) = _prepare_mksymlists_args(\%args);
258
6b09c160 259 require ExtUtils::Mksymlists;
06e8058f
CBW
260 # dl. abbrev for dynamic library
261 ExtUtils::Mksymlists::Mksymlists( %{ $mksymlists_args } );
262
6b09c160 263 # Mksymlists will create one of these files
06e8058f
CBW
264 return grep -e, map "$dl_file_out.$_", qw(ext def opt);
265}
266
267sub _prepare_mksymlists_args {
268 my $args = shift;
269 ($args->{dl_file} = $args->{dl_name}) =~ s/.*::// unless $args->{dl_file};
4392616c 270
06e8058f
CBW
271 my %mksymlists_args = (
272 DL_VARS => $args->{dl_vars} || [],
273 DL_FUNCS => $args->{dl_funcs} || {},
274 FUNCLIST => $args->{dl_func_list} || [],
275 IMPORTS => $args->{dl_imports} || {},
276 NAME => $args->{dl_name}, # Name of the Perl module
277 DLBASE => $args->{dl_base}, # Basename of DLL file
278 FILE => $args->{dl_file}, # Dir + Basename of symlist file
279 VERSION => (defined $args->{dl_version} ? $args->{dl_version} : '0.0'),
280 );
281 return ($args->{dl_file}, \%mksymlists_args);
6b09c160
YST
282}
283
284sub link {
285 my ($self, %args) = @_;
286 return $self->_do_link('lib_file', lddl => 1, %args);
287}
288
289sub link_executable {
290 my ($self, %args) = @_;
291 return $self->_do_link('exe_file', lddl => 0, %args);
292}
2bd31f1a 293
6b09c160
YST
294sub _do_link {
295 my ($self, $type, %args) = @_;
296
297 my $cf = $self->{config}; # For convenience
4392616c 298
6b09c160
YST
299 my $objects = delete $args{objects};
300 $objects = [$objects] unless ref $objects;
5e4a7f81 301 my $out = $args{$type} || $self->$type($objects->[0], %args);
4392616c 302
6b09c160
YST
303 my @temp_files;
304 @temp_files =
06e8058f
CBW
305 $self->prelink(%args, dl_name => $args{module_name})
306 if $args{lddl} && $self->need_prelink;
4392616c 307
06e8058f
CBW
308 my @linker_flags = (
309 $self->split_like_shell($args{extra_linker_flags}),
310 $self->extra_link_args_after_prelink(
311 %args, dl_name => $args{module_name}, prelink_res => \@temp_files
312 )
313 );
d1cf867f 314
06e8058f
CBW
315 my @output = $args{lddl}
316 ? $self->arg_share_object_file($out)
317 : $self->arg_exec_file($out);
6b09c160
YST
318 my @shrp = $self->split_like_shell($cf->{shrpenv});
319 my @ld = $self->split_like_shell($cf->{ld});
4392616c 320
6b09c160
YST
321 $self->do_system(@shrp, @ld, @output, @$objects, @linker_flags)
322 or die "error building $out from @$objects";
4392616c 323
6b09c160
YST
324 return wantarray ? ($out, @temp_files) : $out;
325}
326
327
328sub do_system {
329 my ($self, @cmd) = @_;
330 print "@cmd\n" if !$self->{quiet};
331 return !system(@cmd);
332}
333
334sub split_like_shell {
335 my ($self, $string) = @_;
4392616c 336
6b09c160
YST
337 return () unless defined($string);
338 return @$string if UNIVERSAL::isa($string, 'ARRAY');
339 $string =~ s/^\s+|\s+$//g;
340 return () unless length($string);
4392616c 341
06e8058f
CBW
342 # Text::ParseWords replaces all 'escaped' characters with themselves, which completely
343 # breaks paths under windows. As such, we forcibly replace backwards slashes with forward
344 # slashes on windows.
345 $string =~ s@\\@/@g if $^O eq 'MSWin32';
4392616c 346
6b09c160
YST
347 return Text::ParseWords::shellwords($string);
348}
349
350# if building perl, perl's main source directory
351sub perl_src {
352 # N.B. makemaker actually searches regardless of PERL_CORE, but
353 # only squawks at not finding it if PERL_CORE is set
354
345dbb93
RGS
355 return unless $ENV{PERL_CORE};
356
ea2e6518
RGS
357 my $Updir = File::Spec->updir;
358 my $dir = File::Spec->curdir;
345dbb93
RGS
359
360 # Try up to 5 levels upwards
33d1b122 361 for (0..10) {
345dbb93 362 if (
06e8058f
CBW
363 -f File::Spec->catfile($dir,"config_h.SH")
364 &&
365 -f File::Spec->catfile($dir,"perl.h")
366 &&
367 -f File::Spec->catfile($dir,"lib","Exporter.pm")
368 ) {
8a6e5c04 369 return Cwd::realpath( $dir );
6b09c160
YST
370 }
371
345dbb93 372 $dir = File::Spec->catdir($dir, $Updir);
6b09c160 373 }
ea2e6518 374
345dbb93 375 warn "PERL_CORE is set but I can't find your perl source!\n";
ea2e6518 376 return ''; # return empty string if $ENV{PERL_CORE} but can't find dir ???
6b09c160
YST
377}
378
379# directory of perl's include files
380sub perl_inc {
381 my $self = shift;
382
383 $self->perl_src() || File::Spec->catdir($self->{config}{archlibexp},"CORE");
384}
385
da2b6f33
SH
386sub DESTROY {
387 my $self = shift;
8a6e5c04 388 local($., $@, $!, $^E, $?);
da2b6f33
SH
389 $self->cleanup();
390}
391
6b09c160 3921;
06e8058f
CBW
393
394# vim: ts=2 sw=2 et: