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
25 use ExtUtils
::PkgConfig
;
30 use File::Spec::Functions qw/:ALL/;
32 sub process_swig_files {
34 my $p = $self->{properties};
35 return unless $p->{swig_source};
36 my $files_ref = $p->{swig_source};
37 foreach my $file (@$files_ref) {
38 $self->process_swig($file->[0], $file->[1]);
42 # Check check dependencies for $main_swig_file. These are the
43 # %includes. If needed, arrange to run swig on $main_swig_file to
44 # produce a xxx_wrap.c C file.
47 my ($self, $main_swig_file, $deps_ref) = @_;
48 my ($cf, $p) = ($self->{config}, $self->{properties}); # For convenience
50 # File name. e.g, perlcdio.swg -> perlcdio_wrap.c
51 (my $file_base = $main_swig_file) =~ s/\.[^.]+$//;
52 my $c_file = "${file_base}_wrap.c";
55 $self->add_to_cleanup($c_file);
57 # If any of the swig files that the main swig depends is newer
59 foreach my $depends_on ($main_swig_file, @$deps_ref) {
60 unless ($self->up_to_date($depends_on, $c_file)) {
61 $self->compile_swig($main_swig_file, $c_file);
62 # Only need to build $c_file once no matter how many
69 my $obj_file = $self->compile_c($c_file);
70 $self->add_to_cleanup($obj_file);
72 # The .so files don't go in blib/lib/, they go in blib/arch/auto/.
73 # Unfortunately we have to pre-compute the whole path.
76 my @dirs = splitdir($file_base);
77 $archdir = catdir($self->blib,'arch', @dirs[1..$#dirs]);
81 $self->link_c($archdir, $file_base, $obj_file);
84 # Invoke swig with -perl -outdir and other options.
86 my ($self, $file, $c_file) = @_;
87 my ($cf, $p) = ($self->{config}, $self->{properties}); # For convenience
89 # File name, minus the suffix
90 (my $file_base = $file) =~ s/\.[^.]+$//;
93 if (defined($p->{swig})) {
94 @swig = $self->split_like_shell($p->{swig});
98 if (defined($p->{swig_flags})) {
99 @swig_flags = $self->split_like_shell($p->{swig_flags});
104 my $blib_lib = catfile(qw/blib lib/);
106 mkdir catfile($blib_lib, qw/Math GSL/);
107 my $outdir = catfile($blib_lib, qw/Math GSL/);
108 my $pm_file = "${file_base}.pm";
109 my $from = catfile($blib_lib, qw/Math GSL/, $pm_file);
110 my $to = catfile(qw/lib Math GSL/,$pm_file);
111 chmod 0644, $from, $to;
113 $self->do_system(@swig, '-o', $c_file,
115 '-perl5', @swig_flags, $file)
116 or die "error building $c_file file from '$file'";
119 print "Copying from: $from, to: $to; it makes the CPAN indexer happy.\n";
124 # From Base.pm but modified for a SWIG conventions.
125 # We just pass a $obj_file parameter
126 # SWIG objects have a get created with _wrap added. For example
127 # perlcdio.swg produces perlcdio_wrap.c, and perlcdio_wrap.o.
128 # But the shared object is still perlcdio.so.
129 # Also we modified the die to report the full file name.
131 my ($self, $to, $file_base, $obj_file) = @_;
132 my ($cf, $p) = ($self->{config}, $self->{properties}); # For convenience
134 my $lib_file = catfile($to, File::Basename::basename("$file_base.so"));
136 $self->add_to_cleanup($lib_file);
137 my $objects = $p->{objects} || [];
139 unless ($self->up_to_date([$obj_file, @$objects], $lib_file)) {
140 my @linker_flags = $self->split_like_shell($p->{extra_linker_flags});
141 my @lddlflags = $self->split_like_shell($cf->{lddlflags});
142 my @shrp = $self->split_like_shell($cf->{shrpenv});
143 my @ld = $self->split_like_shell($cf->{ld}) || "gcc";
144 $self->do_system(@shrp, @ld, @lddlflags, @user_libs, '-o', $lib_file,
145 $obj_file, @$objects, @linker_flags)
146 or die "error building $lib_file file from '$obj_file'";
152 # From Base.pm but modified to put package cflags *after*
153 # installed c flags so warning-removal will have an effect.
156 my ($self, $file) = @_;
157 my ($cf, $p) = ($self->{config}, $self->{properties}); # For convenience
159 # File name, minus the suffix
160 (my $file_base = $file) =~ s/\.[^.]+$//;
161 my $obj_file = $file_base . $Config{_o};
163 $self->add_to_cleanup($obj_file);
164 return $obj_file if $self->up_to_date($file, $obj_file);
167 $cf->{installarchlib} = $Config{archlib};
169 my @include_dirs = @{$p->{include_dirs}}
170 ? map {"-I$_"} (@{$p->{include_dirs}}, catdir($cf->{installarchlib}, 'CORE'))
171 : map {"-I$_"} ( catdir($cf->{installarchlib}, 'CORE') ) ;
173 my @extra_compiler_flags = $self->split_like_shell($p->{extra_compiler_flags});
175 my @cccdlflags = $self->split_like_shell($cf->{cccdlflags});
177 my @ccflags = $self->split_like_shell($cf->{ccflags});
178 my @optimize = $self->split_like_shell($cf->{optimize});
180 # Whoah! There seems to be a bug in gcc 4.1.0 and optimization
181 # and swig. I'm not sure who's at fault. But for now the simplest
182 # thing is to turn off all optimization. For the kinds of things that
183 # SWIG does - do conversions between parameters and transfers calls
184 # I doubt optimization makes much of a difference. But if it does,
185 # it can be added back via @extra_compiler_flags.
187 my @flags = (@include_dirs, @cccdlflags, '-c', @ccflags, @extra_compiler_flags, );
189 my @cc = $self->split_like_shell($cf->{cc});
190 @cc = "gcc" unless @cc;
192 $self->do_system(@cc, @flags, '-o', $obj_file, $file)
193 or die "error building $Config{_o} file from '$file'";
208 my $obj_ext = $Config{_o
} || ".o";
209 unlink("$tmp.c", "$tmp$obj_ext");
211 if (open(TMPC
, ">", "$tmp.c")) {
215 my $cccmd = $args{cccmd
};
217 my $ccflags = $Config{'ccflags'};
218 $ccflags .= " $args{ccflags}" if $args{ccflags
};
220 if ($args{silent
} ) {
221 $errornull = "2>/dev/null" unless defined $errornull;
226 $cccmd = "$Config{'cc'} -o $tmp $ccflags $tmp.c $errornull"
227 unless defined $cccmd;
229 printf "cccmd = $cccmd\n" if $args{verbose
};
230 my $res = system($cccmd);
231 $ok = defined($res) && $res == 0;
238 *** The test compile of '$tmp.c' failed: status $?
239 *** (the status means: errno = $errno or '$!')
240 *** DO NOT PANIC: this just means that *some* you may get some innocuous
241 *** compiler warnings.
252 my $c_prog = "int main () { return 0; }\n";
253 print "Checking if $Config{cc} supports \"$ccflags\"...";
254 my $result = try_compile
($c_prog, ccflags
=>$ccflags);
264 print "Checking for GSL..";
265 my %gsl_pkgcfg = ExtUtils
::PkgConfig
->find ('gsl');
267 my $MIN_GSL_VERSION = "1.11";
268 my $gv = $gsl_pkgcfg{'modversion'};
271 if ($gv =~ m{\A(\d+(?:\.\d+)+)}) {
272 my @current= split /\./, $1;
273 my @min= split /\./, $MIN_GSL_VERSION;
274 unless ($current[0] >= $min[0] && $current[1] >= $min[1]) {
277 *** You need to have GSL %s or greater installed. (You have $gv).
278 *** Get GSL at http://www.gnu.org/software/gsl\n", $MIN_GSL_VERSION;
281 print "Found GSL version $gv\n";
287 *** Can't parse GSL version $gv.
288 *** Will continue and keep my fingers crossed for luck.
294 *** Can't find GSL configuration info. Is GSL installed?
295 *** Get GSL at http://www.gnu.org/software/gsl
301 my $ccflags = $gsl_pkgcfg{cflags
};
303 ## Swig produces a number of GCC warnings. Turn them off if we can.
304 $ccflags .= try_cflags
("-Wno-strict-aliasing");
305 $ccflags .= try_cflags
("-Wno-unused-function");
306 $ccflags .= try_cflags
("-Wno-unused-value");
307 $ccflags .= try_cflags
("-Wno-unused-function");
308 $ccflags .= try_cflags
("-Wno-unused-variable");
310 my $ldflags = "$gsl_pkgcfg{libs} -gsl";
311 my $swig_flags='-Wall ' . $gsl_pkgcfg{cflags
};
313 if ('cygwin' eq $Config{osname
} &&
314 $Config{shrpenv
} =~ m{\Aenv LD_RUN_PATH=(.*)\Z} ) {
315 $ldflags .= " -L$1 -lperl";
316 # Should we check the 32-ness?
317 $swig_flags = '-DNEED_LONG';
318 } elsif ('darwin' eq $Config{osname
}) {
319 $ldflags .= " -bundle -flat_namespace";
322 my $class = Module
::Build
->subclass( code
=> $code );
323 my @Subsystems = sort qw
/
324 BLAS Diff Machine Statistics Wavelet
325 Block Eigen Matrix Poly Wavelet2D
330 Heapsort Multifit Randist Sum
331 Combination Histogram Multimin Roots
332 Complex Histogram2D Multiroots SF
333 Const Siman IEEEUtils Sys
334 Integration NTuple Sort
335 DHT Interp ODEIV Vector
336 Deriv Linalg Permutation Spline
338 my $cleanup = qq{core
*.core Makefile Math
-GSL
-* tmp
* pod2ht
*.tmp _build blib
*.so
*.orig
};
340 my $builder = $class->new(
341 module_name
=> 'Math::GSL',
342 add_to_cleanup
=> [ $cleanup ],
343 create_makefile_pl
=> 'passthrough',
344 dist_abstract
=> 'Interface to the GNU Scientific Library using SWIG',
345 dist_author
=> 'Jonathan Leto <jonathan@leto.net>',
346 dist_version_from
=> 'lib/Math/GSL.pm',
348 extra_linker_flags
=> '-shared -I./lib -I../lib ' . $ldflags,
349 extra_compiler_flags
=> q{-shared -Wall -fpic -Dbool=char } . $ccflags,
350 swig_flags
=> $swig_flags,
353 'ExtUtils::PkgConfig' => '1.03',
356 'Test::Exception' => 0.21,
357 'Test::Class' => 0.12,
363 map { [ "$_.i" ] } @Subsystems
366 $builder->add_build_element('swig');
367 $builder->create_build_script();
368 print "Have a great day!\n";