Apply nan/inf handling patch from Sisyphus
[Math-GSL.git] / Build.PL
blobe511d3a42e0dac2314fce0ac945eeb10bfcf75c4
1 #!/usr/bin/perl -w
3 # This script has been heavily modified from the Device::Cdio Build.PL
4 # Jonathan Leto <jonathan@leto.net>
5 # Copyright (C) 2006 Rocky Bernstein <rocky@cpan.org>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 use strict;
21 use warnings;
22 use Config;
23 use Data::Dumper;
24 use Module::Build;
26 BEGIN {
27 eval { require ExtUtils::PkgConfig };
28 if ($@) {
29 print "\nI see that you are a CPANTester, you really should install ExtUtils::PkgConfig !\n"
30 if $ENV{AUTOMATED_TESTING};
31 print <<EXIT;
32 ExtUtils::PkgConfig is currently needed to find GSL for the compilation of this module.
33 It may be bundled with Math::GSL in the future.
34 EXIT
35 exit 0;
39 my $code = <<'EOC';
40 use Config;
41 use File::Copy;
42 use File::Spec::Functions qw/:ALL/;
44 sub process_swig_files {
45 my $self = shift;
46 my $p = $self->{properties};
47 return unless $p->{swig_source};
48 my $files_ref = $p->{swig_source};
49 foreach my $file (@$files_ref) {
50 $self->process_swig($file->[0], $file->[1]);
54 # Check check dependencies for $main_swig_file. These are the
55 # %includes. If needed, arrange to run swig on $main_swig_file to
56 # produce a xxx_wrap.c C file.
58 sub process_swig {
59 my ($self, $main_swig_file, $deps_ref) = @_;
60 my ($cf, $p) = ($self->{config}, $self->{properties}); # For convenience
62 # File name. e.g, perlcdio.swg -> perlcdio_wrap.c
63 (my $file_base = $main_swig_file) =~ s/\.[^.]+$//;
64 my $c_file = "${file_base}_wrap.c";
66 # .swg -> .c
67 $self->add_to_cleanup($c_file);
69 # If any of the swig files that the main swig depends is newer
70 # then rebuild.
71 foreach my $depends_on ($main_swig_file, @$deps_ref) {
72 unless ($self->up_to_date($depends_on, $c_file)) {
73 $self->compile_swig($main_swig_file, $c_file);
74 # Only need to build $c_file once no matter how many
75 # includes there are.
76 last;
80 # .c -> .o
81 my $obj_file = $self->compile_c($c_file);
82 $self->add_to_cleanup($obj_file);
84 # The .so files don't go in blib/lib/, they go in blib/arch/auto/.
85 # Unfortunately we have to pre-compute the whole path.
86 my $archdir;
88 my @dirs = splitdir($file_base);
89 $archdir = catdir($self->blib,'arch', @dirs[1..$#dirs]);
92 # .o -> .so
93 $self->link_c($archdir, $file_base, $obj_file);
96 # Invoke swig with -perl -outdir and other options.
97 sub compile_swig {
98 my ($self, $file, $c_file) = @_;
99 my ($cf, $p) = ($self->{config}, $self->{properties}); # For convenience
101 # File name, minus the suffix
102 (my $file_base = $file) =~ s/\.[^.]+$//;
104 my @swig;
105 if (defined($p->{swig})) {
106 @swig = $self->split_like_shell($p->{swig});
107 } else {
108 @swig = ('swig');
110 if (defined($p->{swig_flags})) {
111 @swig_flags = $self->split_like_shell($p->{swig_flags});
112 } else {
113 @swig_flags = ();
116 my $blib_lib = catfile(qw/blib lib/);
118 mkdir catfile($blib_lib, qw/Math GSL/);
119 my $outdir = catfile($blib_lib, qw/Math GSL/);
120 my $pm_file = "${file_base}.pm";
121 my $from = catfile($blib_lib, qw/Math GSL/, $pm_file);
122 my $to = catfile(qw/lib Math GSL/,$pm_file);
123 chmod 0644, $from, $to;
125 $self->do_system(@swig, '-o', $c_file,
126 '-outdir', $outdir,
127 '-perl5', @swig_flags, $file)
128 or die "error building $c_file file from '$file'";
131 print "Copying from: $from, to: $to; it makes the CPAN indexer happy.\n";
132 copy($from,$to);
133 return $c_file;
135 sub is_windows { $^O =~ /MSWin32/i }
137 # Windows fixes courtesy of <sisyphus@cpan.org>
138 sub link_c {
139 my ($self, $to, $file_base, $obj_file) = @_;
140 my ($cf, $p) = ($self->{config}, $self->{properties}); # For convenience
142 my $lib_file = catfile($to, File::Basename::basename("$file_base.$Config{dlext}"));
144 $self->add_to_cleanup($lib_file);
145 my $objects = $p->{objects} || [];
147 unless ($self->up_to_date([$obj_file, @$objects], $lib_file)) {
148 my @linker_flags = $self->split_like_shell($p->{extra_linker_flags});
150 push @linker_flags, $Config{archlib} . '/CORE/' . $Config{libperl} if is_windows();
152 my @lddlflags = $self->split_like_shell($cf->{lddlflags});
153 my @shrp = $self->split_like_shell($cf->{shrpenv});
154 my @ld = $self->split_like_shell($cf->{ld}) || "gcc";
156 # Strip binaries if we are compiling on windows
157 push @ld, "-s" if (is_windows() && $Config{cc} eq 'gcc');
159 $self->do_system(@shrp, @ld, @lddlflags, @user_libs, '-o', $lib_file,
160 $obj_file, @$objects, @linker_flags)
161 or die "error building $lib_file file from '$obj_file'";
164 return $lib_file;
167 # From Base.pm but modified to put package cflags *after*
168 # installed c flags so warning-removal will have an effect.
170 sub compile_c {
171 my ($self, $file) = @_;
172 my ($cf, $p) = ($self->{config}, $self->{properties}); # For convenience
174 # File name, minus the suffix
175 (my $file_base = $file) =~ s/\.[^.]+$//;
176 my $obj_file = $file_base . $Config{_o};
178 $self->add_to_cleanup($obj_file);
179 return $obj_file if $self->up_to_date($file, $obj_file);
182 $cf->{installarchlib} = $Config{archlib};
184 my @include_dirs = @{$p->{include_dirs}}
185 ? map {"-I$_"} (@{$p->{include_dirs}}, catdir($cf->{installarchlib}, 'CORE'))
186 : map {"-I$_"} ( catdir($cf->{installarchlib}, 'CORE') ) ;
188 my @extra_compiler_flags = $self->split_like_shell($p->{extra_compiler_flags});
190 my @cccdlflags = $self->split_like_shell($cf->{cccdlflags});
192 my @ccflags = $self->split_like_shell($cf->{ccflags});
193 my @optimize = $self->split_like_shell($cf->{optimize});
195 # Whoah! There seems to be a bug in gcc 4.1.0 and optimization
196 # and swig. I'm not sure who's at fault. But for now the simplest
197 # thing is to turn off all optimization. For the kinds of things that
198 # SWIG does - do conversions between parameters and transfers calls
199 # I doubt optimization makes much of a difference. But if it does,
200 # it can be added back via @extra_compiler_flags.
202 my @flags = (@include_dirs, @cccdlflags, '-c', @ccflags, @extra_compiler_flags, );
204 my @cc = $self->split_like_shell($cf->{cc});
205 @cc = "gcc" unless @cc;
207 $self->do_system(@cc, @flags, '-o', $obj_file, $file)
208 or die "error building $Config{_o} file from '$file'";
210 return $obj_file;
216 sub try_compile {
217 my ($c, %args) = @_;
219 my $ok = 0;
220 my $tmp = "tmp$$";
221 local(*TMPC);
223 my $obj_ext = $Config{_o} || ".o";
224 unlink("$tmp.c", "$tmp$obj_ext");
226 if (open(TMPC, ">", "$tmp.c")) {
227 print TMPC $c;
228 close(TMPC);
230 my $cccmd = $args{cccmd};
231 my $errornull;
232 my $ccflags = $Config{'ccflags'};
233 $ccflags .= " $args{ccflags}" if $args{ccflags};
235 if ($args{silent} ) {
236 $errornull = "2>/dev/null" unless defined $errornull;
237 } else {
238 $errornull = '';
241 $cccmd = "$Config{'cc'} -o $tmp $ccflags $tmp.c $errornull"
242 unless defined $cccmd;
244 printf "cccmd = $cccmd\n" if $args{verbose};
245 my $res = system($cccmd);
246 $ok = defined($res) && $res == 0;
248 if ( !$ok ) {
249 my $errno = $? >> 8;
250 local $! = $errno;
251 print "
253 *** The test compile of '$tmp.c' failed: status $?
254 *** (the status means: errno = $errno or '$!')
255 *** DO NOT PANIC: this just means that *some* you may get some innocuous
256 *** compiler warnings.
259 unlink("$tmp.c");
262 return $ok;
265 sub try_cflags ($) {
266 my ($ccflags) = @_;
267 my $c_prog = "int main () { return 0; }\n";
268 print "Checking if $Config{cc} supports \"$ccflags\"...";
269 my $result = try_compile($c_prog, ccflags=>$ccflags);
270 if ($result) {
271 print "yes\n";
272 return " $ccflags";
274 print "no\n";
275 return '';
279 print "Checking for GSL..";
280 my %gsl_pkgcfg = ExtUtils::PkgConfig->find ('gsl');
282 my $MIN_GSL_VERSION = "1.11";
283 my $gv = $gsl_pkgcfg{'modversion'};
285 if (defined $gv) {
286 if ($gv =~ m{\A(\d+(?:\.\d+)+)}) {
287 my @current= split /\./, $1;
288 my @min= split /\./, $MIN_GSL_VERSION;
289 unless ($current[0] >= $min[0] && $current[1] >= $min[1]) {
290 printf "
291 ***
292 *** You need to have GSL %s or greater installed. (You have $gv).
293 *** Get GSL at http://www.gnu.org/software/gsl\n", $MIN_GSL_VERSION;
294 exit 1;
295 } else {
296 print "Found GSL version $gv\n";
299 } else {
300 print "
301 ***
302 *** Can't parse GSL version $gv.
303 *** Will continue and keep my fingers crossed for luck.
306 } else {
307 print "
308 ***
309 *** Can't find GSL configuration info. Is GSL installed?
310 *** Get GSL at http://www.gnu.org/software/gsl
312 exit 1;
316 my $ccflags = $gsl_pkgcfg{cflags};
318 ## Swig produces a number of GCC warnings. Turn them off if we can.
319 $ccflags .= try_cflags("-Wno-strict-aliasing");
320 $ccflags .= try_cflags("-Wno-unused-function");
321 $ccflags .= try_cflags("-Wno-unused-value");
322 $ccflags .= try_cflags("-Wno-unused-function");
323 $ccflags .= try_cflags("-Wno-unused-variable");
325 my $ldflags = "$gsl_pkgcfg{libs} -gsl";
326 my $swig_flags='-Wall ' . $gsl_pkgcfg{cflags};
328 if ('cygwin' eq $Config{osname} &&
329 $Config{shrpenv} =~ m{\Aenv LD_RUN_PATH=(.*)\Z} ) {
330 $ldflags .= " -L$1 -lperl";
331 # Should we check the 32-ness?
332 $swig_flags = '-DNEED_LONG';
333 } elsif ('darwin' eq $Config{osname}) {
334 $ldflags .= " -bundle -flat_namespace";
337 my $class = Module::Build->subclass( code => $code );
338 my @Subsystems = sort qw/
339 BLAS Diff Machine Statistics
340 Eigen Matrix Poly Wavelet2D
341 BSpline Errno PowInt Wavelet
342 CBLAS FFT Min
343 CDF Fit QRNG
344 Chebyshev Monte RNG
345 Heapsort Multifit Randist Sum
346 Combination Histogram Multimin Roots
347 Complex Histogram2D Multiroots SF
348 Const Siman IEEEUtils Sys
349 Integration NTuple Sort
350 DHT Interp ODEIV Vector
351 Deriv Linalg Permutation Spline
353 my $cleanup = qq{core *.core Makefile Math-GSL-* tmp* pod2ht*.tmp _build blib *.so *.orig };
355 my $builder = $class->new(
356 module_name => 'Math::GSL',
357 add_to_cleanup => [ $cleanup ],
358 create_makefile_pl => 'passthrough',
359 dist_abstract => 'Interface to the GNU Scientific Library using SWIG',
360 dist_author => 'Jonathan Leto <jonathan@leto.net>',
361 dist_version_from => 'lib/Math/GSL.pm',
362 include_dirs => q{},
363 extra_linker_flags => '-shared -I./lib -I../lib ' . $ldflags,
364 extra_compiler_flags=> q{-shared -fpic } . $ccflags,
365 swig_flags => $swig_flags,
366 license => 'gpl',
367 requires => {
368 'ExtUtils::PkgConfig' => '1.03',
369 'Scalar::Util' => 0,
370 'Test::More' => 0,
371 'Test::Exception' => 0.21,
372 'Test::Class' => 0.12,
373 version => 0,
374 perl => '5.8.0',
376 sign => 0,
377 swig_source => [
378 map { [ "$_.i" ] } @Subsystems
381 $builder->add_build_element('swig');
382 $builder->create_build_script();
383 print "Have a great day!\n";