Prevent accidentally trying to call SWIG from a CPAN distro
[Math-GSL.git] / inc / GSLBuilder.pm
blobdefa888ad7d2b4fd2f1f709950596f2169b88fed
1 package GSLBuilder;
2 use Config;
3 use File::Copy;
4 use File::Path qw/mkpath/;
5 use File::Spec::Functions qw/:ALL/;
6 use base 'Module::Build';
8 sub process_swig_files {
9 my $self = shift;
10 my $p = $self->{properties};
11 return unless $p->{swig_source};
12 my $files_ref = $p->{swig_source};
13 foreach my $file (@$files_ref) {
14 $self->process_swig($file->[0], $file->[1]);
18 # Check check dependencies for $main_swig_file. These are the
19 # %includes. If needed, arrange to run swig on $main_swig_file to
20 # produce a xxx_wrap.c C file.
22 sub process_swig {
23 my ($self, $main_swig_file, $deps_ref) = @_;
24 my ($cf, $p) = ($self->{config}, $self->{properties}); # For convenience
26 (my $file_base = $main_swig_file) =~ s/\.[^.]+$//;
27 my $c_file = "${file_base}_wrap.c";
29 my @deps = defined $deps_ref ? @$deps_ref : ();
31 if ( !$p->{swig_disabled} && $self->up_to_date( [$main_swig_file,@deps ], $c_file) ) {
32 $self->compile_swig($main_swig_file, $c_file);
35 # .c -> .o
36 my $obj_file = $self->compile_c($c_file);
37 $self->add_to_cleanup($obj_file);
39 my $archdir = catdir($self->blib,'arch','auto','Math','GSL', $file_base);
40 mkpath $archdir unless -d $archdir;
42 # .o -> .so
43 $self->link_c($archdir, $file_base, $obj_file);
46 # Invoke swig with -perl -outdir and other options.
47 sub compile_swig {
48 my ($self, $file, $c_file) = @_;
49 my ($cf, $p) = ($self->{config}, $self->{properties}); # For convenience
51 # File name, minus the suffix
52 (my $file_base = $file) =~ s/\.[^.]+$//;
53 my $pm_file = "${file_base}.pm";
55 my @swig = qw/swig/, defined($p->{swig}) ? ($self->split_like_shell($p->{swig})) : ();
56 my @swig_flags = defined($p->{swig_flags}) ? $self->split_like_shell($p->{swig_flags}) : ();
58 my $blib_lib = catfile(qw/blib lib/);
59 my $gsldir = catfile($blib_lib, qw/Math GSL/);
60 mkdir $gsldir unless -e $gsldir;
62 my $from = catfile($gsldir, $pm_file);
63 my $to = catfile(qw/lib Math GSL/,$pm_file);
64 chmod 0644, $from, $to;
66 $self->do_system(@swig, '-o', $c_file,
67 '-outdir', $gsldir,
68 '-perl5', @swig_flags, $file)
69 or die "error building $c_file file from '$file'";
70 print "Copying from: $from, to: $to; it makes the CPAN indexer happy.\n";
71 copy($from,$to);
72 return $c_file;
74 sub is_windows { $^O =~ /MSWin32/i }
75 sub is_darwin { $^O =~ /darwin/i }
77 # Windows fixes courtesy of <sisyphus@cpan.org>
78 sub link_c {
79 my ($self, $to, $file_base, $obj_file) = @_;
80 my ($cf, $p) = ($self->{config}, $self->{properties}); # For convenience
82 my $lib_file = catfile($to, File::Basename::basename("$file_base.$Config{dlext}"));
84 $self->add_to_cleanup($lib_file);
85 my $objects = $p->{objects} || [];
87 unless ($self->up_to_date([$obj_file, @$objects], $lib_file)) {
88 my @linker_flags = $self->split_like_shell($p->{extra_linker_flags});
90 push @linker_flags, $Config{archlib} . '/CORE/' . $Config{libperl} if (is_windows() or is_darwin());
92 my @lddlflags = $self->split_like_shell($cf->{lddlflags});
93 my @shrp = $self->split_like_shell($cf->{shrpenv});
94 my @ld = $self->split_like_shell($cf->{ld}) || "gcc";
96 # Strip binaries if we are compiling on windows
97 push @ld, "-s" if (is_windows() && $Config{cc} eq 'gcc');
99 $self->do_system(@shrp, @ld, @lddlflags, @user_libs, '-o', $lib_file,
100 $obj_file, @$objects, @linker_flags)
101 or die "error building $lib_file file from '$obj_file'";
104 return $lib_file;
107 # From Base.pm but modified to put package cflags *after*
108 # installed c flags so warning-removal will have an effect.
110 sub compile_c {
111 my ($self, $file) = @_;
112 my ($cf, $p) = ($self->{config}, $self->{properties}); # For convenience
114 # File name, minus the suffix
115 (my $file_base = $file) =~ s/\.[^.]+$//;
116 my $obj_file = $file_base . $Config{_o};
118 $self->add_to_cleanup($obj_file);
119 return $obj_file if $self->up_to_date($file, $obj_file);
122 $cf->{installarchlib} = $Config{archlib};
124 my @include_dirs = @{$p->{include_dirs}}
125 ? map {"-I$_"} (@{$p->{include_dirs}}, catdir($cf->{installarchlib}, 'CORE'))
126 : map {"-I$_"} ( catdir($cf->{installarchlib}, 'CORE') ) ;
128 my @extra_compiler_flags = $self->split_like_shell($p->{extra_compiler_flags});
130 my @cccdlflags = $self->split_like_shell($cf->{cccdlflags});
132 my @ccflags = $self->split_like_shell($cf->{ccflags});
133 push @ccflags, $self->split_like_shell($Config{cppflags});
135 my @optimize = $self->split_like_shell($cf->{optimize});
137 # Whoah! There seems to be a bug in gcc 4.1.0 and optimization
138 # and swig. I'm not sure who's at fault. But for now the simplest
139 # thing is to turn off all optimization. For the kinds of things that
140 # SWIG does - do conversions between parameters and transfers calls
141 # I doubt optimization makes much of a difference. But if it does,
142 # it can be added back via @extra_compiler_flags.
144 my @flags = (@include_dirs, @cccdlflags, '-c', @ccflags, @extra_compiler_flags, );
146 my @cc = $self->split_like_shell($cf->{cc});
147 @cc = "gcc" unless @cc;
149 $self->do_system(@cc, @flags, '-o', $obj_file, $file)
150 or die "error building $Config{_o} file from '$file'";
152 return $obj_file;
155 3.14;