xref: /openbsd-src/gnu/usr.bin/perl/dist/ExtUtils-CBuilder/lib/ExtUtils/CBuilder/Base.pm (revision c90a81c56dcebd6a1b73fe4aff9b03385b8e63b3)
1package ExtUtils::CBuilder::Base;
2$ExtUtils::CBuilder::Base::VERSION = '0.280225';
3use strict;
4use warnings;
5use File::Spec;
6use File::Basename;
7use Cwd ();
8use Config;
9use Text::ParseWords;
10use IPC::Cmd qw(can_run);
11use File::Temp qw(tempfile);
12
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);
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  }
38  $self->{config}{cc} = $ENV{CC} if defined $ENV{CC};
39  $self->{config}{ccflags} = join(" ", $self->{config}{ccflags}, $ENV{CFLAGS})
40     if defined $ENV{CFLAGS};
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};
44  $self->{config}{ldflags} = join(" ", $self->{config}{ldflags}, $ENV{LDFLAGS})
45     if defined $ENV{LDFLAGS};
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};
65      my $cflags = $self->{config}{ccflags};
66      $self->{config}{cxxflags} = '-x c++';
67      $self->{config}{cxxflags} .= " $cflags" if defined $cflags;
68    }
69  }
70
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})
78    or ($perl = $^X); # XXX how about using IPC::Cmd::can_run here?
79  return $perl;
80}
81
82sub add_to_cleanup {
83  my $self = shift;
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  }
94}
95
96sub get_config {
97    return %{ $_[0]->{config} };
98}
99
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
130sub arg_defines {
131  my ($self, %args) = @_;
132  return map "-D$_=$args{$_}", sort keys %args;
133}
134
135sub compile {
136  my ($self, %args) = @_;
137  die "Missing 'source' argument to compile()" unless defined $args{source};
138
139  my $cf = $self->{config}; # For convenience
140
141  my $object_file = $args{object_file}
142    ? $args{object_file}
143    : $self->object_file($args{source});
144
145  my $include_dirs_ref =
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  );
153
154  my @defines = $self->arg_defines( %{$args{defines} || {}} );
155
156  my @extra_compiler_flags =
157    $self->split_like_shell($args{extra_compiler_flags});
158  my @cccdlflags = $self->split_like_shell($cf->{cccdlflags});
159  my @ccflags = $self->split_like_shell($args{'C++'} ? $cf->{cxxflags} : $cf->{ccflags});
160  my @optimize = $self->split_like_shell($cf->{optimize});
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});
172
173  $self->do_system(@cc, @flags, $args{source})
174    or die "error building $object_file from '$args{source}'";
175
176  return $object_file;
177}
178
179sub have_compiler {
180  my ($self, $is_cplusplus) = @_;
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};
184
185  my $result;
186  my $attempts = 3;
187  # tmpdir has issues for some people so fall back to current dir
188
189  # don't clobber existing files (rare, but possible)
190  my ( $FH, $tmpfile ) = tempfile( "compilet-XXXXX", SUFFIX => $suffix );
191  binmode $FH;
192
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  }
199  close $FH;
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;
212  }
213
214  return $self->{$have_compiler_flag} = $result;
215}
216
217sub have_cplusplus {
218  push @_, 1;
219  goto &have_compiler;
220}
221
222sub lib_file {
223  my ($self, $dl_file, %args) = @_;
224  $dl_file =~ s/\.[^.]+$//;
225  $dl_file =~ tr/"//d;
226
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  }
236
237  $dl_file .= ".$self->{config}{dlext}";
238
239  return $dl_file;
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
252sub extra_link_args_after_prelink { return }
253
254sub prelink {
255  my ($self, %args) = @_;
256
257  my ($dl_file_out, $mksymlists_args) = _prepare_mksymlists_args(\%args);
258
259  require ExtUtils::Mksymlists;
260  # dl. abbrev for dynamic library
261  ExtUtils::Mksymlists::Mksymlists( %{ $mksymlists_args } );
262
263  # Mksymlists will create one of these files
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};
270
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);
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}
293
294sub _do_link {
295  my ($self, $type, %args) = @_;
296
297  my $cf = $self->{config}; # For convenience
298
299  my $objects = delete $args{objects};
300  $objects = [$objects] unless ref $objects;
301  my $out = $args{$type} || $self->$type($objects->[0], %args);
302
303  my @temp_files;
304  @temp_files =
305    $self->prelink(%args, dl_name => $args{module_name})
306      if $args{lddl} && $self->need_prelink;
307
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  );
314
315  my @output = $args{lddl}
316    ? $self->arg_share_object_file($out)
317    : $self->arg_exec_file($out);
318  my @shrp = $self->split_like_shell($cf->{shrpenv});
319  my @ld = $self->split_like_shell($cf->{ld});
320
321  $self->do_system(@shrp, @ld, @output, @$objects, @linker_flags)
322    or die "error building $out from @$objects";
323
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) = @_;
336
337  return () unless defined($string);
338  return @$string if UNIVERSAL::isa($string, 'ARRAY');
339  $string =~ s/^\s+|\s+$//g;
340  return () unless length($string);
341
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';
346
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
355  return unless $ENV{PERL_CORE};
356
357  my $Updir = File::Spec->updir;
358  my $dir   = File::Spec->curdir;
359
360  # Try up to 5 levels upwards
361  for (0..10) {
362    if (
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    ) {
369      return Cwd::realpath( $dir );
370    }
371
372    $dir = File::Spec->catdir($dir, $Updir);
373  }
374
375  warn "PERL_CORE is set but I can't find your perl source!\n";
376  return ''; # return empty string if $ENV{PERL_CORE} but can't find dir ???
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
386sub DESTROY {
387  my $self = shift;
388  local($., $@, $!, $^E, $?);
389  $self->cleanup();
390}
391
3921;
393
394# vim: ts=2 sw=2 et:
395