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
27 eval { require ExtUtils
::PkgConfig
};
29 print "\nI see that you are a CPANTester, you really should install ExtUtils::PkgConfig !\n"
30 if $ENV{AUTOMATED_TESTING
};
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.
42 use File::Spec::Functions qw/:ALL/;
44 sub process_swig_files {
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.
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";
67 $self->add_to_cleanup($c_file);
69 # If any of the swig files that the main swig depends is newer
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
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.
88 my @dirs = splitdir($file_base);
89 $archdir = catdir($self->blib,'arch', @dirs[1..$#dirs]);
93 $self->link_c($archdir, $file_base, $obj_file);
96 # Invoke swig with -perl -outdir and other options.
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/\.[^.]+$//;
105 if (defined($p->{swig})) {
106 @swig = $self->split_like_shell($p->{swig});
110 if (defined($p->{swig_flags})) {
111 @swig_flags = $self->split_like_shell($p->{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,
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";
135 sub is_windows { $^O =~ /MSWin32/i }
137 # Windows fixes courtesy of <sisyphus@cpan.org>
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";
155 $self->do_system(@shrp, @ld, @lddlflags, @user_libs, '-o', $lib_file,
156 $obj_file, @$objects, @linker_flags)
157 or die "error building $lib_file file from '$obj_file'";
163 # From Base.pm but modified to put package cflags *after*
164 # installed c flags so warning-removal will have an effect.
167 my ($self, $file) = @_;
168 my ($cf, $p) = ($self->{config}, $self->{properties}); # For convenience
170 # File name, minus the suffix
171 (my $file_base = $file) =~ s/\.[^.]+$//;
172 my $obj_file = $file_base . $Config{_o};
174 $self->add_to_cleanup($obj_file);
175 return $obj_file if $self->up_to_date($file, $obj_file);
178 $cf->{installarchlib} = $Config{archlib};
180 my @include_dirs = @{$p->{include_dirs}}
181 ? map {"-I$_"} (@{$p->{include_dirs}}, catdir($cf->{installarchlib}, 'CORE'))
182 : map {"-I$_"} ( catdir($cf->{installarchlib}, 'CORE') ) ;
184 my @extra_compiler_flags = $self->split_like_shell($p->{extra_compiler_flags});
186 my @cccdlflags = $self->split_like_shell($cf->{cccdlflags});
188 my @ccflags = $self->split_like_shell($cf->{ccflags});
189 my @optimize = $self->split_like_shell($cf->{optimize});
191 # Whoah! There seems to be a bug in gcc 4.1.0 and optimization
192 # and swig. I'm not sure who's at fault. But for now the simplest
193 # thing is to turn off all optimization. For the kinds of things that
194 # SWIG does - do conversions between parameters and transfers calls
195 # I doubt optimization makes much of a difference. But if it does,
196 # it can be added back via @extra_compiler_flags.
198 my @flags = (@include_dirs, @cccdlflags, '-c', @ccflags, @extra_compiler_flags, );
200 my @cc = $self->split_like_shell($cf->{cc});
201 @cc = "gcc" unless @cc;
203 $self->do_system(@cc, @flags, '-o', $obj_file, $file)
204 or die "error building $Config{_o} file from '$file'";
219 my $obj_ext = $Config{_o
} || ".o";
220 unlink("$tmp.c", "$tmp$obj_ext");
222 if (open(TMPC
, ">", "$tmp.c")) {
226 my $cccmd = $args{cccmd
};
228 my $ccflags = $Config{'ccflags'};
229 $ccflags .= " $args{ccflags}" if $args{ccflags
};
231 if ($args{silent
} ) {
232 $errornull = "2>/dev/null" unless defined $errornull;
237 $cccmd = "$Config{'cc'} -o $tmp $ccflags $tmp.c $errornull"
238 unless defined $cccmd;
240 printf "cccmd = $cccmd\n" if $args{verbose
};
241 my $res = system($cccmd);
242 $ok = defined($res) && $res == 0;
249 *** The test compile of '$tmp.c' failed: status $?
250 *** (the status means: errno = $errno or '$!')
251 *** DO NOT PANIC: this just means that *some* you may get some innocuous
252 *** compiler warnings.
263 my $c_prog = "int main () { return 0; }\n";
264 print "Checking if $Config{cc} supports \"$ccflags\"...";
265 my $result = try_compile
($c_prog, ccflags
=>$ccflags);
275 print "Checking for GSL..";
276 my %gsl_pkgcfg = ExtUtils
::PkgConfig
->find ('gsl');
278 my $MIN_GSL_VERSION = "1.11";
279 my $gv = $gsl_pkgcfg{'modversion'};
282 if ($gv =~ m{\A(\d+(?:\.\d+)+)}) {
283 my @current= split /\./, $1;
284 my @min= split /\./, $MIN_GSL_VERSION;
285 unless ($current[0] >= $min[0] && $current[1] >= $min[1]) {
288 *** You need to have GSL %s or greater installed. (You have $gv).
289 *** Get GSL at http://www.gnu.org/software/gsl\n", $MIN_GSL_VERSION;
292 print "Found GSL version $gv\n";
298 *** Can't parse GSL version $gv.
299 *** Will continue and keep my fingers crossed for luck.
305 *** Can't find GSL configuration info. Is GSL installed?
306 *** Get GSL at http://www.gnu.org/software/gsl
312 my $ccflags = $gsl_pkgcfg{cflags
};
314 ## Swig produces a number of GCC warnings. Turn them off if we can.
315 $ccflags .= try_cflags
("-Wno-strict-aliasing");
316 $ccflags .= try_cflags
("-Wno-unused-function");
317 $ccflags .= try_cflags
("-Wno-unused-value");
318 $ccflags .= try_cflags
("-Wno-unused-function");
319 $ccflags .= try_cflags
("-Wno-unused-variable");
321 my $ldflags = "$gsl_pkgcfg{libs} -gsl";
322 my $swig_flags='-Wall ' . $gsl_pkgcfg{cflags
};
324 if ('cygwin' eq $Config{osname
} &&
325 $Config{shrpenv
} =~ m{\Aenv LD_RUN_PATH=(.*)\Z} ) {
326 $ldflags .= " -L$1 -lperl";
327 # Should we check the 32-ness?
328 $swig_flags = '-DNEED_LONG';
329 } elsif ('darwin' eq $Config{osname
}) {
330 $ldflags .= " -bundle -flat_namespace";
333 my $class = Module
::Build
->subclass( code
=> $code );
334 my @Subsystems = sort qw
/
335 BLAS Diff Machine Statistics
336 Eigen Matrix Poly Wavelet2D
337 BSpline Errno PowInt Wavelet
341 Heapsort Multifit Randist Sum
342 Combination Histogram Multimin Roots
343 Complex Histogram2D Multiroots SF
344 Const Siman IEEEUtils Sys
345 Integration NTuple Sort
346 DHT Interp ODEIV Vector
347 Deriv Linalg Permutation Spline
349 my $cleanup = qq{core
*.core Makefile Math
-GSL
-* tmp
* pod2ht
*.tmp _build blib
*.so
*.orig
};
351 my $builder = $class->new(
352 module_name
=> 'Math::GSL',
353 add_to_cleanup
=> [ $cleanup ],
354 create_makefile_pl
=> 'passthrough',
355 dist_abstract
=> 'Interface to the GNU Scientific Library using SWIG',
356 dist_author
=> 'Jonathan Leto <jonathan@leto.net>',
357 dist_version_from
=> 'lib/Math/GSL.pm',
359 extra_linker_flags
=> '-shared -I./lib -I../lib ' . $ldflags,
360 extra_compiler_flags
=> q{-shared -fpic } . $ccflags,
361 swig_flags
=> $swig_flags,
364 'ExtUtils::PkgConfig' => '1.03',
367 'Test::Exception' => 0.21,
368 'Test::Class' => 0.12,
374 map { [ "$_.i" ] } @Subsystems
377 $builder->add_build_element('swig');
378 $builder->create_build_script();
379 print "Have a great day!\n";