# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
# $Id: Portfile 118025 2014-03-19 18:34:46Z eborisch@macports.org $

PortSystem      1.0
PortGroup       muniversal 1.0
PortGroup       compiler_blacklist_versions 1.0
PortGroup       mpi 1.0

# don't change configure.cc, etc. since we'll take care of that manually with
# MPICC env var
mpi.choose
mpi.setup

name            fftw-3
version         3.3.4
revision        0
categories      math
license         GPL-2+
platforms       darwin
maintainers     sean
homepage        http://www.fftw.org/
distname        fftw-${version}

master_sites \
    ${homepage} \
    ftp://ftp.fftw.org/pub/fftw/ \
    ftp://ftp.kusastro.kyoto-u.ac.jp/pub/src/GNU/fftw/

description \
    Fast C routines to compute the Discrete Fourier Transform

long_description \
    FFTW is a C subroutine library for computing the \
    Discrete Fourier Transform (DFT) in one or more \
    dimensions, of both real and complex data, and of \
    arbitrary input size. We believe that FFTW, which is \
    free software, should become the FFT library of choice \
    for most applications. Our benchmarks, performed on a \
    variety of platforms, show that FFTW's performance is \
    typically superior to that of other publicly available \
    FFT software. Moreover, FFTW's performance is portable: \
    the program will perform well on most architectures \
    without modification. \
    This port is of fftw version 3.x. It has many \
    improvements relative to 2.x, but is not backwardly \
    compatible.

checksums \
    rmd160  463b5120d42ec2a3bc1385689176c5e0296d0d4a \
    sha256  8f0cde90929bc05587c3368d2f15cd0530a60b8a9912a8e2979a72dbe5af0982

# blacklist older versions of clang which do not handle the --enable-avx option (#41622, #41673)
compiler.blacklist-append {clang <= 211.10.1}

configure.args \
    --enable-threads \
    --disable-fortran \
    --enable-shared

configure.cflags-append \
    -fno-common \
    -O3 \
    -fomit-frame-pointer \
    -fstrict-aliasing

pre-configure {
    if { [avx_compiler_isset] == 1 } {
        array set merger_configure_args {
            ppc    "--enable-fma"
            ppc64  "--enable-fma"
            i386   "--enable-sse2 --enable-avx"
            x86_64 "--enable-sse2 --enable-avx"
        }
    } else {
        array set merger_configure_args {
            ppc    "--enable-fma"
            ppc64  "--enable-fma"
            i386   "--enable-sse2"
            x86_64 "--enable-sse2"
        }
    }
}

test.run     yes
test.target  check

use_parallel_build yes

platform powerpc {
  if {![variant_isset universal]} {
    configure.args-append --enable-fma
  }
}

subport fftw-3-single {
    configure.args-append --enable-float

    depends_lib port:fftw-3

    platform powerpc {
        if {![variant_isset universal]} {
            configure.args-append --enable-altivec
        }
    }

    pre-configure {
        if { [avx_compiler_isset] == 1 } {
            array set merger_configure_args {
                ppc    "--enable-fma --enable-altivec"
                ppc64  "--enable-fma --enable-altivec"
                i386   "--enable-sse --enable-avx"
                x86_64 "--enable-sse --enable-avx"
            }
        } else {
            array set merger_configure_args {
                ppc    "--enable-fma --enable-altivec"
                ppc64  "--enable-fma --enable-altivec"
                i386   "--enable-sse"
                x86_64 "--enable-sse"
            }
        }

        platform i386 {
            if {![variant_isset universal]} {
                configure.args-delete --enable-sse2
                configure.args-append --enable-sse
                if { [avx_compiler_isset] == 1 } {
                    configure.args-append --enable-avx
                }
            }
        }
    }

    post-destroot {
        file delete ${destroot}${prefix}/include/fftw3.f
        file delete ${destroot}${prefix}/include/fftw3.f03
        file delete ${destroot}${prefix}/include/fftw3l.f03
        file delete ${destroot}${prefix}/include/fftw3q.f03
        file delete ${destroot}${prefix}/include/fftw3.h
        file delete ${destroot}${prefix}/bin/fftw-wisdom-to-conf
        file delete ${destroot}${prefix}/share/man/man1/fftw-wisdom-to-conf.1
        file delete ${destroot}${prefix}/share/info/fftw3.info
        file delete ${destroot}${prefix}/share/info/fftw3.info-1
        file delete ${destroot}${prefix}/share/info/fftw3.info-2
        file delete ${destroot}${prefix}/share/info/fftw3.info-3
        file delete ${destroot}${prefix}/share/info/fftw3.info-4
        file delete ${destroot}${prefix}/share/info/fftw3.info-5

        if {[mpi_variant_isset]} {
          file delete ${destroot}${prefix}/include/fftw3-mpi.f03
          file delete ${destroot}${prefix}/include/fftw3-mpi.h
          file delete ${destroot}${prefix}/include/fftw3l-mpi.f03
        }
    }
}

subport fftw-3-long {
    configure.args-append --enable-long-double

    depends_lib port:fftw-3

    pre-configure {
        array set merger_configure_args {
            ppc    ""
            ppc64  ""
            i386   ""
            x86_64 ""
        }
    }

    post-destroot {
        file delete ${destroot}${prefix}/include/fftw3.f
        file delete ${destroot}${prefix}/include/fftw3.f03
        file delete ${destroot}${prefix}/include/fftw3l.f03
        file delete ${destroot}${prefix}/include/fftw3q.f03
        file delete ${destroot}${prefix}/include/fftw3.h
        file delete ${destroot}${prefix}/bin/fftw-wisdom-to-conf
        file delete ${destroot}${prefix}/share/man/man1/fftw-wisdom-to-conf.1
        file delete ${destroot}${prefix}/share/info/fftw3.info
        file delete ${destroot}${prefix}/share/info/fftw3.info-1
        file delete ${destroot}${prefix}/share/info/fftw3.info-2
        file delete ${destroot}${prefix}/share/info/fftw3.info-3
        file delete ${destroot}${prefix}/share/info/fftw3.info-4
        file delete ${destroot}${prefix}/share/info/fftw3.info-5

        if {[mpi_variant_isset]} {
          file delete ${destroot}${prefix}/include/fftw3-mpi.f03
          file delete ${destroot}${prefix}/include/fftw3-mpi.h
          file delete ${destroot}${prefix}/include/fftw3l-mpi.f03
        }
    }
}

if {${os.arch} eq "i386"} {
    lappend merger_configure_args(ppc)     "--disable-fortran"
    lappend merger_configure_args(ppc64)   "--disable-fortran"
} else {
    lappend merger_configure_args(i386)    "--disable-fortran"
    lappend merger_configure_args(x86_64)  "--disable-fortran"
}

if {[fortran_variant_isset]} {
    configure.args-delete --disable-fortran
}

pre-configure {
    if {[mpi_variant_isset]} {
        configure.env           MPICC=${mpi.cc}
        configure.args-append   --enable-mpi
    }
}

if {${name} == ${subport}} {
    livecheck.type  regex
    livecheck.url   ${homepage}
    livecheck.regex {FFTW (\d+(?:\.\d+)*) is the latest}
} else {
    livecheck.type  none
}
