| 1 | package Module::Build::Pluggable::Fortran; | ||
| 2 | |||
| 3 | # ABSTRACT: Plugin for Module::Build to compile Fortran C<.f> files | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | our $VERSION = '0.27'; | ||
| 8 | use parent qw{Module::Build::Pluggable::Base}; | ||
| 9 | use Text::ParseWords qw(shellwords); | ||
| 10 | |||
| 11 | BEGIN { | ||
| 12 | eval "use ExtUtils::F77"; | ||
| 13 | if ($@) { | ||
| 14 | warn "ExtUtils::F77 module not found. Build not possible.\n"; | ||
| 15 | exit 0; | ||
| 16 | } | ||
| 17 | if ( not ExtUtils::F77->runtimeok ) { | ||
| 18 | warn "No Fortran compiler found. Build not possible.\n"; | ||
| 19 | exit 0; | ||
| 20 | } | ||
| 21 | if ( not ExtUtils::F77->testcompiler ) { | ||
| 22 | warn "No fortran compiler found. Build not possible.\n"; | ||
| 23 | exit 0; | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | sub HOOK_configure { | ||
| 28 | my ($self) = @_; | ||
| 29 | |||
| 30 | $self->builder_class->add_property('f_source'); | ||
| 31 | ## Can't validate here b/c of HOOK_configure calling order | ||
| 32 | |||
| 33 | $self->build_requires( 'ExtUtils::F77' => 0 ); | ||
| 34 | $self->build_requires( 'ExtUtils::CBuilder' => '0.23' ); | ||
| 35 | |||
| 36 | my @runtime = shellwords(ExtUtils::F77->runtime); | ||
| 37 | $self->_add_extra_linker_flags( @runtime, @{ $self->_fortran_obj_files } ); | ||
| 38 | |||
| 39 | $self->builder->add_to_cleanup('f77_underscore'); | ||
| 40 | |||
| 41 | return 1; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub HOOK_build { | ||
| 45 | my ($self) = @_; | ||
| 46 | my $builder = $self->builder; | ||
| 47 | |||
| 48 | my $mycompiler = ExtUtils::F77->compiler(); | ||
| 49 | my $mycflags = ExtUtils::F77->cflags(); | ||
| 50 | undef $mycflags if $mycflags =~ m{^\s*}; # Avoid empty arg in cmd | ||
| 51 | |||
| 52 | my $f_src_files = $self->_fortran_files; | ||
| 53 | for my $f_src_file (@$f_src_files) { | ||
| 54 | ( my $file = $f_src_file ) =~ s{\.f$}{}; | ||
| 55 | |||
| 56 | # Both $self->up_to_date is undocumented and could change in the | ||
| 57 | # future: | ||
| 58 | my $up_to_date = $builder->up_to_date( $f_src_file, ["$file.o"] ); | ||
| 59 | if ( not $up_to_date ) { | ||
| 60 | |||
| 61 | my @cmd = ( | ||
| 62 | $mycompiler, '-c', '-o', "$file.o", ( $mycflags || () ), | ||
| 63 | "-O3", "-fPIC", "$file.f" | ||
| 64 | ); | ||
| 65 | |||
| 66 | warn join( " ", @cmd ), "\n"; | ||
| 67 | $builder->do_system(@cmd) | ||
| 68 | or die "error compiling $file"; | ||
| 69 | } | ||
| 70 | |||
| 71 | $builder->add_to_cleanup("$file.o"); | ||
| 72 | } | ||
| 73 | |||
| 74 | return 1; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub _fortran_files { | ||
| 78 | my ($self) = @_; | ||
| 79 | |||
| 80 | # We don't seem to be able to access the property created by add_property | ||
| 81 | # in HOOK_configure (ditto if we move that to HOOK_build), we are going to | ||
| 82 | # have to pry our way into builder and access f_source directly. | ||
| 83 | # my $f_source = $self->builder->f_source; | ||
| 84 | my $f_source = $self->builder->{properties}->{f_source}; | ||
| 85 | my @f_source_dirs = ref $f_source eq 'ARRAY' ? @$f_source : ($f_source); | ||
| 86 | |||
| 87 | my @f_source_files = (); | ||
| 88 | for my $f_src_dir (@f_source_dirs) { | ||
| 89 | my $f_src_files = $self->builder->rscan_dir( $f_src_dir, qr/\.f$/ ); | ||
| 90 | push @f_source_files, @$f_src_files; | ||
| 91 | } | ||
| 92 | |||
| 93 | return \@f_source_files; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub _fortran_obj_files { | ||
| 97 | my ($self) = @_; | ||
| 98 | |||
| 99 | my $f_src_files = $self->_fortran_files; | ||
| 100 | s{\.f$}{.o} for @$f_src_files; | ||
| 101 | return $f_src_files; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub _add_extra_linker_flags { | ||
| 105 | my ( $self, @new_flags ) = @_; | ||
| 106 | |||
| 107 | my $linker_flags = $self->builder->extra_linker_flags; | ||
| 108 | push @$linker_flags, @new_flags; | ||
| 109 | $self->builder->extra_linker_flags(@$linker_flags); | ||
| 110 | } | ||
| 111 | |||
| 112 | 1; | ||
| 113 | |||
| 114 | __END__ |