Fix bug in gsl version handling and clean up MANIFEST.
[Math-GSL.git] / trunk / Build.PL
blob0734714e57da39ef999182b4268f9e65eadeb110
1 #!/usr/bin/perl -w
2 # $Id: Build.PL,v 1.25 2006/08/05 08:48:12 rocky Exp $
3 # Copyright (C) 2006 Rocky Bernstein <rocky@cpan.org>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 use strict;
19 use warnings;
20 use Module::Build;
21 use ExtUtils::PkgConfig;
22 use Config;
23 use Data::Dumper;
24 use File::Fu;
26 my $code = <<'EOC';
27 use Config;
28 use File::Copy;
29 use File::Fu;
31 sub process_swig_files {
32 my $self = shift;
33 my $p = $self->{properties};
34 return unless $p->{swig_source};
35 my $files_ref = $p->{swig_source};
36 foreach my $file (@$files_ref) {
37 $self->process_swig($file->[0], $file->[1]);
41 # Check check dependencies for $main_swig_file. These are the
42 # %includes. If needed, arrange to run swig on $main_swig_file to
43 # produce a xxx_wrap.c C file.
45 sub process_swig {
46 my ($self, $main_swig_file, $deps_ref) = @_;
47 my ($cf, $p) = ($self->{config}, $self->{properties}); # For convenience
49 # File name. e.g, perlcdio.swg -> perlcdio_wrap.c
50 (my $file_base = $main_swig_file) =~ s/\.[^.]+$//;
51 my $c_file = "${file_base}_wrap.c";
53 if ($p->{swig_installed}) {
54 # .swg -> .c
55 $self->add_to_cleanup($c_file);
57 # print "+++ c_file: $c_file, file: $main_swig_file ", `pwd`, "\n";
58 # If any of the swig files that the main swig depends is newer
59 # then rebuild.
60 foreach my $depends_on ($main_swig_file, @$deps_ref) {
61 unless ($self->up_to_date($depends_on, $c_file)) {
62 $self->compile_swig($main_swig_file, $c_file);
63 # Only need to build $c_file once no matter how many
64 # includes there are.
65 last;
70 # .c -> .o
71 my $obj_file = $self->compile_c($c_file);
72 $self->add_to_cleanup($obj_file);
74 # The .so files don't go in blib/lib/, they go in blib/arch/auto/.
75 # Unfortunately we have to pre-compute the whole path.
76 my $archdir;
78 my @dirs = File::Spec->splitdir($file_base);
79 $archdir = File::Spec->catdir($self->blib,'arch', @dirs[1..$#dirs]);
82 # .o -> .so
83 $self->link_c($archdir, $file_base, $obj_file);
86 # Invoke swig with -perl -outdir and other options.
87 sub compile_swig {
88 my ($self, $file, $c_file) = @_;
89 my ($cf, $p) = ($self->{config}, $self->{properties}); # For convenience
91 # File name, minus the suffix
92 (my $file_base = $file) =~ s/\.[^.]+$//;
94 my @swig;
95 if (defined($p->{swig})) {
96 @swig = $self->split_like_shell($p->{swig});
97 } else {
98 @swig = ('swig');
100 if (defined($p->{swig_flags})) {
101 @swig_flags = $self->split_like_shell($p->{swig_flags});
102 } else {
103 @swig_flags = ();
106 push @swig_flags, $self->split_like_shell( qq{-module Math::GSL::$file_base} );
108 my $blib_lib = File::Spec->catfile('blib','lib');
109 #my $blib_lib = 'blib/lib';
111 mkdir File::Spec->catfile($blib_lib, 'Math/GSL');
113 print "+++swig -o $c_file -outdir $blib_lib -perl5 @swig_flags $file\n";
114 $self->do_system(@swig, '-o', $c_file, '-outdir', $blib_lib . '/Math/GSL/',
115 '-perl5', @swig_flags, $file)
116 or die "error building $c_file file from '$file'";
118 my $pm_file = "${file_base}.pm";
119 my $from = File::Spec->catfile($blib_lib, $pm_file);
120 my $to = File::Spec->catfile("lib", $pm_file);
122 print "Copying from: $from, to: $to; it makes the CPAN indexer happy.\n";
123 copy($from,$to);
124 return $c_file;
127 # From Base.pm but modified for a SWIG conventions.
128 # We just pass a $obj_file parameter
129 # SWIG objects have a get created with _wrap added. For example
130 # perlcdio.swg produces perlcdio_wrap.c, and perlcdio_wrap.o.
131 # But the shared object is still perlcdio.so.
132 # Also we modified the die to report the full file name.
133 sub link_c {
134 my ($self, $to, $file_base, $obj_file) = @_;
135 my ($cf, $p) = ($self->{config}, $self->{properties}); # For convenience
137 my $lib_file = File::Spec->catfile($to, File::Basename::basename("$file_base.so"));
139 $self->add_to_cleanup($lib_file);
140 my $objects = $p->{objects} || [];
142 unless ($self->up_to_date([$obj_file, @$objects], $lib_file)) {
143 my @linker_flags = $self->split_like_shell($p->{extra_linker_flags});
144 my @lddlflags = $self->split_like_shell($cf->{lddlflags});
145 my @shrp = $self->split_like_shell($cf->{shrpenv});
146 my @ld = $self->split_like_shell($cf->{ld}) || "gcc";
147 $self->do_system(@shrp, @ld, @lddlflags, @user_libs, '-o', $lib_file,
148 $obj_file, @$objects, @linker_flags)
149 or die "error building $lib_file file from '$obj_file'";
152 return $lib_file;
155 # From Base.pm but modified to put package cflags *after*
156 # installed c flags so warning-removal will have an effect.
158 sub compile_c {
159 my ($self, $file) = @_;
160 my ($cf, $p) = ($self->{config}, $self->{properties}); # For convenience
162 # File name, minus the suffix
163 (my $file_base = $file) =~ s/\.[^.]+$//;
164 my $obj_file = "$file_base.so";
165 $self->add_to_cleanup($obj_file);
166 return $obj_file if $self->up_to_date($file, $obj_file);
169 $cf->{installarchlib} = $Config{archlib};
171 my @include_dirs = @{$p->{include_dirs}}
172 ? map {"-I$_"} (@{$p->{include_dirs}}, File::Spec->catdir($cf->{installarchlib}, 'CORE'))
173 : map {"-I$_"} ( File::Spec->catdir($cf->{installarchlib}, 'CORE') ) ;
175 my @extra_compiler_flags = #split / /, q{-shared -Wall -fpic -Dbool=char -I/usr/local/include/};
176 $self->split_like_shell($p->{extra_compiler_flags});
178 my @cccdlflags = $self->split_like_shell($cf->{cccdlflags});
180 my @ccflags = split / /, $self->split_like_shell($cf->{ccflags});
181 my @optimize = $self->split_like_shell($cf->{optimize});
183 # Whoah! There seems to be a bug in gcc 4.1.0 and optimization
184 # and swig. I'm not sure who's at fault. But for now the simplest
185 # thing is to turn off all optimization. For the kinds of things that
186 # SWIG does - do conversions between parameters and transfers calls
187 # I doubt optimization makes much of a difference. But if it does,
188 # it can be added back via @extra_compiler_flags.
190 my @flags = (@include_dirs, @cccdlflags, '-c', @ccflags,
191 @extra_compiler_flags,
194 my @cc = $self->split_like_shell($cf->{cc});
195 @cc = "gcc" unless @cc;
197 $self->do_system(@cc, @flags, '-o', $obj_file, $file)
198 or die "error building $cf->{obj_ext} file from '$file'";
200 return $obj_file;
206 sub try_compile {
207 my ($c, %args) = @_;
209 my $ok = 0;
210 my $tmp = "tmp$$";
211 local(*TMPC);
213 my $obj_ext = $Config{obj_ext} || ".o";
214 unlink("$tmp.c", "$tmp$obj_ext");
216 if (open(TMPC, ">", "$tmp.c")) {
217 print TMPC $c;
218 close(TMPC);
220 my $cccmd = $args{cccmd};
221 my $errornull;
222 my $ccflags = $Config{'ccflags'};
223 $ccflags .= " $args{ccflags}" if $args{ccflags};
225 if ($args{silent} ) {
226 $errornull = "2>/dev/null" unless defined $errornull;
227 } else {
228 $errornull = '';
231 $cccmd = "$Config{'cc'} -o $tmp $ccflags $tmp.c $errornull"
232 unless defined $cccmd;
234 printf "cccmd = $cccmd\n" if $args{verbose};
235 my $res = system($cccmd);
236 $ok = defined($res) && $res == 0;
238 if ( !$ok ) {
239 my $errno = $? >> 8;
240 local $! = $errno;
241 print "
243 *** The test compile of '$tmp.c' failed: status $?
244 *** (the status means: errno = $errno or '$!')
245 *** DO NOT PANIC: this just means that *some* you may get some innocuous
246 *** compiler warnings.
249 unlink("$tmp.c");
252 return $ok;
255 sub try_cflags ($) {
256 my ($ccflags) = @_;
257 my $c_prog = "int main () { return 0; }\n";
258 print "Checking if $Config{cc} supports \"$ccflags\"...";
259 my $result = try_compile($c_prog, ccflags=>$ccflags);
260 if ($result) {
261 print "yes\n";
262 return " $ccflags";
264 print "no\n";
265 return '';
269 my %gsl_pkgcfg = ExtUtils::PkgConfig->find ('gsl');
271 use constant MIN_GSL_VERSION => 1.10;
272 my $gv = $gsl_pkgcfg{'modversion'};
274 if (defined $gv) {
275 if ($gsl_pkgcfg{'modversion'} =~ m{\A((?:\d+)(?:\.\d+))}) {
276 my @current= split /\./, $1;
277 my @min= split /\./, MIN_GSL_VERSION;
278 unless ($current[0] > $min[0] && $current[1] >= $min[1]) {
279 printf "
280 ***
281 *** You need to have gsl %s or greater installed. (You have $gv).
282 *** Get gsl at http://www.gnu.org/software/gsl
283 ", MIN_GSL_VERSION;
284 exit 1;
285 } else {
286 print "Good, I found gsl version $gv installed.\n";
289 } else {
290 print "
291 ***
292 *** Can't parse gsl version $gv.
293 *** Will continue and keep my fingers crossed for luck.
296 } else {
297 print "
298 ***
299 *** Can't find gsl configuration info. Is gsl installed?
300 *** Get gsl at http://www.gnu.org/software/gsl
302 exit 1;
305 print "Checking for SWIG...";
306 my @swig_version = `swig -version 2>&1`;
307 my $swig_installed = 0;
308 if ($?) {
309 my $errno = $? >> 8;
310 print "
311 *** I don't see SWIG installed. I'll use the SWIG-generated file
312 *** that comes with the distribution. If you want SWIG, get it
313 *** from http://www.swig.org
315 print "*** Output was:
316 @swig_version
317 " if @swig_version;
318 } else {
319 $swig_installed = 1;
320 print "ok\n";
323 my $ccflags = $gsl_pkgcfg{cflags};
325 ## Swig produces a number of GCC warnings. Turn them off if we can.
326 $ccflags .= try_cflags("-Wno-strict-aliasing");
327 $ccflags .= try_cflags("-Wno-unused-function");
328 $ccflags .= try_cflags("-Wno-unused-value");
329 $ccflags .= try_cflags("-Wno-unused-function");
330 $ccflags .= try_cflags("-Wno-unused-variable");
332 my $ldflags = "$gsl_pkgcfg{libs} -gsl";
333 my $swig_flags='-Wall';
334 if ('cygwin' eq $Config{osname} &&
335 $Config{shrpenv} =~ m{\Aenv LD_RUN_PATH=(.*)\Z} ) {
336 $ldflags .= " -L$1 -lperl";
337 # Should we check the 32-ness?
338 $swig_flags = '-DNEED_LONG';
339 } elsif ('darwin' eq $Config{osname}) {
340 $ldflags .= " -bundle -flat_namespace";
343 my $class = Module::Build->subclass( code => $code );
345 my $builder = $class->new(
346 module_name => 'Math::GSL',
347 add_to_cleanup => [ q{*.core Math-GSL-* tmp* *_wrap.c *.pm pod2ht*.tmp _build *.so} ],
348 create_makefile_pl => 'passthrough',
349 dist_abstract => 'Interface to the GNU Scientific Library',
350 dist_author => 'Jonathan Leto <jonathan@leto.net>',
351 dist_version_from => 'lib/Math/GSL.pm',
352 include_dirs => '/usr/local/lib/perl5/5.10.0/i386-freebsd/CORE',
353 extra_linker_flags => '-shared -I./lib -I../lib -I/usr/local/include/ ' . $ldflags,
354 extra_compiler_flags=> q{-shared -Wall -fpic -Dbool=char -I/usr/local/include/ } . $ccflags,
355 swig_flags => $swig_flags,
356 #'-importall -module Math::GSL -shadow -Wall ' . $swig_flags,
357 swig_installed => $swig_installed,
358 license => 'gpl',
359 requires => {
360 'ExtUtils::PkgConfig' => '1.03',
361 'Test::More' => 0,
362 version => 0,
363 perl => '5.8.6',
365 sign => 1,
366 swig_source => [
367 map { [ "$_.i" ] }
368 qw/ CDF Deriv Errno Fit Machine PowInt Randist SF Types/
372 $builder->add_build_element('swig');
373 $builder->create_build_script();