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";
123 sub _is_windows { $^O =~ /MSWin32/i }
125 # Windows fixes courtesy of <sisyphus@cpan.org>
127 my ($self, $to, $file_base, $obj_file) = @_;
128 my ($cf, $p) = ($self->{config}, $self->{properties}); # For convenience
130 my $lib_file = catfile($to, File::Basename::basename("$file_base.$Config{dlext}"));
132 $self->add_to_cleanup($lib_file);
133 my $objects = $p->{objects} || [];
135 unless ($self->up_to_date([$obj_file, @$objects], $lib_file)) {
136 my @linker_flags = $self->split_like_shell($p->{extra_linker_flags});
138 push @linker_flags, $Config{archlib} . '/CORE/' . $Config{libperl} if _is_windows();
140 my @lddlflags = $self->split_like_shell($cf->{lddlflags});
141 my @shrp = $self->split_like_shell($cf->{shrpenv});
142 my @ld = $self->split_like_shell($cf->{ld}) || "gcc";
143 $self->do_system(@shrp, @ld, @lddlflags, @user_libs, '-o', $lib_file,
144 $obj_file, @$objects, @linker_flags)
145 or die "error building $lib_file file from '$obj_file'";
151 # From Base.pm but modified to put package cflags *after*
152 # installed c flags so warning-removal will have an effect.
155 my ($self, $file) = @_;
156 my ($cf, $p) = ($self->{config}, $self->{properties}); # For convenience
158 # File name, minus the suffix
159 (my $file_base = $file) =~ s/\.[^.]+$//;
160 my $obj_file = $file_base . $Config{_o};
162 $self->add_to_cleanup($obj_file);
163 return $obj_file if $self->up_to_date($file, $obj_file);
166 $cf->{installarchlib} = $Config{archlib};
168 my @include_dirs = @{$p->{include_dirs}}
169 ? map {"-I$_"} (@{$p->{include_dirs}}, catdir($cf->{installarchlib}, 'CORE'))
170 : map {"-I$_"} ( catdir($cf->{installarchlib}, 'CORE') ) ;
172 my @extra_compiler_flags = $self->split_like_shell($p->{extra_compiler_flags});
174 my @cccdlflags = $self->split_like_shell($cf->{cccdlflags});
176 my @ccflags = $self->split_like_shell($cf->{ccflags});
177 my @optimize = $self->split_like_shell($cf->{optimize});
179 # Whoah! There seems to be a bug in gcc 4.1.0 and optimization
180 # and swig. I'm not sure who's at fault. But for now the simplest
181 # thing is to turn off all optimization. For the kinds of things that
182 # SWIG does - do conversions between parameters and transfers calls
183 # I doubt optimization makes much of a difference. But if it does,
184 # it can be added back via @extra_compiler_flags.
186 my @flags = (@include_dirs, @cccdlflags, '-c', @ccflags, @extra_compiler_flags, );
188 my @cc = $self->split_like_shell($cf->{cc});
189 @cc = "gcc" unless @cc;
191 $self->do_system(@cc, @flags, '-o', $obj_file, $file)
192 or die "error building $Config{_o} file from '$file'";
207 my $obj_ext = $Config{_o
} || ".o";
208 unlink("$tmp.c", "$tmp$obj_ext");
210 if (open(TMPC
, ">", "$tmp.c")) {
214 my $cccmd = $args{cccmd
};
216 my $ccflags = $Config{'ccflags'};
217 $ccflags .= " $args{ccflags}" if $args{ccflags
};
219 if ($args{silent
} ) {
220 $errornull = "2>/dev/null" unless defined $errornull;
225 $cccmd = "$Config{'cc'} -o $tmp $ccflags $tmp.c $errornull"
226 unless defined $cccmd;
228 printf "cccmd = $cccmd\n" if $args{verbose
};
229 my $res = system($cccmd);
230 $ok = defined($res) && $res == 0;
237 *** The test compile of '$tmp.c' failed: status $?
238 *** (the status means: errno = $errno or '$!')
239 *** DO NOT PANIC: this just means that *some* you may get some innocuous
240 *** compiler warnings.
251 my $c_prog = "int main () { return 0; }\n";
252 print "Checking if $Config{cc} supports \"$ccflags\"...";
253 my $result = try_compile
($c_prog, ccflags
=>$ccflags);
263 print "Checking for GSL..";
264 my %gsl_pkgcfg = ExtUtils
::PkgConfig
->find ('gsl');
266 my $MIN_GSL_VERSION = "1.11";
267 my $gv = $gsl_pkgcfg{'modversion'};
270 if ($gv =~ m{\A(\d+(?:\.\d+)+)}) {
271 my @current= split /\./, $1;
272 my @min= split /\./, $MIN_GSL_VERSION;
273 unless ($current[0] >= $min[0] && $current[1] >= $min[1]) {
276 *** You need to have GSL %s or greater installed. (You have $gv).
277 *** Get GSL at http://www.gnu.org/software/gsl\n", $MIN_GSL_VERSION;
280 print "Found GSL version $gv\n";
286 *** Can't parse GSL version $gv.
287 *** Will continue and keep my fingers crossed for luck.
293 *** Can't find GSL configuration info. Is GSL installed?
294 *** Get GSL at http://www.gnu.org/software/gsl
300 my $ccflags = $gsl_pkgcfg{cflags
};
302 ## Swig produces a number of GCC warnings. Turn them off if we can.
303 $ccflags .= try_cflags
("-Wno-strict-aliasing");
304 $ccflags .= try_cflags
("-Wno-unused-function");
305 $ccflags .= try_cflags
("-Wno-unused-value");
306 $ccflags .= try_cflags
("-Wno-unused-function");
307 $ccflags .= try_cflags
("-Wno-unused-variable");
309 my $ldflags = "$gsl_pkgcfg{libs} -gsl";
310 my $swig_flags='-Wall ' . $gsl_pkgcfg{cflags
};
312 if ('cygwin' eq $Config{osname
} &&
313 $Config{shrpenv
} =~ m{\Aenv LD_RUN_PATH=(.*)\Z} ) {
314 $ldflags .= " -L$1 -lperl";
315 # Should we check the 32-ness?
316 $swig_flags = '-DNEED_LONG';
317 } elsif ('darwin' eq $Config{osname
}) {
318 $ldflags .= " -bundle -flat_namespace";
321 my $class = Module
::Build
->subclass( code
=> $code );
322 my @Subsystems = sort qw
/
323 BLAS Diff Machine Statistics
324 Eigen Matrix Poly Wavelet2D
325 BSpline Errno PowInt Wavelet
329 Heapsort Multifit Randist Sum
330 Combination Histogram Multimin Roots
331 Complex Histogram2D Multiroots SF
332 Const Siman IEEEUtils Sys
333 Integration NTuple Sort
334 DHT Interp ODEIV Vector
335 Deriv Linalg Permutation Spline
337 my $cleanup = qq{core
*.core Makefile Math
-GSL
-* tmp
* pod2ht
*.tmp _build blib
*.so
*.orig
};
339 my $builder = $class->new(
340 module_name
=> 'Math::GSL',
341 add_to_cleanup
=> [ $cleanup ],
342 create_makefile_pl
=> 'passthrough',
343 dist_abstract
=> 'Interface to the GNU Scientific Library using SWIG',
344 dist_author
=> 'Jonathan Leto <jonathan@leto.net>',
345 dist_version_from
=> 'lib/Math/GSL.pm',
347 extra_linker_flags
=> '-shared -I./lib -I../lib ' . $ldflags,
348 extra_compiler_flags
=> q{-shared -Wall -fpic -Dbool=char } . $ccflags,
349 swig_flags
=> $swig_flags,
352 'ExtUtils::PkgConfig' => '1.03',
355 'Test::Exception' => 0.21,
356 'Test::Class' => 0.12,
362 map { [ "$_.i" ] } @Subsystems
365 $builder->add_build_element('swig');
366 $builder->create_build_script();
367 print "Have a great day!\n";