Auto-generate a swig file to include that has version information
[Math-GSL.git] / Build.PL
blobfe7780e9c0c0a8c6901985d5dd62a5bfcde745b2
1 #!/usr/bin/perl -w
2 # This has been heavily modified from the Device::Cdio Build.PL
3 # 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;
25 use lib 'inc';
26 use GSLBuilder;
27 use File::Spec::Functions qw/:ALL/;
29 BEGIN {
30 eval { require ExtUtils::PkgConfig };
31 if ($@) {
32 print "\nI see that you are a CPANTester, you really should install ExtUtils::PkgConfig !\n"
33 if $ENV{AUTOMATED_TESTING};
34 print <<EXIT;
35 ExtUtils::PkgConfig is currently needed to find GSL for the compilation of this module.
36 It may be bundled with Math::GSL in the future.
37 EXIT
38 exit 0;
44 sub try_compile {
45 my ($c, %args) = @_;
47 my $ok = 0;
48 my $tmp = "tmp$$";
49 local(*TMPC);
51 my $obj_ext = $Config{_o} || ".o";
52 unlink("$tmp.c", "$tmp$obj_ext");
54 if (open(TMPC, ">", "$tmp.c")) {
55 print TMPC $c;
56 close(TMPC);
58 my $cccmd = $args{cccmd};
59 my $errornull;
60 my $ccflags = $Config{'ccflags'};
61 $ccflags .= " $args{ccflags}" if $args{ccflags};
63 if ($args{silent} ) {
64 $errornull = "2>/dev/null" unless defined $errornull;
65 } else {
66 $errornull = '';
69 $cccmd = "$Config{'cc'} -o $tmp $ccflags $tmp.c $errornull"
70 unless defined $cccmd;
72 printf "cccmd = $cccmd\n" if $args{verbose};
73 my $res = system($cccmd);
74 $ok = defined($res) && $res == 0;
76 if ( !$ok ) {
77 my $errno = $? >> 8;
78 local $! = $errno;
79 print "
81 *** The test compile of '$tmp.c' failed: status $?
82 *** (the status means: errno = $errno or '$!')
83 *** DO NOT PANIC: this just means that *some* you may get some innocuous
84 *** compiler warnings.
87 unlink("$tmp.c");
90 return $ok;
93 sub try_cflags ($) {
94 my ($ccflags) = @_;
95 my $c_prog = "int main () { return 0; }\n";
96 print "Checking if $Config{cc} supports \"$ccflags\"...";
97 my $result = try_compile($c_prog, ccflags=>$ccflags);
98 if ($result) {
99 print "yes\n";
100 return " $ccflags";
102 print "no\n";
103 return '';
107 print "Checking for GSL..";
108 my %gsl_pkgcfg = ExtUtils::PkgConfig->find ('gsl');
110 my $MIN_GSL_VERSION = "1.8";
111 my $gv = $gsl_pkgcfg{'modversion'};
112 my $current_minor_version;
114 open my $fh, '>', 'swig/system.i' or die $!;
116 if (defined $gv) {
117 if ($gv =~ m{\A(\d+(?:\.\d+)+)}) {
118 my @current= split /\./, $1;
119 print $fh "#define GSL_MAJOR_VERSION $current[0]\n";
120 print $fh "#define GSL_MINOR_VERSION $current[1]\n";
122 my @min= split /\./, $MIN_GSL_VERSION;
123 $current_minor_version = $current[1];
124 unless ($current[0] >= $min[0] && $current[1] >= $min[1]) {
125 printf "
126 ***
127 *** You need to have GSL %s or greater installed. (You have $gv).
128 *** Get GSL at http://www.gnu.org/software/gsl\n", $MIN_GSL_VERSION;
129 exit 1;
130 } else {
131 print "Found GSL version $gv\n";
134 } else {
135 print "
136 ***
137 *** Can't parse GSL version $gv.
138 *** Will continue and keep my fingers crossed for luck.
141 } else {
142 print "
143 ***
144 *** Can't find GSL configuration info. Is GSL installed?
145 *** Get GSL at http://www.gnu.org/software/gsl
147 exit 1;
150 close $fh;
152 my $ccflags = $gsl_pkgcfg{cflags};
154 ## Swig produces a number of GCC warnings. Turn them off if we can.
155 $ccflags .= try_cflags("-Wno-strict-aliasing");
156 $ccflags .= try_cflags("-Wno-unused-function");
157 $ccflags .= try_cflags("-Wno-unused-value");
158 $ccflags .= try_cflags("-Wno-unused-function");
159 $ccflags .= try_cflags("-Wno-unused-variable");
161 my $ldflags = "$gsl_pkgcfg{libs} -gsl";
162 my $swig_flags = "$gsl_pkgcfg{cflags}"; # -Wall is a bit much
164 if ( $^O eq 'cygwin' && $Config{shrpenv} =~ m{\Aenv LD_RUN_PATH=(.*)\Z} ) {
165 $ldflags .= " -L$1 -lperl";
166 # Should we check the 32-ness?
167 $swig_flags = '-DNEED_LONG';
168 } elsif ( $^O eq 'darwin' ) {
169 $ldflags .= ' -bundle -flat_namespace ';
171 if ($Config{archname} =~ /x86_64/ ) {
172 $ldflags .= ' -fPIC -fno-omit-frame-pointer ';
173 $ccflags .= ' -fPIC -fno-omit-frame-pointer ';
176 my @Subsystems = grep { ! /^Test$/ } GSLBuilder::subsystems;
178 # BSplines appeared in 1.9
179 if ($current_minor_version < 9 ) {
180 @Subsystems = grep { ! /BSpline/ } @Subsystems;
182 my $cleanup = qq{
183 xs/*_wrap.c core *.core swig/system.i
184 swig/*.o Makefile Math-GSL-* tmp* pod2ht*.tmp _build
185 lib/Math/GSL/[A-z]+/* blib *.so *.orig
186 } . join " ", map { catfile( (qw( lib Math GSL ), "$_.pm") ) } @Subsystems;
188 my $builder = GSLBuilder->new(
189 module_name => 'Math::GSL',
190 add_to_cleanup => [ $cleanup ],
191 create_makefile_pl => 'passthrough',
192 dist_abstract => 'Interface to the GNU Scientific Library using SWIG',
193 dist_author => 'Jonathan Leto <jonathan@leto.net>',
194 dist_version_from => 'lib/Math/GSL.pm',
195 include_dirs => q{},
196 extra_linker_flags => '-shared ' . $ldflags,
197 extra_compiler_flags=> $ccflags,
198 swig_flags => $swig_flags,
199 license => 'gpl',
200 requires => {
201 'ExtUtils::PkgConfig' => '1.03',
202 'Scalar::Util' => 0,
203 'Test::More' => 0,
204 'Test::Exception' => 0.21,
205 'Test::Class' => 0.12,
206 version => 0,
207 perl => '5.8.0',
209 sign => 0,
210 configure_requires => {
211 'ExtUtils::PkgConfig' => '1.03',
213 swig_source => [
214 map { [ "swig/$_.i", "pod/$_.pod" ] } @Subsystems ,
217 $builder->add_build_element('swig');
218 $builder->create_build_script();
219 print "Have a great day!\n";